diff options
author | Tobias Brunner <tobias@strongswan.org> | 2010-11-11 15:21:25 +0100 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2010-12-03 17:38:37 +0100 |
commit | 3eb23f3864e4f56f55b00d8519f3f034c8f12f35 (patch) | |
tree | 0df11622bb8da91162c8c1458d1bef58ea979ced | |
parent | f6037d79cf039791b3688f33b14c5aec63fcecf3 (diff) | |
download | strongswan-3eb23f3864e4f56f55b00d8519f3f034c8f12f35.tar.bz2 strongswan-3eb23f3864e4f56f55b00d8519f3f034c8f12f35.tar.xz |
Don't create a section in parse_section.
Just add subsections and values to the passed section.
-rw-r--r-- | src/libstrongswan/settings.c | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/libstrongswan/settings.c b/src/libstrongswan/settings.c index 228c6e377..034539491 100644 --- a/src/libstrongswan/settings.c +++ b/src/libstrongswan/settings.c @@ -450,13 +450,26 @@ METHOD(settings_t, create_key_value_enumerator, enumerator_t*, } /** + * create a section with the given name + */ +static section_t *section_create(char *name) +{ + section_t *this; + INIT(this, + .name = name, + .sections = linked_list_create(), + .kv = linked_list_create(), + ); + return this; +} + +/** * destroy a section */ static void section_destroy(section_t *this) { this->kv->destroy_function(this->kv, free); this->sections->destroy_function(this->sections, (void*)section_destroy); - free(this); } @@ -537,17 +550,11 @@ static char parse(char **text, char *skip, char *term, char *br, char **token) /** * Parse a section */ -static section_t* parse_section(char **text, char *name) +static bool parse_section(char **text, section_t *section) { - section_t *sub, *section; bool finished = FALSE; char *key, *value, *inner; - section = malloc_thing(section_t); - section->name = name; - section->sections = linked_list_create(); - section->kv = linked_list_create(); - while (!finished) { switch (parse(text, "\t\n ", "{=#", NULL, &key)) @@ -555,12 +562,15 @@ static section_t* parse_section(char **text, char *name) case '{': if (parse(text, "\t ", "}", "{", &inner)) { - sub = parse_section(&inner, key); - if (sub) + section_t *sub = section_create(key); + if (parse_section(&inner, sub)) { section->sections->insert_last(section->sections, sub); continue; } + section_destroy(sub); + DBG1(DBG_LIB, "parsing subsection '%s' failed", key); + break; } DBG1(DBG_LIB, "matching '}' not found near %s", *text); break; @@ -582,10 +592,9 @@ static section_t* parse_section(char **text, char *name) finished = TRUE; continue; } - section_destroy(section); - return NULL; + return FALSE; } - return section; + return TRUE; } METHOD(settings_t, destroy, void, @@ -646,11 +655,13 @@ settings_t *settings_create(char *file) fclose(fd); pos = this->text; - this->top = parse_section(&pos, NULL); - if (this->top == NULL) + this->top = section_create(NULL); + if (!parse_section(&pos, this->top)) { free(this->text); this->text = NULL; + section_destroy(this->top); + this->top = NULL; } return &this->public; } |