DEADSOFTWARE

config: read config and args by renders
[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 assert(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 }
46 int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
47 const cfg_t *entry = CFG_find_entry(key, cfg);
48 if (entry != NULL) {
49 void *p = entry->p;
50 switch (entry->t) {
51 case Y_BYTE: *(byte*)p = atoi(value); break;
52 case Y_WORD: *(word*)p = atoi(value); break;
53 case Y_DWORD: *(dword*)p = atoi(value); break;
54 case Y_STRING: strcpy(p, value); break; // TODO fix this security problem
55 case Y_SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
56 case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 1 : 0; break;
57 case Y_FILES: F_addwad(value); break;
58 case Y_KEY: *(int*)p = I_string_to_key(value); break;
59 default: assert(0); // unknown type -> something broken
60 }
61 return 1;
62 } else {
63 return 0;
64 }
65 }
67 /* --- parser --- */
69 int CFG_open_iterator (const char *name) {
70 assert(f == NULL);
71 f = fopen(name, "rb");
72 if (f != NULL) {
73 ch = fgetc(f);
74 }
75 return f != NULL;
76 }
78 static void CFG_skip_space (void) {
79 while (feof(f) == 0 && isspace(ch)) {
80 ch = fgetc(f);
81 }
82 }
84 static void CFG_skip_line (void) {
85 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
86 ch = fgetc(f);
87 }
88 while (feof(f) == 0 && ch == '\n' && ch == '\r') {
89 ch = fgetc(f);
90 }
91 }
93 int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen) {
94 assert(key != NULL);
95 assert(keylen > 0);
96 assert(value != NULL);
97 assert(valuelen > 0);
98 int i;
99 int found = 0;
100 while (feof(f) == 0 && found == 0) {
101 CFG_skip_space();
102 if (ch == ';') {
103 CFG_skip_line();
104 } else if (feof(f) == 0) {
105 found = 1;
106 i = 0;
107 while (feof(f) == 0 && isspace(ch) == 0 && ch != '=') {
108 if (i < keylen - 1) {
109 key[i] = ch;
110 i += 1;
112 ch = fgetc(f);
114 key[i] = 0;
115 CFG_skip_space();
116 if (feof(f) == 0 && ch == '=') {
117 ch = fgetc(f);
118 CFG_skip_space();
120 i = 0;
121 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
122 if (i < valuelen - 1) {
123 value[i] = ch;
124 i += 1;
126 ch = fgetc(f);
128 value[i] = 0;
129 CFG_skip_line();
132 return found;
135 void CFG_close_iterator (void) {
136 assert(f != NULL);
137 fclose(f);
138 f = NULL;
141 /* --- reader --- */
143 int CFG_read_config (const char *name, int n, const cfg_t **cfg) {
144 assert(name != NULL);
145 assert(n >= 0);
146 assert(cfg != NULL);
147 int i;
148 char key[64];
149 char value[64];
150 assert(name != NULL);
151 if (CFG_open_iterator(name)) {
152 while (CFG_scan_iterator(key, 64, value, 64)) {
153 i = 0;
154 while (i < n && CFG_update_key(key, value, cfg[i]) == 0) {
155 i++;
158 CFG_close_iterator();
159 return 1;
160 } else {
161 return 0;
167 /* --- writer --- */
169 static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
170 assert(f != NULL);
171 assert(key != NULL);
172 assert(value != NULL);
173 fwrite(key, strlen(key), 1, f);
174 fwrite("=", 1, 1, f);
175 fwrite(value, strlen(value), 1, f);
176 fwrite("\n", 1, 1, f);
179 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
180 assert(f != NULL);
181 assert(entry != NULL);
182 char buf[16];
183 const char *str;
184 const char *key = entry->cfg;
185 if (key != NULL) {
186 switch (entry->t) {
187 case Y_BYTE:
188 snprintf(buf, 16, "%i", *(byte*)entry->p);
189 CFG_write_key_value(f, key, buf);
190 break;
191 case Y_WORD:
192 snprintf(buf, 16, "%i", *(word*)entry->p);
193 CFG_write_key_value(f, key, buf);
194 break;
195 case Y_DWORD:
196 snprintf(buf, 16, "%i", *(dword*)entry->p);
197 CFG_write_key_value(f, key, buf);
198 break;
199 case Y_STRING:
200 CFG_write_key_value(f, key, entry->p);
201 break;
202 case Y_SW_ON:
203 case Y_SW_OFF:
204 str = *(byte*)entry->p ? "on" : "off";
205 CFG_write_key_value(f, key, str);
206 break;
207 case Y_KEY:
208 str = I_key_to_string(*(int*)entry->p);
209 CFG_write_key_value(f, key, str);
210 break;
211 case Y_FILES: return 1; // ignore
212 case 0: return 0; // end
213 default: assert(0); // unknown type -> something broken
216 return entry->t == 0 ? 0 : 1;
219 int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg) {
220 assert(old != NULL);
221 assert(new != NULL);
222 assert(n >= 0);
223 assert(cfg != NULL);
224 int i, j;
225 char key[64];
226 char value[64];
227 FILE *nf = fopen(new, "wb");
228 if (nf != NULL) {
229 if (msg != NULL) {
230 fwrite("; ", 2, 1, nf);
231 fwrite(msg, strlen(msg), 1, nf);
232 fwrite("\n", 1, 1, nf);
234 if (CFG_open_iterator(old)) {
235 while (CFG_scan_iterator(key, 64, value, 64)) {
236 i = 0;
237 while (i < n && CFG_find_entry(key, cfg[i]) == NULL) {
238 i++;
240 if (i >= n) {
241 CFG_write_key_value(nf, key, value);
244 CFG_close_iterator();
246 for (j = 0; j < n; j++) {
247 i = 0;
248 while (CFG_write_entry(nf, &cfg[j][i])) {
249 i++;
252 fclose(nf);
254 return nf != NULL;