Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

srivathsanmurali
Copy link

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

  group = config_setting_add_with_comment(
      root, "address", CONFIG_TYPE_GROUP, "Address for parcel delivery");

For C++, we make use of polymorphism in C++

  Setting &address = root.add("address", Setting::TypeGroup, "Address for parcel delivery");

example3 produces the following output

// Address for parcel delivery
address :
{
  street = "1 Woz Way";
  city = "San Jose";
  state = "CA";
  zip = 95110;
};
numbers = [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ];

@@ -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);
Copy link
Owner

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)
Copy link
Owner

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.

Copy link
Owner

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(
Copy link
Owner

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);
Copy link
Owner

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 //.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants