settings: Add functions to add sections and key/value pairs to a section
This commit is contained in:
@@ -176,8 +176,7 @@ static section_t *find_section_buffered(section_t *section,
|
||||
if (ensure)
|
||||
{
|
||||
found = settings_section_create(strdup(buf));
|
||||
array_insert_create(§ion->sections, ARRAY_TAIL, found);
|
||||
array_sort(section->sections, settings_section_sort, NULL);
|
||||
settings_section_add(section, found, NULL);
|
||||
}
|
||||
}
|
||||
if (found && pos)
|
||||
@@ -388,8 +387,7 @@ static kv_t *find_value_buffered(section_t *section, char *start, char *key,
|
||||
if (ensure)
|
||||
{
|
||||
found = settings_section_create(strdup(buf));
|
||||
array_insert_create(§ion->sections, ARRAY_TAIL, found);
|
||||
array_sort(section->sections, settings_section_sort, NULL);
|
||||
settings_section_add(section, found, NULL);
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
@@ -418,8 +416,7 @@ static kv_t *find_value_buffered(section_t *section, char *start, char *key,
|
||||
if (ensure)
|
||||
{
|
||||
kv = settings_kv_create(strdup(buf), NULL);
|
||||
array_insert_create(§ion->kv, ARRAY_TAIL, kv);
|
||||
array_sort(section->kv, settings_kv_sort, NULL);
|
||||
settings_kv_add(section, kv, NULL);
|
||||
}
|
||||
else if (section->fallbacks)
|
||||
{
|
||||
|
||||
@@ -215,20 +215,10 @@ static section_t *pop_section(parser_helper_t *ctx)
|
||||
static void add_section(parser_helper_t *ctx, section_t *section)
|
||||
{
|
||||
array_t *sections = (array_t*)ctx->context;
|
||||
section_t *parent, *existing;
|
||||
section_t *parent;
|
||||
|
||||
array_get(sections, ARRAY_TAIL, &parent);
|
||||
if (array_bsearch(parent->sections, section->name, settings_section_find,
|
||||
&existing) == -1)
|
||||
{
|
||||
array_insert_create(&parent->sections, ARRAY_TAIL, section);
|
||||
array_sort(parent->sections, settings_section_sort, NULL);
|
||||
}
|
||||
else
|
||||
{ /* extend the existing section */
|
||||
settings_section_extend(existing, section, NULL);
|
||||
settings_section_destroy(section, NULL);
|
||||
}
|
||||
settings_section_add(parent, section, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,21 +228,9 @@ static void add_setting(parser_helper_t *ctx, kv_t *kv)
|
||||
{
|
||||
array_t *sections = (array_t*)ctx->context;
|
||||
section_t *section;
|
||||
kv_t *existing;
|
||||
|
||||
array_get(sections, ARRAY_TAIL, §ion);
|
||||
if (array_bsearch(section->kv, kv->key, settings_kv_find, &existing) == -1)
|
||||
{
|
||||
array_insert_create(§ion->kv, ARRAY_TAIL, kv);
|
||||
array_sort(section->kv, settings_kv_sort, NULL);
|
||||
}
|
||||
else
|
||||
{ /* move value to existing object */
|
||||
free(existing->value);
|
||||
existing->value = kv->value;
|
||||
kv->value = NULL;
|
||||
settings_kv_destroy(kv, NULL);
|
||||
}
|
||||
settings_kv_add(section, kv, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,7 +35,7 @@ kv_t *settings_kv_create(char *key, char *value)
|
||||
void settings_kv_destroy(kv_t *this, array_t *contents)
|
||||
{
|
||||
free(this->key);
|
||||
if (contents)
|
||||
if (contents && this->value)
|
||||
{
|
||||
array_insert(contents, ARRAY_TAIL, this->value);
|
||||
}
|
||||
@@ -81,6 +81,55 @@ void settings_section_destroy(section_t *this, array_t *contents)
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
void settings_kv_add(section_t *section, kv_t *kv, array_t *contents)
|
||||
{
|
||||
kv_t *found;
|
||||
|
||||
if (array_bsearch(section->kv, kv->key, settings_kv_find, &found) == -1)
|
||||
{
|
||||
array_insert_create(§ion->kv, ARRAY_TAIL, kv);
|
||||
array_sort(section->kv, settings_kv_sort, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (contents && found->value)
|
||||
{
|
||||
array_insert(contents, ARRAY_TAIL, found->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(found->value);
|
||||
}
|
||||
found->value = kv->value;
|
||||
kv->value = NULL;
|
||||
settings_kv_destroy(kv, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
void settings_section_add(section_t *parent, section_t *section,
|
||||
array_t *contents)
|
||||
{
|
||||
section_t *found;
|
||||
|
||||
if (array_bsearch(parent->sections, section->name, settings_section_find,
|
||||
&found) == -1)
|
||||
{
|
||||
array_insert_create(&parent->sections, ARRAY_TAIL, section);
|
||||
array_sort(parent->sections, settings_section_sort, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
settings_section_extend(found, section, contents);
|
||||
settings_section_destroy(section, contents);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
@@ -88,50 +137,22 @@ void settings_section_extend(section_t *base, section_t *extension,
|
||||
array_t *contents)
|
||||
{
|
||||
enumerator_t *enumerator;
|
||||
section_t *sec;
|
||||
section_t *section;
|
||||
kv_t *kv;
|
||||
|
||||
enumerator = array_create_enumerator(extension->sections);
|
||||
while (enumerator->enumerate(enumerator, (void**)&sec))
|
||||
while (enumerator->enumerate(enumerator, (void**)§ion))
|
||||
{
|
||||
section_t *found;
|
||||
if (array_bsearch(base->sections, sec->name, settings_section_find,
|
||||
&found) != -1)
|
||||
{
|
||||
settings_section_extend(found, sec, contents);
|
||||
}
|
||||
else
|
||||
{
|
||||
array_remove_at(extension->sections, enumerator);
|
||||
array_insert_create(&base->sections, ARRAY_TAIL, sec);
|
||||
array_sort(base->sections, settings_section_sort, NULL);
|
||||
}
|
||||
array_remove_at(extension->sections, enumerator);
|
||||
settings_section_add(base, section, contents);
|
||||
}
|
||||
enumerator->destroy(enumerator);
|
||||
|
||||
enumerator = array_create_enumerator(extension->kv);
|
||||
while (enumerator->enumerate(enumerator, (void**)&kv))
|
||||
{
|
||||
kv_t *found;
|
||||
if (array_bsearch(base->kv, kv->key, settings_kv_find, &found) != -1)
|
||||
{
|
||||
if (contents)
|
||||
{
|
||||
array_insert(contents, ARRAY_TAIL, found->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(found->value);
|
||||
}
|
||||
found->value = kv->value;
|
||||
kv->value = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
array_remove_at(extension->kv, enumerator);
|
||||
array_insert_create(&base->kv, ARRAY_TAIL, kv);
|
||||
array_sort(base->kv, settings_kv_sort, NULL);
|
||||
}
|
||||
array_remove_at(extension->kv, enumerator);
|
||||
settings_kv_add(base, kv, contents);
|
||||
}
|
||||
enumerator->destroy(enumerator);
|
||||
}
|
||||
|
||||
@@ -87,6 +87,15 @@ kv_t *settings_kv_create(char *key, char *value);
|
||||
*/
|
||||
void settings_kv_destroy(kv_t *this, array_t *contents);
|
||||
|
||||
/**
|
||||
* Add the given key/value pair to the given section.
|
||||
*
|
||||
* @param section section to add pair to
|
||||
* @param kv key/value pair to add (gets adopted)
|
||||
* @param contents optional array to store replaced values in
|
||||
*/
|
||||
void settings_kv_add(section_t *section, kv_t *kv, array_t *contents);
|
||||
|
||||
/**
|
||||
* Create a section with the given name.
|
||||
*
|
||||
@@ -103,10 +112,19 @@ section_t *settings_section_create(char *name);
|
||||
*/
|
||||
void settings_section_destroy(section_t *this, array_t *contents);
|
||||
|
||||
/**
|
||||
* Add the given section to the given parent section.
|
||||
*
|
||||
* @param parent section to add section to
|
||||
* @param section section to add (gets adopted)
|
||||
* @param contents optional array to store replaced values in
|
||||
*/
|
||||
void settings_section_add(section_t *parent, section_t *section,
|
||||
array_t *contents);
|
||||
|
||||
/**
|
||||
* Extend the first section with the values and sub-sections of the second
|
||||
* section, from where they are consequently moved/removed (but there might
|
||||
* still remain some leftovers).
|
||||
* section, from where they are consequently removed.
|
||||
*
|
||||
* @param base base section to extend
|
||||
* @param extension section whose data is extracted
|
||||
|
||||
Reference in New Issue
Block a user