From: Tobias Brunner Date: Tue, 11 Mar 2014 09:58:03 +0000 (+0100) Subject: settings: Add functions to add sections and key/value pairs to a section X-Git-Tag: 5.2.0dr4~1^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=725c479f8bbc2d05790c4794e86c37afeda45bff;p=thirdparty%2Fstrongswan.git settings: Add functions to add sections and key/value pairs to a section --- diff --git a/src/libstrongswan/settings/settings.c b/src/libstrongswan/settings/settings.c index b36d1dfcc0..3891578df1 100644 --- a/src/libstrongswan/settings/settings.c +++ b/src/libstrongswan/settings/settings.c @@ -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) { diff --git a/src/libstrongswan/settings/settings_parser.y b/src/libstrongswan/settings/settings_parser.y index fda349797a..b81003f297 100644 --- a/src/libstrongswan/settings/settings_parser.y +++ b/src/libstrongswan/settings/settings_parser.y @@ -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); } /** diff --git a/src/libstrongswan/settings/settings_types.c b/src/libstrongswan/settings/settings_types.c index 4976a3b6c8..8171bbc3e1 100644 --- a/src/libstrongswan/settings/settings_types.c +++ b/src/libstrongswan/settings/settings_types.c @@ -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(§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); } diff --git a/src/libstrongswan/settings/settings_types.h b/src/libstrongswan/settings/settings_types.h index 28e7fd6f5b..49f75dc16d 100644 --- a/src/libstrongswan/settings/settings_types.h +++ b/src/libstrongswan/settings/settings_types.h @@ -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