-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
227 lines (177 loc) · 3.95 KB
/
settings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"
#define OUTSTREAM stdout
#include "settings.h"
char joy_device[256];
int joy_left=300;
int joy_right=800;
int joy_top=300;
int joy_bottom=800;
int joy_buttonA=0x0000;
int joy_buttonB=0x0000;
int joy_buttonSTART=0x0000;
int joy_buttonSELECT=0x0000;
#define JOY_DEV 0
#define JOY_LEFT 1
#define JOY_RIGHT 2
#define JOY_TOP 3
#define JOY_BOTTOM 4
#define JOY_BTNA 5
#define JOY_BTNB 6
#define JOY_BTNSTART 7
#define JOY_BTNSELECT 8
char *settingskeys[]={
"joy_dev",
"joy_left",
"joy_right",
"joy_top",
"joy_bottom",
"joy_buttonA",
"joy_buttonB",
"joy_buttonSTART",
"joy_buttonSELECT",
NULL
};
int settings_getconffilename(char *cfgname,int cfglen) {
char *homeenv;
homeenv=getenv("HOME");
if (homeenv==NULL) return -1;
#ifdef UNIX
strncpy(cfgname,homeenv,
cfglen-strlen(SETTINGS_FILE)-5);
strcat(cfgname,"/");
#else
strcpy(cfgname,"\\");
#endif
strcat(cfgname,SETTINGS_FILE);
return 0;
}
int settings_read(void) {
FILE *setf;
char *key,*value;
char buffer[1024];
char settingsfilename[512];
joy_device[0]=0;
if (settings_getconffilename(settingsfilename,sizeof(settingsfilename))<0) {
fprintf(OUTSTREAM,"Cannot find user's home.\n");
return 0;
}
fprintf(OUTSTREAM,"Opening settings in file %s ... ",settingsfilename);
setf=fopen(settingsfilename,"r");
if (setf==NULL) {
out_failed;
fprintf(OUTSTREAM,"*!* Please run 'cingb_conf', if you need "
"joystick support.\n");
return 0;
}
out_ok;
fprintf(OUTSTREAM,"Reading settings ... ");
while (!feof(setf)) {
if (fgets(buffer,sizeof(buffer),setf)==NULL) {
break;
}
settings_trunccomment(buffer);
if (settings_getkeyandvalue(buffer,&key,&value)==0) {
/* got valid key+value combination */
/*fprintf(OUTSTREAM,"'%s' = '%s'\n",key,value);*/
switch (settings_getlistindex(settingskeys,key)) {
case JOY_DEV:
strncpy(joy_device,value,sizeof(joy_device));
break;
case JOY_LEFT:
joy_left=atoi(value);
break;
case JOY_RIGHT:
joy_right=atoi(value);
break;
case JOY_TOP:
joy_top=atoi(value);
break;
case JOY_BOTTOM:
joy_bottom=atoi(value);
break;
case JOY_BTNA:
joy_buttonA=atoi(value);
/* printf("a=%d\n",joy_buttonA);*/
break;
case JOY_BTNB:
joy_buttonB=atoi(value);
/* printf("b=%d\n",joy_buttonB);*/
break;
case JOY_BTNSTART:
joy_buttonSTART=atoi(value);
/* printf("st=%d\n",joy_buttonSTART);*/
break;
case JOY_BTNSELECT:
joy_buttonSELECT=atoi(value);
/* printf("se=%d\n",joy_buttonSELECT);*/
break;
default:
out_failed;
fprintf(OUTSTREAM,"Error in configuration near key:\n\t%s\n",key);
return -1;
}
}
}
if (ferror(setf)) {
out_failed;
return -1;
}
out_ok;
return 0;
}
void settings_trunccomment(char *line) {
char *p;
assert(line!=NULL,"line==NULL in settings_trunccomment");
p=strchr(line,'#');
if (p==NULL) return;
*p=0;
}
int settings_getkeyandvalue(char *line,char **key,char **val) {
settings_trimline(line);
if (strlen(line)==0) return -1;
*key=line;
*val=strchr(*key,'=');
if (*val==NULL) {
fprintf(OUTSTREAM,"Error in configuration file near '%s'\n",line);
return -1;
}
*((*val)++)=0;
settings_trimline(*key);
settings_trimline(*val);
return 0;
}
void settings_trimline(char *line) {
char *p;
int idx;
assert(line!=NULL,"line==NULL in settings_trimline");
/* fprintf(stderr,"trim('%s')\n",line);*/
p=line;
idx=0;
while (*p<=' ' && *p>0) {
p++;
idx++;
}
if (idx>0) memmove(line,p,(strlen(line)+1)-idx);
idx=strlen(line)-1;
while (idx>=0) {
if (p[idx]>' ') break;
p[idx--]=0;
}
}
int settings_getlistindex(char *list[],char *searchkey) {
int i=0;
while (list[i]!=NULL) {
if (!strcmp(list[i],searchkey)) return i;
i++;
}
return UNKNOWN_KEY;
}
/*
DEBUG routine
void main(void) {
settings_read();
}
*/