-
Notifications
You must be signed in to change notification settings - Fork 369
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
Optionally write comments to config files #180
base: master
Are you sure you want to change the base?
Conversation
@@ -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 comment
The 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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
also a couple of unit tests would be nice
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a config_setting_set_comment() function as well.
{ | ||
if(depth > 1) | ||
__config_indent(stream, depth, config->tab_width); | ||
fprintf(stream, "// %s\n", setting->comment); |
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 //.
This PR adds the functionality to optionally add comments while writing configs. This helps give the user an idea about what the setting is.
You can add comments to your settings by using the following API as demonstrated in the example3
For C: We have a new function to add settings
For C++, we make use of polymorphism in C++
example3 produces the following output