don't crash on bad timing
[picodrive.git] / platform / common / config_file.c
CommitLineData
835e7900 1/*
2 * Human-readable config file management for PicoDrive
cff531af 3 * (C) notaz, 2008-2010
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
835e7900 7 */
8
4609d0cd 9#include <stdio.h>
835e7900 10#include <string.h>
11#include <stdlib.h>
f8af9634 12#ifdef __EPOC32__
ca482e5d 13#include <unistd.h>
14#endif
e743be20 15
16#include "../libpicofe/input.h"
17#include "../libpicofe/plat.h"
18#include "../libpicofe/lprintf.h"
39e6f973 19#include "config_file.h"
4609d0cd 20
21static char *mystrip(char *str);
22
23#ifndef _MSC_VER
24
21ebcfd3 25#include "menu_pico.h"
835e7900 26#include "emu.h"
efcba75f 27#include <pico/pico.h>
835e7900 28
823b9004 29// always output DOS endlines
30#ifdef _WIN32
31#define NL "\n"
32#else
7b802576 33#define NL "\r\n"
823b9004 34#endif
835e7900 35
835e7900 36static int seek_sect(FILE *f, const char *section)
37{
38 char line[128], *tmp;
39 int len;
40
41 len = strlen(section);
42 // seek to the section needed
43 while (!feof(f))
44 {
45 tmp = fgets(line, sizeof(line), f);
46 if (tmp == NULL) break;
47
48 if (line[0] != '[') continue; // not section start
49 if (strncmp(line + 1, section, len) == 0 && line[len+1] == ']')
50 return 1; // found it
51 }
52
53 return 0;
54}
55
504e2f56 56static void keys_write(FILE *fn, int dev_id, const int *binds)
1ca2ea4f 57{
7e4c661a 58 char act[48];
45285368 59 int key_count = 0, k, i;
7e4c661a 60
45285368 61 in_get_config(dev_id, IN_CFG_BIND_COUNT, &key_count);
1ca2ea4f 62
093b8a42 63 for (k = 0; k < key_count; k++)
1ca2ea4f 64 {
7e4c661a 65 const char *name;
504e2f56 66 int mask;
1ca2ea4f 67 act[0] = act[31] = 0;
7e4c661a 68
093b8a42 69 name = in_get_key_name(dev_id, k);
ca482e5d 70
67b7b971 71 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
093b8a42 72 mask = me_ctrl_actions[i].mask;
73 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
74 strncpy(act, me_ctrl_actions[i].name, 31);
504e2f56 75 fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act));
093b8a42 76 }
77 mask = me_ctrl_actions[i].mask << 16;
78 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) {
bdec53c9 79 strncpy(act, me_ctrl_actions[i].name, 31);
504e2f56 80 fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act));
1ca2ea4f 81 }
82 }
1ca2ea4f 83
bdec53c9 84 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
093b8a42 85 mask = emuctrl_actions[i].mask;
86 if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) {
bdec53c9 87 strncpy(act, emuctrl_actions[i].name, 31);
504e2f56 88 fprintf(fn, "bind %s = %s" NL, name, mystrip(act));
bdec53c9 89 }
90 }
1ca2ea4f 91 }
92}
93
504e2f56 94int config_write(const char *fname)
dabe23b6 95{
504e2f56 96 FILE *fn = NULL;
835e7900 97 menu_entry *me;
504e2f56 98 int t;
99 char line[128];
c46ffd31 100
504e2f56 101 fn = fopen(fname, "w");
102 if (fn == NULL)
835e7900 103 return -1;
835e7900 104
fcdefcf6 105 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
835e7900 106 {
713c9224 107 int dummy;
504e2f56 108 if (!me->need_to_save || !me->enabled)
fcdefcf6 109 continue;
45285368 110 if (me->name == NULL || me->name[0] == 0)
111 continue;
fcdefcf6 112
dabe23b6 113 if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) {
504e2f56 114 fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0);
fcdefcf6 115 }
116 else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) {
504e2f56 117 fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var);
fcdefcf6 118 }
119 else if (me->beh == MB_OPT_ENUM && me->data != NULL) {
120 const char **names = (const char **)me->data;
504e2f56 121 for (t = 0; names[t] != NULL; t++) {
122 if (*(int *)me->var == t) {
fcdefcf6 123 strncpy(line, names[t], sizeof(line));
124 goto write_line;
125 }
504e2f56 126 }
fcdefcf6 127 }
45285368 128 else if (me->generate_name != NULL) {
504e2f56 129 strncpy(line, me->generate_name(0, &dummy), sizeof(line));
130 goto write_line;
fcdefcf6 131 }
132 else
133 lprintf("config: unhandled write: %i\n", me->id);
134 continue;
135
136write_line:
137 line[sizeof(line) - 1] = 0;
138 mystrip(line);
139 fprintf(fn, "%s = %s" NL, me->name, line);
835e7900 140 }
1ca2ea4f 141
7e4c661a 142 /* input: save binds */
143 for (t = 0; t < IN_MAX_DEVS; t++)
144 {
145 const int *binds = in_get_dev_binds(t);
713c9224 146 const char *name = in_get_dev_name(t, 0, 0);
45285368 147 int count = 0;
7e4c661a 148
149 if (binds == NULL || name == NULL)
150 continue;
151
504e2f56 152 fprintf(fn, "binddev = %s" NL, name);
7e4c661a 153
45285368 154 in_get_config(t, IN_CFG_BIND_COUNT, &count);
504e2f56 155 keys_write(fn, t, binds);
7e4c661a 156 }
157
504e2f56 158 fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume);
7b802576 159
835e7900 160 fprintf(fn, NL);
161
835e7900 162 fclose(fn);
163 return 0;
164}
165
835e7900 166int config_writelrom(const char *fname)
167{
168 char line[128], *tmp, *optr = NULL;
169 char *old_data = NULL;
170 int size;
171 FILE *f;
172
713c9224 173 if (strlen(rom_fname_loaded) == 0) return -1;
835e7900 174
175 f = fopen(fname, "r");
176 if (f != NULL)
177 {
178 fseek(f, 0, SEEK_END);
179 size = ftell(f);
180 fseek(f, 0, SEEK_SET);
181 old_data = malloc(size + size/8);
182 if (old_data != NULL)
183 {
184 optr = old_data;
185 while (!feof(f))
186 {
187 tmp = fgets(line, sizeof(line), f);
188 if (tmp == NULL) break;
189 mystrip(line);
190 if (strncasecmp(line, "LastUsedROM", 11) == 0)
191 continue;
192 sprintf(optr, "%s", line);
193 optr += strlen(optr);
194 }
195 }
196 fclose(f);
197 }
198
199 f = fopen(fname, "w");
200 if (f == NULL) return -1;
201
202 if (old_data != NULL) {
203 fwrite(old_data, 1, optr - old_data, f);
204 free(old_data);
205 }
713c9224 206 fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded);
835e7900 207 fclose(f);
208 return 0;
209}
210
1ca2ea4f 211/* --------------------------------------------------------------------------*/
212
213int config_readlrom(const char *fname)
214{
215 char line[128], *tmp;
216 int i, len, ret = -1;
217 FILE *f;
218
219 f = fopen(fname, "r");
220 if (f == NULL) return -1;
221
222 // seek to the section needed
223 while (!feof(f))
224 {
225 tmp = fgets(line, sizeof(line), f);
226 if (tmp == NULL) break;
227
228 if (strncasecmp(line, "LastUsedROM", 11) != 0) continue;
229 len = strlen(line);
230 for (i = 0; i < len; i++)
231 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
232 tmp = strchr(line, '=');
233 if (tmp == NULL) break;
234 tmp++;
235 mystrip(tmp);
236
713c9224 237 len = sizeof(rom_fname_loaded);
238 strncpy(rom_fname_loaded, tmp, len);
239 rom_fname_loaded[len-1] = 0;
1ca2ea4f 240 ret = 0;
241 break;
242 }
243 fclose(f);
244 return ret;
245}
246
835e7900 247static int custom_read(menu_entry *me, const char *var, const char *val)
248{
249 char *tmp;
835e7900 250
251 switch (me->id)
252 {
835e7900 253 case MA_OPT_FRAMESKIP:
254 if (strcasecmp(var, "Frameskip") != 0) return 0;
255 if (strcasecmp(val, "Auto") == 0)
256 currentConfig.Frameskip = -1;
257 else currentConfig.Frameskip = atoi(val);
258 return 1;
259
260 case MA_OPT_SOUND_QUALITY:
261 if (strcasecmp(var, "Sound Quality") != 0) return 0;
262 PsndRate = strtoul(val, &tmp, 10);
263 if (PsndRate < 8000 || PsndRate > 44100)
264 PsndRate = 22050;
21ecaf23 265 if (*tmp == 'H' || *tmp == 'h') tmp++;
266 if (*tmp == 'Z' || *tmp == 'z') tmp++;
835e7900 267 while (*tmp == ' ') tmp++;
268 if (strcasecmp(tmp, "stereo") == 0) {
602133e1 269 PicoOpt |= POPT_EN_STEREO;
835e7900 270 } else if (strcasecmp(tmp, "mono") == 0) {
602133e1 271 PicoOpt &= ~POPT_EN_STEREO;
835e7900 272 } else
273 return 0;
274 return 1;
275
276 case MA_OPT_REGION:
277 if (strcasecmp(var, "Region") != 0) return 0;
278 if (strncasecmp(val, "Auto: ", 6) == 0)
279 {
280 const char *p = val + 5, *end = val + strlen(val);
281 int i;
282 PicoRegionOverride = PicoAutoRgnOrder = 0;
c46ffd31 283 for (i = 0; p < end && i < 3; i++)
284 {
285 while (*p == ' ') p++;
835e7900 286 if (p[0] == 'J' && p[1] == 'P') {
287 PicoAutoRgnOrder |= 1 << (i*4);
288 } else if (p[0] == 'U' && p[1] == 'S') {
289 PicoAutoRgnOrder |= 4 << (i*4);
290 } else if (p[0] == 'E' && p[1] == 'U') {
291 PicoAutoRgnOrder |= 8 << (i*4);
292 }
c46ffd31 293 while (*p != ' ' && *p != 0) p++;
294 if (*p == 0) break;
835e7900 295 }
296 }
297 else if (strcasecmp(val, "Auto") == 0) {
298 PicoRegionOverride = 0;
299 } else if (strcasecmp(val, "Japan NTSC") == 0) {
300 PicoRegionOverride = 1;
301 } else if (strcasecmp(val, "Japan PAL") == 0) {
302 PicoRegionOverride = 2;
303 } else if (strcasecmp(val, "USA") == 0) {
304 PicoRegionOverride = 4;
305 } else if (strcasecmp(val, "Europe") == 0) {
306 PicoRegionOverride = 8;
307 } else
308 return 0;
309 return 1;
310
835e7900 311 case MA_OPT2_GAMMA:
312 if (strcasecmp(var, "Gamma correction") != 0) return 0;
313 currentConfig.gamma = (int) (atof(val) * 100.0);
314 return 1;
315
835e7900 316 case MA_CDOPT_READAHEAD:
317 if (strcasecmp(var, "ReadAhead buffer") != 0) return 0;
318 PicoCDBuffers = atoi(val) / 2;
319 return 1;
320
fcdefcf6 321 case MA_32XOPT_MSH2_CYCLES:
322 case MA_32XOPT_SSH2_CYCLES: {
323 int *mul = (me->id == MA_32XOPT_MSH2_CYCLES) ? &p32x_msh2_multiplier : &p32x_ssh2_multiplier;
324 *mul = ((unsigned int)atoi(val) << SH2_MULTI_SHIFT) / 7670;
325 return 1;
326 }
327
6fc57144 328 /* PSP */
329 case MA_OPT3_SCALE:
330 if (strcasecmp(var, "Scale factor") != 0) return 0;
331 currentConfig.scale = atof(val);
332 return 1;
333 case MA_OPT3_HSCALE32:
334 if (strcasecmp(var, "Hor. scale (for low res. games)") != 0) return 0;
335 currentConfig.hscale32 = atof(val);
336 return 1;
337 case MA_OPT3_HSCALE40:
338 if (strcasecmp(var, "Hor. scale (for hi res. games)") != 0) return 0;
339 currentConfig.hscale40 = atof(val);
340 return 1;
6fc57144 341 case MA_OPT3_VSYNC:
fcdefcf6 342 // XXX: use enum
6fc57144 343 if (strcasecmp(var, "Wait for vsync") != 0) return 0;
344 if (strcasecmp(val, "never") == 0) {
345 currentConfig.EmuOpt &= ~0x12000;
346 } else if (strcasecmp(val, "sometimes") == 0) {
347 currentConfig.EmuOpt |= 0x12000;
348 } else if (strcasecmp(val, "always") == 0) {
349 currentConfig.EmuOpt &= ~0x12000;
350 currentConfig.EmuOpt |= 0x02000;
351 } else
352 return 0;
353 return 1;
354
835e7900 355 default:
fcdefcf6 356 lprintf("unhandled custom_read %i: %s\n", me->id, var);
835e7900 357 return 0;
358 }
359}
360
093b8a42 361static int parse_bind_val(const char *val, int *type)
7e4c661a 362{
363 int i;
364
093b8a42 365 *type = IN_BINDTYPE_NONE;
7e4c661a 366 if (val[0] == 0)
367 return 0;
368
369 if (strncasecmp(val, "player", 6) == 0)
370 {
093b8a42 371 int player, shift = 0;
7e4c661a 372 player = atoi(val + 6) - 1;
093b8a42 373
7e4c661a 374 if (player > 1)
375 return -1;
093b8a42 376 if (player == 1)
377 shift = 16;
7e4c661a 378
093b8a42 379 *type = IN_BINDTYPE_PLAYER12;
67b7b971 380 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
7e4c661a 381 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
093b8a42 382 return me_ctrl_actions[i].mask << shift;
7e4c661a 383 }
384 }
385 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
093b8a42 386 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
387 *type = IN_BINDTYPE_EMU;
7e4c661a 388 return emuctrl_actions[i].mask;
093b8a42 389 }
7e4c661a 390 }
391
392 return -1;
393}
394
504e2f56 395static void keys_parse_all(FILE *f)
1ca2ea4f 396{
504e2f56 397 char line[256], *var, *val;
398 int dev_id = -1;
093b8a42 399 int acts, type;
504e2f56 400 int ret;
1ca2ea4f 401
504e2f56 402 while (!feof(f))
403 {
404 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
405 if (ret == 0) break;
406 if (ret == -1) continue;
1ca2ea4f 407
504e2f56 408 if (strcasecmp(var, "binddev") == 0) {
409 dev_id = in_config_parse_dev(val);
410 if (dev_id < 0) {
411 printf("input: can't handle dev: %s\n", val);
412 continue;
413 }
414 in_unbind_all(dev_id, -1, -1);
415 continue;
416 }
417 if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0)
418 continue;
7e4c661a 419
504e2f56 420 acts = parse_bind_val(val, &type);
421 if (acts == -1) {
422 lprintf("config: unhandled action \"%s\"\n", val);
423 return;
424 }
7e4c661a 425
504e2f56 426 mystrip(var + 5);
427 in_config_bind_key(dev_id, var + 5, acts, type);
428 }
7e4c661a 429}
430
504e2f56 431static void parse(const char *var, const char *val, int *keys_encountered)
835e7900 432{
433 menu_entry *me;
fcdefcf6 434 int tmp;
835e7900 435
1ca2ea4f 436 if (strcasecmp(var, "LastUsedROM") == 0)
437 return; /* handled elsewhere */
438
7e4c661a 439 if (strncasecmp(var, "bind", 4) == 0) {
504e2f56 440 *keys_encountered = 1;
441 return; /* handled elsewhere */
442 }
7e4c661a 443
504e2f56 444 if (strcasecmp(var, "Sound Volume") == 0) {
445 currentConfig.volume = atoi(val);
1ca2ea4f 446 return;
447 }
7e4c661a 448
fcdefcf6 449 for (me = me_list_get_first(); me != NULL; me = me_list_get_next())
835e7900 450 {
fcdefcf6 451 char *p;
452
713c9224 453 if (!me->need_to_save)
fcdefcf6 454 continue;
45285368 455 if (me->name == NULL || strcasecmp(var, me->name) != 0)
456 continue;
fcdefcf6 457
45285368 458 if (me->beh == MB_OPT_ONOFF) {
459 tmp = strtol(val, &p, 0);
460 if (*p != 0)
fcdefcf6 461 goto bad_val;
45285368 462 if (tmp) *(int *)me->var |= me->mask;
463 else *(int *)me->var &= ~me->mask;
464 return;
465 }
466 else if (me->beh == MB_OPT_RANGE) {
467 tmp = strtol(val, &p, 0);
468 if (*p != 0)
469 goto bad_val;
470 if (tmp < me->min) tmp = me->min;
471 if (tmp > me->max) tmp = me->max;
472 *(int *)me->var = tmp;
473 return;
474 }
475 else if (me->beh == MB_OPT_ENUM) {
476 const char **names, *p1;
477 int i;
478
479 names = (const char **)me->data;
480 if (names == NULL)
481 goto bad_val;
482 for (i = 0; names[i] != NULL; i++) {
483 for (p1 = names[i]; *p1 == ' '; p1++)
484 ;
485 if (strcasecmp(p1, val) == 0) {
486 *(int *)me->var = i;
487 return;
488 }
fcdefcf6 489 }
45285368 490 goto bad_val;
835e7900 491 }
45285368 492 else if (custom_read(me, var, val))
493 return;
835e7900 494 }
fcdefcf6 495
496 lprintf("config_readsect: unhandled var: \"%s\"\n", var);
497 return;
498
499bad_val:
504e2f56 500 lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val);
1ca2ea4f 501}
502
835e7900 503int config_readsect(const char *fname, const char *section)
504{
4609d0cd 505 char line[128], *var, *val;
504e2f56 506 int keys_encountered = 0;
1ca2ea4f 507 FILE *f;
4609d0cd 508 int ret;
835e7900 509
1ca2ea4f 510 f = fopen(fname, "r");
d6114368 511 if (f == NULL) return -1;
835e7900 512
513 if (section != NULL)
514 {
504e2f56 515 /* for game_def.cfg only */
835e7900 516 ret = seek_sect(f, section);
517 if (!ret) {
c46ffd31 518 lprintf("config_readsect: %s: missing section [%s]\n", fname, section);
835e7900 519 fclose(f);
520 return -1;
521 }
522 }
523
504e2f56 524 emu_set_defconfig();
bdec53c9 525
835e7900 526 while (!feof(f))
527 {
4609d0cd 528 ret = config_get_var_val(f, line, sizeof(line), &var, &val);
529 if (ret == 0) break;
530 if (ret == -1) continue;
835e7900 531
504e2f56 532 parse(var, val, &keys_encountered);
533 }
534
535 if (keys_encountered) {
536 rewind(f);
537 keys_parse_all(f);
835e7900 538 }
539
540 fclose(f);
504e2f56 541
542 lprintf("config_readsect: loaded from %s", fname);
543 if (section != NULL)
544 lprintf(" [%s]", section);
545 printf("\n");
546
835e7900 547 return 0;
548}
549
4609d0cd 550#endif // _MSC_VER
551
552static char *mystrip(char *str)
553{
554 int i, len;
555
556 len = strlen(str);
557 for (i = 0; i < len; i++)
558 if (str[i] != ' ') break;
559 if (i > 0) memmove(str, str + i, len - i + 1);
560
561 len = strlen(str);
562 for (i = len - 1; i >= 0; i--)
563 if (str[i] != ' ') break;
564 str[i+1] = 0;
565
566 return str;
567}
568
569/* returns:
570 * 0 - EOF, end
571 * 1 - parsed ok
572 * -1 - failed to parse line
573 */
574int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval)
575{
576 char *var, *val, *tmp;
577 FILE *f = file;
578 int len, i;
579
580 tmp = fgets(line, lsize, f);
581 if (tmp == NULL) return 0;
582
583 if (line[0] == '[') return 0; // other section
584
585 // strip comments, linefeed, spaces..
586 len = strlen(line);
587 for (i = 0; i < len; i++)
588 if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; }
589 mystrip(line);
590 len = strlen(line);
591 if (len <= 0) return -1;;
592
593 // get var and val
594 for (i = 0; i < len; i++)
595 if (line[i] == '=') break;
596 if (i >= len || strchr(&line[i+1], '=') != NULL) {
597 lprintf("config_readsect: can't parse: %s\n", line);
598 return -1;
599 }
600 line[i] = 0;
601 var = line;
602 val = &line[i+1];
603 mystrip(var);
604 mystrip(val);
605
606#ifndef _MSC_VER
607 if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) {
608 lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val);
609 return -1;;
610 }
611#endif
612
613 *rvar = var;
614 *rval = val;
615 return 1;
616}
617