X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Fconfig.c;h=4f80c86c885f79af86aeb0a3bab0013fc0e66321;hb=ee0f881e9f135db6ed9337b99f611b3cae955f8c;hp=2f987e81441c2c2dc21ecd1de38b6e1212a34af1;hpb=3e85ebdd8598be8816138335483f7a774519e5f8;p=libpicofe.git diff --git a/common/config.c b/common/config.c index 2f987e8..4f80c86 100644 --- a/common/config.c +++ b/common/config.c @@ -1,28 +1,27 @@ /* * Human-readable config file management for PicoDrive - * (c) + * (c) notaz, 2008 */ +#include #include #include +#ifdef __EPOC32__ +#include +#endif #include "config.h" -#include "menu.h" -#include "emu.h" +#include "input.h" #include "lprintf.h" -#include -extern menu_entry opt_entries[]; -extern menu_entry opt2_entries[]; -extern menu_entry cdopt_entries[]; -extern const int opt_entry_count; -extern const int opt2_entry_count; -extern const int cdopt_entry_count; +static char *mystrip(char *str); -static menu_entry *cfg_opts[] = { opt_entries, opt2_entries, cdopt_entries }; -static const int *cfg_opt_counts[] = { &opt_entry_count, &opt2_entry_count, &cdopt_entry_count }; +#ifndef _MSC_VER -#define NL "\n" +#include "menu.h" +#include "emu.h" +#include +#define NL "\r\n" static int seek_sect(FILE *f, const char *section) { @@ -47,64 +46,10 @@ static int seek_sect(FILE *f, const char *section) static void custom_write(FILE *f, const menu_entry *me, int no_def) { - char *str, str24[24]; + char str24[24]; switch (me->id) { - case MA_OPT_RENDERER: - if (no_def && !((defaultConfig.s_PicoOpt^PicoOpt)&0x10) && - !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x80)) return; - if (PicoOpt&0x10) - str = "8bit fast"; - else if (currentConfig.EmuOpt&0x80) - str = "16bit accurate"; - else - str = "8bit accurate"; - fprintf(f, "Renderer = %s", str); - break; - - case MA_OPT_SCALING: - if (no_def && defaultConfig.scaling == currentConfig.scaling) return; - switch (currentConfig.scaling) { - default: str = "OFF"; break; - case 1: str = "hw horizontal"; break; - case 2: str = "hw horiz. + vert."; break; - case 3: str = "sw horizontal"; break; - } - fprintf(f, "Scaling = %s", str); - break; - case MA_OPT_FRAMESKIP: - if (no_def && defaultConfig.Frameskip == currentConfig.Frameskip) return; - if (currentConfig.Frameskip < 0) - strcpy(str24, "Auto"); - else sprintf(str24, "%i", currentConfig.Frameskip); - fprintf(f, "Frameskip = %s", str24); - break; - case MA_OPT_SOUND_QUALITY: - if (no_def && !((defaultConfig.s_PicoOpt^PicoOpt)&8) && - defaultConfig.s_PsndRate == PsndRate) return; - str = (PicoOpt&0x08)?"stereo":"mono"; - fprintf(f, "Sound Quality = %i %s", PsndRate, str); - break; - case MA_OPT_REGION: - if (no_def && defaultConfig.s_PicoRegion == PicoRegionOverride && - defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder) return; - fprintf(f, "Region = %s", me_region_name(PicoRegionOverride, PicoAutoRgnOrder)); - break; - case MA_OPT_CONFIRM_STATES: - if (no_def && !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&(5<<9))) return; - switch ((currentConfig.EmuOpt >> 9) & 5) { - default: str = "OFF"; break; - case 1: str = "writes"; break; - case 4: str = "loads"; break; - case 5: str = "both"; break; - } - fprintf(f, "Confirm savestate = %s", str); - break; - case MA_OPT_CPU_CLOCKS: - if (no_def && defaultConfig.CPUclock == currentConfig.CPUclock) return; - fprintf(f, "GP2X CPU clocks = %i", currentConfig.CPUclock); - break; case MA_OPT2_GAMMA: if (no_def && defaultConfig.gamma == currentConfig.gamma) return; fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0); @@ -118,6 +63,38 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def) sprintf(str24, "%i", PicoCDBuffers * 2); fprintf(f, "ReadAhead buffer = %s", str24); break; + /* PSP */ + case MA_OPT3_SCALE: + if (no_def && defaultConfig.scale == currentConfig.scale) return; + fprintf(f, "Scale factor = %.2f", currentConfig.scale); + break; + case MA_OPT3_HSCALE32: + if (no_def && defaultConfig.hscale32 == currentConfig.hscale32) return; + fprintf(f, "Hor. scale (for low res. games) = %.2f", currentConfig.hscale32); + break; + case MA_OPT3_HSCALE40: + if (no_def && defaultConfig.hscale40 == currentConfig.hscale40) return; + fprintf(f, "Hor. scale (for hi res. games) = %.2f", currentConfig.hscale40); + break; + case MA_OPT3_FILTERING: + if (no_def && defaultConfig.scaling == currentConfig.scaling) return; + fprintf(f, "Bilinear filtering = %i", currentConfig.scaling); + break; + case MA_OPT3_GAMMAA: + if (no_def && defaultConfig.gamma == currentConfig.gamma) return; + fprintf(f, "Gamma adjustment = %i", currentConfig.gamma); + break; + case MA_OPT3_BLACKLVL: + if (no_def && defaultConfig.gamma2 == currentConfig.gamma2) return; + fprintf(f, "Black level = %i", currentConfig.gamma2); + break; + case MA_OPT3_VSYNC: + if (no_def && (defaultConfig.EmuOpt&0x12000) == (currentConfig.gamma2&0x12000)) return; + strcpy(str24, "never"); + if (currentConfig.EmuOpt & 0x2000) + strcpy(str24, (currentConfig.EmuOpt & 0x10000) ? "sometimes" : "always"); + fprintf(f, "Wait for vsync = %s", str24); + break; default: lprintf("unhandled custom_write: %i\n", me->id); @@ -126,6 +103,52 @@ static void custom_write(FILE *f, const menu_entry *me, int no_def) fprintf(f, NL); } + +static void keys_write(FILE *fn, const char *bind_str, int dev_id, const int *binds, int no_defaults) +{ + char act[48]; + int key_count, t, i; + const int *def_binds; + + key_count = in_get_dev_info(dev_id, IN_INFO_BIND_COUNT); + def_binds = in_get_dev_def_binds(dev_id); + + for (t = 0; t < key_count; t++) + { + const char *name; + act[0] = act[31] = 0; + + if (no_defaults && binds[t] == def_binds[t]) + continue; + + name = in_get_key_name(dev_id, t); +#ifdef __GP2X__ + if (strcmp(name, "SELECT") == 0) continue; +#endif + + if (binds[t] == 0 && def_binds[t] != 0) { + fprintf(fn, "%s %s =" NL, bind_str, name); + continue; + } + + for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) { + if (me_ctrl_actions[i].mask & binds[t]) { + strncpy(act, me_ctrl_actions[i].name, 31); + fprintf(fn, "%s %s = player%i %s" NL, bind_str, name, + ((binds[t]>>16)&1)+1, mystrip(act)); + } + } + + for (i = 0; emuctrl_actions[i].name != NULL; i++) { + if (emuctrl_actions[i].mask & binds[t]) { + strncpy(act, emuctrl_actions[i].name, 31); + fprintf(fn, "%s %s = %s" NL, bind_str, name, mystrip(act)); + } + } + } +} + + static int default_var(const menu_entry *me) { switch (me->id) @@ -137,6 +160,7 @@ static int default_var(const menu_entry *me) case MA_OPT2_ENABLE_Z80: case MA_OPT2_ENABLE_YM2612: case MA_OPT2_ENABLE_SN76496: + case MA_OPT2_SVP_DYNAREC: case MA_CDOPT_CDDA: case MA_CDOPT_PCM: case MA_CDOPT_SAVERAM: @@ -155,6 +179,10 @@ static int default_var(const menu_entry *me) case MA_CDOPT_LEDS: return defaultConfig.EmuOpt; + case MA_CTRL_TURBO_RATE: return defaultConfig.turbo_rate; + case MA_OPT_SCALING: return defaultConfig.scaling; + case MA_OPT_ROTATION: return defaultConfig.rotation; + case MA_OPT_SAVE_SLOT: default: return 0; @@ -166,7 +194,7 @@ int config_writesect(const char *fname, const char *section) FILE *fo = NULL, *fn = NULL; // old and new int no_defaults = 0; // avoid saving defaults menu_entry *me; - int t, i, tlen, ret; + int t, tlen, ret; char line[128], *tmp; if (section != NULL) @@ -235,24 +263,63 @@ write: if (section != NULL) fprintf(fn, "[%s]" NL, section); - for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]); t++) + me = me_list_get_first(); + while (me != NULL) { - me = cfg_opts[t]; - tlen = *(cfg_opt_counts[t]); - for (i = 0; i < tlen; i++, me++) - { - if (!me->need_to_save) continue; - if ((me->beh != MB_ONOFF && me->beh != MB_RANGE) || me->name == NULL) - custom_write(fn, me, no_defaults); - else if (me->beh == MB_ONOFF) { - if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask)) - fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0); - } else if (me->beh == MB_RANGE) { - if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask)) - fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var); - } - } + int dummy; + if (!me->need_to_save) + goto next; + if (me->beh == MB_OPT_ONOFF) { + if (!no_defaults || ((*(int *)me->var ^ default_var(me)) & me->mask)) + fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0); + } else if (me->beh == MB_OPT_RANGE) { + if (!no_defaults || (*(int *)me->var ^ default_var(me))) + fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var); + } else if (me->name != NULL && me->generate_name != NULL) { + strncpy(line, me->generate_name(0, &dummy), sizeof(line)); + line[sizeof(line) - 1] = 0; + mystrip(line); + fprintf(fn, "%s = %s" NL, me->name, line); + } else + custom_write(fn, me, no_defaults); +next: + me = me_list_get_next(); + } + + /* input: save device names */ + for (t = 0; t < IN_MAX_DEVS; t++) + { + const int *binds = in_get_dev_binds(t); + const char *name = in_get_dev_name(t, 0, 0); + if (binds == NULL || name == NULL) + continue; + + fprintf(fn, "input%d = %s" NL, t, name); } + + /* input: save binds */ + for (t = 0; t < IN_MAX_DEVS; t++) + { + const int *binds = in_get_dev_binds(t); + const char *name = in_get_dev_name(t, 0, 0); + char strbind[16]; + int count; + + if (binds == NULL || name == NULL) + continue; + + sprintf(strbind, "bind%d", t); + if (t == 0) strbind[4] = 0; + + count = in_get_dev_info(t, IN_INFO_BIND_COUNT); + keys_write(fn, strbind, t, binds, no_defaults); + } + +#ifndef PSP + if (section == NULL) + fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume); +#endif + fprintf(fn, NL); if (fo != NULL) @@ -274,21 +341,6 @@ write: } -static void 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; -} - - int config_writelrom(const char *fname) { char line[128], *tmp, *optr = NULL; @@ -296,7 +348,7 @@ int config_writelrom(const char *fname) int size; FILE *f; - if (strlen(currentConfig.lastRomFile) == 0) return 0; + if (strlen(rom_fname_loaded) == 0) return -1; f = fopen(fname, "r"); if (f != NULL) @@ -329,11 +381,47 @@ int config_writelrom(const char *fname) fwrite(old_data, 1, optr - old_data, f); free(old_data); } - fprintf(f, "LastUsedROM = %s" NL, currentConfig.lastRomFile); + fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded); fclose(f); return 0; } +/* --------------------------------------------------------------------------*/ + +int config_readlrom(const char *fname) +{ + char line[128], *tmp; + int i, len, ret = -1; + FILE *f; + + f = fopen(fname, "r"); + if (f == NULL) return -1; + + // seek to the section needed + while (!feof(f)) + { + tmp = fgets(line, sizeof(line), f); + if (tmp == NULL) break; + + if (strncasecmp(line, "LastUsedROM", 11) != 0) continue; + len = strlen(line); + for (i = 0; i < len; i++) + if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; } + tmp = strchr(line, '='); + if (tmp == NULL) break; + tmp++; + mystrip(tmp); + + len = sizeof(rom_fname_loaded); + strncpy(rom_fname_loaded, tmp, len); + rom_fname_loaded[len-1] = 0; + ret = 0; + break; + } + fclose(f); + return ret; +} + static int custom_read(menu_entry *me, const char *var, const char *val) { @@ -344,15 +432,15 @@ static int custom_read(menu_entry *me, const char *var, const char *val) { case MA_OPT_RENDERER: if (strcasecmp(var, "Renderer") != 0) return 0; - if (strcasecmp(val, "8bit fast") == 0) { - PicoOpt |= 0x10; + if (strcasecmp(val, "8bit fast") == 0 || strcasecmp(val, "fast") == 0) { + PicoOpt |= POPT_ALT_RENDERER; } - else if (strcasecmp(val, "16bit accurate") == 0) { - PicoOpt &= ~0x10; + else if (strcasecmp(val, "16bit accurate") == 0 || strcasecmp(val, "accurate") == 0) { + PicoOpt &= ~POPT_ALT_RENDERER; currentConfig.EmuOpt |= 0x80; } else if (strcasecmp(val, "8bit accurate") == 0) { - PicoOpt &= ~0x10; + PicoOpt &= ~POPT_ALT_RENDERER; currentConfig.EmuOpt &= ~0x80; } else @@ -360,18 +448,22 @@ static int custom_read(menu_entry *me, const char *var, const char *val) return 1; case MA_OPT_SCALING: +#ifdef __GP2X__ if (strcasecmp(var, "Scaling") != 0) return 0; if (strcasecmp(val, "OFF") == 0) { - currentConfig.scaling = 0; + currentConfig.scaling = EOPT_SCALE_NONE; } else if (strcasecmp(val, "hw horizontal") == 0) { - currentConfig.scaling = 1; + currentConfig.scaling = EOPT_SCALE_HW_H; } else if (strcasecmp(val, "hw horiz. + vert.") == 0) { - currentConfig.scaling = 2; + currentConfig.scaling = EOPT_SCALE_HW_HV; } else if (strcasecmp(val, "sw horizontal") == 0) { - currentConfig.scaling = 3; + currentConfig.scaling = EOPT_SCALE_SW_H; } else return 0; return 1; +#else + return 0; +#endif case MA_OPT_FRAMESKIP: if (strcasecmp(var, "Frameskip") != 0) return 0; @@ -387,9 +479,9 @@ static int custom_read(menu_entry *me, const char *var, const char *val) PsndRate = 22050; while (*tmp == ' ') tmp++; if (strcasecmp(tmp, "stereo") == 0) { - PicoOpt |= 8; + PicoOpt |= POPT_EN_STEREO; } else if (strcasecmp(tmp, "mono") == 0) { - PicoOpt &= ~8; + PicoOpt &= ~POPT_EN_STEREO; } else return 0; return 1; @@ -432,24 +524,30 @@ static int custom_read(menu_entry *me, const char *var, const char *val) case MA_OPT_CONFIRM_STATES: if (strcasecmp(var, "Confirm savestate") != 0) return 0; if (strcasecmp(val, "OFF") == 0) { - currentConfig.EmuOpt &= 5<<9; + currentConfig.EmuOpt &= ~(5<<9); } else if (strcasecmp(val, "writes") == 0) { - currentConfig.EmuOpt &= 5<<9; - currentConfig.EmuOpt |= 1<<9; + currentConfig.EmuOpt &= ~(5<<9); + currentConfig.EmuOpt |= 1<<9; } else if (strcasecmp(val, "loads") == 0) { - currentConfig.EmuOpt &= 5<<9; - currentConfig.EmuOpt |= 4<<9; + currentConfig.EmuOpt &= ~(5<<9); + currentConfig.EmuOpt |= 4<<9; } else if (strcasecmp(val, "both") == 0) { - currentConfig.EmuOpt &= 5<<9; - currentConfig.EmuOpt |= 5<<9; + currentConfig.EmuOpt &= ~(5<<9); + currentConfig.EmuOpt |= 5<<9; } else return 0; return 1; +#if 0 // TODO rm? case MA_OPT_CPU_CLOCKS: +#ifdef __GP2X__ if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0; +#elif defined(PSP) + if (strcasecmp(var, "PSP CPU clock") != 0) return 0; +#endif currentConfig.CPUclock = atoi(val); return 1; +#endif case MA_OPT2_GAMMA: if (strcasecmp(var, "Gamma correction") != 0) return 0; @@ -468,61 +566,206 @@ static int custom_read(menu_entry *me, const char *var, const char *val) PicoCDBuffers = atoi(val) / 2; return 1; + /* PSP */ + case MA_OPT3_SCALE: + if (strcasecmp(var, "Scale factor") != 0) return 0; + currentConfig.scale = atof(val); + return 1; + case MA_OPT3_HSCALE32: + if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0; + currentConfig.hscale32 = atof(val); + return 1; + case MA_OPT3_HSCALE40: + if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0; + currentConfig.hscale40 = atof(val); + return 1; + case MA_OPT3_FILTERING: + if (strcasecmp(var, "Bilinear filtering") != 0) return 0; + currentConfig.scaling = atoi(val); + return 1; + case MA_OPT3_GAMMAA: + if (strcasecmp(var, "Gamma adjustment") != 0) return 0; + currentConfig.gamma = atoi(val); + return 1; + case MA_OPT3_BLACKLVL: + if (strcasecmp(var, "Black level") != 0) return 0; + currentConfig.gamma2 = atoi(val); + return 1; + case MA_OPT3_VSYNC: + if (strcasecmp(var, "Wait for vsync") != 0) return 0; + if (strcasecmp(val, "never") == 0) { + currentConfig.EmuOpt &= ~0x12000; + } else if (strcasecmp(val, "sometimes") == 0) { + currentConfig.EmuOpt |= 0x12000; + } else if (strcasecmp(val, "always") == 0) { + currentConfig.EmuOpt &= ~0x12000; + currentConfig.EmuOpt |= 0x02000; + } else + return 0; + return 1; + default: - if (strcasecmp(var, "LastUsedROM") == 0) { - tmpi = sizeof(currentConfig.lastRomFile); - strncpy(currentConfig.lastRomFile, val, tmpi); - currentConfig.lastRomFile[tmpi-1] = 0; - return 1; - } lprintf("unhandled custom_read: %i\n", me->id); return 0; } } +static unsigned int keys_encountered = 0; + +static int parse_bind_val(const char *val) +{ + int i; + + if (val[0] == 0) + return 0; + + if (strncasecmp(val, "player", 6) == 0) + { + unsigned int player; + player = atoi(val + 6) - 1; + if (player > 1) + return -1; + + for (i = 0; i < sizeof(me_ctrl_actions) / sizeof(me_ctrl_actions[0]); i++) { + if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0) + return me_ctrl_actions[i].mask | (player<<16); + } + } + for (i = 0; emuctrl_actions[i].name != NULL; i++) { + if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) + return emuctrl_actions[i].mask; + } + + return -1; +} + +static void keys_parse(const char *key, const char *val, int dev_id) +{ + int binds; + + binds = parse_bind_val(val); + if (binds == -1) { + lprintf("config: unhandled action \"%s\"\n", val); + return; + } + + in_config_bind_key(dev_id, key, binds); +} + +static int get_numvar_num(const char *var) +{ + char *p = NULL; + int num; + + if (var[0] == ' ') + return 0; + + num = strtoul(var, &p, 10); + if (*p == 0 || *p == ' ') + return num; + + return -1; +} + +/* map dev number in confing to input dev number */ +static unsigned char input_dev_map[IN_MAX_DEVS]; + static void parse(const char *var, const char *val) { menu_entry *me; - int t, i, tlen, tmp, ret = 0; + int tmp, ret = 0; + + if (strcasecmp(var, "LastUsedROM") == 0) + return; /* handled elsewhere */ + + if (strcasecmp(var, "Sound Volume") == 0) { + currentConfig.volume = atoi(val); + return; + } - for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]) && ret == 0; t++) + /* input: device name */ + if (strncasecmp(var, "input", 5) == 0) { + int num = get_numvar_num(var + 5); + if (num >= 0 && num < IN_MAX_DEVS) + input_dev_map[num] = in_config_parse_dev(val); + else + lprintf("config: failed to parse: %s\n", var); + return; + } + + // key binds + if (strncasecmp(var, "bind", 4) == 0) { + const char *p = var + 4; + int num = get_numvar_num(p); + if (num < 0 || num >= IN_MAX_DEVS) { + lprintf("config: failed to parse: %s\n", var); + return; + } + + num = input_dev_map[num]; + if (num < 0 || num >= IN_MAX_DEVS) { + lprintf("config: invalid device id: %s\n", var); + return; + } + + while (*p && *p != ' ') p++; + while (*p && *p == ' ') p++; + keys_parse(p, val, num); + return; + } + + me = me_list_get_first(); + while (me != NULL && ret == 0) { - me = cfg_opts[t]; - tlen = *(cfg_opt_counts[t]); - for (i = 0; i < tlen && ret == 0; i++, me++) - { - if (!me->need_to_save) continue; - if (me->name != NULL) { - if (strcasecmp(var, me->name) != 0) continue; // surely not this one - if (me->beh == MB_ONOFF) { - tmp = atoi(val); - if (tmp) *(int *)me->var |= me->mask; - else *(int *)me->var &= ~me->mask; - return; - } else if (me->beh == MB_RANGE) { - tmp = atoi(val); - if (tmp < me->min) tmp = me->min; - if (tmp > me->max) tmp = me->max; - *(int *)me->var = tmp; - return; - } + if (!me->need_to_save) + goto next; + if (me->name != NULL && me->name[0] != 0) { + if (strcasecmp(var, me->name) != 0) + goto next; /* surely not this one */ + if (me->beh == MB_OPT_ONOFF) { + tmp = atoi(val); + if (tmp) *(int *)me->var |= me->mask; + else *(int *)me->var &= ~me->mask; + return; + } else if (me->beh == MB_OPT_RANGE) { + tmp = atoi(val); + if (tmp < me->min) tmp = me->min; + if (tmp > me->max) tmp = me->max; + *(int *)me->var = tmp; + return; } - ret = custom_read(me, var, val); } + ret = custom_read(me, var, val); +next: + me = me_list_get_next(); } - if (!ret) lprintf("config_readsect: unhandled var: %s\n", var); + if (!ret) lprintf("config_readsect: unhandled var: \"%s\"\n", var); } -int config_readsect(const char *fname, const char *section) +int config_havesect(const char *fname, const char *section) { - char line[128], *var, *val, *tmp; - FILE *f = fopen(fname, "r"); - int len, i, ret; + FILE *f; + int ret; + f = fopen(fname, "r"); if (f == NULL) return 0; + ret = seek_sect(f, section); + fclose(f); + return ret; +} + +int config_readsect(const char *fname, const char *section) +{ + char line[128], *var, *val; + FILE *f; + int ret; + + f = fopen(fname, "r"); + if (f == NULL) return -1; + if (section != NULL) { ret = seek_sect(f, section); @@ -533,42 +776,89 @@ int config_readsect(const char *fname, const char *section) } } + keys_encountered = 0; + memset(input_dev_map, 0xff, sizeof(input_dev_map)); + + in_config_start(); 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) { - 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); } + in_config_end(); fclose(f); 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; +} +