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/>.
29 const cfg_t
*CFG_find_entry (const char *key
, const cfg_t
*cfg
) {
33 while (cfg
[i
].cfg
&& strcasecmp(cfg
[i
].cfg
, key
) != 0) {
36 return cfg
[i
].cfg
? &cfg
[i
] : NULL
;
42 int CFG_update_key (const char *key
, const char *value
, const cfg_t
*cfg
) {
44 assert(value
!= NULL
);
45 const cfg_t
*entry
= CFG_find_entry(key
, cfg
);
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
59 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
68 int CFG_open_iterator (const char *name
) {
70 f
= fopen(name
, "rb");
77 static void CFG_skip_space (void) {
78 while (feof(f
) == 0 && isspace(ch
)) {
83 static void CFG_skip_line (void) {
84 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
87 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
92 int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
95 assert(value
!= NULL
);
99 while (feof(f
) == 0 && found
== 0) {
103 } else if (feof(f
) == 0) {
106 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
107 if (i
< keylen
- 1) {
115 if (feof(f
) == 0 && ch
== '=') {
120 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
121 if (i
< valuelen
- 1) {
134 void CFG_close_iterator (void) {
142 int CFG_read_config (const char *name
, int n
, const cfg_t
**cfg
) {
143 assert(name
!= NULL
);
149 assert(name
!= NULL
);
150 if (CFG_open_iterator(name
)) {
151 while (CFG_scan_iterator(key
, 64, value
, 64)) {
153 while (i
< n
&& CFG_update_key(key
, value
, cfg
[i
]) == 0) {
157 CFG_close_iterator();
168 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
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
) {
180 assert(entry
!= NULL
);
183 const char *key
= entry
->cfg
;
187 snprintf(buf
, 16, "%i", *(byte
*)entry
->p
);
188 CFG_write_key_value(f
, key
, buf
);
191 snprintf(buf
, 16, "%i", *(word
*)entry
->p
);
192 CFG_write_key_value(f
, key
, buf
);
195 snprintf(buf
, 16, "%i", *(dword
*)entry
->p
);
196 CFG_write_key_value(f
, key
, buf
);
199 CFG_write_key_value(f
, key
, entry
->p
);
203 str
= *(byte
*)entry
->p
? "on" : "off";
204 CFG_write_key_value(f
, key
, str
);
207 str
= I_key_to_string(*(int*)entry
->p
);
208 CFG_write_key_value(f
, key
, str
);
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
) {
226 FILE *nf
= fopen(new, "wb");
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)) {
236 while (i
< n
&& CFG_find_entry(key
, cfg
[i
]) == NULL
) {
240 CFG_write_key_value(nf
, key
, value
);
243 CFG_close_iterator();
245 for (j
= 0; j
< n
; j
++) {
246 if (cfg
[j
] != NULL
) {
248 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {