DEADSOFTWARE

config: rewrite config parser
[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 char *par, *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 cfg_t cfg[] = {
57 {"file", NULL, NULL, FILES},
58 {"cheat", NULL, &cheat, SW_ON},
59 {"vga", "screenshot", &shot_vga, SW_ON},
60 {"sndvol", "sound_volume", &snd_vol, WORD},
61 {"musvol", "music_volume", &mus_vol, WORD},
62 // {"fullscr", "fullscreen", &fullscreen, SW_ON},
63 // {"window", NULL, &fullscreen, SW_OFF},
64 {NULL, "sky", &w_horiz, SW_ON},
65 {"mon", NULL, &nomon, SW_OFF},
66 // {"gamma", "gamma", &gammaa, DWORD},
67 {"warp", NULL, &_warp, BYTE},
68 {"width", "screen_width", &SCRW, DWORD},
69 {"height", "screen_height", &SCRH, DWORD},
70 {NULL, "music_random", &music_random, SW_ON},
71 {NULL, "music_time", &music_time, DWORD},
72 {NULL, "music_fade", &music_fade, DWORD},
73 {NULL, "pl1_left", &pl1.kl, KEY},
74 {NULL, "pl1_right",&pl1.kr, KEY},
75 {NULL, "pl1_up", &pl1.ku, KEY},
76 {NULL, "pl1_down", &pl1.kd, KEY},
77 {NULL, "pl1_jump", &pl1.kj, KEY},
78 {NULL, "pl1_fire", &pl1.kf, KEY},
79 {NULL, "pl1_next", &pl1.kwr, KEY},
80 {NULL, "pl1_prev", &pl1.kwl, KEY},
81 {NULL, "pl1_use", &pl1.kp, KEY},
82 {NULL, "pl2_left", &pl2.kl, KEY},
83 {NULL, "pl2_right",&pl2.kr, KEY},
84 {NULL, "pl2_up", &pl2.ku, KEY},
85 {NULL, "pl2_down", &pl2.kd, KEY},
86 {NULL, "pl2_jump", &pl2.kj, KEY},
87 {NULL, "pl2_fire", &pl2.kf, KEY},
88 {NULL, "pl2_next", &pl2.kwr, KEY},
89 {NULL, "pl2_prev", &pl2.kwl, KEY},
90 {NULL, "pl2_use", &pl2.kp, KEY},
91 // {"config", NULL, cfg_file, STRING},
92 {NULL, NULL, NULL, NONE} // end
93 };
95 static int CFG_open_iterator (const char *name) {
96 assert(f == NULL);
97 f = fopen(name, "rb");
98 if (f != NULL) {
99 ch = fgetc(f);
101 return f != NULL;
104 static void CFG_skip_space (void) {
105 while (feof(f) == 0 && isspace(ch)) {
106 ch = fgetc(f);
110 static void CFG_skip_line (void) {
111 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
112 ch = fgetc(f);
114 while (feof(f) == 0 && ch == '\n' && ch == '\r') {
115 ch = fgetc(f);
119 static int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen) {
120 assert(key != NULL);
121 assert(keylen > 0);
122 assert(value != NULL);
123 assert(valuelen > 0);
124 int i;
125 int found = 0;
126 while (feof(f) == 0 && found == 0) {
127 CFG_skip_space();
128 if (ch == ';') {
129 CFG_skip_line();
130 } else if (feof(f) == 0) {
131 found = 1;
132 i = 0;
133 while (feof(f) == 0 && isspace(ch) == 0 && ch != '=') {
134 if (i < keylen - 1) {
135 key[i] = ch;
136 i += 1;
138 ch = fgetc(f);
140 key[i] = 0;
141 CFG_skip_space();
142 if (feof(f) == 0 && ch == '=') {
143 ch = fgetc(f);
144 CFG_skip_space();
146 i = 0;
147 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
148 if (i < valuelen - 1) {
149 value[i] = ch;
150 i += 1;
152 ch = fgetc(f);
154 value[i] = 0;
155 CFG_skip_line();
158 return found;
161 static void CFG_close_iterator (void) {
162 assert(f != NULL);
163 fclose(f);
164 f = NULL;
167 static int CFG_update_key (const char *key, const char *value, int iscfg) {
168 // logo("CFG_update_key: [%s] [%s] %i\n", key, value, iscfg);
169 int i = 0;
170 while (cfg[i].t != NONE && ((iscfg ? cfg[i].cfg : cfg[i].par) == NULL || strcasecmp(key, iscfg ? cfg[i].cfg : cfg[i].par) != 0)) {
171 i += 1;
173 switch (cfg[i].t) {
174 case BYTE: *(byte*)cfg[i].p = atoi(value); break;
175 case WORD: *(word*)cfg[i].p = atoi(value); break;
176 case DWORD: *(dword*)cfg[i].p = atoi(value); break;
177 case STRING: cfg[i].p = strcpy(malloc(strlen(value) + 1), value); break;
178 case SW_ON: *(byte*)cfg[i].p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
179 case SW_OFF: *(byte*)cfg[i].p = strcasecmp(value, "off") == 0 ? 0 : 1; break;
180 case FILES: F_addwad(value); break;
181 case KEY: *(int*)cfg[i].p = I_string_to_key(value); break;
182 case NONE: return 0;
183 default: assert(0); // unknown type -> something broken
185 return 1;
188 static int CFG_read_config (const char *name) {
189 char key[64];
190 char value[64];
191 assert(name != NULL);
192 if (CFG_open_iterator(name)) {
193 while (CFG_scan_iterator(key, 64, value, 64)) {
194 CFG_update_key(key, value, 1);
196 CFG_close_iterator();
197 return 1;
198 } else {
199 return 0;
203 void CFG_args (int argc, const char **argv) {
204 int i;
205 for (i = 1; i < argc; i++) {
206 if (argv[i][0] == '-' && argv[i][1] != 0) {
207 if (i + 1 >= argc) {
208 ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv[i]);
209 } else {
210 if (CFG_update_key(&argv[i][1], argv[i + 1], 0) != 0) {
211 ERR_failinit("CFG_args: unknown parameter %s\n", argv[i]);
213 i += 1;
215 } else {
216 ERR_failinit("CFG_args: something wrong here: %s\n", argv[i]);
221 void CFG_load (void) {
222 if (CFG_read_config("default.cfg") == 0) {
223 // TODO alt config at $HOME and system directories
225 CFG_read_config("user.cfg");
228 void CFG_save (void) {
229 // TODO