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
;
46 int CFG_update_key (const char *key
, const char *value
, const cfg_t
*cfg
) {
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
= strcasecmp(value
, "on") == 0 ? 1 : 0; break;
56 case Y_SW_OFF
: *(byte
*)p
= 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
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
++) {
248 while (CFG_write_entry(nf
, &cfg
[j
][i
])) {