1 /* Copyright (C) 2020 SovietPony
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.
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.
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/>.
30 const cfg_t
*CFG_find_entry (const char *key
, const cfg_t
*cfg
) {
34 while (cfg
[i
].cfg
&& cp866_strcasecmp(cfg
[i
].cfg
, key
) != 0) {
37 return cfg
[i
].cfg
? &cfg
[i
] : NULL
;
43 int CFG_update_key (const char *key
, const char *value
, const cfg_t
*cfg
) {
45 assert(value
!= NULL
);
46 const cfg_t
*entry
= CFG_find_entry(key
, cfg
);
50 case Y_BYTE
: *(byte
*)p
= atoi(value
); break;
51 case Y_WORD
: *(word
*)p
= atoi(value
); break;
52 case Y_DWORD
: *(dword
*)p
= atoi(value
); break;
53 case Y_STRING
: strcpy(p
, value
); break; // TODO fix this security problem
54 case Y_SW_ON
: *(byte
*)p
= cp866_strcasecmp(value
, "on") == 0 ? 1 : 0; break;
55 case Y_SW_OFF
: *(byte
*)p
= cp866_strcasecmp(value
, "off") == 0 ? 1 : 0; break;
56 case Y_FILES
: F_addwad(value
); break;
57 case Y_KEY
: *(int*)p
= I_string_to_key(value
); break;
58 default: assert(0); // unknown type -> something broken
60 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
69 int CFG_open_iterator (const char *name
) {
71 f
= fopen(name
, "rb");
78 static void CFG_skip_space (void) {
79 while (feof(f
) == 0 && isspace(ch
)) {
84 static void CFG_skip_line (void) {
85 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
88 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
93 int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
96 assert(value
!= NULL
);
100 while (feof(f
) == 0 && found
== 0) {
104 } else if (feof(f
) == 0) {
107 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
108 if (i
< keylen
- 1) {
116 if (feof(f
) == 0 && ch
== '=') {
121 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
122 if (i
< valuelen
- 1) {
135 void CFG_close_iterator (void) {
143 int CFG_read_config (const char *name
, int n
, const cfg_t
**cfg
) {
144 assert(name
!= NULL
);
150 assert(name
!= NULL
);
151 if (CFG_open_iterator(name
)) {
152 while (CFG_scan_iterator(key
, 64, value
, 64)) {
154 while (i
< n
&& CFG_update_key(key
, value
, cfg
[i
]) == 0) {
158 CFG_close_iterator();
169 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
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
) {
181 assert(entry
!= NULL
);
184 const char *key
= entry
->cfg
;
188 snprintf(buf
, 16, "%i", *(byte
*)entry
->p
);
189 CFG_write_key_value(f
, key
, buf
);
192 snprintf(buf
, 16, "%i", *(word
*)entry
->p
);
193 CFG_write_key_value(f
, key
, buf
);
196 snprintf(buf
, 16, "%i", *(dword
*)entry
->p
);
197 CFG_write_key_value(f
, key
, buf
);
200 CFG_write_key_value(f
, key
, entry
->p
);
204 str
= *(byte
*)entry
->p
? "on" : "off";
205 CFG_write_key_value(f
, key
, str
);
208 str
= I_key_to_string(*(int*)entry
->p
);
209 CFG_write_key_value(f
, key
, str
);
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
) {
227 FILE *nf
= fopen(new, "wb");
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)) {
237 while (i
< n
&& CFG_find_entry(key
, cfg
[i
]) == NULL
) {
241 CFG_write_key_value(nf
, key
, value
);
244 CFG_close_iterator();
246 for (j
= 0; j
< n
; j
++) {
247 if (cfg
[j
] != NULL
) {
249 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {