Revert config_file.c

This commit is contained in:
twinaphex 2017-12-20 02:52:01 +01:00
parent 5eb6c5dae9
commit 2f422a457f

View File

@ -44,7 +44,6 @@
#include <file/file_path.h> #include <file/file_path.h>
#include <lists/string_list.h> #include <lists/string_list.h>
#include <string/stdstring.h> #include <string/stdstring.h>
#include <streams/file_stream.h>
#define MAX_INCLUDE_DEPTH 16 #define MAX_INCLUDE_DEPTH 16
@ -78,6 +77,40 @@ struct config_file
static config_file_t *config_file_new_internal( static config_file_t *config_file_new_internal(
const char *path, unsigned depth); const char *path, unsigned depth);
static char *getaline(FILE *file)
{
char* newline = (char*)malloc(9);
char* newline_tmp = NULL;
size_t cur_size = 8;
size_t idx = 0;
int in = fgetc(file);
if (!newline)
return NULL;
while (in != EOF && in != '\n')
{
if (idx == cur_size)
{
cur_size *= 2;
newline_tmp = (char*)realloc(newline, cur_size + 1);
if (!newline_tmp)
{
free(newline);
return NULL;
}
newline = newline_tmp;
}
newline[idx++] = in;
in = fgetc(file);
}
newline[idx] = '\0';
return newline;
}
static char *strip_comment(char *str) static char *strip_comment(char *str)
{ {
/* Remove everything after comment. /* Remove everything after comment.
@ -141,17 +174,16 @@ static char *extract_value(char *line, bool is_value)
if (*line == '"') if (*line == '"')
{ {
line++; line++;
if (*line == '"') return strdup("");
tok = strtok_r(line, "\"", &save); tok = strtok_r(line, "\"", &save);
goto end;
} }
else if (*line == '\0') /* Nothing */ else if (*line == '\0') /* Nothing */
return NULL; return NULL;
else
{
/* We don't have that. Read until next space. */
tok = strtok_r(line, " \n\t\f\r\v", &save);
}
/* We don't have that. Read until next space. */
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;
@ -275,8 +307,8 @@ static bool parse_line(config_file_t *conf,
comment = strip_comment(line); comment = strip_comment(line);
/* Starting line with #include includes config files. */ /* Starting line with # and include includes config files. */
if (comment == line) if ((comment == line) && (conf->include_depth < MAX_INCLUDE_DEPTH))
{ {
comment++; comment++;
if (strstr(comment, "include ") == comment) if (strstr(comment, "include ") == comment)
@ -284,15 +316,14 @@ static bool parse_line(config_file_t *conf,
char *line = comment + strlen("include "); char *line = comment + strlen("include ");
char *path = extract_value(line, false); char *path = extract_value(line, false);
if (path) if (path)
{ add_sub_conf(conf, 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);
}
goto error; goto error;
} }
} }
else if (conf->include_depth >= MAX_INCLUDE_DEPTH)
{
fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n");
}
/* Skips to first character. */ /* Skips to first character. */
while (isspace((int)*line)) while (isspace((int)*line))
@ -313,11 +344,10 @@ static bool parse_line(config_file_t *conf,
key[idx++] = *line++; key[idx++] = *line++;
} }
key[idx] = '\0'; key[idx] = '\0';
list->key = key; list->key = key;
list->value = extract_value(line, true);
list->value = extract_value(line, true);
if (!list->value) if (!list->value)
{ {
list->key = NULL; list->key = NULL;
@ -334,7 +364,7 @@ error:
static config_file_t *config_file_new_internal( static config_file_t *config_file_new_internal(
const char *path, unsigned depth) const char *path, unsigned depth)
{ {
RFILE *file = NULL; FILE *file = NULL;
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf)); struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
if (!conf) if (!conf)
return NULL; return NULL;
@ -356,9 +386,7 @@ static config_file_t *config_file_new_internal(
goto error; goto error;
conf->include_depth = depth; conf->include_depth = depth;
file = filestream_open(path, file = fopen_utf8(path, "r");
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!file) if (!file)
{ {
@ -366,7 +394,7 @@ static config_file_t *config_file_new_internal(
goto error; goto error;
} }
while (!filestream_eof(file)) while (!feof(file))
{ {
char *line = NULL; 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));
@ -374,7 +402,7 @@ static config_file_t *config_file_new_internal(
if (!list) if (!list)
{ {
config_file_free(conf); config_file_free(conf);
filestream_close(file); fclose(file);
return NULL; return NULL;
} }
@ -383,7 +411,7 @@ static config_file_t *config_file_new_internal(
list->value = NULL; list->value = NULL;
list->next = NULL; list->next = NULL;
line = filestream_getline(file); line = getaline(file);
if (!line) if (!line)
{ {
@ -407,7 +435,7 @@ static config_file_t *config_file_new_internal(
free(list); free(list);
} }
filestream_close(file); fclose(file);
return conf; return conf;
@ -544,8 +572,11 @@ config_file_t *config_file_new(const char *path)
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)
{ {
struct config_entry_list *entry = NULL; struct config_entry_list *entry;
struct config_entry_list *previous = prev ? *prev : NULL; struct config_entry_list *previous = NULL;
if (prev)
previous = *prev;
for (entry = conf->entries; entry; entry = entry->next) for (entry = conf->entries; entry; entry = entry->next)
{ {