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 <lists/string_list.h>
#include <string/stdstring.h>
#include <streams/file_stream.h>
#define MAX_INCLUDE_DEPTH 16
@ -78,6 +77,40 @@ struct config_file
static config_file_t *config_file_new_internal(
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)
{
/* Remove everything after comment.
@ -141,17 +174,16 @@ static char *extract_value(char *line, bool is_value)
if (*line == '"')
{
line++;
if (*line == '"') return strdup("");
tok = strtok_r(line, "\"", &save);
goto end;
}
else if (*line == '\0') /* Nothing */
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)
return strdup(tok);
return NULL;
@ -191,7 +223,7 @@ static void add_child_list(config_file_t *parent, config_file_t *child)
/* Rebase tail. */
if (parent->entries)
{
struct config_entry_list *head =
struct config_entry_list *head =
(struct config_entry_list*)parent->entries;
while (head->next)
@ -275,8 +307,8 @@ static bool parse_line(config_file_t *conf,
comment = strip_comment(line);
/* Starting line with #include includes config files. */
if (comment == line)
/* Starting line with # and include includes config files. */
if ((comment == line) && (conf->include_depth < MAX_INCLUDE_DEPTH))
{
comment++;
if (strstr(comment, "include ") == comment)
@ -284,15 +316,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);
}
add_sub_conf(conf, path);
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. */
while (isspace((int)*line))
@ -313,11 +344,10 @@ static bool parse_line(config_file_t *conf,
key[idx++] = *line++;
}
key[idx] = '\0';
list->key = key;
list->value = extract_value(line, true);
key[idx] = '\0';
list->key = key;
list->value = extract_value(line, true);
if (!list->value)
{
list->key = NULL;
@ -334,7 +364,7 @@ error:
static config_file_t *config_file_new_internal(
const char *path, unsigned depth)
{
RFILE *file = NULL;
FILE *file = NULL;
struct config_file *conf = (struct config_file*)malloc(sizeof(*conf));
if (!conf)
return NULL;
@ -356,9 +386,7 @@ static config_file_t *config_file_new_internal(
goto error;
conf->include_depth = depth;
file = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
file = fopen_utf8(path, "r");
if (!file)
{
@ -366,7 +394,7 @@ static config_file_t *config_file_new_internal(
goto error;
}
while (!filestream_eof(file))
while (!feof(file))
{
char *line = NULL;
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)
{
config_file_free(conf);
filestream_close(file);
fclose(file);
return NULL;
}
@ -383,7 +411,7 @@ static config_file_t *config_file_new_internal(
list->value = NULL;
list->next = NULL;
line = filestream_getline(file);
line = getaline(file);
if (!line)
{
@ -407,7 +435,7 @@ static config_file_t *config_file_new_internal(
free(list);
}
filestream_close(file);
fclose(file);
return conf;
@ -492,7 +520,7 @@ config_file_t *config_file_new_from_string(const char *from_string)
conf->tail = NULL;
conf->includes = NULL;
conf->include_depth = 0;
lines = string_split(from_string, "\n");
if (!lines)
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,
const char *key, struct config_entry_list **prev)
{
struct config_entry_list *entry = NULL;
struct config_entry_list *previous = prev ? *prev : NULL;
struct config_entry_list *entry;
struct config_entry_list *previous = NULL;
if (prev)
previous = *prev;
for (entry = conf->entries; entry; entry = entry->next)
{