section_t *top;
/**
- * allocated file text
+ * text of loaded files
*/
- char *text;
+ linked_list_t *files;
};
/**
return TRUE;
}
+/**
+ * parse a file and add the settings to the given section.
+ */
+static bool parse_file(private_settings_t *this, char *file, section_t *section)
+{
+ bool success;
+ char *text, *pos;
+ FILE *fd;
+ int len;
+
+ DBG2(DBG_LIB, "loading config file '%s'", file);
+ fd = fopen(file, "r");
+ if (fd == NULL)
+ {
+ DBG1(DBG_LIB, "'%s' does not exist or is not readable", file);
+ return FALSE;
+ }
+ fseek(fd, 0, SEEK_END);
+ len = ftell(fd);
+ rewind(fd);
+ text = malloc(len + 1);
+ text[len] = '\0';
+ if (fread(text, 1, len, fd) != len)
+ {
+ free(text);
+ return FALSE;
+ }
+ fclose(fd);
+
+ pos = text;
+ success = parse_section(&pos, section);
+ if (!success)
+ {
+ free(text);
+ }
+ else
+ {
+ this->files->insert_last(this->files, text);
+ }
+ return success;
+}
+
METHOD(settings_t, destroy, void,
private_settings_t *this)
{
{
section_destroy(this->top);
}
- free(this->text);
+ this->files->destroy_function(this->files, (void*)free);
free(this);
}
settings_t *settings_create(char *file)
{
private_settings_t *this;
- char *pos;
- FILE *fd;
- int len;
INIT(this,
.public = {
.create_key_value_enumerator = _create_key_value_enumerator,
.destroy = _destroy,
},
+ .files = linked_list_create(),
);
if (file == NULL)
{
file = STRONGSWAN_CONF;
}
- fd = fopen(file, "r");
- if (fd == NULL)
- {
- DBG1(DBG_LIB, "'%s' does not exist or is not readable", file);
- return &this->public;
- }
- fseek(fd, 0, SEEK_END);
- len = ftell(fd);
- rewind(fd);
- this->text = malloc(len + 1);
- this->text[len] = '\0';
- if (fread(this->text, 1, len, fd) != len)
- {
- free(this->text);
- this->text = NULL;
- return &this->public;
- }
- fclose(fd);
- pos = this->text;
this->top = section_create(NULL);
- if (!parse_section(&pos, this->top))
+ if (!parse_file(this, file, this->top))
{
- free(this->text);
- this->text = NULL;
section_destroy(this->top);
this->top = NULL;
}