settings: Add functions to add sections and key/value pairs to a section
authorTobias Brunner <tobias@strongswan.org>
Tue, 11 Mar 2014 09:58:03 +0000 (10:58 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 May 2014 09:28:07 +0000 (11:28 +0200)
src/libstrongswan/settings/settings.c
src/libstrongswan/settings/settings_parser.y
src/libstrongswan/settings/settings_types.c
src/libstrongswan/settings/settings_types.h

index b36d1df..3891578 100644 (file)
@@ -176,8 +176,7 @@ static section_t *find_section_buffered(section_t *section,
                if (ensure)
                {
                        found = settings_section_create(strdup(buf));
-                       array_insert_create(&section->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(&section->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(&section->kv, ARRAY_TAIL, kv);
-                               array_sort(section->kv, settings_kv_sort, NULL);
+                               settings_kv_add(section, kv, NULL);
                        }
                        else if (section->fallbacks)
                        {
index fda3497..b81003f 100644 (file)
@@ -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, &section);
-       if (array_bsearch(section->kv, kv->key, settings_kv_find, &existing) == -1)
-       {
-               array_insert_create(&section->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);
 }
 
 /**
index 4976a3b..8171bbc 100644 (file)
@@ -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);
        }
@@ -84,54 +84,75 @@ void settings_section_destroy(section_t *this, array_t *contents)
 /*
  * 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(&section->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**)&section))
+       {
+               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);
 }
index 28e7fd6..49f75dc 100644 (file)
@@ -88,6 +88,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.
  *
  * @param name         name (gets adopted)
@@ -104,9 +113,18 @@ 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