/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include <stdio.h>
#include "xbt/misc.h"
#include "xbt/sysdep.h"
#include "xbt/log.h"
static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
e_xbt_cfgelm_type_t type);
-static void xbt_cfg_str_free(void *d){
- free(*(void**)d);
-}
-
/*----[ Memory management ]-----------------------------------------------*/
/** @brief Constructor
xbt_cfgelm_t variable=NULL;
char *name=NULL;
+ DEBUG1("Copy cfg set %p",tocopy);
*whereto=NULL;
xbt_assert0(tocopy,"cannot copy NULL config");
/** @brief Destructor */
void xbt_cfg_free(xbt_cfg_t *cfg) {
+ DEBUG1("Frees cfg set %p",cfg);
xbt_dict_free((xbt_dict_t*)cfg);
}
void xbt_cfgelm_free(void *data) {
xbt_cfgelm_t c=(xbt_cfgelm_t)data;
+ DEBUG1("Frees cfgelm %p",c);
if (!c) return;
xbt_dynar_free(&(c->content));
free(c);
int min, int max,
xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm){
xbt_cfgelm_t res;
- xbt_ex_t e;
- int found=0;
xbt_assert(cfg);
xbt_assert4(type>=xbt_cfgelm_int && type<=xbt_cfgelm_peer,
"type of %s not valid (%d should be between %d and %d)",
name,type,xbt_cfgelm_int, xbt_cfgelm_peer);
- DEBUG5("Register cfg elm %s (%d to %d %s (=%d))",
- name,min,max,xbt_cfgelm_type_name[type],type);
- TRY {
- res = xbt_dict_get((xbt_dict_t)cfg,name);
- } CATCH(e) {
- if (e.category == not_found_error) {
- found = 1;
- xbt_ex_free(e);
- } else {
- RETHROW;
- }
- }
+ res = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
- if (!found) {
+ if (res) {
WARN1("Config elem %s registered twice.",name);
/* Will be removed by the insertion of the new one */
}
res=xbt_new(s_xbt_cfgelm_t,1);
+ DEBUG7("Register cfg elm %s (%d to %d %s (=%d) @%p in set %p)",
+ name,min,max,xbt_cfgelm_type_name[type],type,res,cfg);
res->type=type;
res->min=min;
break;
case xbt_cfgelm_string:
- res->content = xbt_dynar_new(sizeof(char*),&xbt_cfg_str_free);
+ res->content = xbt_dynar_new(sizeof(char*),xbt_free_ref);
break;
case xbt_cfgelm_peer:
- res->content = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
+ res->content = xbt_dynar_new(sizeof(xbt_peer_t),xbt_peer_free_voidp);
break;
default:
void
xbt_cfg_unregister(xbt_cfg_t cfg,const char *name) {
+ DEBUG2("Unregister elm '%s' from set %p",name,cfg);
xbt_dict_remove((xbt_dict_t)cfg,name);
}
int min,max;
e_xbt_cfgelm_type_t type;
+ DEBUG1("Register string '%s'",entry);
tok=strchr(entrycpy, ':');
xbt_assert2(tok,"Invalid config element descriptor: %s%s",
int size;
xbt_assert0(cfg,"NULL config set.");
+ DEBUG1("Check cfg set %p",cfg);
xbt_dict_foreach((xbt_dict_t)cfg,cursor,name,variable) {
size = xbt_dynar_length(variable->content);
const char *name,
e_xbt_cfgelm_type_t type){
xbt_cfgelm_t res=NULL;
- xbt_ex_t e;
- TRY {
- res = xbt_dict_get((xbt_dict_t)cfg,name);
- } CATCH(e) {
- if (e.category == not_found_error) {
- xbt_ex_free(e);
- THROW1(not_found_error,0,
- "No registered variable '%s' in this config set",name);
- }
- RETHROW;
- }
+ res = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
+ if (!res)
+ THROW1(not_found_error,0,
+ "No registered variable '%s' in this config set",name);
xbt_assert3(type == xbt_cfgelm_any || res->type == type,
"You tried to access to the config element %s as an %s, but its type is %s.",
xbt_cfg_get_type(xbt_cfg_t cfg, const char *name) {
xbt_cfgelm_t variable = NULL;
- xbt_ex_t e;
- TRY {
- variable = xbt_dict_get((xbt_dict_t)cfg,name);
- } CATCH(e) {
- if (e.category == not_found_error) {
- xbt_ex_free(e);
- THROW1(not_found_error,0,
- "Can't get the type of '%s' since this variable does not exist",name);
- }
- RETHROW;
- }
+ variable = xbt_dict_get_or_null((xbt_dict_t)cfg,name);
+ if (!variable)
+ THROW1(not_found_error,0,
+ "Can't get the type of '%s' since this variable does not exist",name);
INFO1("type in variable = %d",variable->type);
xbt_dynar_set(variable->content,0,&val);
} else {
- if (variable->max && xbt_dynar_length(variable->content) == variable->max)
+ if (variable->max && xbt_dynar_length(variable->content) == (unsigned long)variable->max)
THROW3(mismatch_error,0,
"Cannot add value %d to the config element %s since it's already full (size=%d)",
val,name,variable->max);
VERB2("Configuration setting: %s=%s",name,val);
variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
+ DEBUG5("Variable: %d to %d %s (=%d) @%p",
+ variable->min,variable->max,xbt_cfgelm_type_name[variable->type],variable->type,variable);
if (variable->max == 1) {
- if (variable->cb_rm && xbt_dynar_length(variable->content))
- (*variable->cb_rm)(name, 0);
+ if (xbt_dynar_length(variable->content)) {
+ if (variable->cb_rm)
+ (*variable->cb_rm)(name, 0);
+ else if (variable->type == xbt_cfgelm_string) {
+ char * sval=xbt_dynar_get_as(variable->content,0,char*);
+ free(sval);
+ }
+ }
xbt_dynar_set(variable->content,0,&newval);
} else {
void xbt_cfg_rm_int(xbt_cfg_t cfg,const char*name, int val) {
xbt_cfgelm_t variable;
- int cpt,seen;
+ unsigned int cpt;
+ int seen;
variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
void xbt_cfg_rm_double(xbt_cfg_t cfg,const char*name, double val) {
xbt_cfgelm_t variable;
- int cpt;
+ unsigned int cpt;
double seen;
variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_double);
void
xbt_cfg_rm_string(xbt_cfg_t cfg,const char*name, const char *val) {
xbt_cfgelm_t variable;
- int cpt;
+ unsigned int cpt;
char *seen;
variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_string);
void
xbt_cfg_rm_peer(xbt_cfg_t cfg,const char*name, const char *peer,int port) {
xbt_cfgelm_t variable;
- int cpt;
+ unsigned int cpt;
xbt_peer_t seen;
variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_peer);
if (variable) {
if (variable->cb_rm) {
- int cpt;
+ unsigned int cpt;
void *ignored;
xbt_dynar_foreach(variable->content,cpt,ignored) {
(*variable->cb_rm)(name, cpt);
}
XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
- xbt_cfg_t set = set=make_set();
-
xbt_test_add0("Get a single value");
{
dyn = xbt_cfg_get_dynar(myset,"user");
if (xbt_dynar_length(dyn) != 3)
- xbt_test_fail1("Dynar length = %d, I expected 3", (int)xbt_dynar_length(dyn));
+ xbt_test_fail1("Dynar length = %lu, I expected 3", xbt_dynar_length(dyn));
if (strcmp(xbt_dynar_get_as(dyn,0,char*),"foo"))
xbt_test_fail1("Dynar[0] = %s, I expected foo", xbt_dynar_get_as(dyn,0,char*));