DEADSOFTWARE

Expose `fexists` function
[flatwaifu.git] / src / config.c
1 /*
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
21 */
23 #include "glob.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 //#include <process.h>
28 //#include <dos.h>
29 #include "config.h"
30 #include "vga.h"
31 #include "error.h"
32 #include "sound.h"
33 #include "keyb.h"
34 #include "files.h"
35 #include "memory.h"
36 #include "view.h"
37 #include "player.h"
38 #include "my.h"
40 #include <SDL_keyboard.h>
42 extern byte _warp,fastdraw,nomon;
43 extern int mem_chk_sz;
45 enum{NONE,BYTE,WORD,DWORD,STRING,SW_ON,SW_OFF,FILES,KEY};
47 typedef struct{
48 char *par,*cfg;
49 void *p;
50 byte t,o;
51 }cfg_t;
54 byte cheat=0;
56 byte shot_vga=0;
59 char cd_path[128]="";
61 static cfg_t cfg[]={
62 {"file",NULL,NULL,FILES,0},
63 {"cheat",NULL,&cheat,SW_ON,0},
64 {"vga","screenshot",&shot_vga,SW_ON,0},
65 {"sndvol","sound_volume",&snd_vol,WORD,0},
66 {"musvol","music_volume",&mus_vol,WORD,0},
67 {"fullscr","fullscreen",&fullscreen,SW_ON,0},
68 {"window",NULL,&fullscreen,SW_OFF,0},
69 {NULL,"sky",&w_horiz,SW_ON,0},
70 {"mon",NULL,&nomon,SW_OFF,0},
71 {"gamma","gamma",&gammaa,DWORD,0},
72 {"warp",NULL,&_warp,BYTE,0},
73 {"width","screen_width",&SCRW,DWORD,0},
74 {"height","screen_height",&SCRH,DWORD,0},
75 {NULL,"music_random",&music_random,SW_ON,0},
76 {NULL,"music_time",&music_time,DWORD,0},
77 {NULL,"music_fade",&music_fade,DWORD,0},
78 {NULL,"pl1_left", &pl1.kl,KEY,0},
79 {NULL,"pl1_right",&pl1.kr,KEY,0},
80 {NULL,"pl1_up", &pl1.ku,KEY,0},
81 {NULL,"pl1_down", &pl1.kd,KEY,0},
82 {NULL,"pl1_jump", &pl1.kj,KEY,0},
83 {NULL,"pl1_fire", &pl1.kf,KEY,0},
84 {NULL,"pl1_next", &pl1.kwr,KEY,0},
85 {NULL,"pl1_prev", &pl1.kwl,KEY,0},
86 {NULL,"pl1_use", &pl1.kp,KEY,0},
87 {NULL,"pl2_left", &pl2.kl,KEY,0},
88 {NULL,"pl2_right",&pl2.kr,KEY,0},
89 {NULL,"pl2_up", &pl2.ku,KEY,0},
90 {NULL,"pl2_down", &pl2.kd,KEY,0},
91 {NULL,"pl2_jump", &pl2.kj,KEY,0},
92 {NULL,"pl2_fire", &pl2.kf,KEY,0},
93 {NULL,"pl2_next", &pl2.kwr,KEY,0},
94 {NULL,"pl2_prev", &pl2.kwl,KEY,0},
95 {NULL,"pl2_use", &pl2.kp,KEY,0},
96 {"config",NULL,cfg_file,STRING,0},
97 {NULL,NULL,NONE,0}
98 };
101 char cfg_file[128]="default.cfg";
103 static char buf[256];
105 void CFG_args(int argc, char *argv[]) {
106 int j;
107 dword n;
108 char *s;
110 logo("CFG_args: проверка командной строки\n");
112 int i;
113 char *pbuf = buf;
114 for (i=1;i<argc;i++){
115 strcpy(pbuf,argv[i]);
116 pbuf+=strlen(argv[i]);
117 strcpy(pbuf," ");
118 pbuf++;
121 for(s=strtok(buf," \r\n\t");s;s=strtok(NULL," \r\n\t")) {//for(s=strtok(getcmd(buf)," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
122 next:
123 if(*s=='/' || *s=='-') ++s;
124 for(j=0;cfg[j].t;++j) if(cfg[j].par) if(strcasecmp(s,cfg[j].par)==0) {
125 switch(cfg[j].t) {
126 case BYTE:
127 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
128 *((byte *)cfg[j].p)=(byte)n;
129 break;
130 case WORD:
131 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
132 *((word *)cfg[j].p)=(word)n;
133 break;
134 case DWORD:
135 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
136 *((dword *)cfg[j].p)=n;
137 break;
138 case STRING:
139 strcpy((char *)cfg[j].p,s=strtok(NULL," \r\n\t"));
140 break;
141 case SW_ON:
142 *((byte *)cfg[j].p)=ON;
143 if(cfg[j+1].t==SW_OFF && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
144 if(j>0) if(cfg[j-1].t==SW_OFF && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
145 break;
146 case SW_OFF:
147 *((byte *)cfg[j].p)=OFF;
148 if(cfg[j+1].t==SW_ON && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
149 if(j>0) if(cfg[j-1].t==SW_ON && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
150 break;
151 case FILES:
152 for(s=strtok(NULL," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
153 if(*s=='/' || *s=='-') goto next;
154 #ifdef DEMO
155 logo(" %s НЕ подключен!\n",s);
156 #else
157 F_addwad(s);
158 #endif
159 }break;
160 default:
161 ERR_failinit("!!! Неизвестный тип в cfg !!!");
163 cfg[j].o=1;break;
168 int get_key(char *name)
170 int i;
171 for(i=1; i<SDLK_LAST; i++) {
172 char* s = SDL_GetKeyName(i);
173 if (s && strcasecmp(name,s) == 0) {
175 return i;
178 return 0;
181 void CFG_load(void) {
182 int j;
183 FILE *h;
184 dword n;
185 char s[128];
186 char *p1,*p2;
188 char pc[50];
189 char *e = getenv("HOME");
190 strncpy(pc, e, 30);
191 strcpy(&pc[strlen(pc)], "/default.cfg");
192 if (!fexists(pc)) {
193 strcpy(pc, "default.cfg");
194 if (!fexists(pc)) {
195 strcpy(pc, "/usr/share/doom2d-rembo/default.cfg");
196 if (!fexists(pc)) {
197 logo("default.cfg not found\n");
198 return;
203 logo("CFG_load: загрузка конфигурации из %s\n",pc);
204 if((h=fopen(pc,"rb"))==NULL) {
205 perror("Cannot open file");return;
207 while(!feof(h)) {
208 F_readstr(h,s,127);
209 if(*s==';' || s[1]==';') continue; // comment
210 if(!(p1=strtok(s,"\r\n\t=;"))) continue;//if(!(p1=strtok(s,"\r\n\t =;"))) continue;
211 if(!(p2=strtok(NULL,"\r\n\t=;"))) continue;//if(!(p2=strtok(NULL,"\r\n\t =;"))) continue;
212 for(j=0;cfg[j].t;++j) if(cfg[j].cfg && !cfg[j].o)
213 if(strcasecmp(p1,cfg[j].cfg)==0) {
214 switch(cfg[j].t) {
215 case BYTE:
216 n=strtol(p2,NULL,0);
217 *((byte *)cfg[j].p)=(byte)n;
218 break;
219 case WORD:
220 n=strtol(p2,NULL,0);
221 *((word *)cfg[j].p)=(word)n;
222 break;
223 case DWORD:
224 n=strtol(p2,NULL,0);
225 *((dword *)cfg[j].p)=n;
226 break;
227 case STRING:
228 strcpy((char *)cfg[j].p,p2);
229 break;
230 case SW_ON:
231 case SW_OFF:
232 if(strcasecmp(p2,"ON")==0) {*((byte *)cfg[j].p)=ON;break;}
233 if(strcasecmp(p2,"OFF")==0) {*((byte *)cfg[j].p)=OFF;break;}
234 *((byte *)cfg[j].p)=strtol(p2,NULL,0);
235 break;
236 case FILES:
237 break;
239 case KEY:
241 int k = get_key(p2);
242 if (k) {
243 *((int *)cfg[j].p)=k;
245 else {
246 logo("Unknown key in cfg: %s=%s\n",p1,p2);
247 logo("List available key names:\n");
248 int i;
249 for(i=1; i<SDLK_LAST; i++) {
250 char* s = SDL_GetKeyName(i);
252 if (!strcasecmp(s,"unknown key") == 0) {
253 logo("%s\n", s);
258 break;
260 default:
261 ERR_failinit("!!! Неизвестный тип в cfg !!!");
263 break;
266 fclose(h);
269 void CFG_save(void) {
270 /*
271 char s[140],str[140];
272 char *p;
273 FILE *h,*oh;
275 remove("CONFIG.ZZZ");
276 if(rename(cfg_file,"CONFIG.ZZZ")) return;
277 if(!(h=fopen("CONFIG.ZZZ","rt")))
278 {rename("CONFIG.ZZZ",cfg_file);return;}
279 if(!(oh=fopen(cfg_file,"wt")))
280 {fclose(h);rename("CONFIG.ZZZ",cfg_file);return;}
281 for(;;) {
282 if(!fgets(s,128,h)) break;
283 strcpy(str,s);
284 if(!(p=strtok(str,"\r\n\t =;"))) {fprintf(oh,"%s",s);continue;}
285 if(strcasecmp(p,"sound_volume")==0)
286 sprintf(s,"sound_volume=%d\n",snd_vol);
287 else if(strcasecmp(p,"music_volume")==0)
288 sprintf(s,"music_volume=%d\n",mus_vol);
289 else if(strcasecmp(p,"gamma")==0)
290 sprintf(s,"gamma=%d\n",gammaa);
291 else if(strcasecmp(p,"sound_interp")==0)
292 sprintf(s,"sound_interp=%s\n",s_interp?"on":"off");
293 fprintf(oh,"%s",s);
295 fclose(oh);fclose(h);
296 remove("CONFIG.ZZZ");
297 */