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/>.
26 #include "common/cp866.h"
31 const cfg_t
*CFG_find_entry (const char *key
, const cfg_t
*cfg
) {
35 while (cfg
[i
].cfg
&& cp866_strcasecmp(cfg
[i
].cfg
, key
) != 0) {
38 return cfg
[i
].cfg
? &cfg
[i
] : NULL
;
44 int CFG_update_key (const char *key
, const char *value
, const cfg_t
*cfg
) {
46 assert(value
!= NULL
);
47 const cfg_t
*entry
= CFG_find_entry(key
, cfg
);
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
= cp866_strcasecmp(value
, "on") == 0 ? 1 : 0; break;
56 case Y_SW_OFF
: *(byte
*)p
= cp866_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
61 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
70 int CFG_open_iterator (const char *name
) {
72 f
= fopen(name
, "rb");
79 static void CFG_skip_space (void) {
80 while (feof(f
) == 0 && isspace(ch
)) {
85 static void CFG_skip_line (void) {
86 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
89 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
94 int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
97 assert(value
!= NULL
);
101 while (feof(f
) == 0 && found
== 0) {
105 } else if (feof(f
) == 0) {
108 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
109 if (i
< keylen
- 1) {
117 if (feof(f
) == 0 && ch
== '=') {
122 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
123 if (i
< valuelen
- 1) {
136 void CFG_close_iterator (void) {
144 int CFG_read_config (const char *name
, int n
, const cfg_t
**cfg
) {
145 assert(name
!= NULL
);
151 assert(name
!= NULL
);
152 if (CFG_open_iterator(name
)) {
153 while (CFG_scan_iterator(key
, 64, value
, 64)) {
155 while (i
< n
&& CFG_update_key(key
, value
, cfg
[i
]) == 0) {
159 CFG_close_iterator();
170 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
173 assert(value
!= NULL
);
174 fwrite(key
, strlen(key
), 1, f
);
175 fwrite("=", 1, 1, f
);
176 fwrite(value
, strlen(value
), 1, f
);
177 fwrite("\n", 1, 1, f
);
180 static int CFG_write_entry (FILE *f
, const cfg_t
*entry
) {
182 assert(entry
!= NULL
);
185 const char *key
= entry
->cfg
;
189 snprintf(buf
, 16, "%i", *(byte
*)entry
->p
);
190 CFG_write_key_value(f
, key
, buf
);
193 snprintf(buf
, 16, "%i", *(word
*)entry
->p
);
194 CFG_write_key_value(f
, key
, buf
);
197 snprintf(buf
, 16, "%i", *(dword
*)entry
->p
);
198 CFG_write_key_value(f
, key
, buf
);
201 CFG_write_key_value(f
, key
, entry
->p
);
205 str
= *(byte
*)entry
->p
? "on" : "off";
206 CFG_write_key_value(f
, key
, str
);
209 str
= I_key_to_string(*(int*)entry
->p
);
210 CFG_write_key_value(f
, key
, str
);
212 case Y_FILES
: return 1; // ignore
213 case 0: return 0; // end
214 default: assert(0); // unknown type -> something broken
217 return entry
->t
== 0 ? 0 : 1;
220 int CFG_update_config (const char *old
, const char *new, int n
, const cfg_t
**cfg
, const char *msg
) {
228 FILE *nf
= fopen(new, "wb");
231 fwrite("; ", 2, 1, nf
);
232 fwrite(msg
, strlen(msg
), 1, nf
);
233 fwrite("\n", 1, 1, nf
);
235 if (CFG_open_iterator(old
)) {
236 while (CFG_scan_iterator(key
, 64, value
, 64)) {
238 while (i
< n
&& CFG_find_entry(key
, cfg
[i
]) == NULL
) {
242 CFG_write_key_value(nf
, key
, value
);
245 CFG_close_iterator();
247 for (j
= 0; j
< n
; j
++) {
248 if (cfg
[j
] != NULL
) {
250 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {