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
) {
47 assert(value
!= NULL
);
48 entry
= CFG_find_entry(key
, cfg
);
52 case Y_BYTE
: *(byte
*)p
= atoi(value
); break;
53 case Y_WORD
: *(word
*)p
= atoi(value
); break;
54 case Y_DWORD
: *(dword
*)p
= atoi(value
); break;
55 case Y_STRING
: strcpy(p
, value
); break; // TODO fix this security problem
56 case Y_SW_ON
: *(byte
*)p
= cp866_strcasecmp(value
, "on") == 0 ? 1 : 0; break;
57 case Y_SW_OFF
: *(byte
*)p
= cp866_strcasecmp(value
, "off") == 0 ? 1 : 0; break;
58 case Y_FILES
: F_addwad(value
); break;
59 case Y_KEY
: *(int*)p
= I_string_to_key(value
); break;
60 default: assert(0); // unknown type -> something broken
62 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
71 int CFG_open_iterator (const char *name
) {
73 f
= fopen(name
, "rb");
80 static void CFG_skip_space (void) {
81 while (feof(f
) == 0 && isspace(ch
)) {
86 static void CFG_skip_line (void) {
87 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
90 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
95 int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
100 assert(value
!= NULL
);
101 assert(valuelen
> 0);
102 while (feof(f
) == 0 && found
== 0) {
106 } else if (feof(f
) == 0) {
109 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
110 if (i
< keylen
- 1) {
118 if (feof(f
) == 0 && ch
== '=') {
123 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
124 if (i
< valuelen
- 1) {
137 void CFG_close_iterator (void) {
145 int CFG_read_config (const char *name
, int n
, const cfg_t
**cfg
) {
149 assert(name
!= NULL
);
152 assert(name
!= NULL
);
153 if (CFG_open_iterator(name
)) {
154 while (CFG_scan_iterator(key
, 64, value
, 64)) {
156 while (i
< n
&& CFG_update_key(key
, value
, cfg
[i
]) == 0) {
160 CFG_close_iterator();
171 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
174 assert(value
!= NULL
);
175 fwrite(key
, strlen(key
), 1, f
);
176 fwrite("=", 1, 1, f
);
177 fwrite(value
, strlen(value
), 1, f
);
178 fwrite("\n", 1, 1, f
);
181 static int CFG_write_entry (FILE *f
, const cfg_t
*entry
) {
186 assert(entry
!= NULL
);
191 snprintf(buf
, 16, "%i", *(byte
*)entry
->p
);
192 CFG_write_key_value(f
, key
, buf
);
195 snprintf(buf
, 16, "%i", *(word
*)entry
->p
);
196 CFG_write_key_value(f
, key
, buf
);
199 snprintf(buf
, 16, "%i", *(dword
*)entry
->p
);
200 CFG_write_key_value(f
, key
, buf
);
203 CFG_write_key_value(f
, key
, entry
->p
);
207 str
= *(byte
*)entry
->p
? "on" : "off";
208 CFG_write_key_value(f
, key
, str
);
211 str
= I_key_to_string(*(int*)entry
->p
);
212 CFG_write_key_value(f
, key
, str
);
214 case Y_FILES
: return 1; // ignore
215 case 0: return 0; // end
216 default: assert(0); // unknown type -> something broken
219 return entry
->t
== 0 ? 0 : 1;
222 int CFG_update_config (const char *old
, const char *new, int n
, const cfg_t
**cfg
, const char *msg
) {
231 nf
= fopen(new, "wb");
234 fwrite("; ", 2, 1, nf
);
235 fwrite(msg
, strlen(msg
), 1, nf
);
236 fwrite("\n", 1, 1, nf
);
238 if (CFG_open_iterator(old
)) {
239 while (CFG_scan_iterator(key
, 64, value
, 64)) {
241 while (i
< n
&& CFG_find_entry(key
, cfg
[i
]) == NULL
) {
245 CFG_write_key_value(nf
, key
, value
);
248 CFG_close_iterator();
250 for (j
= 0; j
< n
; j
++) {
251 if (cfg
[j
] != NULL
) {
253 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {