Debian Codegraph @

src/common/conf.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2004,2005 Dizzy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "common/setup_before.h"
20 #include "common/conf.h"
21
22 #include <cassert>
23 #include <cstring>
24 #include <string>
25
26 #include "common/eventlog.h"
27 #include "common/xalloc.h"
28 #include "common/util.h"
29 #include "common/setup_after.h"
30
31 namespace pvpgn
32 {
33
34 extern int conf_set_int(unsigned *pint, const char *valstr, unsigned def)
35 {
36 if (!valstr) *pint = def;
37 else {
38 unsigned temp;
39
40 if (str_to_uint(valstr, &temp) < 0) {
41 eventlog(eventlog_level_error, __FUNCTION__, "invalid integer value '{}'", valstr);
42 return -1;
43 }
44 else *pint = temp;
45 }
46
47 return 0;
48 }
49
50 extern int conf_set_bool(unsigned *pbool, const char *valstr, unsigned def)
51 {
52 if (!valstr) *pbool = def;
53 else {
54 switch (str_get_bool(valstr)) {
55 case 1:
56 *pbool = 1;
57 break;
58 case 0:
59 *pbool = 0;
60 break;
61 default:
62 eventlog(eventlog_level_error, __FUNCTION__, "invalid boolean value '{}'", valstr);
63 return -1;
64 }
65 }
66
67 return 0;
68 }
69
70 extern int conf_set_str(const char **pstr, const char *valstr, const char *def)
71 {
72 if (*pstr) xfree((void*)*pstr);
73 if (!valstr && !def) *pstr = NULL;
74 else *pstr = xstrdup(valstr ? valstr : def);
75
76 return 0;
77 }
78
79 extern int conf_set_timestr(std::time_t* ptime, const char *valstr, std::time_t def)
80 {
81 if (!valstr) *ptime = def;
82 else if (timestr_to_time(valstr, ptime) < 0) {
83 eventlog(eventlog_level_error, __FUNCTION__, "invalid timestr value '{}'", valstr);
84 return -1;
85 }
86
87 return 0;
88 }
89
90 extern const char* conf_get_int(unsigned ival)
91 {
92 static char buffer[128] = {};
93 std::snprintf(buffer, sizeof buffer, "%u", ival);
94 return buffer;
95 }
96
97 extern const char* conf_get_bool(unsigned ival)
98 {
99 return ival ? "true" : "false";
100 }
101
102 static void _conf_reset_defaults(t_conf_entry *conftab)
103 {
104 t_conf_entry *curr;
105
106 assert(conftab);
107 for (curr = conftab; curr->name; curr++)
108 curr->setdef();
109 }
110
111 static char * _get_value(char *str, unsigned lineno)
112 {
113 char *cp, prev;
114
115 if (*str == '"') {
116 for (cp = ++str, prev = '\0'; *cp; cp++) {
117 switch (*cp) {
118 case '"':
119 if (prev != '\\') break;
120 prev = '"';
121 continue;
122 case '\\':
123 if (prev == '\\') prev = '\0';
124 else prev = '\\';
125 continue;
126 default:
127 prev = *cp;
128 continue;
129 }
130 break;
131 }
132
133 if (*cp != '"') {
134 eventlog(eventlog_level_error, __FUNCTION__, "missing end quota at line {}", lineno);
135 return NULL;
136 }
137
138 *cp = '\0';
139 cp = str_skip_space(cp + 1);
140 if (*cp) {
141 eventlog(eventlog_level_error, __FUNCTION__, "extra characters in value after ending quote at line {}", lineno);
142 return NULL;
143 }
144 }
145 else {
146 cp = str_skip_word(str);
147 if (*cp) {
148 *cp = '\0';
149 cp = str_skip_space(cp + 1);
150 if (*cp) {
151 eventlog(eventlog_level_error, __FUNCTION__, "extra characters after the value at line {}", lineno);
152 return NULL;
153 }
154 }
155 }
156
157 return str;
158 }
159
160 static void _process_option(const char *key, const char *val, t_conf_entry *conftab)
161 {
162 t_conf_entry *curr;
163
164 for (curr = conftab; curr->name; curr++)
165 if (!std::strcmp(key, curr->name)) {
166 curr->set(val);
167 return;
168 }
169
170 eventlog(eventlog_level_error, __FUNCTION__, "option '{}' unknown", key);
171 }
172
173 extern int conf_load_file(std::FILE *fd, t_conf_entry *conftab)
174 {
175 char *buff, *directive, *value, *cp;
176 unsigned cflag, lineno;
177
178 if (!fd) {
179 eventlog(eventlog_level_error, __FUNCTION__, "got NULL file");
180 return -1;
181 }
182
183 if (!conftab) {
184 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conftab");
185 return -1;
186 }
187
188 /* restore defaults */
189 _conf_reset_defaults(conftab);
190
191 for (lineno = 1; (buff = file_get_line(fd)); lineno++) {
192 cflag = 1;
193 for (cp = buff; *cp; cp++) {
194 switch (*cp) {
195 case '\\':
196 if (!*(++cp)) cflag = 0;
197 break;
198 case '"':
199 switch (cflag) {
200 case 1: cflag = 2; break;
201 case 2: cflag = 1; break;
202 }
203 break;
204 case '#':
205 if (cflag == 1) cflag = 0;
206 break;
207 }
208 if (!cflag) break;
209 }
210
211 if (*cp == '#') *cp = '\0';
212 cp = str_skip_space(buff);
213 if (!*cp) continue;
214
215 directive = cp;
216 cp = str_skip_word(cp + 1);
217 if (*cp) *(cp++) = '\0';
218
219 cp = str_skip_space(cp);
220 if (*cp != '=') {
221 eventlog(eventlog_level_error, __FUNCTION__, "missing = on line {}", lineno);
222 continue;
223 }
224
225 cp = str_skip_space(cp + 1);
226 if (!*cp) {
227 eventlog(eventlog_level_error, __FUNCTION__, "missing value at line {}", lineno);
228 continue;
229 }
230
231 value = _get_value(cp, lineno);
232 if (!value) continue;
233
234 _process_option(directive, value, conftab);
235 }
236
237 return 0;
238 }
239
240 extern void conf_unload(t_conf_entry *conftab)
241 {
242 t_conf_entry *curr;
243
244 if (!conftab) {
245 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conftab");
246 return;
247 }
248
249 for (curr = conftab; curr->name; curr++)
250 curr->set(NULL);
251 }
252
253 extern int conf_load_cmdline(int argc, char **argv, t_conf_entry *conftab)
254 {
255 int i;
256 char *key, *val, *newkey;
257
258 if (!conftab) {
259 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conftab");
260 return -1;
261 }
262
263 /* restore defaults */
264 _conf_reset_defaults(conftab);
265
266 for (i = 1; i < argc; ++i) {
267 newkey = NULL;
268 key = argv[i];
269
270 if (*(key++) != '-') /* skip non options */
271 continue;
272
273 if (*key == '-') key++; /* allow both - and -- options */
274
275 if ((val = std::strchr(key, '='))) { /* we got option=value format */
276 newkey = xstrdup(key);
277 key = newkey;
278 val = std::strchr(key, '=');
279 *(val++) = '\0';
280 }
281 else if (i + 1 < argc && argv[i + 1][0] != '-') {
282 val = argv[++i];
283 }
284
285 if (!val) /* option without argument, so it's like boolean */
286 val = (char *)("true");
287
288 _process_option(key, val, conftab);
289
290 if (newkey) xfree(newkey);
291 }
292
293 return 0;
294 }
295
296 }