DEADSOFTWARE

config: move config loading to system driver
[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 ? 0 : 1; 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, const cfg_t *cfg) {
144 char key[64];
145 char value[64];
146 assert(name != NULL);
147 if (CFG_open_iterator(name)) {
148 while (CFG_scan_iterator(key, 64, value, 64)) {
149 CFG_update_key(key, value, cfg);
151 CFG_close_iterator();
152 return 1;
153 } else {
154 return 0;
160 /* --- writer --- */
162 static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
163 assert(f != NULL);
164 assert(key != NULL);
165 assert(value != NULL);
166 fwrite(key, strlen(key), 1, f);
167 fwrite("=", 1, 1, f);
168 fwrite(value, strlen(value), 1, f);
169 fwrite("\n", 1, 1, f);
172 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
173 assert(f != NULL);
174 assert(entry != NULL);
175 char buf[64];
176 const char *str;
177 const char *key = entry->cfg;
178 if (key != NULL) {
179 switch (entry->t) {
180 case Y_BYTE:
181 snprintf(buf, 64, "%i", *(byte*)entry->p);
182 CFG_write_key_value(f, key, buf);
183 break;
184 case Y_WORD:
185 snprintf(buf, 64, "%i", *(word*)entry->p);
186 CFG_write_key_value(f, key, buf);
187 break;
188 case Y_DWORD:
189 snprintf(buf, 64, "%i", *(dword*)entry->p);
190 CFG_write_key_value(f, key, buf);
191 break;
192 case Y_STRING:
193 CFG_write_key_value(f, key, entry->p);
194 break;
195 case Y_SW_ON:
196 case Y_SW_OFF:
197 str = *(byte*)entry->p ? "on" : "off";
198 CFG_write_key_value(f, key, str);
199 break;
200 case Y_KEY:
201 str = I_key_to_string(*(int*)entry->p);
202 CFG_write_key_value(f, key, str);
203 break;
204 case Y_FILES: return 1; // ignore
205 case 0: return 0; // end
206 default: assert(0); // unknown type -> something broken
209 return entry->t == 0 ? 0 : 1;
212 int CFG_update_config (const char *old, const char *new, const cfg_t *cfg, const char *msg) {
213 assert(old != NULL);
214 assert(new != NULL);
215 assert(cfg != NULL);
216 char key[64];
217 char value[64];
218 FILE *nf = fopen(new, "wb");
219 if (nf != NULL) {
220 if (msg != NULL) {
221 fwrite("; ", 2, 1, nf);
222 fwrite(msg, strlen(msg), 1, nf);
223 fwrite("\n", 1, 1, nf);
225 if (CFG_open_iterator(old)) {
226 while (CFG_scan_iterator(key, 64, value, 64)) {
227 if (CFG_find_entry(key, cfg) == NULL) {
228 CFG_write_key_value(nf, key, value);
231 CFG_close_iterator();
233 int i = 0;
234 while (CFG_write_entry(nf, &cfg[i])) {
235 i++;
237 fclose(nf);
239 return nf != NULL;