(void*)kv_filter, NULL, NULL);
}
+/**
+ * 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
*/
{
this->kv->destroy_function(this->kv, free);
this->sections->destroy_function(this->sections, (void*)section_destroy);
-
free(this);
}
/**
* 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))
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;
finished = TRUE;
continue;
}
- section_destroy(section);
- return NULL;
+ return FALSE;
}
- return section;
+ return TRUE;
}
METHOD(settings_t, destroy, void,
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;
}