From 0be7e726ad839d36d50db630b24ea0f1dc141c08 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Sun, 5 Jul 2015 14:21:34 -0700 Subject: WIP changes to support reading cart memory map from ROM DB --- config.c | 60 ++++++++++++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 36 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index eab0308..6fd29b5 100644 --- a/config.c +++ b/config.c @@ -9,8 +9,6 @@ #include #include -#define MAX_NEST 30 //way more than I'll ever need - #ifdef _WIN32 char * strtok_r(char * input, char * sep, char ** state) { @@ -32,69 +30,59 @@ char * strtok_r(char * input, char * sep, char ** state) } #endif -tern_node * parse_config(char * config_data) +tern_node * parse_config_int(char **state, int started, int *line) { - char *state, *curline; - char *prefix = NULL; - int nest_level = 0; - char * prefix_parts[MAX_NEST]; - int line = 1; + char *config_data, *curline; tern_node * head = NULL; - while ((curline = strtok_r(config_data, "\n", &state))) + config_data = started ? NULL : *state; + while ((curline = strtok_r(config_data, "\n", state))) { + config_data = NULL; curline = strip_ws(curline); int len = strlen(curline); if (!len) { + *line++; continue; } if (curline[0] == '#') { + *line++; continue; } if (curline[0] == '}') { - if (!nest_level) { - fprintf(stderr, "unexpected } on line %d\n", line); - exit(1); + if (started) { + return head; } - if (prefix) { - free(prefix); - prefix = NULL; - } - nest_level--; - curline = strip_ws(curline+1); + fprintf(stderr, "unexpected } on line %d\n", *line); + exit(1); } + char * end = curline + len - 1; if (*end == '{') { *end = 0; curline = strip_ws(curline); - prefix_parts[nest_level++] = curline; - if (prefix) { - free(prefix); - prefix = NULL; - } + *line++; + head = tern_insert_node(head, curline, parse_config_int(state, 1, line)); } else { - if (nest_level && !prefix) { - prefix = alloc_concat_m(nest_level, prefix_parts); - } char * val = strip_ws(split_keyval(curline)); char * key = curline; - if (*key) { - if (prefix) { - key = alloc_concat(prefix, key); - } + if (*val) { head = tern_insert_ptr(head, key, strdup(val)); - if (prefix) { - free(key); - } + } else { + fprintf(stderr, "Key %s is missing a value on line %d\n", key, *line); } + *line++; } } - if (prefix) { - free(prefix); - } return head; } +tern_node * parse_config(char * config_data) +{ + int line = 1; + return parse_config_int(&config_data, 0, &line); +} + tern_node * parse_config_file(char * config_path) { tern_node * ret = NULL; -- cgit v1.2.3 From 80ff833dd8ad011b579bff26ac654819e6735bce Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Sat, 25 Jul 2015 18:22:07 -0700 Subject: Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows). --- config.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 6fd29b5..9d71ceb 100644 --- a/config.c +++ b/config.c @@ -53,8 +53,7 @@ tern_node * parse_config_int(char **state, int started, int *line) if (started) { return head; } - fprintf(stderr, "unexpected } on line %d\n", *line); - exit(1); + fatal_error("unexpected } on line %d\n", *line); } char * end = curline + len - 1; @@ -133,7 +132,8 @@ success: return ret; } no_config: - fputs("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n", stderr); - exit(1); + fatal_error("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n"); + //this will never get reached, but the compiler doesn't know that. Let's make it happy + return NULL; } -- cgit v1.2.3 From eb2f5614ca10aed240f2d7b310c6929874effc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=3D=3FUTF-8=3Fq=3FHigor=3D20Eur=3DC3=3DADpedes=3F=3D?= Date: Sun, 26 Jul 2015 10:59:41 -0700 Subject: Fix mingw-w64 build and cross-compilation --- config.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'config.c') diff --git a/config.c b/config.c index 6fd29b5..b34aec0 100644 --- a/config.c +++ b/config.c @@ -9,7 +9,13 @@ #include #include -#ifdef _WIN32 +#ifdef __MINGW64_VERSION_MAJOR +#define MINGW_W64_VERSION (__MINGW64_VERSION_MAJOR * 1000 + __MINGW64_VERSION_MINOR) +#else +#define MINGW_W64_VERSION 0 +#endif + +#if defined(_WIN32) && (MINGW_W64_VERSION < 3003) char * strtok_r(char * input, char * sep, char ** state) { if (input) { -- cgit v1.2.3 From 6c280f3b1f8e072f86fc41b371cf616be1483030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=3D=3FUTF-8=3Fq=3FHigor=3D20Eur=3DC3=3DADpedes=3F=3D?= Date: Sun, 26 Jul 2015 11:00:25 -0700 Subject: Fix some memory issues --- config.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index b34aec0..16091b0 100644 --- a/config.c +++ b/config.c @@ -48,11 +48,11 @@ tern_node * parse_config_int(char **state, int started, int *line) curline = strip_ws(curline); int len = strlen(curline); if (!len) { - *line++; + *line = *line + 1; continue; } if (curline[0] == '#') { - *line++; + *line = *line + 1; continue; } if (curline[0] == '}') { @@ -67,7 +67,7 @@ tern_node * parse_config_int(char **state, int started, int *line) if (*end == '{') { *end = 0; curline = strip_ws(curline); - *line++; + *line = *line + 1; head = tern_insert_node(head, curline, parse_config_int(state, 1, line)); } else { char * val = strip_ws(split_keyval(curline)); @@ -77,7 +77,7 @@ tern_node * parse_config_int(char **state, int started, int *line) } else { fprintf(stderr, "Key %s is missing a value on line %d\n", key, *line); } - *line++; + *line = *line + 1; } } return head; @@ -100,10 +100,12 @@ tern_node * parse_config_file(char * config_path) if (!config_size) { goto config_empty; } - char * config_data = malloc(config_size); + char * config_data = malloc(config_size+1); if (fread(config_data, 1, config_size, config_file) != config_size) { goto config_read_fail; } + config_data[config_size] = '\0'; + ret = parse_config(config_data); config_read_fail: free(config_data); -- cgit v1.2.3