Commit 462425bb authored by jltallon's avatar jltallon

Merge pull request #44 from jltallon/issue41

Merge fix for issue #41 from random85 ; Documentation and fixes by me
parents f9cc4e36 6c996c08
...@@ -1091,6 +1091,10 @@ This function removes and destroys the setting named @var{name} from ...@@ -1091,6 +1091,10 @@ This function removes and destroys the setting named @var{name} from
the parent setting @var{parent}, which must be a group. Any child the parent setting @var{parent}, which must be a group. Any child
settings of the setting are recursively destroyed as well. settings of the setting are recursively destroyed as well.
The @var{name} parameter can also specify a setting @i{path} relative to
the provided @var{parent}.
(In that case, the setting will be looked up and removed.)
The function returns @code{CONFIG_TRUE} on success. If @var{parent} is The function returns @code{CONFIG_TRUE} on success. If @var{parent} is
not a group, or if it has no setting with the given name, it returns not a group, or if it has no setting with the given name, it returns
@code{CONFIG_FALSE}. @code{CONFIG_FALSE}.
......
...@@ -1598,6 +1598,8 @@ int config_setting_remove(config_setting_t *parent, const char *name) ...@@ -1598,6 +1598,8 @@ int config_setting_remove(config_setting_t *parent, const char *name)
{ {
unsigned int idx; unsigned int idx;
config_setting_t *setting; config_setting_t *setting;
const char *settingName;
const char *lastFound;
if(! parent) if(! parent)
return(CONFIG_FALSE); return(CONFIG_FALSE);
...@@ -1605,10 +1607,29 @@ int config_setting_remove(config_setting_t *parent, const char *name) ...@@ -1605,10 +1607,29 @@ int config_setting_remove(config_setting_t *parent, const char *name)
if(parent->type != CONFIG_TYPE_GROUP) if(parent->type != CONFIG_TYPE_GROUP)
return(CONFIG_FALSE); return(CONFIG_FALSE);
if(! (setting = __config_list_search(parent->value.list, name, &idx))) setting = config_setting_lookup(parent, name);
if(!setting)
return(CONFIG_FALSE); return(CONFIG_FALSE);
__config_list_remove(parent->value.list, idx); settingName = name;
do
{
lastFound = settingName;
while(settingName && !strchr(PATH_TOKENS, *settingName))
++settingName;
if(*settingName == '\0')
{
settingName = lastFound;
break;
}
}while(*++settingName);
if(!(setting = __config_list_search(setting->parent->value.list, settingName, &idx)))
return(CONFIG_FALSE);
__config_list_remove(setting->parent->value.list, idx);
__config_setting_destroy(setting); __config_setting_destroy(setting);
return(CONFIG_TRUE); return(CONFIG_TRUE);
......
...@@ -411,6 +411,36 @@ TT_TEST(BigInt7) ...@@ -411,6 +411,36 @@ TT_TEST(BigInt7)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
TT_TEST(RemoveSetting)
{
char *buf;
config_t cfg;
int rc;
config_setting_t* rootSetting;
buf = "a:{b:3;c:4;}";
config_init(&cfg);
rc = config_read_string(&cfg, buf);
TT_ASSERT_TRUE(rc);
rootSetting = config_root_setting(&cfg);
rc = config_setting_remove(rootSetting, "a.c");
TT_ASSERT_TRUE(rc);
// a and a.b are found
rootSetting = config_lookup(&cfg, "a");
TT_EXPECT_PTR_NOTNULL(rootSetting);
rootSetting = config_lookup(&cfg, "a.b");
TT_EXPECT_PTR_NOTNULL(rootSetting);
rootSetting = config_lookup(&cfg, "a.c");
TT_EXPECT_PTR_NULL(rootSetting);
config_destroy(&cfg);
}
/* ------------------------------------------------------------------------- */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int failures; int failures;
...@@ -426,6 +456,7 @@ int main(int argc, char **argv) ...@@ -426,6 +456,7 @@ int main(int argc, char **argv)
TT_SUITE_TEST(LibConfigTests, BigInt5); TT_SUITE_TEST(LibConfigTests, BigInt5);
TT_SUITE_TEST(LibConfigTests, BigInt6); TT_SUITE_TEST(LibConfigTests, BigInt6);
TT_SUITE_TEST(LibConfigTests, BigInt7); TT_SUITE_TEST(LibConfigTests, BigInt7);
TT_SUITE_TEST(LibConfigTests, RemoveSetting);
TT_SUITE_RUN(LibConfigTests); TT_SUITE_RUN(LibConfigTests);
failures = TT_SUITE_NUM_FAILURES(LibConfigTests); failures = TT_SUITE_NUM_FAILURES(LibConfigTests);
TT_SUITE_END(LibConfigTests); TT_SUITE_END(LibConfigTests);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment