From: notaz Date: Thu, 22 May 2008 20:52:23 +0000 (+0000) Subject: 1.45a Pico win32, code move X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7836e87c28521036b0239d4ace3dd42eab8bbb5c;p=libpicofe.git 1.45a Pico win32, code move git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@457 be3aeb3a-fb24-0410-a615-afba39da0efa --- diff --git a/common/config.c b/common/config.c index f90ab0b..3959706 100644 --- a/common/config.c +++ b/common/config.c @@ -3,12 +3,18 @@ * (c) */ +#include #include #include #include "config.h" +#include "lprintf.h" + +static char *mystrip(char *str); + +#ifndef _MSC_VER + #include "menu.h" #include "emu.h" -#include "lprintf.h" #include extern menu_entry opt_entries[]; @@ -45,24 +51,6 @@ static const int *cfg_opt_counts[] = #define NL "\r\n" -static char *mystrip(char *str) -{ - int i, len; - - len = strlen(str); - for (i = 0; i < len; i++) - if (str[i] != ' ') break; - if (i > 0) memmove(str, str + i, len - i + 1); - - len = strlen(str); - for (i = len - 1; i >= 0; i--) - if (str[i] != ' ') break; - str[i+1] = 0; - - return str; -} - - static int seek_sect(FILE *f, const char *section) { char line[128], *tmp; @@ -807,12 +795,11 @@ int config_havesect(const char *fname, const char *section) return ret; } - int config_readsect(const char *fname, const char *section) { - char line[128], *var, *val, *tmp; - int len, i, ret; + char line[128], *var, *val; FILE *f; + int ret; f = fopen(fname, "r"); if (f == NULL) return -1; @@ -831,35 +818,9 @@ int config_readsect(const char *fname, const char *section) while (!feof(f)) { - tmp = fgets(line, sizeof(line), f); - if (tmp == NULL) break; - - if (line[0] == '[') break; // other section - - // strip comments, linefeed, spaces.. - len = strlen(line); - for (i = 0; i < len; i++) - if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; } - mystrip(line); - len = strlen(line); - if (len <= 0) continue; - - // get var and val - for (i = 0; i < len; i++) - if (line[i] == '=') break; - if (i >= len || strchr(&line[i+1], '=') != NULL) { - lprintf("config_readsect: can't parse: %s\n", line); - continue; - } - line[i] = 0; - var = line; - val = &line[i+1]; - mystrip(var); - mystrip(val); - if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) { - lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val); - continue; - } + ret = config_get_var_val(f, line, sizeof(line), &var, &val); + if (ret == 0) break; + if (ret == -1) continue; parse(var, val); } @@ -868,3 +829,71 @@ int config_readsect(const char *fname, const char *section) return 0; } +#endif // _MSC_VER + +static char *mystrip(char *str) +{ + int i, len; + + len = strlen(str); + for (i = 0; i < len; i++) + if (str[i] != ' ') break; + if (i > 0) memmove(str, str + i, len - i + 1); + + len = strlen(str); + for (i = len - 1; i >= 0; i--) + if (str[i] != ' ') break; + str[i+1] = 0; + + return str; +} + +/* returns: + * 0 - EOF, end + * 1 - parsed ok + * -1 - failed to parse line + */ +int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval) +{ + char *var, *val, *tmp; + FILE *f = file; + int len, i; + + tmp = fgets(line, lsize, f); + if (tmp == NULL) return 0; + + if (line[0] == '[') return 0; // other section + + // strip comments, linefeed, spaces.. + len = strlen(line); + for (i = 0; i < len; i++) + if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; } + mystrip(line); + len = strlen(line); + if (len <= 0) return -1;; + + // get var and val + for (i = 0; i < len; i++) + if (line[i] == '=') break; + if (i >= len || strchr(&line[i+1], '=') != NULL) { + lprintf("config_readsect: can't parse: %s\n", line); + return -1; + } + line[i] = 0; + var = line; + val = &line[i+1]; + mystrip(var); + mystrip(val); + +#ifndef _MSC_VER + if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) { + lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val); + return -1;; + } +#endif + + *rvar = var; + *rval = val; + return 1; +} + diff --git a/common/config.h b/common/config.h index 2d8a303..c4c0a17 100644 --- a/common/config.h +++ b/common/config.h @@ -1,6 +1,16 @@ + +#ifdef __cplusplus +extern "C" { +#endif + int config_writesect(const char *fname, const char *section); int config_writelrom(const char *fname); int config_readsect(const char *fname, const char *section); int config_readlrom(const char *fname); int config_havesect(const char *fname, const char *section); +int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval); + +#ifdef __cplusplus +} +#endif