DEADSOFTWARE

update copyrights
[flatwaifu.git] / src / config.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #include "glob.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <ctype.h>
22 #include "system.h"
23 #include "files.h"
24 #include "input.h"
26 static FILE *f;
27 static int ch;
29 const cfg_t *CFG_find_entry (const char *key, const cfg_t *cfg) {
30 assert(key != NULL);
31 if (cfg != NULL) {
32 int i = 0;
33 while (cfg[i].cfg && strcasecmp(cfg[i].cfg, key) != 0) {
34 i++;
35 }
36 return cfg[i].cfg ? &cfg[i] : NULL;
37 } else {
38 return NULL;
39 }
40 }
42 int CFG_update_key (const char *key, const char *value, const cfg_t *cfg) {
43 assert(key != NULL);
44 assert(value != NULL);
45 const cfg_t *entry = CFG_find_entry(key, cfg);
46 if (entry != NULL) {
47 void *p = entry->p;
48 switch (entry->t) {
49 case Y_BYTE: *(byte*)p = atoi(value); break;
50 case Y_WORD: *(word*)p = atoi(value); break;
51 case Y_DWORD: *(dword*)p = atoi(value); break;
52 case Y_STRING: strcpy(p, value); break; // TODO fix this security problem
53 case Y_SW_ON: *(byte*)p = strcasecmp(value, "on") == 0 ? 1 : 0; break;
54 case Y_SW_OFF: *(byte*)p = strcasecmp(value, "off") == 0 ? 1 : 0; break;
55 case Y_FILES: F_addwad(value); break;
56 case Y_KEY: *(int*)p = I_string_to_key(value); break;
57 default: assert(0); // unknown type -> something broken
58 }
59 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
60 return 1;
61 } else {
62 return 0;
63 }
64 }
66 /* --- parser --- */
68 int CFG_open_iterator (const char *name) {
69 assert(f == NULL);
70 f = fopen(name, "rb");
71 if (f != NULL) {
72 ch = fgetc(f);
73 }
74 return f != NULL;
75 }
77 static void CFG_skip_space (void) {
78 while (feof(f) == 0 && isspace(ch)) {
79 ch = fgetc(f);
80 }
81 }
83 static void CFG_skip_line (void) {
84 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
85 ch = fgetc(f);
86 }
87 while (feof(f) == 0 && ch == '\n' && ch == '\r') {
88 ch = fgetc(f);
89 }
90 }
92 int CFG_scan_iterator (char *key, int keylen, char *value, int valuelen) {
93 assert(key != NULL);
94 assert(keylen > 0);
95 assert(value != NULL);
96 assert(valuelen > 0);
97 int i;
98 int found = 0;
99 while (feof(f) == 0 && found == 0) {
100 CFG_skip_space();
101 if (ch == ';') {
102 CFG_skip_line();
103 } else if (feof(f) == 0) {
104 found = 1;
105 i = 0;
106 while (feof(f) == 0 && isspace(ch) == 0 && ch != '=') {
107 if (i < keylen - 1) {
108 key[i] = ch;
109 i += 1;
111 ch = fgetc(f);
113 key[i] = 0;
114 CFG_skip_space();
115 if (feof(f) == 0 && ch == '=') {
116 ch = fgetc(f);
117 CFG_skip_space();
119 i = 0;
120 while (feof(f) == 0 && ch != '\n' && ch != '\r') {
121 if (i < valuelen - 1) {
122 value[i] = ch;
123 i += 1;
125 ch = fgetc(f);
127 value[i] = 0;
128 CFG_skip_line();
131 return found;
134 void CFG_close_iterator (void) {
135 assert(f != NULL);
136 fclose(f);
137 f = NULL;
140 /* --- reader --- */
142 int CFG_read_config (const char *name, int n, const cfg_t **cfg) {
143 assert(name != NULL);
144 assert(n >= 0);
145 assert(cfg != NULL);
146 int i;
147 char key[64];
148 char value[64];
149 assert(name != NULL);
150 if (CFG_open_iterator(name)) {
151 while (CFG_scan_iterator(key, 64, value, 64)) {
152 i = 0;
153 while (i < n && CFG_update_key(key, value, cfg[i]) == 0) {
154 i++;
157 CFG_close_iterator();
158 return 1;
159 } else {
160 return 0;
166 /* --- writer --- */
168 static void CFG_write_key_value (FILE *f, const char *key, const char *value) {
169 assert(f != NULL);
170 assert(key != NULL);
171 assert(value != NULL);
172 fwrite(key, strlen(key), 1, f);
173 fwrite("=", 1, 1, f);
174 fwrite(value, strlen(value), 1, f);
175 fwrite("\n", 1, 1, f);
178 static int CFG_write_entry (FILE *f, const cfg_t *entry) {
179 assert(f != NULL);
180 assert(entry != NULL);
181 char buf[16];
182 const char *str;
183 const char *key = entry->cfg;
184 if (key != NULL) {
185 switch (entry->t) {
186 case Y_BYTE:
187 snprintf(buf, 16, "%i", *(byte*)entry->p);
188 CFG_write_key_value(f, key, buf);
189 break;
190 case Y_WORD:
191 snprintf(buf, 16, "%i", *(word*)entry->p);
192 CFG_write_key_value(f, key, buf);
193 break;
194 case Y_DWORD:
195 snprintf(buf, 16, "%i", *(dword*)entry->p);
196 CFG_write_key_value(f, key, buf);
197 break;
198 case Y_STRING:
199 CFG_write_key_value(f, key, entry->p);
200 break;
201 case Y_SW_ON:
202 case Y_SW_OFF:
203 str = *(byte*)entry->p ? "on" : "off";
204 CFG_write_key_value(f, key, str);
205 break;
206 case Y_KEY:
207 str = I_key_to_string(*(int*)entry->p);
208 CFG_write_key_value(f, key, str);
209 break;
210 case Y_FILES: return 1; // ignore
211 case 0: return 0; // end
212 default: assert(0); // unknown type -> something broken
215 return entry->t == 0 ? 0 : 1;
218 int CFG_update_config (const char *old, const char *new, int n, const cfg_t **cfg, const char *msg) {
219 assert(old != NULL);
220 assert(new != NULL);
221 assert(n >= 0);
222 assert(cfg != NULL);
223 int i, j;
224 char key[64];
225 char value[64];
226 FILE *nf = fopen(new, "wb");
227 if (nf != NULL) {
228 if (msg != NULL) {
229 fwrite("; ", 2, 1, nf);
230 fwrite(msg, strlen(msg), 1, nf);
231 fwrite("\n", 1, 1, nf);
233 if (CFG_open_iterator(old)) {
234 while (CFG_scan_iterator(key, 64, value, 64)) {
235 i = 0;
236 while (i < n && CFG_find_entry(key, cfg[i]) == NULL) {
237 i++;
239 if (i >= n) {
240 CFG_write_key_value(nf, key, value);
243 CFG_close_iterator();
245 for (j = 0; j < n; j++) {
246 if (cfg[j] != NULL) {
247 i = 0;
248 while (CFG_write_entry(nf, &cfg[j][i])) {
249 i++;
253 fclose(nf);
255 return nf != NULL;