Don't create a section in parse_section.
Just add subsections and values to the passed section.
This commit is contained in:
@@ -449,6 +449,20 @@ METHOD(settings_t, create_key_value_enumerator, enumerator_t*,
|
|||||||
(void*)kv_filter, NULL, NULL);
|
(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
|
* destroy a section
|
||||||
*/
|
*/
|
||||||
@@ -456,7 +470,6 @@ static void section_destroy(section_t *this)
|
|||||||
{
|
{
|
||||||
this->kv->destroy_function(this->kv, free);
|
this->kv->destroy_function(this->kv, free);
|
||||||
this->sections->destroy_function(this->sections, (void*)section_destroy);
|
this->sections->destroy_function(this->sections, (void*)section_destroy);
|
||||||
|
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -537,17 +550,11 @@ static char parse(char **text, char *skip, char *term, char *br, char **token)
|
|||||||
/**
|
/**
|
||||||
* Parse a section
|
* 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;
|
bool finished = FALSE;
|
||||||
char *key, *value, *inner;
|
char *key, *value, *inner;
|
||||||
|
|
||||||
section = malloc_thing(section_t);
|
|
||||||
section->name = name;
|
|
||||||
section->sections = linked_list_create();
|
|
||||||
section->kv = linked_list_create();
|
|
||||||
|
|
||||||
while (!finished)
|
while (!finished)
|
||||||
{
|
{
|
||||||
switch (parse(text, "\t\n ", "{=#", NULL, &key))
|
switch (parse(text, "\t\n ", "{=#", NULL, &key))
|
||||||
@@ -555,12 +562,15 @@ static section_t* parse_section(char **text, char *name)
|
|||||||
case '{':
|
case '{':
|
||||||
if (parse(text, "\t ", "}", "{", &inner))
|
if (parse(text, "\t ", "}", "{", &inner))
|
||||||
{
|
{
|
||||||
sub = parse_section(&inner, key);
|
section_t *sub = section_create(key);
|
||||||
if (sub)
|
if (parse_section(&inner, sub))
|
||||||
{
|
{
|
||||||
section->sections->insert_last(section->sections, sub);
|
section->sections->insert_last(section->sections, sub);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
section_destroy(sub);
|
||||||
|
DBG1(DBG_LIB, "parsing subsection '%s' failed", key);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
DBG1(DBG_LIB, "matching '}' not found near %s", *text);
|
DBG1(DBG_LIB, "matching '}' not found near %s", *text);
|
||||||
break;
|
break;
|
||||||
@@ -582,10 +592,9 @@ static section_t* parse_section(char **text, char *name)
|
|||||||
finished = TRUE;
|
finished = TRUE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
section_destroy(section);
|
return FALSE;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
return section;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(settings_t, destroy, void,
|
METHOD(settings_t, destroy, void,
|
||||||
@@ -646,11 +655,13 @@ settings_t *settings_create(char *file)
|
|||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
pos = this->text;
|
pos = this->text;
|
||||||
this->top = parse_section(&pos, NULL);
|
this->top = section_create(NULL);
|
||||||
if (this->top == NULL)
|
if (!parse_section(&pos, this->top))
|
||||||
{
|
{
|
||||||
free(this->text);
|
free(this->text);
|
||||||
this->text = NULL;
|
this->text = NULL;
|
||||||
|
section_destroy(this->top);
|
||||||
|
this->top = NULL;
|
||||||
}
|
}
|
||||||
return &this->public;
|
return &this->public;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user