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
42 enum {NONE
, BYTE
, WORD
, DWORD
, STRING
, SW_ON
, SW_OFF
, FILES
, KEY
};
44 typedef struct cfg_t
{
56 static cfg_t cfg
[] = {
57 {"file", NULL
, NULL
, FILES
},
58 {"cheat", NULL
, &cheat
, SW_ON
},
59 {"vga", "screenshot", &shot_vga
, SW_ON
},
60 {"sndvol", "sound_volume", &snd_vol
, WORD
},
61 {"musvol", "music_volume", &mus_vol
, WORD
},
62 // {"fullscr", "fullscreen", &fullscreen, SW_ON},
63 // {"window", NULL, &fullscreen, SW_OFF},
64 {NULL
, "sky", &w_horiz
, SW_ON
},
65 {"mon", NULL
, &nomon
, SW_OFF
},
66 // {"gamma", "gamma", &gammaa, DWORD},
67 {"warp", NULL
, &_warp
, BYTE
},
68 {"width", "screen_width", &SCRW
, DWORD
},
69 {"height", "screen_height", &SCRH
, DWORD
},
70 {NULL
, "music_random", &music_random
, SW_ON
},
71 {NULL
, "music_time", &music_time
, DWORD
},
72 {NULL
, "music_fade", &music_fade
, DWORD
},
73 {NULL
, "pl1_left", &pl1
.kl
, KEY
},
74 {NULL
, "pl1_right",&pl1
.kr
, KEY
},
75 {NULL
, "pl1_up", &pl1
.ku
, KEY
},
76 {NULL
, "pl1_down", &pl1
.kd
, KEY
},
77 {NULL
, "pl1_jump", &pl1
.kj
, KEY
},
78 {NULL
, "pl1_fire", &pl1
.kf
, KEY
},
79 {NULL
, "pl1_next", &pl1
.kwr
, KEY
},
80 {NULL
, "pl1_prev", &pl1
.kwl
, KEY
},
81 {NULL
, "pl1_use", &pl1
.kp
, KEY
},
82 {NULL
, "pl2_left", &pl2
.kl
, KEY
},
83 {NULL
, "pl2_right",&pl2
.kr
, KEY
},
84 {NULL
, "pl2_up", &pl2
.ku
, KEY
},
85 {NULL
, "pl2_down", &pl2
.kd
, KEY
},
86 {NULL
, "pl2_jump", &pl2
.kj
, KEY
},
87 {NULL
, "pl2_fire", &pl2
.kf
, KEY
},
88 {NULL
, "pl2_next", &pl2
.kwr
, KEY
},
89 {NULL
, "pl2_prev", &pl2
.kwl
, KEY
},
90 {NULL
, "pl2_use", &pl2
.kp
, KEY
},
91 // {"config", NULL, cfg_file, STRING},
92 {NULL
, NULL
, NULL
, NONE
} // end
95 static int CFG_open_iterator (const char *name
) {
97 f
= fopen(name
, "rb");
104 static void CFG_skip_space (void) {
105 while (feof(f
) == 0 && isspace(ch
)) {
110 static void CFG_skip_line (void) {
111 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
114 while (feof(f
) == 0 && ch
== '\n' && ch
== '\r') {
119 static int CFG_scan_iterator (char *key
, int keylen
, char *value
, int valuelen
) {
122 assert(value
!= NULL
);
123 assert(valuelen
> 0);
126 while (feof(f
) == 0 && found
== 0) {
130 } else if (feof(f
) == 0) {
133 while (feof(f
) == 0 && isspace(ch
) == 0 && ch
!= '=') {
134 if (i
< keylen
- 1) {
142 if (feof(f
) == 0 && ch
== '=') {
147 while (feof(f
) == 0 && ch
!= '\n' && ch
!= '\r') {
148 if (i
< valuelen
- 1) {
161 static void CFG_close_iterator (void) {
167 static int CFG_update_key (const char *key
, const char *value
, int iscfg
) {
168 // logo("CFG_update_key: [%s] [%s] %i\n", key, value, iscfg);
170 while (cfg
[i
].t
!= NONE
&& ((iscfg
? cfg
[i
].cfg
: cfg
[i
].par
) == NULL
|| strcasecmp(key
, iscfg
? cfg
[i
].cfg
: cfg
[i
].par
) != 0)) {
174 case BYTE
: *(byte
*)cfg
[i
].p
= atoi(value
); break;
175 case WORD
: *(word
*)cfg
[i
].p
= atoi(value
); break;
176 case DWORD
: *(dword
*)cfg
[i
].p
= atoi(value
); break;
177 case STRING
: cfg
[i
].p
= strcpy(malloc(strlen(value
) + 1), value
); break;
178 case SW_ON
: *(byte
*)cfg
[i
].p
= strcasecmp(value
, "on") == 0 ? 1 : 0; break;
179 case SW_OFF
: *(byte
*)cfg
[i
].p
= strcasecmp(value
, "off") == 0 ? 0 : 1; break;
180 case FILES
: F_addwad(value
); break;
181 case KEY
: *(int*)cfg
[i
].p
= I_string_to_key(value
); break;
183 default: assert(0); // unknown type -> something broken
188 static int CFG_read_config (const char *name
) {
191 assert(name
!= NULL
);
192 if (CFG_open_iterator(name
)) {
193 while (CFG_scan_iterator(key
, 64, value
, 64)) {
194 CFG_update_key(key
, value
, 1);
196 CFG_close_iterator();
203 void CFG_args (int argc
, const char **argv
) {
205 for (i
= 1; i
< argc
; i
++) {
206 if (argv
[i
][0] == '-' && argv
[i
][1] != 0) {
208 ERR_failinit("CFG_args: not enough arguments for parameter %s\n", argv
[i
]);
210 if (CFG_update_key(&argv
[i
][1], argv
[i
+ 1], 0) != 0) {
211 ERR_failinit("CFG_args: unknown parameter %s\n", argv
[i
]);
216 ERR_failinit("CFG_args: something wrong here: %s\n", argv
[i
]);
221 void CFG_load (void) {
222 if (CFG_read_config("default.cfg") == 0) {
223 // TODO alt config at $HOME and system directories
225 CFG_read_config("user.cfg");
228 void CFG_save (void) {