2 * Human-readable config file management for PicoDrive
11 #include <Pico/Pico.h>
13 extern menu_entry opt_entries[];
14 extern menu_entry opt2_entries[];
15 extern menu_entry cdopt_entries[];
16 extern const int opt_entry_count;
17 extern const int opt2_entry_count;
18 extern const int cdopt_entry_count;
20 static menu_entry *cfg_opts[] = { opt_entries, opt2_entries, cdopt_entries };
21 static const int *cfg_opt_counts[] = { &opt_entry_count, &opt2_entry_count, &cdopt_entry_count };
26 static int seek_sect(FILE *f, const char *section)
31 len = strlen(section);
32 // seek to the section needed
35 tmp = fgets(line, sizeof(line), f);
36 if (tmp == NULL) break;
38 if (line[0] != '[') continue; // not section start
39 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
47 static void custom_write(FILE *f, const menu_entry *me)
54 if (!((defaultConfig.s_PicoOpt^PicoOpt)&0x10) &&
55 !((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x80)) return;
58 else if (currentConfig.EmuOpt&0x80)
59 str = "16bit accurate";
61 str = "8bit accurate";
62 fprintf(f, "Renderer = %s", str);
66 if (defaultConfig.scaling == currentConfig.scaling) return;
67 switch (currentConfig.scaling) {
68 default: str = "OFF"; break;
69 case 1: str = "hw horizontal"; break;
70 case 2: str = "hw horiz. + vert."; break;
71 case 3: str = "sw horizontal"; break;
73 fprintf(f, "Scaling = %s", str);
75 case MA_OPT_FRAMESKIP:
76 if (defaultConfig.Frameskip == currentConfig.Frameskip) return;
77 if (currentConfig.Frameskip < 0)
78 strcpy(str24, "Auto");
79 else sprintf(str24, "%i", currentConfig.Frameskip);
80 fprintf(f, "Frameskip = %s", str24);
82 case MA_OPT_SOUND_QUALITY:
83 if (!((defaultConfig.s_PicoOpt^PicoOpt)&8) &&
84 defaultConfig.s_PsndRate == PsndRate) return;
85 str = (PicoOpt&0x08)?"stereo":"mono";
86 fprintf(f, "Sound Quality = %i %s", PsndRate, str);
89 if (defaultConfig.s_PicoRegion == PicoRegionOverride &&
90 defaultConfig.s_PicoAutoRgnOrder == PicoAutoRgnOrder) return;
91 fprintf(f, "Region = %s", me_region_name(PicoRegionOverride, PicoAutoRgnOrder));
93 case MA_OPT_CONFIRM_STATES:
94 if (!((defaultConfig.EmuOpt^currentConfig.EmuOpt)&(5<<9))) return;
95 switch ((currentConfig.EmuOpt >> 9) & 5) {
96 default: str = "OFF"; break;
97 case 1: str = "writes"; break;
98 case 4: str = "loads"; break;
99 case 5: str = "both"; break;
101 fprintf(f, "Confirm savestate = %s", str);
103 case MA_OPT_CPU_CLOCKS:
104 if (defaultConfig.CPUclock == currentConfig.CPUclock) return;
105 fprintf(f, "GP2X CPU clocks = %i", currentConfig.CPUclock);
108 if (defaultConfig.gamma == currentConfig.gamma) return;
109 fprintf(f, "Gamma correction = %.3f", (double)currentConfig.gamma / 100.0);
111 case MA_OPT2_SQUIDGEHACK:
112 if (!((defaultConfig.EmuOpt^currentConfig.EmuOpt)&0x0010)) return;
113 fprintf(f, "Squidgehack = %i", (currentConfig.EmuOpt&0x0010)>>4);
115 case MA_CDOPT_READAHEAD:
116 if (defaultConfig.s_PicoCDBuffers == PicoCDBuffers) return;
117 sprintf(str24, "%i", PicoCDBuffers * 2);
118 fprintf(f, "ReadAhead buffer = %s", str24);
122 printf("unhandled custom_write: %i\n", me->id);
129 int config_writesect(const char *fname, const char *section)
131 FILE *fo = NULL, *fn = NULL; // old and new
134 char line[128], *tmp;
138 fo = fopen(fname, "r");
140 fn = fopen(fname, "w");
144 ret = seek_sect(fo, section);
146 // sect not found, we can simply append
147 fclose(fo); fo = NULL;
148 fn = fopen(fname, "a");
154 rename(fname, "tmp.cfg");
155 fo = fopen("tmp.cfg", "r");
156 fn = fopen(fname, "w");
157 if (fo == NULL || fn == NULL) goto write;
159 // copy everything until sect
160 tlen = strlen(section);
163 tmp = fgets(line, sizeof(line), fo);
164 if (tmp == NULL) break;
166 if (line[0] == '[' && strncmp(line + 1, section, tlen) == 0 && line[tlen+1] == ']')
171 // now skip to next sect
174 tmp = fgets(line, sizeof(line), fo);
175 if (tmp == NULL) break;
176 if (line[0] == '[') {
177 fseek(fo, -strlen(line), SEEK_CUR);
183 fclose(fo); fo = NULL;
189 fn = fopen(fname, "w");
198 fprintf(fn, "[%s]" NL, section);
200 for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]); t++)
203 tlen = *(cfg_opt_counts[t]);
204 for (i = 0; i < tlen; i++, me++)
206 if (!me->need_to_save) continue;
207 if ((me->beh != MB_ONOFF && me->beh != MB_RANGE) || me->name == NULL)
208 custom_write(fn, me);
209 else if (me->beh == MB_ONOFF)
210 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
211 else if (me->beh == MB_RANGE)
212 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
219 // copy whatever is left
222 tmp = fgets(line, sizeof(line), fo);
223 if (tmp == NULL) break;
236 static void mystrip(char *str)
241 for (i = 0; i < len; i++)
242 if (str[i] != ' ') break;
243 if (i > 0) memmove(str, str + i, len - i + 1);
245 for (i = len - 1; i >= 0; i--)
246 if (str[i] != ' ') break;
251 int config_writelrom(const char *fname)
253 char line[128], *tmp, *optr = NULL;
254 char *old_data = NULL;
258 if (strlen(currentConfig.lastRomFile) == 0) return 0;
260 f = fopen(fname, "r");
263 fseek(f, 0, SEEK_END);
265 fseek(f, 0, SEEK_SET);
266 old_data = malloc(size + size/8);
267 if (old_data != NULL)
272 tmp = fgets(line, sizeof(line), f);
273 if (tmp == NULL) break;
275 if (strncasecmp(line, "LastUsedROM", 11) == 0)
277 sprintf(optr, "%s", line);
278 optr += strlen(optr);
284 f = fopen(fname, "w");
285 if (f == NULL) return -1;
287 if (old_data != NULL) {
288 fwrite(old_data, 1, optr - old_data, f);
291 fprintf(f, "LastUsedROM = %s" NL, currentConfig.lastRomFile);
297 static int custom_read(menu_entry *me, const char *var, const char *val)
304 case MA_OPT_RENDERER:
305 if (strcasecmp(var, "Renderer") != 0) return 0;
306 if (strcasecmp(val, "8bit fast") == 0) {
309 else if (strcasecmp(val, "16bit accurate") == 0) {
311 currentConfig.EmuOpt |= 0x80;
313 else if (strcasecmp(val, "8bit accurate") == 0) {
315 currentConfig.EmuOpt &= ~0x80;
322 if (strcasecmp(var, "Scaling") != 0) return 0;
323 if (strcasecmp(val, "OFF") == 0) {
324 currentConfig.scaling = 0;
325 } else if (strcasecmp(val, "hw horizontal") == 0) {
326 currentConfig.scaling = 1;
327 } else if (strcasecmp(val, "hw horiz. + vert.") == 0) {
328 currentConfig.scaling = 2;
329 } else if (strcasecmp(val, "sw horizontal") == 0) {
330 currentConfig.scaling = 3;
335 case MA_OPT_FRAMESKIP:
336 if (strcasecmp(var, "Frameskip") != 0) return 0;
337 if (strcasecmp(val, "Auto") == 0)
338 currentConfig.Frameskip = -1;
339 else currentConfig.Frameskip = atoi(val);
342 case MA_OPT_SOUND_QUALITY:
343 if (strcasecmp(var, "Sound Quality") != 0) return 0;
344 PsndRate = strtoul(val, &tmp, 10);
345 if (PsndRate < 8000 || PsndRate > 44100)
347 while (*tmp == ' ') tmp++;
348 if (strcasecmp(tmp, "stereo") == 0) {
350 } else if (strcasecmp(tmp, "mono") == 0) {
357 if (strcasecmp(var, "Region") != 0) return 0;
358 if (strncasecmp(val, "Auto: ", 6) == 0)
360 const char *p = val + 5, *end = val + strlen(val);
362 PicoRegionOverride = PicoAutoRgnOrder = 0;
363 for (i = 0; p < end && i < 3; p += 3, i++) {
364 if (p[0] == 'J' && p[1] == 'P') {
365 PicoAutoRgnOrder |= 1 << (i*4);
366 } else if (p[0] == 'U' && p[1] == 'S') {
367 PicoAutoRgnOrder |= 4 << (i*4);
368 } else if (p[0] == 'E' && p[1] == 'U') {
369 PicoAutoRgnOrder |= 8 << (i*4);
373 else if (strcasecmp(val, "Auto") == 0) {
374 PicoRegionOverride = 0;
375 } else if (strcasecmp(val, "Japan NTSC") == 0) {
376 PicoRegionOverride = 1;
377 } else if (strcasecmp(val, "Japan PAL") == 0) {
378 PicoRegionOverride = 2;
379 } else if (strcasecmp(val, "USA") == 0) {
380 PicoRegionOverride = 4;
381 } else if (strcasecmp(val, "Europe") == 0) {
382 PicoRegionOverride = 8;
387 case MA_OPT_CONFIRM_STATES:
388 if (strcasecmp(var, "Confirm savestate") != 0) return 0;
389 if (strcasecmp(val, "OFF") == 0) {
390 currentConfig.EmuOpt &= 5<<9;
391 } else if (strcasecmp(val, "writes") == 0) {
392 currentConfig.EmuOpt &= 5<<9;
393 currentConfig.EmuOpt |= 1<<9;
394 } else if (strcasecmp(val, "loads") == 0) {
395 currentConfig.EmuOpt &= 5<<9;
396 currentConfig.EmuOpt |= 4<<9;
397 } else if (strcasecmp(val, "both") == 0) {
398 currentConfig.EmuOpt &= 5<<9;
399 currentConfig.EmuOpt |= 5<<9;
404 case MA_OPT_CPU_CLOCKS:
405 if (strcasecmp(var, "GP2X CPU clocks") != 0) return 0;
406 currentConfig.CPUclock = atoi(val);
410 if (strcasecmp(var, "Gamma correction") != 0) return 0;
411 currentConfig.gamma = (int) (atof(val) * 100.0);
414 case MA_OPT2_SQUIDGEHACK:
415 if (strcasecmp(var, "Squidgehack") != 0) return 0;
417 if (tmpi) *(int *)me->var |= me->mask;
418 else *(int *)me->var &= ~me->mask;
421 case MA_CDOPT_READAHEAD:
422 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
423 PicoCDBuffers = atoi(val) / 2;
427 if (strcasecmp(var, "LastUsedROM") == 0) {
428 tmpi = sizeof(currentConfig.lastRomFile);
429 strncpy(currentConfig.lastRomFile, val, tmpi);
430 currentConfig.lastRomFile[tmpi-1] = 0;
433 printf("unhandled custom_read: %i\n", me->id);
439 static void parse(const char *var, const char *val)
442 int t, i, tlen, tmp, ret = 0;
444 for (t = 0; t < sizeof(cfg_opts) / sizeof(cfg_opts[0]) && ret == 0; t++)
447 tlen = *(cfg_opt_counts[t]);
448 for (i = 0; i < tlen && ret == 0; i++, me++)
450 if (!me->need_to_save) continue;
451 if (me->name != NULL) {
452 if (strcasecmp(var, me->name) != 0) continue; // surely not this one
453 if (me->beh == MB_ONOFF) {
455 if (tmp) *(int *)me->var |= me->mask;
456 else *(int *)me->var &= ~me->mask;
458 } else if (me->beh == MB_RANGE) {
460 if (tmp < me->min) tmp = me->min;
461 if (tmp > me->max) tmp = me->max;
462 *(int *)me->var = tmp;
466 ret = custom_read(me, var, val);
469 if (!ret) printf("config_readsect: unhandled var: %s\n", var);
473 int config_readsect(const char *fname, const char *section)
475 char line[128], *var, *val, *tmp;
476 FILE *f = fopen(fname, "r");
479 if (f == NULL) return 0;
483 ret = seek_sect(f, section);
485 printf("config_readsect: %s: missing section [%s]\n", fname, section);
493 tmp = fgets(line, sizeof(line), f);
494 if (tmp == NULL) break;
496 if (line[0] == '[') break; // other section
498 // strip comments, linefeed, spaces..
500 for (i = 0; i < len; i++)
501 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
504 if (len <= 0) continue;
507 for (i = 0; i < len; i++)
508 if (line[i] == '=') break;
509 if (i >= len || strchr(&line[i+1], '=') != NULL) {
510 printf("config_readsect: can't parse: %s\n", line);
518 if (strlen(var) == 0 || strlen(val) == 0) {
519 printf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);