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 | |
21 | static char *mystrip(char *str); |
22 | |
23 | #ifndef _MSC_VER |
24 | |
21ebcfd3 |
25 | #include "menu_pico.h" |
835e7900 |
26 | #include "emu.h" |
f7741cac |
27 | #include <pico/pico_int.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 |
36 | static int seek_sect(FILE *f, const char *section) |
37 | { |
2d84b925 |
38 | char line[640], *tmp; |
835e7900 |
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 | |
20c9a3ba |
56 | static void keys_write(FILE *fn, int dev_id, const int *binds, const int *kbd_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); |
438abd99 |
70 | if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign |
576c1b8a |
71 | if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign |
ca482e5d |
72 | |
67b7b971 |
73 | for (i = 0; me_ctrl_actions[i].name != NULL; i++) { |
093b8a42 |
74 | mask = me_ctrl_actions[i].mask; |
75 | if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) { |
76 | strncpy(act, me_ctrl_actions[i].name, 31); |
504e2f56 |
77 | fprintf(fn, "bind %s = player1 %s" NL, name, mystrip(act)); |
093b8a42 |
78 | } |
79 | mask = me_ctrl_actions[i].mask << 16; |
80 | if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)]) { |
bdec53c9 |
81 | strncpy(act, me_ctrl_actions[i].name, 31); |
504e2f56 |
82 | fprintf(fn, "bind %s = player2 %s" NL, name, mystrip(act)); |
1ca2ea4f |
83 | } |
84 | } |
1ca2ea4f |
85 | |
1d5885dd |
86 | for (i = 0; me_ctrl_actions[i].name != NULL; i++) { |
87 | mask = me_ctrl_actions[i].mask; |
88 | if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) { |
89 | strncpy(act, me_ctrl_actions[i].name, 31); |
90 | fprintf(fn, "bind %s = player3 %s" NL, name, mystrip(act)); |
91 | } |
92 | mask = me_ctrl_actions[i].mask << 16; |
93 | if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)]) { |
94 | strncpy(act, me_ctrl_actions[i].name, 31); |
95 | fprintf(fn, "bind %s = player4 %s" NL, name, mystrip(act)); |
96 | } |
97 | } |
98 | |
bdec53c9 |
99 | for (i = 0; emuctrl_actions[i].name != NULL; i++) { |
093b8a42 |
100 | mask = emuctrl_actions[i].mask; |
101 | if (mask & binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)]) { |
bdec53c9 |
102 | strncpy(act, emuctrl_actions[i].name, 31); |
504e2f56 |
103 | fprintf(fn, "bind %s = %s" NL, name, mystrip(act)); |
bdec53c9 |
104 | } |
105 | } |
1ca2ea4f |
106 | } |
369f7e94 |
107 | |
108 | for (k = 0; k < key_count; k++) { |
109 | const char *name = in_get_key_name(dev_id, k); |
438abd99 |
110 | if (strcmp(name, "#") == 0) name = "\\x23"; // replace comment sign |
576c1b8a |
111 | if (strcmp(name, "=") == 0) name = "\\x3d"; // replace assignment sign |
20c9a3ba |
112 | if (kbd_binds[k]) |
113 | fprintf(fn, "bind %s = key%02x" NL, name, kbd_binds[k]); |
369f7e94 |
114 | } |
1ca2ea4f |
115 | } |
116 | |
504e2f56 |
117 | int config_write(const char *fname) |
dabe23b6 |
118 | { |
504e2f56 |
119 | FILE *fn = NULL; |
835e7900 |
120 | menu_entry *me; |
504e2f56 |
121 | int t; |
2d84b925 |
122 | char line[640]; |
c46ffd31 |
123 | |
504e2f56 |
124 | fn = fopen(fname, "w"); |
125 | if (fn == NULL) |
835e7900 |
126 | return -1; |
835e7900 |
127 | |
fcdefcf6 |
128 | for (me = me_list_get_first(); me != NULL; me = me_list_get_next()) |
835e7900 |
129 | { |
713c9224 |
130 | int dummy; |
70db485b |
131 | if (!me->need_to_save) |
fcdefcf6 |
132 | continue; |
45285368 |
133 | if (me->name == NULL || me->name[0] == 0) |
134 | continue; |
fcdefcf6 |
135 | |
dabe23b6 |
136 | if (me->beh == MB_OPT_ONOFF || me->beh == MB_OPT_CUSTONOFF) { |
504e2f56 |
137 | fprintf(fn, "%s = %i" NL, me->name, (*(int *)me->var & me->mask) ? 1 : 0); |
fcdefcf6 |
138 | } |
139 | else if (me->beh == MB_OPT_RANGE || me->beh == MB_OPT_CUSTRANGE) { |
504e2f56 |
140 | fprintf(fn, "%s = %i" NL, me->name, *(int *)me->var); |
fcdefcf6 |
141 | } |
70db485b |
142 | else if (me->beh == MB_OPT_ENUM) { |
fcdefcf6 |
143 | const char **names = (const char **)me->data; |
70db485b |
144 | if (names == NULL) |
145 | continue; |
504e2f56 |
146 | for (t = 0; names[t] != NULL; t++) { |
147 | if (*(int *)me->var == t) { |
21707975 |
148 | strncpy(line, names[t], sizeof(line)-1); |
149 | line[sizeof(line)-1] = '\0'; |
fcdefcf6 |
150 | goto write_line; |
151 | } |
504e2f56 |
152 | } |
fcdefcf6 |
153 | } |
45285368 |
154 | else if (me->generate_name != NULL) { |
34c7e34f |
155 | strncpy(line, me->generate_name(me->id, &dummy), sizeof(line)-1); |
21707975 |
156 | line[sizeof(line)-1] = '\0'; |
504e2f56 |
157 | goto write_line; |
fcdefcf6 |
158 | } |
159 | else |
70db485b |
160 | lprintf("config: unhandled write: '%s' id %d behavior %d\n", |
161 | me->name, me->id, me->beh); |
fcdefcf6 |
162 | continue; |
163 | |
164 | write_line: |
165 | line[sizeof(line) - 1] = 0; |
166 | mystrip(line); |
167 | fprintf(fn, "%s = %s" NL, me->name, line); |
835e7900 |
168 | } |
1ca2ea4f |
169 | |
7e4c661a |
170 | /* input: save binds */ |
171 | for (t = 0; t < IN_MAX_DEVS; t++) |
172 | { |
173 | const int *binds = in_get_dev_binds(t); |
713c9224 |
174 | const char *name = in_get_dev_name(t, 0, 0); |
45285368 |
175 | int count = 0; |
7e4c661a |
176 | |
177 | if (binds == NULL || name == NULL) |
178 | continue; |
179 | |
504e2f56 |
180 | fprintf(fn, "binddev = %s" NL, name); |
7e4c661a |
181 | |
45285368 |
182 | in_get_config(t, IN_CFG_BIND_COUNT, &count); |
20c9a3ba |
183 | keys_write(fn, t, binds, in_get_dev_kbd_binds(t)); |
7e4c661a |
184 | } |
185 | |
504e2f56 |
186 | fprintf(fn, "Sound Volume = %i" NL, currentConfig.volume); |
7b802576 |
187 | |
835e7900 |
188 | fprintf(fn, NL); |
189 | |
835e7900 |
190 | fclose(fn); |
191 | return 0; |
192 | } |
193 | |
835e7900 |
194 | int config_writelrom(const char *fname) |
195 | { |
2d84b925 |
196 | char line[640], *tmp, *optr = NULL; |
835e7900 |
197 | char *old_data = NULL; |
198 | int size; |
199 | FILE *f; |
200 | |
713c9224 |
201 | if (strlen(rom_fname_loaded) == 0) return -1; |
835e7900 |
202 | |
203 | f = fopen(fname, "r"); |
204 | if (f != NULL) |
205 | { |
206 | fseek(f, 0, SEEK_END); |
207 | size = ftell(f); |
208 | fseek(f, 0, SEEK_SET); |
209 | old_data = malloc(size + size/8); |
210 | if (old_data != NULL) |
211 | { |
212 | optr = old_data; |
213 | while (!feof(f)) |
214 | { |
215 | tmp = fgets(line, sizeof(line), f); |
216 | if (tmp == NULL) break; |
217 | mystrip(line); |
218 | if (strncasecmp(line, "LastUsedROM", 11) == 0) |
219 | continue; |
220 | sprintf(optr, "%s", line); |
221 | optr += strlen(optr); |
222 | } |
223 | } |
224 | fclose(f); |
225 | } |
226 | |
227 | f = fopen(fname, "w"); |
228 | if (f == NULL) return -1; |
229 | |
230 | if (old_data != NULL) { |
231 | fwrite(old_data, 1, optr - old_data, f); |
232 | free(old_data); |
233 | } |
713c9224 |
234 | fprintf(f, "LastUsedROM = %s" NL, rom_fname_loaded); |
835e7900 |
235 | fclose(f); |
236 | return 0; |
237 | } |
238 | |
1ca2ea4f |
239 | /* --------------------------------------------------------------------------*/ |
240 | |
241 | int config_readlrom(const char *fname) |
242 | { |
2d84b925 |
243 | char line[640], *tmp; |
1ca2ea4f |
244 | int i, len, ret = -1; |
245 | FILE *f; |
246 | |
247 | f = fopen(fname, "r"); |
248 | if (f == NULL) return -1; |
249 | |
250 | // seek to the section needed |
251 | while (!feof(f)) |
252 | { |
253 | tmp = fgets(line, sizeof(line), f); |
254 | if (tmp == NULL) break; |
255 | |
256 | if (strncasecmp(line, "LastUsedROM", 11) != 0) continue; |
257 | len = strlen(line); |
258 | for (i = 0; i < len; i++) |
259 | if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; } |
260 | tmp = strchr(line, '='); |
261 | if (tmp == NULL) break; |
262 | tmp++; |
263 | mystrip(tmp); |
264 | |
21707975 |
265 | len = sizeof(rom_fname_loaded)-1; |
713c9224 |
266 | strncpy(rom_fname_loaded, tmp, len); |
21707975 |
267 | rom_fname_loaded[len] = 0; |
1ca2ea4f |
268 | ret = 0; |
269 | break; |
270 | } |
271 | fclose(f); |
272 | return ret; |
273 | } |
274 | |
835e7900 |
275 | static int custom_read(menu_entry *me, const char *var, const char *val) |
276 | { |
277 | char *tmp; |
835e7900 |
278 | |
279 | switch (me->id) |
280 | { |
835e7900 |
281 | case MA_OPT_FRAMESKIP: |
282 | if (strcasecmp(var, "Frameskip") != 0) return 0; |
283 | if (strcasecmp(val, "Auto") == 0) |
284 | currentConfig.Frameskip = -1; |
285 | else currentConfig.Frameskip = atoi(val); |
286 | return 1; |
287 | |
288 | case MA_OPT_SOUND_QUALITY: |
289 | if (strcasecmp(var, "Sound Quality") != 0) return 0; |
6311a3ba |
290 | PicoIn.sndRate = strtoul(val, &tmp, 10); |
68a95087 |
291 | if (PicoIn.sndRate < 8000 || PicoIn.sndRate > 54000) { |
a987b678 |
292 | if (strncasecmp(tmp, "native", 6) == 0) { |
293 | tmp += 6; |
294 | PicoIn.sndRate = 53000; |
295 | } else |
296 | PicoIn.sndRate = 22050; |
297 | } |
21ecaf23 |
298 | if (*tmp == 'H' || *tmp == 'h') tmp++; |
299 | if (*tmp == 'Z' || *tmp == 'z') tmp++; |
835e7900 |
300 | while (*tmp == ' ') tmp++; |
301 | if (strcasecmp(tmp, "stereo") == 0) { |
93f9619e |
302 | PicoIn.opt |= POPT_EN_STEREO; |
835e7900 |
303 | } else if (strcasecmp(tmp, "mono") == 0) { |
93f9619e |
304 | PicoIn.opt &= ~POPT_EN_STEREO; |
835e7900 |
305 | } else |
306 | return 0; |
307 | return 1; |
308 | |
37631374 |
309 | case MA_OPT_SOUND_ALPHA: |
8b55d189 |
310 | if (strcasecmp(var, "Filter strength") != 0) return 0; |
37631374 |
311 | PicoIn.sndFilterAlpha = 0x10000 * atof(val); |
312 | return 1; |
313 | |
835e7900 |
314 | case MA_OPT_REGION: |
315 | if (strcasecmp(var, "Region") != 0) return 0; |
316 | if (strncasecmp(val, "Auto: ", 6) == 0) |
317 | { |
318 | const char *p = val + 5, *end = val + strlen(val); |
319 | int i; |
93f9619e |
320 | PicoIn.regionOverride = PicoIn.autoRgnOrder = 0; |
c46ffd31 |
321 | for (i = 0; p < end && i < 3; i++) |
322 | { |
323 | while (*p == ' ') p++; |
835e7900 |
324 | if (p[0] == 'J' && p[1] == 'P') { |
93f9619e |
325 | PicoIn.autoRgnOrder |= 1 << (i*4); |
835e7900 |
326 | } else if (p[0] == 'U' && p[1] == 'S') { |
93f9619e |
327 | PicoIn.autoRgnOrder |= 4 << (i*4); |
835e7900 |
328 | } else if (p[0] == 'E' && p[1] == 'U') { |
93f9619e |
329 | PicoIn.autoRgnOrder |= 8 << (i*4); |
835e7900 |
330 | } |
c46ffd31 |
331 | while (*p != ' ' && *p != 0) p++; |
332 | if (*p == 0) break; |
835e7900 |
333 | } |
334 | } |
335 | else if (strcasecmp(val, "Auto") == 0) { |
93f9619e |
336 | PicoIn.regionOverride = 0; |
835e7900 |
337 | } else if (strcasecmp(val, "Japan NTSC") == 0) { |
93f9619e |
338 | PicoIn.regionOverride = 1; |
835e7900 |
339 | } else if (strcasecmp(val, "Japan PAL") == 0) { |
93f9619e |
340 | PicoIn.regionOverride = 2; |
835e7900 |
341 | } else if (strcasecmp(val, "USA") == 0) { |
93f9619e |
342 | PicoIn.regionOverride = 4; |
835e7900 |
343 | } else if (strcasecmp(val, "Europe") == 0) { |
93f9619e |
344 | PicoIn.regionOverride = 8; |
835e7900 |
345 | } else |
346 | return 0; |
347 | return 1; |
348 | |
fcdefcf6 |
349 | case MA_32XOPT_MSH2_CYCLES: |
ed4402a7 |
350 | currentConfig.msh2_khz = atoi(val); |
351 | Pico32xSetClocks(currentConfig.msh2_khz * 1000, 0); |
352 | return 1; |
353 | |
354 | case MA_32XOPT_SSH2_CYCLES: |
355 | currentConfig.ssh2_khz = atoi(val); |
356 | Pico32xSetClocks(0, currentConfig.ssh2_khz * 1000); |
fcdefcf6 |
357 | return 1; |
fcdefcf6 |
358 | |
68af34fe |
359 | case MA_OPT2_GAMMA: |
360 | currentConfig.gamma = atoi(val); |
361 | return 1; |
362 | |
e7ee7bc0 |
363 | case MA_OPT2_MAX_FRAMESKIP: |
364 | currentConfig.max_skip = atoi(val); |
365 | return 1; |
366 | |
bac44b18 |
367 | case MA_CTRL_KEYBOARD: |
368 | currentConfig.keyboard = 0; |
eb815ab4 |
369 | if (strcasecmp(val, "physical") == 0) |
eb815ab4 |
370 | currentConfig.keyboard = 2; |
4814d19e |
371 | else if (strcasecmp(val, "virtual") == 0) |
372 | currentConfig.keyboard = 1; |
bac44b18 |
373 | return 1; |
374 | |
6fc57144 |
375 | /* PSP */ |
6fc57144 |
376 | case MA_OPT3_VSYNC: |
fcdefcf6 |
377 | // XXX: use enum |
6fc57144 |
378 | if (strcasecmp(var, "Wait for vsync") != 0) return 0; |
379 | if (strcasecmp(val, "never") == 0) { |
37631374 |
380 | currentConfig.EmuOpt &= ~(EOPT_VSYNC|EOPT_VSYNC_MODE); |
6fc57144 |
381 | } else if (strcasecmp(val, "sometimes") == 0) { |
37631374 |
382 | currentConfig.EmuOpt |= (EOPT_VSYNC|EOPT_VSYNC_MODE); |
6fc57144 |
383 | } else if (strcasecmp(val, "always") == 0) { |
37631374 |
384 | currentConfig.EmuOpt &= ~EOPT_VSYNC_MODE; |
385 | currentConfig.EmuOpt |= EOPT_VSYNC; |
6fc57144 |
386 | } else |
387 | return 0; |
388 | return 1; |
389 | |
835e7900 |
390 | default: |
fcdefcf6 |
391 | lprintf("unhandled custom_read %i: %s\n", me->id, var); |
835e7900 |
392 | return 0; |
393 | } |
394 | } |
395 | |
093b8a42 |
396 | static int parse_bind_val(const char *val, int *type) |
7e4c661a |
397 | { |
398 | int i; |
399 | |
093b8a42 |
400 | *type = IN_BINDTYPE_NONE; |
7e4c661a |
401 | if (val[0] == 0) |
402 | return 0; |
403 | |
369f7e94 |
404 | if (strncasecmp(val, "key", 3) == 0) |
405 | { |
20c9a3ba |
406 | *type = IN_BINDTYPE_KEYBOARD; |
369f7e94 |
407 | return strtol(val + 3, NULL, 16); |
408 | } |
409 | |
7e4c661a |
410 | if (strncasecmp(val, "player", 6) == 0) |
411 | { |
093b8a42 |
412 | int player, shift = 0; |
7e4c661a |
413 | player = atoi(val + 6) - 1; |
093b8a42 |
414 | |
1d5885dd |
415 | if (player > 3) |
7e4c661a |
416 | return -1; |
1d5885dd |
417 | if (player & 1) |
093b8a42 |
418 | shift = 16; |
7e4c661a |
419 | |
1d5885dd |
420 | *type = IN_BINDTYPE_PLAYER12 + (player >> 1); |
67b7b971 |
421 | for (i = 0; me_ctrl_actions[i].name != NULL; i++) { |
7e4c661a |
422 | if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0) |
093b8a42 |
423 | return me_ctrl_actions[i].mask << shift; |
7e4c661a |
424 | } |
425 | } |
426 | for (i = 0; emuctrl_actions[i].name != NULL; i++) { |
093b8a42 |
427 | if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) { |
428 | *type = IN_BINDTYPE_EMU; |
7e4c661a |
429 | return emuctrl_actions[i].mask; |
093b8a42 |
430 | } |
7e4c661a |
431 | } |
432 | |
433 | return -1; |
434 | } |
435 | |
504e2f56 |
436 | static void keys_parse_all(FILE *f) |
1ca2ea4f |
437 | { |
4cc0fcaf |
438 | char line[640], *var, *val; |
504e2f56 |
439 | int dev_id = -1; |
093b8a42 |
440 | int acts, type; |
504e2f56 |
441 | int ret; |
1ca2ea4f |
442 | |
504e2f56 |
443 | while (!feof(f)) |
444 | { |
445 | ret = config_get_var_val(f, line, sizeof(line), &var, &val); |
446 | if (ret == 0) break; |
447 | if (ret == -1) continue; |
1ca2ea4f |
448 | |
504e2f56 |
449 | if (strcasecmp(var, "binddev") == 0) { |
450 | dev_id = in_config_parse_dev(val); |
451 | if (dev_id < 0) { |
452 | printf("input: can't handle dev: %s\n", val); |
453 | continue; |
454 | } |
455 | in_unbind_all(dev_id, -1, -1); |
456 | continue; |
457 | } |
458 | if (dev_id < 0 || strncasecmp(var, "bind ", 5) != 0) |
459 | continue; |
7e4c661a |
460 | |
504e2f56 |
461 | acts = parse_bind_val(val, &type); |
462 | if (acts == -1) { |
463 | lprintf("config: unhandled action \"%s\"\n", val); |
9db6a544 |
464 | continue; |
504e2f56 |
465 | } |
7e4c661a |
466 | |
504e2f56 |
467 | mystrip(var + 5); |
20c9a3ba |
468 | if (type == IN_BINDTYPE_KEYBOARD) |
469 | in_config_bind_kbd_key(dev_id, var + 5, acts); |
369f7e94 |
470 | else |
471 | in_config_bind_key(dev_id, var + 5, acts, type); |
504e2f56 |
472 | } |
8c10129f |
473 | in_clean_binds(); |
7e4c661a |
474 | } |
475 | |
504e2f56 |
476 | static void parse(const char *var, const char *val, int *keys_encountered) |
835e7900 |
477 | { |
478 | menu_entry *me; |
fcdefcf6 |
479 | int tmp; |
835e7900 |
480 | |
1ca2ea4f |
481 | if (strcasecmp(var, "LastUsedROM") == 0) |
482 | return; /* handled elsewhere */ |
483 | |
7e4c661a |
484 | if (strncasecmp(var, "bind", 4) == 0) { |
504e2f56 |
485 | *keys_encountered = 1; |
486 | return; /* handled elsewhere */ |
487 | } |
7e4c661a |
488 | |
504e2f56 |
489 | if (strcasecmp(var, "Sound Volume") == 0) { |
490 | currentConfig.volume = atoi(val); |
1ca2ea4f |
491 | return; |
492 | } |
7e4c661a |
493 | |
fcdefcf6 |
494 | for (me = me_list_get_first(); me != NULL; me = me_list_get_next()) |
835e7900 |
495 | { |
fcdefcf6 |
496 | char *p; |
497 | |
713c9224 |
498 | if (!me->need_to_save) |
fcdefcf6 |
499 | continue; |
45285368 |
500 | if (me->name == NULL || strcasecmp(var, me->name) != 0) |
501 | continue; |
fcdefcf6 |
502 | |
45285368 |
503 | if (me->beh == MB_OPT_ONOFF) { |
504 | tmp = strtol(val, &p, 0); |
505 | if (*p != 0) |
fcdefcf6 |
506 | goto bad_val; |
45285368 |
507 | if (tmp) *(int *)me->var |= me->mask; |
508 | else *(int *)me->var &= ~me->mask; |
509 | return; |
510 | } |
511 | else if (me->beh == MB_OPT_RANGE) { |
512 | tmp = strtol(val, &p, 0); |
513 | if (*p != 0) |
514 | goto bad_val; |
515 | if (tmp < me->min) tmp = me->min; |
516 | if (tmp > me->max) tmp = me->max; |
517 | *(int *)me->var = tmp; |
518 | return; |
519 | } |
520 | else if (me->beh == MB_OPT_ENUM) { |
521 | const char **names, *p1; |
522 | int i; |
523 | |
524 | names = (const char **)me->data; |
525 | if (names == NULL) |
526 | goto bad_val; |
527 | for (i = 0; names[i] != NULL; i++) { |
528 | for (p1 = names[i]; *p1 == ' '; p1++) |
529 | ; |
530 | if (strcasecmp(p1, val) == 0) { |
531 | *(int *)me->var = i; |
532 | return; |
533 | } |
fcdefcf6 |
534 | } |
45285368 |
535 | goto bad_val; |
835e7900 |
536 | } |
45285368 |
537 | else if (custom_read(me, var, val)) |
538 | return; |
835e7900 |
539 | } |
fcdefcf6 |
540 | |
541 | lprintf("config_readsect: unhandled var: \"%s\"\n", var); |
542 | return; |
543 | |
544 | bad_val: |
504e2f56 |
545 | lprintf("config_readsect: unhandled val for \"%s\": \"%s\"\n", var, val); |
1ca2ea4f |
546 | } |
547 | |
835e7900 |
548 | int config_readsect(const char *fname, const char *section) |
549 | { |
2d84b925 |
550 | char line[640], *var, *val; |
504e2f56 |
551 | int keys_encountered = 0; |
1ca2ea4f |
552 | FILE *f; |
4609d0cd |
553 | int ret; |
835e7900 |
554 | |
1ca2ea4f |
555 | f = fopen(fname, "r"); |
d6114368 |
556 | if (f == NULL) return -1; |
835e7900 |
557 | |
558 | if (section != NULL) |
559 | { |
504e2f56 |
560 | /* for game_def.cfg only */ |
835e7900 |
561 | ret = seek_sect(f, section); |
562 | if (!ret) { |
c46ffd31 |
563 | lprintf("config_readsect: %s: missing section [%s]\n", fname, section); |
835e7900 |
564 | fclose(f); |
565 | return -1; |
566 | } |
567 | } |
568 | |
504e2f56 |
569 | emu_set_defconfig(); |
bdec53c9 |
570 | |
835e7900 |
571 | while (!feof(f)) |
572 | { |
4609d0cd |
573 | ret = config_get_var_val(f, line, sizeof(line), &var, &val); |
574 | if (ret == 0) break; |
575 | if (ret == -1) continue; |
835e7900 |
576 | |
504e2f56 |
577 | parse(var, val, &keys_encountered); |
578 | } |
579 | |
580 | if (keys_encountered) { |
581 | rewind(f); |
582 | keys_parse_all(f); |
835e7900 |
583 | } |
584 | |
585 | fclose(f); |
504e2f56 |
586 | |
587 | lprintf("config_readsect: loaded from %s", fname); |
588 | if (section != NULL) |
589 | lprintf(" [%s]", section); |
590 | printf("\n"); |
591 | |
835e7900 |
592 | return 0; |
593 | } |
594 | |
4609d0cd |
595 | #endif // _MSC_VER |
596 | |
597 | static char *mystrip(char *str) |
598 | { |
599 | int i, len; |
600 | |
601 | len = strlen(str); |
602 | for (i = 0; i < len; i++) |
603 | if (str[i] != ' ') break; |
604 | if (i > 0) memmove(str, str + i, len - i + 1); |
605 | |
606 | len = strlen(str); |
607 | for (i = len - 1; i >= 0; i--) |
608 | if (str[i] != ' ') break; |
609 | str[i+1] = 0; |
610 | |
611 | return str; |
612 | } |
613 | |
614 | /* returns: |
615 | * 0 - EOF, end |
616 | * 1 - parsed ok |
617 | * -1 - failed to parse line |
618 | */ |
619 | int config_get_var_val(void *file, char *line, int lsize, char **rvar, char **rval) |
620 | { |
621 | char *var, *val, *tmp; |
622 | FILE *f = file; |
623 | int len, i; |
624 | |
625 | tmp = fgets(line, lsize, f); |
626 | if (tmp == NULL) return 0; |
627 | |
628 | if (line[0] == '[') return 0; // other section |
629 | |
630 | // strip comments, linefeed, spaces.. |
631 | len = strlen(line); |
632 | for (i = 0; i < len; i++) |
633 | if (line[i] == '#' || line[i] == '\r' || line[i] == '\n') { line[i] = 0; break; } |
634 | mystrip(line); |
635 | len = strlen(line); |
636 | if (len <= 0) return -1;; |
637 | |
638 | // get var and val |
639 | for (i = 0; i < len; i++) |
640 | if (line[i] == '=') break; |
641 | if (i >= len || strchr(&line[i+1], '=') != NULL) { |
642 | lprintf("config_readsect: can't parse: %s\n", line); |
643 | return -1; |
644 | } |
645 | line[i] = 0; |
646 | var = line; |
647 | val = &line[i+1]; |
648 | mystrip(var); |
649 | mystrip(val); |
650 | |
651 | #ifndef _MSC_VER |
652 | if (strlen(var) == 0 || (strlen(val) == 0 && strncasecmp(var, "bind", 4) != 0)) { |
653 | lprintf("config_readsect: something's empty: \"%s\" = \"%s\"\n", var, val); |
654 | return -1;; |
655 | } |
656 | #endif |
657 | |
658 | *rvar = var; |
659 | *rval = val; |
660 | return 1; |
661 | } |
662 | |