config_file.c - cleanups

This commit is contained in:
twinaphex 2017-11-07 08:08:22 +01:00
parent f681cb455c
commit b05b27bff6
1 changed files with 39 additions and 23 deletions

View File

@ -177,15 +177,15 @@ static char *extract_value(char *line, bool is_value)
{ {
line++; line++;
tok = strtok_r(line, "\"", &save); tok = strtok_r(line, "\"", &save);
if (!tok) goto end;
return NULL;
return strdup(tok);
} }
else if (*line == '\0') /* Nothing */ else if (*line == '\0') /* Nothing */
return NULL; return NULL;
/* We don't have that. Read until next space. */ /* We don't have that. Read until next space. */
tok = strtok_r(line, " \n\t\f\r\v", &save); tok = strtok_r(line, " \n\t\f\r\v", &save);
end:
if (tok) if (tok)
return strdup(tok); return strdup(tok);
return NULL; return NULL;
@ -575,7 +575,6 @@ config_file_t *config_file_new(const char *path)
return config_file_new_internal(path, 0); return config_file_new_internal(path, 0);
} }
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) const char *key, struct config_entry_list **prev)
{ {
@ -606,9 +605,12 @@ 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); const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry) if (entry)
{
*in = strtod(entry->value, NULL); *in = strtod(entry->value, NULL);
return true;
}
return entry != NULL; return false;
} }
bool config_get_float(config_file_t *conf, const char *key, float *in) bool config_get_float(config_file_t *conf, const char *key, float *in)
@ -619,9 +621,9 @@ bool config_get_float(config_file_t *conf, const char *key, float *in)
{ {
/* strtof() is C99/POSIX. Just use the more portable kind. */ /* strtof() is C99/POSIX. Just use the more portable kind. */
*in = (float)strtod(entry->value, NULL); *in = (float)strtod(entry->value, NULL);
return true;
} }
return false;
return entry != NULL;
} }
bool config_get_int(config_file_t *conf, const char *key, int *in) bool config_get_int(config_file_t *conf, const char *key, int *in)
@ -634,10 +636,13 @@ bool config_get_int(config_file_t *conf, const char *key, int *in)
int val = (int)strtol(entry->value, NULL, 0); int val = (int)strtol(entry->value, NULL, 0);
if (errno == 0) if (errno == 0)
{
*in = val; *in = val;
return true;
}
} }
return entry != NULL && errno == 0; return false;
} }
#if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L #if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
@ -651,10 +656,12 @@ bool config_get_uint64(config_file_t *conf, const char *key, uint64_t *in)
uint64_t val = strtoull(entry->value, NULL, 0); uint64_t val = strtoull(entry->value, NULL, 0);
if (errno == 0) if (errno == 0)
{
*in = val; *in = val;
return true;
} }
}
return entry != NULL && errno == 0; return false;
} }
#endif #endif
@ -668,10 +675,13 @@ bool config_get_uint(config_file_t *conf, const char *key, unsigned *in)
unsigned val = (unsigned)strtoul(entry->value, NULL, 0); unsigned val = (unsigned)strtoul(entry->value, NULL, 0);
if (errno == 0) if (errno == 0)
{
*in = val; *in = val;
return true;
}
} }
return entry != NULL && errno == 0; return false;
} }
bool config_get_hex(config_file_t *conf, const char *key, unsigned *in) bool config_get_hex(config_file_t *conf, const char *key, unsigned *in)
@ -684,10 +694,13 @@ bool config_get_hex(config_file_t *conf, const char *key, unsigned *in)
unsigned val = (unsigned)strtoul(entry->value, NULL, 16); unsigned val = (unsigned)strtoul(entry->value, NULL, 16);
if (errno == 0) if (errno == 0)
{
*in = val; *in = val;
return true;
}
} }
return entry != NULL && errno == 0; return false;
} }
bool config_get_char(config_file_t *conf, const char *key, char *in) bool config_get_char(config_file_t *conf, const char *key, char *in)
@ -700,9 +713,10 @@ bool config_get_char(config_file_t *conf, const char *key, char *in)
return false; return false;
*in = *entry->value; *in = *entry->value;
return true;
} }
return entry != NULL; return false;
} }
bool config_get_string(config_file_t *conf, const char *key, char **str) bool config_get_string(config_file_t *conf, const char *key, char **str)
@ -710,9 +724,11 @@ 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); const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry) if (entry)
{
*str = strdup(entry->value); *str = strdup(entry->value);
return true;
return entry != NULL; }
return false;
} }
bool config_get_config_path(config_file_t *conf, char *s, size_t len) bool config_get_config_path(config_file_t *conf, char *s, size_t len)
@ -730,23 +746,25 @@ bool config_get_array(config_file_t *conf, const char *key,
if (entry) if (entry)
return strlcpy(buf, entry->value, size) < size; return strlcpy(buf, entry->value, size) < size;
return false;
return entry != NULL;
} }
bool config_get_path(config_file_t *conf, const char *key, bool config_get_path(config_file_t *conf, const char *key,
char *buf, size_t size) char *buf, size_t size)
{ {
#if defined(RARCH_CONSOLE) #if defined(RARCH_CONSOLE)
return config_get_array(conf, key, buf, size); if (config_get_array(conf, key, buf, size))
return true;
#else #else
const struct config_entry_list *entry = config_get_entry(conf, key, NULL); const struct config_entry_list *entry = config_get_entry(conf, key, NULL);
if (entry) if (entry)
{
fill_pathname_expand_special(buf, entry->value, size); fill_pathname_expand_special(buf, entry->value, size);
return true;
return entry != NULL; }
#endif #endif
return false;
} }
bool config_get_bool(config_file_t *conf, const char *key, bool *in) bool config_get_bool(config_file_t *conf, const char *key, bool *in)
@ -911,9 +929,7 @@ bool config_file_write(config_file_t *conf, const char *path)
config_file_dump(conf, filestream_get_fp(file)); config_file_dump(conf, filestream_get_fp(file));
} }
else else
{
config_file_dump(conf, stdout); config_file_dump(conf, stdout);
}
if (file) if (file)
filestream_close(file); filestream_close(file);