DEADSOFTWARE

menu: remove data field from menu_t
[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 "system.h"
30 #include "files.h"
31 #include "input.h"
33 static FILE *f;
34 static int ch;
36 const cfg_t *CFG_find_entry (const char *key, const cfg_t *cfg) {
37 assert(key != NULL);
38 if (cfg != NULL) {
39 int i = 0;
40 while (cfg[i].cfg && strcasecmp(cfg[i].cfg, key) != 0) {
41 i++;
42 }
43 return cfg[i].cfg ? &cfg[i] : NULL;
44 } else {
45 return NULL;
46 }
47 }
49 int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
50 assert(key != NULL);
51 assert(value != NULL);
52 const cfg_t *entry = CFG_find_entry(key, cfg);
53 if (entry != NULL) {
54 void *p = entry->p;
55 switch (entry->t) {
56 case Y_BYTE: *(byte*)p = atoi(value); break;
57 case Y_WORD: *(word*)p = atoi(value); break;
58 case Y_DWORD: *(dword*)p = atoi(value); break;
59 case Y_STRING: strcpy(p, value); break; // TODO fix this security problem
60 case Y_SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
61 case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 1 : 0; break;
62 case Y_FILES: F_addwad(value); break;
63 case Y_KEY: *(int*)p = I_string_to_key(value); break;
64 default: assert(0); // unknown type -> something broken
65 }
66 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
67 return 1;
68 } else {
69 return 0;
70 }
71 }
73 /* --- parser --- */
75 int CFG_open_iterator (const char *name) {
76 assert(f == NULL);
77 f = fopen(name, "rb");
78 if (f != NULL) {
79 ch = fgetc(f);
80 }
81 return f != NULL;
82 }
84 static void CFG_skip_space (void) {
85 while (feof(f) == 0 && isspace(ch)) {
86 ch = fgetc(f);
87 }
88 }
90 static void CFG_skip_line (void) {
91 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
92 ch = fgetc(f);
93 }
94 while (feof(f) == 0 && ch == '\n' && ch == '\r') {
95 ch = fgetc(f);
96 }
97 }
99 int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen) {
100 assert(key != NULL);
101 assert(keylen > 0);
102 assert(value != NULL);
103 assert(valuelen > 0);
104 int i;
105 int found = 0;
106 while (feof(f) == 0 && found == 0) {
107 CFG_skip_space();
108 if (ch == ';') {
109 CFG_skip_line();
110 } else if (feof(f) == 0) {
111 found = 1;
112 i = 0;
113 while (feof(f) == 0 && isspace(ch) == 0 && ch != '=') {
114 if (i < keylen - 1) {
115 key[i] = ch;
116 i += 1;
118 ch = fgetc(f);
120 key[i] = 0;
121 CFG_skip_space();
122 if (feof(f) == 0 && ch == '=') {
123 ch = fgetc(f);
124 CFG_skip_space();
126 i = 0;
127 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
128 if (i < valuelen - 1) {
129 value[i] = ch;
130 i += 1;
132 ch = fgetc(f);
134 value[i] = 0;
135 CFG_skip_line();
138 return found;
141 void CFG_close_iterator (void) {
142 assert(f != NULL);
143 fclose(f);
144 f = NULL;
147 /* --- reader --- */
149 int CFG_read_config (const char *name, int n, const cfg_t **cfg) {
150 assert(name != NULL);
151 assert(n >= 0);
152 assert(cfg != NULL);
153 int i;
154 char key[64];
155 char value[64];
156 assert(name != NULL);
157 if (CFG_open_iterator(name)) {
158 while (CFG_scan_iterator(key, 64, value, 64)) {
159 i = 0;
160 while (i < n && CFG_update_key(key, value, cfg[i]) == 0) {
161 i++;
164 CFG_close_iterator();
165 return 1;
166 } else {
167 return 0;
173 /* --- writer --- */
175 static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
176 assert(f != NULL);
177 assert(key != NULL);
178 assert(value != NULL);
179 fwrite(key, strlen(key), 1, f);
180 fwrite("=", 1, 1, f);
181 fwrite(value, strlen(value), 1, f);
182 fwrite("\n", 1, 1, f);
185 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
186 assert(f != NULL);
187 assert(entry != NULL);
188 char buf[16];
189 const char *str;
190 const char *key = entry->cfg;
191 if (key != NULL) {
192 switch (entry->t) {
193 case Y_BYTE:
194 snprintf(buf, 16, "%i", *(byte*)entry->p);
195 CFG_write_key_value(f, key, buf);
196 break;
197 case Y_WORD:
198 snprintf(buf, 16, "%i", *(word*)entry->p);
199 CFG_write_key_value(f, key, buf);
200 break;
201 case Y_DWORD:
202 snprintf(buf, 16, "%i", *(dword*)entry->p);
203 CFG_write_key_value(f, key, buf);
204 break;
205 case Y_STRING:
206 CFG_write_key_value(f, key, entry->p);
207 break;
208 case Y_SW_ON:
209 case Y_SW_OFF:
210 str = *(byte*)entry->p ? "on" : "off";
211 CFG_write_key_value(f, key, str);
212 break;
213 case Y_KEY:
214 str = I_key_to_string(*(int*)entry->p);
215 CFG_write_key_value(f, key, str);
216 break;
217 case Y_FILES: return 1; // ignore
218 case 0: return 0; // end
219 default: assert(0); // unknown type -> something broken
222 return entry->t == 0 ? 0 : 1;
225 int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg) {
226 assert(old != NULL);
227 assert(new != NULL);
228 assert(n >= 0);
229 assert(cfg != NULL);
230 int i, j;
231 char key[64];
232 char value[64];
233 FILE *nf = fopen(new, "wb");
234 if (nf != NULL) {
235 if (msg != NULL) {
236 fwrite("; ", 2, 1, nf);
237 fwrite(msg, strlen(msg), 1, nf);
238 fwrite("\n", 1, 1, nf);
240 if (CFG_open_iterator(old)) {
241 while (CFG_scan_iterator(key, 64, value, 64)) {
242 i = 0;
243 while (i < n && CFG_find_entry(key, cfg[i]) == NULL) {
244 i++;
246 if (i >= n) {
247 CFG_write_key_value(nf, key, value);
250 CFG_close_iterator();
252 for (j = 0; j < n; j++) {
253 if (cfg[j] != NULL) {
254 i = 0;
255 while (CFG_write_entry(nf, &cfg[j][i])) {
256 i++;
260 fclose(nf);
262 return nf != NULL;