Restore this code
This commit is contained in:
parent
ceca44b233
commit
42a3d433dc
|
@ -56,8 +56,7 @@ struct config_include_list
|
||||||
struct config_include_list *next;
|
struct config_include_list *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool config_file_initialize_internal(
|
static config_file_t *config_file_new_internal(
|
||||||
struct config_file *conf,
|
|
||||||
const char *path, unsigned depth, config_file_cb_t *cb);
|
const char *path, unsigned depth, config_file_cb_t *cb);
|
||||||
|
|
||||||
static int config_sort_compare_func(struct config_entry_list *a,
|
static int config_sort_compare_func(struct config_entry_list *a,
|
||||||
|
@ -325,6 +324,7 @@ static void add_child_list(config_file_t *parent, config_file_t *child)
|
||||||
static void add_sub_conf(config_file_t *conf, char *path, config_file_cb_t *cb)
|
static void add_sub_conf(config_file_t *conf, char *path, config_file_cb_t *cb)
|
||||||
{
|
{
|
||||||
char real_path[PATH_MAX_LENGTH];
|
char real_path[PATH_MAX_LENGTH];
|
||||||
|
config_file_t *sub_conf = NULL;
|
||||||
struct config_include_list *head = conf->includes;
|
struct config_include_list *head = conf->includes;
|
||||||
struct config_include_list *node = (struct config_include_list*)
|
struct config_include_list *node = (struct config_include_list*)
|
||||||
malloc(sizeof(*node));
|
malloc(sizeof(*node));
|
||||||
|
@ -367,22 +367,15 @@ static void add_sub_conf(config_file_t *conf, char *path, config_file_cb_t *cb)
|
||||||
path, sizeof(real_path));
|
path, sizeof(real_path));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{
|
sub_conf = (config_file_t*)
|
||||||
config_file_t *sub_conf = config_file_new_alloc();
|
config_file_new_internal(real_path, conf->include_depth + 1, cb);
|
||||||
if (!sub_conf)
|
if (!sub_conf)
|
||||||
return;
|
return;
|
||||||
if (!config_file_initialize_internal(
|
|
||||||
sub_conf, real_path, conf->include_depth + 1, cb))
|
|
||||||
{
|
|
||||||
config_file_free(conf);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Pilfer internal list. */
|
/* Pilfer internal list. */
|
||||||
add_child_list(conf, sub_conf);
|
add_child_list(conf, sub_conf);
|
||||||
config_file_free(sub_conf);
|
config_file_free(sub_conf);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static bool parse_line(config_file_t *conf,
|
static bool parse_line(config_file_t *conf,
|
||||||
struct config_entry_list *list, char *line, config_file_cb_t *cb)
|
struct config_entry_list *list, char *line, config_file_cb_t *cb)
|
||||||
|
@ -483,32 +476,17 @@ static bool parse_line(config_file_t *conf,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
config_file_t *config_file_new_alloc(void)
|
static config_file_t *config_file_new_internal(
|
||||||
{
|
|
||||||
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
|
|
||||||
if (!conf)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
conf->path = NULL;
|
|
||||||
conf->entries = NULL;
|
|
||||||
conf->tail = NULL;
|
|
||||||
conf->last = NULL;
|
|
||||||
conf->includes = NULL;
|
|
||||||
conf->include_depth = 0;
|
|
||||||
conf->guaranteed_no_duplicates = false;
|
|
||||||
conf->modified = false;
|
|
||||||
|
|
||||||
return conf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool config_file_initialize_internal(
|
|
||||||
config_file_t *conf,
|
|
||||||
const char *path, unsigned depth, config_file_cb_t *cb)
|
const char *path, unsigned depth, config_file_cb_t *cb)
|
||||||
{
|
{
|
||||||
RFILE *file = NULL;
|
RFILE *file = NULL;
|
||||||
|
struct config_file *conf = config_file_new_alloc();
|
||||||
|
|
||||||
|
if (!path || !*path)
|
||||||
|
return conf;
|
||||||
conf->path = strdup(path);
|
conf->path = strdup(path);
|
||||||
if (!conf->path)
|
if (!conf->path)
|
||||||
return false;
|
goto error;
|
||||||
|
|
||||||
conf->include_depth = depth;
|
conf->include_depth = depth;
|
||||||
file = filestream_open(path,
|
file = filestream_open(path,
|
||||||
|
@ -518,7 +496,7 @@ static bool config_file_initialize_internal(
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
free(conf->path);
|
free(conf->path);
|
||||||
return false;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!filestream_eof(file))
|
while (!filestream_eof(file))
|
||||||
|
@ -529,8 +507,9 @@ static bool config_file_initialize_internal(
|
||||||
|
|
||||||
if (!list)
|
if (!list)
|
||||||
{
|
{
|
||||||
|
config_file_free(conf);
|
||||||
filestream_close(file);
|
filestream_close(file);
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
list->readonly = false;
|
list->readonly = false;
|
||||||
|
@ -569,14 +548,22 @@ static bool config_file_initialize_internal(
|
||||||
|
|
||||||
filestream_close(file);
|
filestream_close(file);
|
||||||
|
|
||||||
return true;
|
return conf;
|
||||||
|
|
||||||
|
error:
|
||||||
|
free(conf);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void config_file_deinitialize_internal(config_file_t *conf)
|
void config_file_free(config_file_t *conf)
|
||||||
{
|
{
|
||||||
struct config_include_list *inc_tmp = NULL;
|
struct config_include_list *inc_tmp = NULL;
|
||||||
struct config_entry_list *tmp = conf->entries;
|
struct config_entry_list *tmp = NULL;
|
||||||
|
if (!conf)
|
||||||
|
return;
|
||||||
|
|
||||||
|
tmp = conf->entries;
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
struct config_entry_list *hold = NULL;
|
struct config_entry_list *hold = NULL;
|
||||||
|
@ -609,24 +596,9 @@ static void config_file_deinitialize_internal(config_file_t *conf)
|
||||||
|
|
||||||
if (conf->path)
|
if (conf->path)
|
||||||
free(conf->path);
|
free(conf->path);
|
||||||
}
|
|
||||||
|
|
||||||
void config_file_free(config_file_t *conf)
|
|
||||||
{
|
|
||||||
if (!conf)
|
|
||||||
return;
|
|
||||||
config_file_deinitialize_internal(conf);
|
|
||||||
free(conf);
|
free(conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool config_file_deinitialize(config_file_t *conf)
|
|
||||||
{
|
|
||||||
if (!conf)
|
|
||||||
return false;
|
|
||||||
config_file_deinitialize_internal(conf);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool config_append_file(config_file_t *conf, const char *path)
|
bool config_append_file(config_file_t *conf, const char *path)
|
||||||
{
|
{
|
||||||
config_file_t *new_conf = config_file_new_from_path_to_string(path);
|
config_file_t *new_conf = config_file_new_from_path_to_string(path);
|
||||||
|
@ -650,11 +622,20 @@ config_file_t *config_file_new_from_string(char *from_string,
|
||||||
char *lines = from_string;
|
char *lines = from_string;
|
||||||
char *save_ptr = NULL;
|
char *save_ptr = NULL;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
struct config_file *conf = NULL;
|
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
|
||||||
|
|
||||||
if (!(conf = config_file_new_alloc()))
|
if (!conf)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
conf->path = NULL;
|
||||||
|
conf->entries = NULL;
|
||||||
|
conf->tail = NULL;
|
||||||
|
conf->last = NULL;
|
||||||
|
conf->includes = NULL;
|
||||||
|
conf->include_depth = 0;
|
||||||
|
conf->guaranteed_no_duplicates = false;
|
||||||
|
conf->modified = false;
|
||||||
|
|
||||||
if (!string_is_empty(path))
|
if (!string_is_empty(path))
|
||||||
conf->path = strdup(path);
|
conf->path = strdup(path);
|
||||||
|
|
||||||
|
@ -728,31 +709,29 @@ config_file_t *config_file_new_from_path_to_string(const char *path)
|
||||||
config_file_t *config_file_new_with_callback(
|
config_file_t *config_file_new_with_callback(
|
||||||
const char *path, config_file_cb_t *cb)
|
const char *path, config_file_cb_t *cb)
|
||||||
{
|
{
|
||||||
struct config_file *conf = config_file_new_alloc();
|
return config_file_new_internal(path, 0, cb);
|
||||||
if (!conf)
|
|
||||||
return NULL;
|
|
||||||
if (!path || !*path)
|
|
||||||
return conf;
|
|
||||||
if (!config_file_initialize_internal(conf, path, 0, cb))
|
|
||||||
{
|
|
||||||
config_file_free(conf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return conf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config_file_t *config_file_new(const char *path)
|
config_file_t *config_file_new(const char *path)
|
||||||
{
|
{
|
||||||
struct config_file *conf = config_file_new_alloc();
|
return config_file_new_internal(path, 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
config_file_t *config_file_new_alloc(void)
|
||||||
|
{
|
||||||
|
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
|
||||||
if (!conf)
|
if (!conf)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!path || !*path)
|
|
||||||
return conf;
|
conf->path = NULL;
|
||||||
if (!config_file_initialize_internal(conf, path, 0, NULL))
|
conf->entries = NULL;
|
||||||
{
|
conf->tail = NULL;
|
||||||
config_file_free(conf);
|
conf->last = NULL;
|
||||||
return NULL;
|
conf->includes = NULL;
|
||||||
}
|
conf->include_depth = 0;
|
||||||
|
conf->guaranteed_no_duplicates = false;
|
||||||
|
conf->modified = false;
|
||||||
|
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -986,16 +965,10 @@ void config_set_string(config_file_t *conf, const char *key, const char *val)
|
||||||
if (!conf || !key || !val)
|
if (!conf || !key || !val)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
last = conf->entries;
|
last = (conf->guaranteed_no_duplicates && conf->last) ?
|
||||||
|
conf->last : conf->entries;
|
||||||
if (conf->guaranteed_no_duplicates)
|
entry = conf->guaranteed_no_duplicates ?
|
||||||
{
|
NULL : config_get_entry(conf, key, &last);
|
||||||
if (conf->last)
|
|
||||||
last = conf->last;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
entry = config_get_entry(conf, key, &last);
|
|
||||||
|
|
||||||
if (entry)
|
if (entry)
|
||||||
{
|
{
|
||||||
|
@ -1021,7 +994,6 @@ void config_set_string(config_file_t *conf, const char *key, const char *val)
|
||||||
conf->modified = true;
|
conf->modified = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Entry corresponding to 'key' does not exist
|
/* Entry corresponding to 'key' does not exist
|
||||||
* > Create new entry */
|
* > Create new entry */
|
||||||
|
|
Loading…
Reference in New Issue