(config_file.c) Cleanups

This commit is contained in:
twinaphex 2019-04-26 06:28:10 +02:00
parent 89563ce9c8
commit ddace03872

View File

@ -74,17 +74,13 @@ static config_file_t *config_file_new_internal(
static int config_sort_compare_func(struct config_entry_list *a,
struct config_entry_list *b)
{
const char *a_key = a ? a->key : NULL;
const char *b_key = b ? b->key : NULL;
if (!a_key || !b_key)
return 0;
return strcasecmp(a_key, b_key);
return (a && b) ? strcasecmp(a->key, b->key) : 0;
}
/* https://stackoverflow.com/questions/7685/merge-sort-a-linked-list */
static struct config_entry_list* merge_sort_linked_list(struct config_entry_list *list, int (*compare)(struct config_entry_list *one,struct config_entry_list *two))
static struct config_entry_list* merge_sort_linked_list(
struct config_entry_list *list, int (*compare)(
struct config_entry_list *one,struct config_entry_list *two))
{
struct config_entry_list
*right = list,
@ -98,15 +94,17 @@ static struct config_entry_list* merge_sort_linked_list(struct config_entry_list
if (!list || !list->next)
return list;
/* Find halfway through the list (by running two pointers, one at twice the speed of the other). */
/* Find halfway through the list (by running two pointers,
* one at twice the speed of the other). */
while (temp && temp->next)
{
last = right;
last = right;
right = right->next;
temp = temp->next->next;
temp = temp->next->next;
}
/* Break the list in two. (prev pointers are broken here, but we fix later) */
/* Break the list in two. (prev pointers are broken here,
* but we fix later) */
last->next = 0;
/* Recurse on the two smaller lists: */
@ -331,10 +329,10 @@ static bool parse_line(config_file_t *conf,
struct config_entry_list *list, char *line, config_file_cb_t *cb,
bool no_checks)
{
char *key = NULL;
char *key_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;
char *key = (char*)malloc(9);
char *comment = strip_comment(line);
/* Starting line with #include includes config files. */
@ -346,15 +344,14 @@ static bool parse_line(config_file_t *conf,
char *line = comment + strlen("include ");
char *path = extract_value(line, false);
if (path)
{
if (conf->include_depth >= MAX_INCLUDE_DEPTH)
fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n");
else
add_sub_conf(conf, path, cb, no_checks);
free(path);
}
goto error;
if (!path)
return false;
if (conf->include_depth >= MAX_INCLUDE_DEPTH)
fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n");
else
add_sub_conf(conf, path, cb, no_checks);
free(path);
}
}
@ -362,15 +359,20 @@ static bool parse_line(config_file_t *conf,
while (isspace((int)*line))
line++;
key = (char*)malloc(9);
while (isgraph((int)*line))
{
if (idx == cur_size)
{
cur_size *= 2;
key_tmp = (char*)realloc(key, cur_size + 1);
key_tmp = (char*)realloc(key, cur_size + 1);
if (!key_tmp)
goto error;
{
free(key);
return false;
}
key = key_tmp;
}
@ -385,14 +387,11 @@ static bool parse_line(config_file_t *conf,
if (!list->value)
{
list->key = NULL;
goto error;
free(key);
return false;
}
return true;
error:
free(key);
return false;
}
static config_file_t *config_file_new_internal(
@ -436,7 +435,8 @@ static config_file_t *config_file_new_internal(
while (!filestream_eof(file))
{
char *line = NULL;
struct config_entry_list *list = (struct config_entry_list*)malloc(sizeof(*list));
struct config_entry_list *list = (struct config_entry_list*)
malloc(sizeof(*list));
if (!list)
{
@ -463,7 +463,7 @@ static config_file_t *config_file_new_internal(
if (conf->entries)
conf->tail->next = list;
else
conf->entries = list;
conf->entries = list;
conf->tail = list;
@ -565,13 +565,14 @@ config_file_t *config_file_new_from_string(const char *from_string)
conf->include_depth = 0;
conf->guaranteed_no_duplicates = false ;
lines = string_split(from_string, "\n");
lines = string_split(from_string, "\n");
if (!lines)
return conf;
for (i = 0; i < lines->size; i++)
{
struct config_entry_list *list = (struct config_entry_list*)malloc(sizeof(*list));
struct config_entry_list *list = (struct config_entry_list*)
malloc(sizeof(*list));
char *line = lines->elems[i].data;
if (!list)
@ -593,9 +594,9 @@ config_file_t *config_file_new_from_string(const char *from_string)
if (conf->entries)
conf->tail->next = list;
else
conf->entries = list;
conf->entries = list;
conf->tail = list;
conf->tail = list;
}
}
@ -608,10 +609,12 @@ config_file_t *config_file_new_from_string(const char *from_string)
return conf;
}
config_file_t *config_file_new_with_callback(const char *path, config_file_cb_t *cb)
config_file_t *config_file_new_with_callback(
const char *path, config_file_cb_t *cb)
{
return config_file_new_internal(path, 0, cb, false);
}
config_file_t *config_file_new(const char *path)
{
return config_file_new_internal(path, 0, NULL, false);
@ -622,7 +625,8 @@ config_file_t *config_file_read(const char *path)
return config_file_new_internal(path, 0, NULL, true);
}
static struct config_entry_list *config_get_entry(const config_file_t *conf,
static struct config_entry_list *config_get_entry(
const config_file_t *conf,
const char *key, struct config_entry_list **prev)
{
struct config_entry_list *entry = NULL;
@ -646,26 +650,23 @@ bool config_get_double(config_file_t *conf, const char *key, double *in)
{
const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry)
{
*in = strtod(entry->value, NULL);
return true;
}
if (!entry)
return false;
return false;
*in = strtod(entry->value, NULL);
return true;
}
bool config_get_float(config_file_t *conf, const char *key, float *in)
{
const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry)
{
/* strtof() is C99/POSIX. Just use the more portable kind. */
*in = (float)strtod(entry->value, NULL);
return true;
}
return false;
if (!entry)
return false;
/* strtof() is C99/POSIX. Just use the more portable kind. */
*in = (float)strtod(entry->value, NULL);
return true;
}
bool config_get_int(config_file_t *conf, const char *key, int *in)
@ -783,19 +784,17 @@ bool config_get_string(config_file_t *conf, const char *key, char **str)
{
const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry)
{
*str = strdup(entry->value);
return true;
}
return false;
if (!entry)
return false;
*str = strdup(entry->value);
return true;
}
bool config_get_config_path(config_file_t *conf, char *s, size_t len)
{
if (!conf)
return false;
return strlcpy(s, conf->path, len);
}
@ -877,8 +876,7 @@ void config_set_string(config_file_t *conf, const char *key, const char *val)
else
conf->entries = entry;
conf->last = entry ;
conf->last = entry;
}
void config_unset(config_file_t *conf, const char *key)
@ -988,7 +986,7 @@ bool config_file_write(config_file_t *conf, const char *path, bool sort)
{
void* buf = NULL;
#ifdef ORBIS
int fd = orbisOpen(path,O_RDWR|O_CREAT,0644);
int fd = orbisOpen(path,O_RDWR|O_CREAT,0644);
RARCH_LOG("[Config]config_file_write orbisOpen path=%s fd=%d\n", path, fd);
if (fd < 0)
return false;
@ -1060,7 +1058,8 @@ void config_file_dump(config_file_t *conf, FILE *file, bool sort)
}
if (sort)
list = merge_sort_linked_list((struct config_entry_list*)conf->entries, config_sort_compare_func);
list = merge_sort_linked_list((struct config_entry_list*)
conf->entries, config_sort_compare_func);
else
list = (struct config_entry_list*)conf->entries;