Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
53e31e1b8474471e4b299d8d5f6f9d3a8d8e18fa
[simgrid.git] / src / xbt / config.cpp
1 /* Copyright (c) 2004-2014,2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 #include <stdio.h>
5
6 #include <cerrno>
7 #include <cstring>
8 #include <climits>
9 #include <functional>
10 #include <stdexcept>
11 #include <string>
12 #include <type_traits>
13
14 #include <xbt/config.h>
15 #include <xbt/config.hpp>
16 #include "xbt/misc.h"
17 #include "xbt/sysdep.h"
18 #include "xbt/log.h"
19 #include "xbt/ex.h"
20 #include "xbt/dynar.h"
21 #include "xbt/dict.h"
22
23 // *****
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_cfg, xbt, "configuration support");
26
27 XBT_EXPORT_NO_IMPORT(xbt_cfg_t) simgrid_config = NULL;
28
29 namespace {
30
31 static inline
32 void increment(e_xbt_cfgelm_type_t& type)
33 {
34   typedef std::underlying_type<e_xbt_cfgelm_type_t>::type underlying_type;
35   type = (e_xbt_cfgelm_type_t) ((underlying_type) type + 1);
36 }
37
38 }
39
40 namespace simgrid {
41 namespace config {
42
43 // A configuration variable:
44 struct ConfigurationElement {
45   /* Description */
46   std::string desc;
47
48   /* Allowed type of the variable */
49   e_xbt_cfgelm_type_t type;
50   bool isdefault = true;
51
52   /* Callback */
53   xbt_cfg_cb_t cb_set = nullptr;
54
55   /* Advanced callback (for xbt_cfgelm_string only) */
56   std::function<void(const char* value)> callback;
57
58   union {
59     int int_content;
60     double double_content;
61     char* string_content;
62     bool boolean_content;
63   };
64
65   ~ConfigurationElement()
66   {
67     XBT_DEBUG("Frees cfgelm %p", this);
68     switch(this->type) {
69     case xbt_cfgelm_int:
70     case xbt_cfgelm_double:
71     case xbt_cfgelm_boolean:
72       break;
73     case xbt_cfgelm_string:
74     case xbt_cfgelm_alias:
75       free(string_content);
76       break;
77     default:
78       xbt_die("Unexpected config type");
79     }
80   }
81
82 };
83
84 struct Config {
85   xbt_dict_t options;
86
87   Config();
88   ~Config();
89
90   // No copy:
91   Config(Config const&) = delete;
92   Config& operator=(Config const&) = delete;
93 };
94
95 /* Internal stuff used in cache to free a variable */
96 static void xbt_cfgelm_free(void *data)
97 {
98   if (data)
99     delete (simgrid::config::ConfigurationElement*) data;
100 }
101
102 Config::Config() :
103   options(xbt_dict_new_homogeneous(xbt_cfgelm_free))
104 {}
105
106 Config::~Config()
107 {
108   XBT_DEBUG("Frees cfg set %p", this);
109   xbt_dict_free(&this->options);
110 }
111
112 }
113 }
114
115 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] = { "int", "double", "string", "boolean", "any", "outofbound" };
116
117 const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
118   { "yes",    "no"},
119   {  "on",   "off"},
120   {"true", "false"},
121   {   "1",     "0"},
122   {  NULL,    NULL}
123 };
124
125 /* Retrieve the variable we'll modify */
126 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type);
127
128 /*----[ Memory management ]-----------------------------------------------*/
129 /** @brief Constructor
130  *
131  * Initialise a config set
132  */
133 xbt_cfg_t xbt_cfg_new(void)
134 {
135   return new simgrid::config::Config();
136 }
137
138 /** @brief Destructor */
139 void xbt_cfg_free(xbt_cfg_t * cfg)
140 {
141   delete *cfg;
142 }
143
144 /** @brief Dump a config set for debuging purpose
145  *
146  * @param name The name to give to this config set
147  * @param indent what to write at the beginning of each line (right number of spaces)
148  * @param cfg the config set
149  */
150 void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
151 {
152   xbt_dict_t dict = cfg->options;
153   xbt_dict_cursor_t cursor = NULL;
154   simgrid::config::ConfigurationElement* variable = NULL;
155   char *key = NULL;
156
157   if (name)
158     printf("%s>> Dumping of the config set '%s':\n", indent, name);
159
160   xbt_dict_foreach(dict, cursor, key, variable) {
161     const char* type_name = xbt_cfgelm_type_name[variable->type];
162     switch (variable->type) {
163     case xbt_cfgelm_int:
164       printf("%s  %s: ()%s) %i", indent, key, type_name, variable->int_content);
165       break;
166     case xbt_cfgelm_double:
167       printf("%s  %s: ()%s) %f", indent, key, type_name, variable->double_content);
168       break;
169     case xbt_cfgelm_string:
170       printf("%s  %s: ()%s) %s", indent, key, type_name, variable->string_content);
171       break;
172     case xbt_cfgelm_boolean:
173       printf("%s  %s: ()%s) %s", indent, key, type_name,
174         variable->boolean_content ? "true" : "false");
175       break;
176     case xbt_cfgelm_alias:
177       /* no content */
178       break;
179     default:
180       printf("%s    Invalid type!!\n", indent);
181       break;
182     }
183   }
184
185   if (name)
186     printf("%s<< End of the config set '%s'\n", indent, name);
187   fflush(stdout);
188
189   xbt_dict_cursor_free(&cursor);
190 }
191
192 /*----[ Registering stuff ]-----------------------------------------------*/
193 /** @brief Register an element within a config set
194  *
195  *  @param cfg the config set
196  *  @param name the name of the config element
197  *  @param desc a description for this item (used by xbt_cfg_help())
198  *  @param type the type of the config element
199  *  @param cb_set callback function called when a value is set
200  */
201 static simgrid::config::ConfigurationElement* xbt_cfg_register(
202   xbt_cfg_t * cfg, const char *name, const char *desc, e_xbt_cfgelm_type_t type, xbt_cfg_cb_t cb_set)
203 {
204   if (*cfg == NULL)
205     *cfg = xbt_cfg_new();
206   xbt_assert(type >= xbt_cfgelm_int && type <= xbt_cfgelm_boolean,
207               "type of %s not valid (%d should be between %d and %d)",
208              name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
209
210   simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null((*cfg)->options, name);
211   xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
212
213   res = new simgrid::config::ConfigurationElement();
214   XBT_DEBUG("Register cfg elm %s (%s) (%s (=%d) @%p in set %p)",
215             name, desc, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
216   res->type = type;
217   res->cb_set = cb_set;
218   if (desc)
219     res->desc = desc;
220
221   // Set a default default value:
222   switch (type) {
223   case xbt_cfgelm_int:
224     res->int_content = 0;
225     break;
226   case xbt_cfgelm_double:
227     res->double_content = 0.0;
228     break;
229   case xbt_cfgelm_string:
230     res->string_content = nullptr;
231     break;
232   case xbt_cfgelm_boolean:
233     res->boolean_content = false;
234     break;
235   default:
236     XBT_ERROR("%d is an invalid type code", (int)type);
237     break;
238   }
239
240   xbt_dict_set((*cfg)->options, name, res, NULL);
241   return res;
242 }
243
244 void xbt_cfg_register_double(const char *name, double default_value,xbt_cfg_cb_t cb_set, const char *desc)
245 {
246   xbt_cfg_register(&simgrid_config, name, desc, xbt_cfgelm_double, cb_set);
247   xbt_cfg_setdefault_double(name, default_value);
248 }
249 void xbt_cfg_register_int(const char *name, int default_value,xbt_cfg_cb_t cb_set, const char *desc)
250 {
251   xbt_cfg_register(&simgrid_config, name, desc, xbt_cfgelm_int, cb_set);
252   xbt_cfg_setdefault_int(name, default_value);
253 }
254 void xbt_cfg_register_string(const char *name, const char *default_value, xbt_cfg_cb_t cb_set, const char *desc)
255 {
256   xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_string,cb_set);
257   xbt_cfg_setdefault_string(name, default_value);
258 }
259 void xbt_cfg_register_boolean(const char *name, const char*default_value,xbt_cfg_cb_t cb_set, const char *desc)
260 {
261   xbt_cfg_register(&simgrid_config,name,desc,xbt_cfgelm_boolean,cb_set);
262   xbt_cfg_setdefault_boolean(name, default_value);
263 }
264
265 void xbt_cfg_register_alias(const char *newname, const char *oldname)
266 {
267   if (simgrid_config == NULL)
268     simgrid_config = xbt_cfg_new();
269
270   simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(simgrid_config->options, oldname);
271   xbt_assert(NULL == res, "Refusing to register the option '%s' twice.", oldname);
272
273   res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(simgrid_config->options, newname);
274   xbt_assert(res, "Cannot define an alias to the non-existing option '%s'.", newname);
275
276   res = new simgrid::config::ConfigurationElement();
277   XBT_DEBUG("Register cfg alias %s -> %s)",oldname,newname);
278
279   res->desc = std::string("Deprecated alias for ")+std::string(newname);
280   res->type = xbt_cfgelm_alias;
281   res->string_content = xbt_strdup(newname);
282
283   xbt_dict_set(simgrid_config->options, oldname, res, NULL);
284 }
285
286 /**
287  * @brief Parse a string and register the stuff described.
288  *
289  * @param cfg the config set
290  * @param entry a string describing the element to register
291  *
292  * The string may consist in several variable descriptions separated by a space.
293  * Each of them must use the following syntax: \<name\>:\<type\>
294  * with type being one of  'string','int','bool' or 'double'.
295  *
296  * Note that this does not allow to set the description, so you should prefer the other interface
297  */
298 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
299 {
300   char *entrycpy = xbt_strdup(entry);
301   char *tok;
302
303   e_xbt_cfgelm_type_t type;
304   XBT_DEBUG("Register string '%s'", entry);
305
306   tok = strchr(entrycpy, ':');
307   xbt_assert(tok, "Invalid config element descriptor: %s; Should be <name>:<type>", entry);
308   *(tok++) = '\0';
309
310   for (type = (e_xbt_cfgelm_type_t)0; type < xbt_cfgelm_type_count && strcmp(tok, xbt_cfgelm_type_name[type]); increment(type));
311   xbt_assert(type < xbt_cfgelm_type_count,
312       "Invalid type in config element descriptor: %s; Should be one of 'string', 'int' or 'double'.", entry);
313
314   xbt_cfg_register(cfg, entrycpy, NULL, type, NULL);
315
316   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
317 }
318
319 /** @brief Displays the declared aliases and their description */
320 void xbt_cfg_aliases(void)
321 {
322   xbt_dict_cursor_t dict_cursor;
323   unsigned int dynar_cursor;
324   simgrid::config::ConfigurationElement* variable;
325   char *name;
326   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
327
328   xbt_dict_foreach(simgrid_config->options, dict_cursor, name, variable)
329     xbt_dynar_push(names, &name);
330   xbt_dynar_sort_strings(names);
331
332   xbt_dynar_foreach(names, dynar_cursor, name) {
333     variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, name);
334
335     if (variable->type == xbt_cfgelm_alias)
336       printf("   %s: %s\n", name, variable->desc.c_str());
337   }
338 }
339
340 /** @brief Displays the declared options and their description */
341 void xbt_cfg_help(void)
342 {
343   xbt_dict_cursor_t dict_cursor;
344   unsigned int dynar_cursor;
345   simgrid::config::ConfigurationElement* variable;
346   char *name;
347   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
348
349   xbt_dict_foreach(simgrid_config->options, dict_cursor, name, variable)
350     xbt_dynar_push(names, &name);
351   xbt_dynar_sort_strings(names);
352
353   xbt_dynar_foreach(names, dynar_cursor, name) {
354     variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, name);
355     if (variable->type == xbt_cfgelm_alias)
356       continue;
357
358     printf("   %s: %s\n", name, variable->desc.c_str());
359     printf("       Type: %s; ", xbt_cfgelm_type_name[variable->type]);
360     printf("Current value: ");
361
362       switch (variable->type) {
363       case xbt_cfgelm_int:
364         printf("%d\n", variable->int_content);
365         break;
366       case xbt_cfgelm_double:
367         printf("%f\n", variable->double_content);
368         break;
369       case xbt_cfgelm_string:
370         printf("'%s'\n", variable->string_content);
371         break;
372       case xbt_cfgelm_boolean:
373         printf("'%s'\n", variable->boolean_content ? "true" : "false");
374         break;
375       default:
376         printf("Invalid type!!\n");
377         break;
378       }
379
380   }
381   xbt_dynar_free(&names);
382 }
383
384 static simgrid::config::ConfigurationElement* xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
385 {
386   simgrid::config::ConfigurationElement* res = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(cfg->options, name);
387
388   // The user used the old name. Switch to the new one after a short warning
389   while (res && res->type == xbt_cfgelm_alias) {
390     const char* newname = res->string_content;
391     XBT_INFO("Option %s has been renamed to %s. Consider switching.", name, newname);
392     res = xbt_cfgelm_get(cfg, newname, type);
393   }
394
395   if (!res) {
396     xbt_cfg_help();
397     fflush(stdout);
398     THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
399   }
400
401   xbt_assert(type == xbt_cfgelm_any || res->type == type,
402               "You tried to access to the config element %s as an %s, but its type is %s.",
403               name, xbt_cfgelm_type_name[type], xbt_cfgelm_type_name[res->type]);
404   return res;
405 }
406
407 /** @brief Get the type of this variable in that configuration set
408  *
409  * @param cfg the config set
410  * @param name the name of the element
411  *
412  * @return the type of the given element
413  */
414 e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
415 {
416   simgrid::config::ConfigurationElement* variable = NULL;
417
418   variable = (simgrid::config::ConfigurationElement*) xbt_dict_get_or_null(cfg->options, name);
419   if (!variable)
420     THROWF(not_found_error, 0, "Can't get the type of '%s' since this variable does not exist", name);
421
422   XBT_DEBUG("type in variable = %d", (int)variable->type);
423   return variable->type;
424 }
425
426 /*----[ Setting ]---------------------------------------------------------*/
427 /**  @brief va_args version of xbt_cfg_set
428  *
429  * @param cfg config set to fill
430  * @param name  variable name
431  * @param pa  variable value
432  *
433  * Add some values to the config set.
434  */
435 void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
436 {
437   char *str;
438   int i;
439   double d;
440   e_xbt_cfgelm_type_t type = xbt_cfgelm_type_count; /* Set a dummy value to make gcc happy. It cannot get uninitialized */
441
442   xbt_ex_t e;
443
444   TRY {
445     type = xbt_cfg_get_type(cfg, name);
446   }
447   CATCH(e) {
448     if (e.category == not_found_error) {
449       xbt_ex_free(e);
450       THROWF(not_found_error, 0, "Can't set the property '%s' since it's not registered", name);
451     }
452     RETHROW;
453   }
454
455   switch (type) {
456   case xbt_cfgelm_string:
457     str = va_arg(pa, char *);
458     xbt_cfg_set_string(name, str);
459     break;
460   case xbt_cfgelm_int:
461     i = va_arg(pa, int);
462     xbt_cfg_set_int(name, i);
463     break;
464   case xbt_cfgelm_double:
465     d = va_arg(pa, double);
466     xbt_cfg_set_double(name, d);
467     break;
468   case xbt_cfgelm_boolean:
469     str = va_arg(pa, char *);
470     xbt_cfg_set_boolean(name, str);
471     break;
472   default:
473     xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
474   }
475 }
476
477 /** @brief Add a NULL-terminated list of pairs {(char*)key, value} to the set
478  *
479  * @param cfg config set to fill
480  * @param name variable name
481  * @param ... variable value
482  */
483 void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
484 {
485   va_list pa;
486
487   va_start(pa, name);
488   xbt_cfg_set_vargs(cfg, name, pa);
489   va_end(pa);
490 }
491
492 /** @brief Add values parsed from a string into a config set
493  *
494  * @param options a string containing the content to add to the config set. This is a '\\t',' ' or '\\n' or ','
495  * separated list of variables. Each individual variable is like "[name]:[value]" where [name] is the name of an
496  * already registered variable, and [value] conforms to the data type under which this variable was registered.
497  *
498  * @todo This is a crude manual parser, it should be a proper lexer.
499  */
500 void xbt_cfg_set_parse(const char *options)
501 {
502   if (!options || !strlen(options)) {   /* nothing to do */
503     return;
504   }
505   char *optionlist_cpy = xbt_strdup(options);
506
507   XBT_DEBUG("List to parse and set:'%s'", options);
508   char *option = optionlist_cpy;
509   while (1) {                   /* breaks in the code */
510     if (!option)
511       break;
512     char *name = option;
513     int len = strlen(name);
514     XBT_DEBUG("Still to parse and set: '%s'. len=%d; option-name=%ld", name, len, (long) (option - name));
515
516     /* Pass the value */
517     while (option - name <= (len - 1) && *option != ' ' && *option != '\n' && *option != '\t' && *option != ',') {
518       XBT_DEBUG("Take %c.", *option);
519       option++;
520     }
521     if (option - name == len) {
522       XBT_DEBUG("Boundary=EOL");
523       option = NULL;            /* don't do next iteration */
524     } else {
525       XBT_DEBUG("Boundary on '%c'. len=%d;option-name=%ld", *option, len, (long) (option - name));
526       /* Pass the following blank chars */
527       *(option++) = '\0';
528       while (option - name < (len - 1) && (*option == ' ' || *option == '\n' || *option == '\t')) {
529         /*      fprintf(stderr,"Ignore a blank char.\n"); */
530         option++;
531       }
532       if (option - name == len - 1)
533         option = NULL;          /* don't do next iteration */
534     }
535     XBT_DEBUG("parse now:'%s'; parse later:'%s'", name, option);
536
537     if (name[0] == ' ' || name[0] == '\n' || name[0] == '\t')
538       continue;
539     if (!strlen(name))
540       break;
541
542     char *val = strchr(name, ':');
543     xbt_assert(val, "Option '%s' badly formatted. Should be of the form 'name:value'", name);
544     /* don't free(optionlist_cpy) if the assert fails, 'name' points inside it */
545     *(val++) = '\0';
546
547     if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
548       XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
549
550     TRY {
551       xbt_cfg_set_as_string(name,val);
552     } CATCH_ANONYMOUS {
553       free(optionlist_cpy);
554       RETHROW;
555     }
556   }
557   free(optionlist_cpy);
558 }
559
560 /** @brief Set the value of a variable, using the string representation of that value
561  *
562  * @param key name of the variable to modify
563  * @param value string representation of the value to set
564  *
565  * @return the first char after the parsed value in val
566  */
567
568 void *xbt_cfg_set_as_string(const char *key, const char *value) {
569   xbt_ex_t e;
570
571   char *ret;
572   volatile simgrid::config::ConfigurationElement* variable = NULL;
573   int i;
574   double d;
575
576   TRY {
577     while (variable == NULL) {
578       variable = (simgrid::config::ConfigurationElement*) xbt_dict_get(simgrid_config->options, key);
579       if (variable->type == xbt_cfgelm_alias) {
580         const char *newname = variable->string_content;
581         XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
582         key = newname;
583         variable = NULL;
584       }
585     }
586   } CATCH(e) {
587     if (e.category == not_found_error) {
588       xbt_ex_free(e);
589       THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
590     }
591     RETHROW;
592   }
593
594   switch (variable->type) {
595   case xbt_cfgelm_string:
596     xbt_cfg_set_string(key, value);     /* throws */
597     break;
598   case xbt_cfgelm_int:
599     i = strtol(value, &ret, 0);
600     if (ret == value) {
601       xbt_die("Value of option %s not valid. Should be an integer", key);
602     }
603     xbt_cfg_set_int(key, i);  /* throws */
604     break;
605   case xbt_cfgelm_double:
606     d = strtod(value, &ret);
607     if (ret == value) {
608       xbt_die("Value of option %s not valid. Should be a double", key);
609     }
610     xbt_cfg_set_double(key, d);       /* throws */
611     break;
612   case xbt_cfgelm_boolean:
613     xbt_cfg_set_boolean(key, value);  /* throws */
614     ret = (char *)value + strlen(value);
615     break;
616   default:
617     THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
618     break;
619   }
620   return ret;
621 }
622
623 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
624  *
625  * This is useful to change the default value of a variable while allowing
626  * users to override it with command line arguments
627  */
628 void xbt_cfg_setdefault_int(const char *name, int val)
629 {
630   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
631
632   if (variable->isdefault){
633     xbt_cfg_set_int(name, val);
634     variable->isdefault = true;
635   } else
636     XBT_DEBUG("Do not override configuration variable '%s' with value '%d' because it was already set.", name, val);
637 }
638
639 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
640  *
641  * This is useful to change the default value of a variable while allowing
642  * users to override it with command line arguments
643  */
644 void xbt_cfg_setdefault_double(const char *name, double val)
645 {
646   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
647
648   if (variable->isdefault) {
649     xbt_cfg_set_double(name, val);
650     variable->isdefault = true;
651   } else
652     XBT_DEBUG("Do not override configuration variable '%s' with value '%f' because it was already set.", name, val);
653 }
654
655 /** @brief Set a string value to \a name within \a cfg if it wasn't changed yet
656  *
657  * This is useful to change the default value of a variable while allowing
658  * users to override it with command line arguments
659  */
660 void xbt_cfg_setdefault_string(const char *name, const char *val)
661 {
662   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
663
664   if (variable->isdefault){
665     xbt_cfg_set_string(name, val);
666     variable->isdefault = true;
667   } else
668     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
669 }
670
671 /** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
672  *
673  * This is useful to change the default value of a variable while allowing
674  * users to override it with command line arguments
675  */
676 void xbt_cfg_setdefault_boolean(const char *name, const char *val)
677 {
678   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
679
680   if (variable->isdefault){
681     xbt_cfg_set_boolean(name, val);
682     variable->isdefault = true;
683   }
684    else
685     XBT_DEBUG("Do not override configuration variable '%s' with value '%s' because it was already set.", name, val);
686 }
687
688 /** @brief Set an integer value to \a name within \a cfg
689  *
690  * @param name the name of the variable
691  * @param val the value of the variable
692  */
693 void xbt_cfg_set_int(const char *name, int val)
694 {
695   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
696   variable->int_content = val;
697   if (variable->cb_set)
698     variable->cb_set(name);
699   variable->isdefault = false;
700 }
701
702 /** @brief Set or add a double value to \a name within \a cfg
703  *
704  * @param name the name of the variable
705  * @param val the double to set
706  */
707 void xbt_cfg_set_double(const char *name, double val)
708 {
709   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
710   variable->double_content = val;
711   if (variable->cb_set)
712     variable->cb_set(name);
713   variable->isdefault = false;
714 }
715
716 /** @brief Set or add a string value to \a name within \a cfg
717  *
718  * @param cfg the config set
719  * @param name the name of the variable
720  * @param val the value to be added
721  *
722  */
723 void xbt_cfg_set_string(const char *name, const char *val)
724 {
725   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
726   free(variable->string_content);
727   variable->string_content = xbt_strdup(val);
728   if (variable->cb_set)
729     variable->cb_set(name);
730   variable->isdefault = false;
731
732   if (variable->callback) {
733     try {
734       variable->callback(val);
735     }
736     catch(std::range_error& e) {
737       xbt_die("Invalid flag %s=%s: %s", val, name, e.what());
738     }
739     catch(std::exception& e) {
740       xbt_die("Error for flag %s=%s: %s", val, name, e.what());
741     }
742     catch(...) {
743       xbt_die("Error for flag %s=%s", val, name);
744     }
745   }
746 }
747
748 /** @brief Set or add a boolean value to \a name within \a cfg
749  *
750  * @param name the name of the variable
751  * @param val the value of the variable
752  */
753 void xbt_cfg_set_boolean(const char *name, const char *val)
754 {
755   int bval=-1;
756   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
757
758   for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
759     if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
760       bval = 1;
761       break;
762     }
763     if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
764       bval = 0;
765       break;
766     }
767   }
768   xbt_assert(bval != -1, "Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
769   variable->boolean_content = bval;
770   if (variable->cb_set)
771     variable->cb_set(name);
772   variable->isdefault = false;
773 }
774
775
776 /* Say if the value is the default value */
777 int xbt_cfg_is_default_value(const char *name)
778 {
779   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_any);
780   return variable->isdefault;
781 }
782
783 /*----[ Getting ]---------------------------------------------------------*/
784 /** @brief Retrieve an integer value of a variable (get a warning if not uniq)
785  *
786  * @param name the name of the variable
787  *
788  * Returns the first value from the config set under the given name.
789  */
790 int xbt_cfg_get_int(const char *name)
791 {
792   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_int);
793   return variable->int_content;
794 }
795
796 /** @brief Retrieve a double value of a variable (get a warning if not uniq)
797  *
798  * @param cfg the config set
799  * @param name the name of the variable
800  *
801  * Returns the first value from the config set under the given name.
802  */
803 double xbt_cfg_get_double(const char *name)
804 {
805   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_double);
806   return variable->double_content;
807 }
808
809 /** @brief Retrieve a string value of a variable (get a warning if not uniq)
810  *
811  * @param cfg the config set
812  * @param name the name of the variable
813  *
814  * Returns the first value from the config set under the given name.
815  * If there is more than one value, it will issue a warning.
816  * Returns NULL if there is no value.
817  *
818  * \warning the returned value is the actual content of the config set
819  */
820 char *xbt_cfg_get_string(const char *name)
821 {
822   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_string);
823   return variable->string_content;
824 }
825
826 /** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
827  *
828  * @param cfg the config set
829  * @param name the name of the variable
830  *
831  * Returns the first value from the config set under the given name.
832  * If there is more than one value, it will issue a warning.
833  */
834 int xbt_cfg_get_boolean(const char *name)
835 {
836   simgrid::config::ConfigurationElement* variable = xbt_cfgelm_get(simgrid_config, name, xbt_cfgelm_boolean);
837   return variable->boolean_content;
838 }
839
840 namespace simgrid {
841 namespace config {
842
843 bool parseBool(const char* value)
844 {
845   for (int i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
846     if (std::strcmp(value, xbt_cfgelm_boolean_values[i].true_val) == 0)
847       return true;
848     if (std::strcmp(value, xbt_cfgelm_boolean_values[i].false_val) == 0)
849       return false;
850   }
851   throw std::range_error("not a boolean");
852 }
853
854 double parseDouble(const char* value)
855 {
856   char* end;
857   errno = 0;
858   double res = std::strtod(value, &end);
859   if (errno == ERANGE)
860     throw std::range_error("out of range");
861   else if (errno)
862     xbt_die("Unexpected errno");
863   if (end == value || *end != '\0')
864     throw std::range_error("invalid double");
865   else
866     return res;
867 }
868
869 long int parseLong(const char* value)
870 {
871   char* end;
872   errno = 0;
873   long int res = std::strtol(value, &end, 0);
874   if (errno) {
875     if (res == LONG_MIN && errno == ERANGE)
876       throw std::range_error("underflow");
877     else if (res == LONG_MAX && errno == ERANGE)
878       throw std::range_error("overflow");
879     xbt_die("Unexpected errno");
880   }
881   if (end == value || *end != '\0')
882     throw std::range_error("invalid integer");
883   else
884     return res;
885 }
886
887 void declareFlag(const char* name, const char* description,
888   std::function<void(const char* value)> callback)
889 {
890   simgrid::config::ConfigurationElement* e = xbt_cfg_register(
891     &simgrid_config, name, description, xbt_cfgelm_string, NULL);
892   e->callback = std::move(callback);
893 }
894
895 }
896 }
897
898 #ifdef SIMGRID_TEST
899
900 #include <string>
901
902 #include "xbt.h"
903 #include "xbt/ex.h"
904
905 #include <xbt/config.hpp>
906
907 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
908
909 XBT_TEST_SUITE("config", "Configuration support");
910
911 static xbt_cfg_t make_set()
912 {
913   xbt_cfg_t set = NULL;
914
915   xbt_log_threshold_set(&_XBT_LOGV(xbt_cfg), xbt_log_priority_critical);
916   xbt_cfg_register_str(&set, "speed:int");
917   xbt_cfg_register_str(&set, "peername:string");
918   xbt_cfg_register_str(&set, "user:string");
919
920   return set;
921 }                               /* end_of_make_set */
922
923 XBT_PUBLIC_DATA(xbt_cfg_t) simgrid_config;
924
925 XBT_TEST_UNIT("memuse", test_config_memuse, "Alloc and free a config set")
926 {
927   simgrid_config = make_set();
928   xbt_test_add("Alloc and free a config set");
929   xbt_cfg_set_parse("peername:veloce user:bidule");
930   xbt_cfg_free(&simgrid_config);
931 }
932
933 XBT_TEST_UNIT("use", test_config_use, "Data retrieving tests")
934 {
935   simgrid_config = make_set();
936   xbt_test_add("Get a single value");
937   {
938     /* get_single_value */
939     int ival;
940
941     xbt_cfg_set_parse("peername:toto:42 speed:42");
942     ival = xbt_cfg_get_int("speed");
943     if (ival != 42)
944       xbt_test_fail("Speed value = %d, I expected 42", ival);
945   }
946
947   xbt_test_add("Access to a non-existant entry");
948   {
949     xbt_ex_t e;
950
951     TRY {
952       xbt_cfg_set_parse("color:blue");
953     } CATCH(e) {
954       if (e.category != not_found_error)
955         xbt_test_exception(e);
956       xbt_ex_free(e);
957     }
958   }
959   xbt_cfg_free(&simgrid_config);
960 }
961
962 XBT_TEST_UNIT("c++flags", test_config_cxx_flags, "C++ flags")
963 {
964   simgrid_config = make_set();
965   xbt_test_add("C++ declaration of flags");
966
967   simgrid::config::Flag<int> int_flag("int", "", 0);
968   simgrid::config::Flag<std::string> string_flag("string", "", "foo");
969   simgrid::config::Flag<double> double_flag("double", "", 0.32);
970   simgrid::config::Flag<bool> bool_flag1("bool1", "", false);
971   simgrid::config::Flag<bool> bool_flag2("bool2", "", true);
972
973   xbt_test_add("Parse values");
974   xbt_cfg_set_parse("int:42 string:bar double:8.0 bool1:true bool2:false");
975   xbt_test_assert(int_flag == 42, "Check int flag");
976   xbt_test_assert(string_flag == "bar", "Check string flag");
977   xbt_test_assert(double_flag == 8.0, "Check double flag");
978   xbt_test_assert(bool_flag1, "Check bool1 flag");
979   xbt_test_assert(!bool_flag2, "Check bool2 flag");
980
981   xbt_cfg_free(&simgrid_config);
982 }
983
984 #endif                          /* SIMGRID_TEST */