DEADSOFTWARE

render: opengl: add screen scaling
[flatwaifu.git] / src / config.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include "map.h"
30 #include "sound.h"
31 #include "music.h"
32 #include "view.h"
33 #include "monster.h"
34 #include "player.h"
35 #include "menu.h"
36 #include "files.h"
37 #include "render.h"
38 #include "error.h"
39 #include "input.h"
40 #include "my.h"
42 enum {NONE, BYTE, WORD, DWORD, STRING, SW_ON, SW_OFF, FILES, KEY};
44 typedef struct cfg_t {
45 const char *cfg;
46 void *p;
47 byte t;
48 } cfg_t;
50 byte cheat;
51 byte shot_vga;
53 static FILE *f;
54 static int ch;
56 static const cfg_t arg[] = {
57 {"file", NULL, FILES},
58 {"cheat", &cheat, SW_ON},
59 {"vga", &shot_vga, SW_ON},
60 {"sndvol", &snd_vol, WORD},
61 {"musvol",&mus_vol, WORD},
62 // {"fullscr", &fullscreen, SW_ON},
63 // {"window", &fullscreen, SW_OFF},
64 {"mon", &nomon, SW_OFF},
65 // {"gamma", &gammaa, DWORD},
66 {"warp", &_warp, BYTE},
67 // {"width", &SCRW, DWORD},
68 // {"height", &SCRH, DWORD},
69 // {"config", NULL, cfg_file, STRING},
70 {NULL, NULL, NONE} // end
71 };
73 static const cfg_t cfg[] = {
74 {"screenshot", &shot_vga, SW_ON},
75 {"sound_volume", &snd_vol, WORD},
76 {"music_volume", &mus_vol, WORD},
77 // {"fullscreen", &fullscreen, SW_ON},
78 {"sky", &w_horiz, SW_ON},
79 // {"gamma", &gammaa, DWORD},
80 // {"screen_width", &SCRW, DWORD},
81 // {"screen_height", &SCRH, DWORD},
82 {"music_random", &music_random, SW_ON},
83 {"music_time", &music_time, DWORD},
84 {"music_fade", &music_fade, DWORD},
85 {"pl1_left", &pl1.kl, KEY},
86 {"pl1_right",&pl1.kr, KEY},
87 {"pl1_up", &pl1.ku, KEY},
88 {"pl1_down", &pl1.kd, KEY},
89 {"pl1_jump", &pl1.kj, KEY},
90 {"pl1_fire", &pl1.kf, KEY},
91 {"pl1_next", &pl1.kwr, KEY},
92 {"pl1_prev", &pl1.kwl, KEY},
93 {"pl1_use", &pl1.kp, KEY},
94 {"pl2_left", &pl2.kl, KEY},
95 {"pl2_right",&pl2.kr, KEY},
96 {"pl2_up", &pl2.ku, KEY},
97 {"pl2_down", &pl2.kd, KEY},
98 {"pl2_jump", &pl2.kj, KEY},
99 {"pl2_fire", &pl2.kf, KEY},
100 {"pl2_next", &pl2.kwr, KEY},
101 {"pl2_prev", &pl2.kwl, KEY},
102 {"pl2_use", &pl2.kp, KEY},
103 {NULL, NULL, NONE} // end
104 };
106 static const cfg_t *CFG_find_entry (const char *key, const cfg_t *cfg) {
107 assert(key != NULL);
108 assert(cfg != NULL);
109 int i = 0;
110 while (cfg[i].cfg && strcasecmp(cfg[i].cfg, key) != 0) {
111 i++;
113 return cfg[i].cfg ? &cfg[i] : NULL;
116 static int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
117 const cfg_t *entry = CFG_find_entry(key, cfg);
118 if (entry != NULL) {
119 void *p = entry->p;
120 switch (entry->t) {
121 case BYTE: *(byte*)p = atoi(value); break;
122 case WORD: *(word*)p = atoi(value); break;
123 case DWORD: *(dword*)p = atoi(value); break;
124 case STRING: strcpy(p, value); break; // TODO fix this security problem
125 case SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
126 case SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 0 : 1; break;
127 case FILES: F_addwad(value); break;
128 case KEY: *(int*)p = I_string_to_key(value); break;
129 default: assert(0); // unknown type -> something broken
131 return 1;
132 } else {
133 return 0;
137 /* --- parser --- */
139 static int CFG_open_iterator (const char *name) {
140 assert(f == NULL);
141 f = fopen(name, "rb");
142 if (f != NULL) {
143 ch = fgetc(f);
145 return f != NULL;
148 static void CFG_skip_space (void) {
149 while (feof(f) == 0 && isspace(ch)) {
150 ch = fgetc(f);
154 static void CFG_skip_line (void) {
155 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
156 ch = fgetc(f);
158 while (feof(f) == 0 && ch == '\n' && ch == '\r') {
159 ch = fgetc(f);
163 static int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen) {
164 assert(key != NULL);
165 assert(keylen > 0);
166 assert(value != NULL);
167 assert(valuelen > 0);
168 int i;
169 int found = 0;
170 while (feof(f) == 0 && found == 0) {
171 CFG_skip_space();
172 if (ch == ';') {
173 CFG_skip_line();
174 } else if (feof(f) == 0) {
175 found = 1;
176 i = 0;
177 while (feof(f) == 0 && isspace(ch) == 0 && ch != '=') {
178 if (i < keylen - 1) {
179 key[i] = ch;
180 i += 1;
182 ch = fgetc(f);
184 key[i] = 0;
185 CFG_skip_space();
186 if (feof(f) == 0 && ch == '=') {
187 ch = fgetc(f);
188 CFG_skip_space();
190 i = 0;
191 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
192 if (i < valuelen - 1) {
193 value[i] = ch;
194 i += 1;
196 ch = fgetc(f);
198 value[i] = 0;
199 CFG_skip_line();
202 return found;
205 static void CFG_close_iterator (void) {
206 assert(f != NULL);
207 fclose(f);
208 f = NULL;
211 /* --- reader --- */
213 static int CFG_read_config (const char *name) {
214 char key[64];
215 char value[64];
216 assert(name != NULL);
217 if (CFG_open_iterator(name)) {
218 while (CFG_scan_iterator(key, 64, value, 64)) {
219 CFG_update_key(key, value, cfg);
221 CFG_close_iterator();
222 return 1;
223 } else {
224 return 0;
228 void CFG_args (int argc, const char **argv) {
229 int i;
230 for (i = 1; i < argc; i++) {
231 if (argv[i][0] == '-' && argv[i][1] != 0) {
232 if (i + 1 >= argc) {
233 ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv[i]);
234 } else {
235 if (CFG_update_key(&argv[i][1], argv[i + 1], arg) != 0) {
236 ERR_failinit("CFG_args: unknown parameter %s\n", argv[i]);
238 i += 1;
240 } else {
241 ERR_failinit("CFG_args: something wrong here: %s\n", argv[i]);
246 void CFG_load (void) {
247 CFG_read_config("default.cfg");
248 CFG_read_config("doom2d.cfg");
251 /* --- writer --- */
253 static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
254 assert(f != NULL);
255 assert(key != NULL);
256 assert(value != NULL);
257 fwrite(key, strlen(key), 1, f);
258 fwrite("=", 1, 1, f);
259 fwrite(value, strlen(value), 1, f);
260 fwrite("\n", 1, 1, f);
263 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
264 assert(f != NULL);
265 assert(entry != NULL);
266 char buf[64];
267 const char *str;
268 const char *key = entry->cfg;
269 if (key != NULL) {
270 switch (entry->t) {
271 case BYTE:
272 snprintf(buf, 64, "%i", *(byte*)entry->p);
273 CFG_write_key_value(f, key, buf);
274 break;
275 case WORD:
276 snprintf(buf, 64, "%i", *(word*)entry->p);
277 CFG_write_key_value(f, key, buf);
278 break;
279 case DWORD:
280 snprintf(buf, 64, "%i", *(dword*)entry->p);
281 CFG_write_key_value(f, key, buf);
282 break;
283 case STRING:
284 CFG_write_key_value(f, key, entry->p);
285 break;
286 case SW_ON:
287 case SW_OFF:
288 str = *(byte*)entry->p ? "on" : "off";
289 CFG_write_key_value(f, key, str);
290 break;
291 case KEY:
292 str = I_key_to_string(*(int*)entry->p);
293 CFG_write_key_value(f, key, str);
294 break;
295 case FILES: return 1; // ignore
296 case NONE: return 0; // end
297 default: assert(0); // unknown type -> something broken
300 return entry->t == NONE ? 0 : 1;
303 static int CFG_update_config (const char *old, const char *new, const cfg_t *cfg, const char *msg) {
304 assert(old != NULL);
305 assert(new != NULL);
306 assert(cfg != NULL);
307 char key[64];
308 char value[64];
309 FILE *nf = fopen(new, "wb");
310 if (nf != NULL) {
311 if (msg != NULL) {
312 fwrite("; ", 2, 1, nf);
313 fwrite(msg, strlen(msg), 1, nf);
314 fwrite("\n", 1, 1, nf);
316 if (CFG_open_iterator(old)) {
317 while (CFG_scan_iterator(key, 64, value, 64)) {
318 if (CFG_find_entry(key, cfg) == NULL) {
319 CFG_write_key_value(nf, key, value);
322 CFG_close_iterator();
324 int i = 0;
325 while (CFG_write_entry(nf, &cfg[i])) {
326 i++;
328 fclose(nf);
330 return nf != NULL;
333 void CFG_save (void) {
334 CFG_update_config("doom2d.cfg", "doom2d.cfg", cfg, "generated by doom2d, do not modify");
335 //CFG_update_config("doom2d.cfg", "doom2d.tmp", cfg, "temporary file");
336 //CFG_update_config("doom2d.tmp", "doom2d.cfg", cfg, "generated by doom2d, do not modify");
337 //remove("doom2d.tmp");