885a0e4b8aeee0b998c6d0e270c114b38939235a
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 ? 0 : 1; 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
, const cfg_t
*cfg
) {
146 assert(name
!= NULL
);
147 if (CFG_open_iterator(name
)) {
148 while (CFG_scan_iterator(key
, 64, value
, 64)) {
149 CFG_update_key(key
, value
, cfg
);
151 CFG_close_iterator();
162 static void CFG_write_key_value (FILE *f
, const char *key
, const char *value
) {
165 assert(value
!= NULL
);
166 fwrite(key
, strlen(key
), 1, f
);
167 fwrite("=", 1, 1, f
);
168 fwrite(value
, strlen(value
), 1, f
);
169 fwrite("\n", 1, 1, f
);
172 static int CFG_write_entry (FILE *f
, const cfg_t
*entry
) {
174 assert(entry
!= NULL
);
177 const char *key
= entry
->cfg
;
181 snprintf(buf
, 64, "%i", *(byte
*)entry
->p
);
182 CFG_write_key_value(f
, key
, buf
);
185 snprintf(buf
, 64, "%i", *(word
*)entry
->p
);
186 CFG_write_key_value(f
, key
, buf
);
189 snprintf(buf
, 64, "%i", *(dword
*)entry
->p
);
190 CFG_write_key_value(f
, key
, buf
);
193 CFG_write_key_value(f
, key
, entry
->p
);
197 str
= *(byte
*)entry
->p
? "on" : "off";
198 CFG_write_key_value(f
, key
, str
);
201 str
= I_key_to_string(*(int*)entry
->p
);
202 CFG_write_key_value(f
, key
, str
);
204 case Y_FILES
: return 1; // ignore
205 case 0: return 0; // end
206 default: assert(0); // unknown type -> something broken
209 return entry
->t
== 0 ? 0 : 1;
212 int CFG_update_config (const char *old
, const char *new, const cfg_t
*cfg
, const char *msg
) {
218 FILE *nf
= fopen(new, "wb");
221 fwrite("; ", 2, 1, nf
);
222 fwrite(msg
, strlen(msg
), 1, nf
);
223 fwrite("\n", 1, 1, nf
);
225 if (CFG_open_iterator(old
)) {
226 while (CFG_scan_iterator(key
, 64, value
, 64)) {
227 if (CFG_find_entry(key
, cfg
) == NULL
) {
228 CFG_write_key_value(nf
, key
, value
);
231 CFG_close_iterator();
234 while (CFG_write_entry(nf
, &cfg
[i
])) {