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)
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)
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)
{
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);
}
/**
{
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);
}
/**
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);
}
/*
* Described in header
*/
-void settings_section_extend(section_t *base, section_t *extension,
- array_t *contents)
+void settings_kv_add(section_t *section, kv_t *kv, array_t *contents)
{
- enumerator_t *enumerator;
- section_t *sec;
- kv_t *kv;
+ kv_t *found;
- enumerator = array_create_enumerator(extension->sections);
- while (enumerator->enumerate(enumerator, (void**)&sec))
+ if (array_bsearch(section->kv, kv->key, settings_kv_find, &found) == -1)
{
- section_t *found;
- if (array_bsearch(base->sections, sec->name, settings_section_find,
- &found) != -1)
+ array_insert_create(§ion->kv, ARRAY_TAIL, kv);
+ array_sort(section->kv, settings_kv_sort, NULL);
+ }
+ else
+ {
+ if (contents && found->value)
{
- settings_section_extend(found, sec, contents);
+ array_insert(contents, ARRAY_TAIL, found->value);
}
else
{
- array_remove_at(extension->sections, enumerator);
- array_insert_create(&base->sections, ARRAY_TAIL, sec);
- array_sort(base->sections, settings_section_sort, NULL);
+ 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
+ */
+void settings_section_extend(section_t *base, section_t *extension,
+ array_t *contents)
+{
+ enumerator_t *enumerator;
+ section_t *section;
+ kv_t *kv;
+
+ enumerator = array_create_enumerator(extension->sections);
+ while (enumerator->enumerate(enumerator, (void**)§ion))
+ {
+ 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);
}
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.
*
* @param name name (gets adopted)
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