2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 const cfg_t
*CFG_find_entry (const char *key
, const cfg_t
*cfg
) {
40 while (cfg
[i
].cfg
&& strcasecmp(cfg
[i
].cfg
, key
) != 0) {
43 return cfg
[i
].cfg
? &cfg
[i
] : NULL
;
49 int CFG_update_key (const char *key
, const char *value
, const cfg_t
*cfg
) {
51 assert(value
!= NULL
);
52 const cfg_t
*entry
= CFG_find_entry(key
, cfg
);
56 case Y_BYTE
: *(byte
*)p
= atoi(value
); break;
57 case Y_WORD
: *(word
*)p
= atoi(value
); break;
58 case Y_DWORD
: *(dword
*)p
= atoi(value
); break;
59 case Y_STRING
: strcpy(p
, value
); break; // TODO fix this security problem
60 case Y_SW_ON
: *(byte
*)p
= strcasecmp(value
, "on") == 0 ? 1 : 0; break;
61 case Y_SW_OFF
: *(byte
*)p
= strcasecmp(value
, "off") == 0 ? 1 : 0; break;
62 case Y_FILES
: F_addwad(value
); break;
63 case Y_KEY
: *(int*)p
= I_string_to_key(value
); break;
64 default: assert(0); // unknown type -> something broken
66 //logo("CFG_update_key: [%s] = [%s]\n", key, value);
75 int CFG_open_iterator (const char *name
) {
77 f
= fopen(name
, "rb");
84 static void CFG_skip_space (void) {
85 while (feof(f
) == 0 && isspace(ch
)) {
90 static void CFG_skip_line (void) {
91 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
94 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
99 int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
102 assert(value
!= NULL
);
103 assert(valuelen
> 0);
106 while (feof(f
) == 0 && found
== 0) {
110 } else if (feof(f
) == 0) {
113 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
114 if (i
< keylen
- 1) {
122 if (feof(f
) == 0 && ch
== '=') {
127 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
128 if (i
< valuelen
- 1) {
141 void CFG_close_iterator (void) {
149 int CFG_read_config (const char *name
, int n
, const cfg_t
**cfg
) {
150 assert(name
!= NULL
);
156 assert(name
!= NULL
);
157 if (CFG_open_iterator(name
)) {
158 while (CFG_scan_iterator(key
, 64, value
, 64)) {
160 while (i
< n
&& CFG_update_key(key
, value
, cfg
[i
]) == 0) {
164 CFG_close_iterator();
175 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
178 assert(value
!= NULL
);
179 fwrite(key
, strlen(key
), 1, f
);
180 fwrite("=", 1, 1, f
);
181 fwrite(value
, strlen(value
), 1, f
);
182 fwrite("\n", 1, 1, f
);
185 static int CFG_write_entry (FILE *f
, const cfg_t
*entry
) {
187 assert(entry
!= NULL
);
190 const char *key
= entry
->cfg
;
194 snprintf(buf
, 16, "%i", *(byte
*)entry
->p
);
195 CFG_write_key_value(f
, key
, buf
);
198 snprintf(buf
, 16, "%i", *(word
*)entry
->p
);
199 CFG_write_key_value(f
, key
, buf
);
202 snprintf(buf
, 16, "%i", *(dword
*)entry
->p
);
203 CFG_write_key_value(f
, key
, buf
);
206 CFG_write_key_value(f
, key
, entry
->p
);
210 str
= *(byte
*)entry
->p
? "on" : "off";
211 CFG_write_key_value(f
, key
, str
);
214 str
= I_key_to_string(*(int*)entry
->p
);
215 CFG_write_key_value(f
, key
, str
);
217 case Y_FILES
: return 1; // ignore
218 case 0: return 0; // end
219 default: assert(0); // unknown type -> something broken
222 return entry
->t
== 0 ? 0 : 1;
225 int CFG_update_config (const char *old
, const char *new, int n
, const cfg_t
**cfg
, const char *msg
) {
233 FILE *nf
= fopen(new, "wb");
236 fwrite("; ", 2, 1, nf
);
237 fwrite(msg
, strlen(msg
), 1, nf
);
238 fwrite("\n", 1, 1, nf
);
240 if (CFG_open_iterator(old
)) {
241 while (CFG_scan_iterator(key
, 64, value
, 64)) {
243 while (i
< n
&& CFG_find_entry(key
, cfg
[i
]) == NULL
) {
247 CFG_write_key_value(nf
, key
, value
);
250 CFG_close_iterator();
252 for (j
= 0; j
< n
; j
++) {
253 if (cfg
[j
] != NULL
) {
255 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {