-
Notifications
You must be signed in to change notification settings - Fork 381
Optionally write comments to config files #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -608,6 +608,13 @@ static void __config_write_setting(const config_t *config, | |
char nongroup_assign_char = config_get_option( | ||
config, CONFIG_OPTION_COLON_ASSIGNMENT_FOR_NON_GROUPS) ? ':' : '='; | ||
|
||
if(setting->comment) | ||
{ | ||
if(depth > 1) | ||
__config_indent(stream, depth, config->tab_width); | ||
fprintf(stream, "// %s\n", setting->comment); | ||
} | ||
|
||
if(depth > 1) | ||
__config_indent(stream, depth, config->tab_width); | ||
|
||
|
@@ -824,7 +831,8 @@ void config_set_hook(config_t *config, void *hook) | |
/* ------------------------------------------------------------------------- */ | ||
|
||
static config_setting_t *config_setting_create(config_setting_t *parent, | ||
const char *name, int type) | ||
const char *name, | ||
int type, const char *comment) | ||
{ | ||
config_setting_t *setting; | ||
config_list_t *list; | ||
|
@@ -839,6 +847,7 @@ static config_setting_t *config_setting_create(config_setting_t *parent, | |
setting->config = parent->config; | ||
setting->hook = NULL; | ||
setting->line = 0; | ||
setting->comment = (comment == NULL) ? NULL : strdup(comment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. __config_setting_destroy() needs to be updated to free this string, otherwise we have a memory leak |
||
|
||
list = parent->value.list; | ||
|
||
|
@@ -1343,7 +1352,7 @@ config_setting_t *config_setting_set_int_elem(config_setting_t *setting, | |
if(! __config_list_checktype(setting, CONFIG_TYPE_INT)) | ||
return(NULL); | ||
|
||
element = config_setting_create(setting, NULL, CONFIG_TYPE_INT); | ||
element = config_setting_create(setting, NULL, CONFIG_TYPE_INT, NULL); | ||
} | ||
else | ||
{ | ||
|
@@ -1385,7 +1394,7 @@ config_setting_t *config_setting_set_int64_elem(config_setting_t *setting, | |
if(! __config_list_checktype(setting, CONFIG_TYPE_INT64)) | ||
return(NULL); | ||
|
||
element = config_setting_create(setting, NULL, CONFIG_TYPE_INT64); | ||
element = config_setting_create(setting, NULL, CONFIG_TYPE_INT64, NULL); | ||
} | ||
else | ||
{ | ||
|
@@ -1426,7 +1435,7 @@ config_setting_t *config_setting_set_float_elem(config_setting_t *setting, | |
if(! __config_list_checktype(setting, CONFIG_TYPE_FLOAT)) | ||
return(NULL); | ||
|
||
element = config_setting_create(setting, NULL, CONFIG_TYPE_FLOAT); | ||
element = config_setting_create(setting, NULL, CONFIG_TYPE_FLOAT, NULL); | ||
} | ||
else | ||
element = config_setting_get_elem(setting, idx); | ||
|
@@ -1471,7 +1480,7 @@ config_setting_t *config_setting_set_bool_elem(config_setting_t *setting, | |
if(! __config_list_checktype(setting, CONFIG_TYPE_BOOL)) | ||
return(NULL); | ||
|
||
element = config_setting_create(setting, NULL, CONFIG_TYPE_BOOL); | ||
element = config_setting_create(setting, NULL, CONFIG_TYPE_BOOL, NULL); | ||
} | ||
else | ||
element = config_setting_get_elem(setting, idx); | ||
|
@@ -1517,7 +1526,7 @@ config_setting_t *config_setting_set_string_elem(config_setting_t *setting, | |
if(! __config_list_checktype(setting, CONFIG_TYPE_STRING)) | ||
return(NULL); | ||
|
||
element = config_setting_create(setting, NULL, CONFIG_TYPE_STRING); | ||
element = config_setting_create(setting, NULL, CONFIG_TYPE_STRING, NULL); | ||
} | ||
else | ||
element = config_setting_get_elem(setting, idx); | ||
|
@@ -1535,7 +1544,7 @@ config_setting_t *config_setting_set_string_elem(config_setting_t *setting, | |
|
||
config_setting_t *config_setting_get_elem(const config_setting_t *setting, | ||
unsigned int idx) | ||
{ | ||
{ | ||
config_list_t *list; | ||
|
||
if(! config_setting_is_aggregate(setting)) | ||
|
@@ -1606,8 +1615,8 @@ void config_setting_set_hook(config_setting_t *setting, void *hook) | |
|
||
/* ------------------------------------------------------------------------- */ | ||
|
||
config_setting_t *config_setting_add(config_setting_t *parent, | ||
const char *name, int type) | ||
config_setting_t *config_setting_add_with_comment(config_setting_t *parent, | ||
const char *name, int type, const char *comment) | ||
{ | ||
if((type < CONFIG_TYPE_NONE) || (type > CONFIG_TYPE_LIST)) | ||
return(NULL); | ||
|
@@ -1635,7 +1644,15 @@ config_setting_t *config_setting_add(config_setting_t *parent, | |
return(NULL); /* already exists */ | ||
} | ||
|
||
return(config_setting_create(parent, name, type)); | ||
return(config_setting_create(parent, name, type, comment)); | ||
} | ||
|
||
/* ------------------------------------------------------------------------- */ | ||
|
||
config_setting_t *config_setting_add(config_setting_t *parent, | ||
const char *name, int type) | ||
{ | ||
config_setting_add_with_comment(parent, name, type, NULL); | ||
} | ||
|
||
/* ------------------------------------------------------------------------- */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,7 @@ typedef struct config_setting_t | |
void *hook; | ||
unsigned int line; | ||
const char *file; | ||
char *comment; | ||
} config_setting_t; | ||
|
||
typedef enum | ||
|
@@ -290,6 +291,8 @@ extern LIBCONFIG_API config_setting_t *config_setting_get_member( | |
|
||
extern LIBCONFIG_API config_setting_t *config_setting_add( | ||
config_setting_t *parent, const char *name, int type); | ||
extern LIBCONFIG_API config_setting_t *config_setting_add_with_comment( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a config_setting_set_comment() function as well. |
||
config_setting_t *parent, const char *name, int type, const char *comment); | ||
extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent, | ||
const char *name); | ||
extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1106,7 +1106,7 @@ void Setting::remove(unsigned int idx) | |
|
||
// --------------------------------------------------------------------------- | ||
|
||
Setting & Setting::add(const char *name, Setting::Type type) | ||
Setting & Setting::add(const char *name, Setting::Type type, const char *comment) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should have an overload that takes a const std::string &comment (a simple inline function in libconfig.h++) doc/libconfig.texi needs to be updated with documentation for the new APIs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also a couple of unit tests would be nice |
||
{ | ||
assertType(TypeGroup); | ||
|
||
|
@@ -1115,7 +1115,8 @@ Setting & Setting::add(const char *name, Setting::Type type) | |
if(typecode == CONFIG_TYPE_NONE) | ||
throw SettingTypeException(*this, name); | ||
|
||
config_setting_t *setting = config_setting_add(_setting, name, typecode); | ||
config_setting_t *setting = config_setting_add_with_comment( | ||
_setting, name, typecode, comment); | ||
|
||
if(! setting) | ||
throw SettingNameException(*this, name); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the comment contains newlines, this will result in a syntax error in the generated file. The comment should be written out such that each line begins with //.