Fri, 15 Mar 2013 20:39:29 +0200
don't crash if g_CurrentFile is null
0 | 1 | #include <stdio.h> |
2 | // #include <stdlib.h> | |
3 | #include <errno.h> | |
4 | #include <time.h> | |
5 | #include "str.h" | |
6 | #include "config.h" | |
7 | ||
8 | #ifdef CONFIG_WITH_QT | |
9 | #include <QDir> | |
10 | #endif // CONFIG_WITH_QT | |
11 | ||
12 | // ============================================================================= | |
13 | // Define the configs | |
14 | #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) \ | |
15 | TYPE##config SECT##_##NAME (CFGSECTNAME (SECT), DESCR, \ | |
16 | DEFAULT, #NAME, #SECT "_" #NAME, #TYPE, #DEFAULT); | |
17 | ||
18 | #define SECT(...) | |
19 | #include "cfgdef.h" | |
20 | #undef CFG | |
21 | #undef SECT | |
22 | ||
23 | // ============================================================================= | |
24 | config* config::pointers[] = { | |
25 | #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) &SECT##_##NAME, | |
26 | #define SECT(...) | |
27 | #include "cfgdef.h" | |
28 | #undef CFG | |
29 | #undef SECT | |
30 | }; | |
31 | ||
32 | // ============================================================================= | |
33 | const char* config::sections[] = { | |
34 | #define CFG(...) | |
35 | #define SECT(A,B) #A, | |
36 | #include "cfgdef.h" | |
37 | #undef CFG | |
38 | #undef SECT | |
39 | }; | |
40 | ||
41 | // ============================================================================= | |
42 | const char* config::sectionNames[] = { | |
43 | #define CFG(...) | |
44 | #define SECT(A,B) #B, | |
45 | #include "cfgdef.h" | |
46 | #undef CFG | |
47 | #undef SECT | |
48 | }; | |
49 | ||
50 | // ============================================================================= | |
51 | const char* g_WeekdayNames[7] = { | |
52 | "Sunday", | |
53 | "Monday", | |
54 | "Tuesday", | |
55 | "Wednesday", | |
56 | "Thursday", | |
57 | "Friday", | |
58 | "Saturday", | |
59 | }; | |
60 | ||
61 | // ============================================================================= | |
62 | static const char* g_MonthNames[12] = { | |
63 | "Januray", | |
64 | "February", | |
65 | "March", | |
66 | "April", | |
67 | "May", | |
68 | "June", | |
69 | "July", | |
70 | "August", | |
71 | "September", | |
72 | "October", | |
73 | "November" | |
74 | "December", | |
75 | }; | |
76 | ||
77 | static const char* g_ConfigTypeNames[] = { | |
78 | "None", | |
79 | "Integer", | |
80 | "String", | |
81 | "Float", | |
82 | "Boolean", | |
83 | "Color", | |
84 | }; | |
85 | ||
86 | // ============================================================================= | |
87 | // Load the configuration from file | |
88 | bool config::load () { | |
89 | FILE* fp = fopen (filepath().chars(), "r"); | |
90 | char linedata[MAX_INI_LINE]; | |
91 | char* line; | |
92 | size_t ln = 0; | |
93 | configsection_e section = NO_CONFIG_SECTION; | |
94 | ||
95 | if (!fp) | |
96 | return false; // can't open for reading | |
97 | ||
98 | // Read the values. | |
99 | while (fgets (linedata, MAX_INI_LINE, fp)) { | |
100 | ln++; | |
101 | line = linedata; | |
102 | ||
103 | while (*line != 0 && (*line <= 32 || *line >= 127)) | |
104 | line++; // Skip junk | |
105 | ||
106 | if (*line == '\0' || line[0] == '#') | |
107 | continue; // Empty line or comment. | |
108 | ||
109 | if (line[0] == '[') { | |
110 | // Section | |
111 | char* endbracket = strchr (line, ']'); | |
112 | ||
113 | if (!endbracket) { | |
114 | fprintf (stderr, "badly formed section: %s", line); | |
115 | continue; | |
116 | } | |
117 | ||
118 | str sectionName = str (line).substr (1, endbracket - line); | |
119 | const configsection_e oldsection = section; | |
120 | section = NO_CONFIG_SECTION; | |
121 | ||
122 | // Find the section | |
123 | for (unsigned i = 0; i < NUM_ConfigSections && section == NO_CONFIG_SECTION; i++) | |
124 | if (sectionName.compare (sectionNames[i]) == 0) | |
125 | section = (configsection_e)i; | |
126 | ||
127 | if (section == NO_CONFIG_SECTION) { | |
128 | fprintf (stderr, "unknown config section `%s`\n", sectionName.chars()); | |
129 | section = oldsection; | |
130 | } | |
131 | ||
132 | continue; | |
133 | } | |
134 | ||
135 | // Find the equals sign. | |
136 | char* equals = strchr (line, '='); | |
137 | if (!equals) { | |
138 | fprintf (stderr, "couldn't find `=` sign in entry `%s`\n", line); | |
139 | continue; | |
140 | } | |
141 | ||
142 | str entry = str (line).substr (0, equals - line); | |
143 | ||
144 | str configname; | |
145 | configname.format ("%s_%s", sections[section], entry.chars ()); | |
146 | ||
147 | // Find the config entry for this. | |
148 | config* cfg = NULL; | |
149 | for (size_t i = 0; i < NUM_CONFIG && !cfg; i++) | |
150 | if (configname.compare (pointers[i]->fullname) == 0) | |
151 | cfg = pointers[i]; | |
152 | ||
153 | if (!cfg) { | |
154 | fprintf (stderr, "unknown config `%s`\n", configname.chars()); | |
155 | continue; | |
156 | } | |
157 | ||
158 | str valstring = str (line).substr (equals - line + 1, -1); | |
159 | ||
160 | // Trim the crap off the end | |
161 | while (~valstring) { | |
162 | char c = valstring[~valstring - 1]; | |
163 | if (c <= 32 || c >= 127) | |
164 | valstring -= 1; | |
165 | else | |
166 | break; | |
167 | } | |
168 | ||
169 | switch (const_cast<config*> (cfg)->getType()) { | |
170 | case CONFIG_int: | |
171 | static_cast<intconfig*> (cfg)->value = atoi (valstring.chars()); | |
172 | break; | |
173 | case CONFIG_str: | |
174 | static_cast<strconfig*> (cfg)->value = valstring; | |
175 | break; | |
176 | case CONFIG_float: | |
177 | static_cast<floatconfig*> (cfg)->value = atof (valstring.chars()); | |
178 | break; | |
179 | case CONFIG_bool: | |
180 | { | |
181 | bool& val = static_cast<boolconfig*> (cfg)->value; | |
182 | ||
183 | if (+valstring == "TRUE" || valstring == "1") | |
184 | val = true; | |
185 | else if (+valstring == "FALSE" || valstring == "0") | |
186 | val = false; | |
187 | break; | |
188 | } | |
189 | case CONFIG_color: | |
190 | static_cast<colorconfig*> (cfg)->value.parseFromString (valstring); | |
191 | break; | |
192 | default: | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
197 | fclose (fp); | |
198 | return true; | |
199 | } | |
200 | ||
201 | // ============================================================================= | |
202 | // Write a given formatted string to the given file stream | |
203 | static size_t writef (FILE* fp, const char* fmt, ...) { | |
204 | va_list va; | |
205 | ||
206 | va_start (va, fmt); | |
207 | char* buf = vdynformat (fmt, va, 256); | |
208 | va_end (va); | |
209 | ||
210 | size_t len = fwrite (buf, 1, strlen (buf), fp); | |
211 | delete[] buf; | |
212 | ||
213 | return len; | |
214 | } | |
215 | ||
216 | // ============================================================================= | |
217 | // Save the configuration to disk | |
218 | bool config::save () { | |
219 | #ifdef APPNAME | |
220 | #ifdef CONFIG_WITH_QT | |
221 | // If the directory doesn't exist, create it now. | |
222 | if (!QDir (dirpath().chars()).exists ()) { | |
223 | fprintf (stderr, "Creating config path %s...\n", dirpath().chars()); | |
224 | if (!QDir ().mkpath (dirpath().chars())) { | |
225 | fprintf (stderr, "Failed to create the directory. Configuration cannot be saved!\n"); | |
226 | return false; // Couldn't create directory | |
227 | } | |
228 | } | |
229 | #else | |
230 | #warning Need QT to check for directories. Config will not be able | |
231 | #warning to save properly if ~/.APPNAME/ does not exist. | |
232 | #endif // CONFIG_WITH_QT | |
233 | #endif // CONFIG_DIRECTORY | |
234 | ||
235 | FILE* fp = fopen (filepath().chars(), "w"); | |
236 | printf ("writing cfg to %s\n", filepath().chars()); | |
237 | ||
238 | if (!fp) { | |
239 | printf ("Couldn't open %s for writing\n", filepath().chars()); | |
240 | return false; | |
241 | } | |
242 | ||
243 | const time_t curtime = time (NULL); | |
244 | const struct tm* timeinfo = localtime (&curtime); | |
245 | const char* daysuffix = | |
246 | (timeinfo->tm_mday % 10 == 1) ? "st" : | |
247 | (timeinfo->tm_mday % 10 == 2) ? "nd" : | |
248 | (timeinfo->tm_mday % 10 == 3) ? "rd" : "th"; | |
249 | ||
250 | writef (fp, "# Configuration file for " APPNAME "\n"); | |
251 | writef (fp, "# Written on %s, %s %d%s %d %.2d:%.2d:%.2d\n", | |
252 | g_WeekdayNames[timeinfo->tm_wday], g_MonthNames[timeinfo->tm_mon], | |
253 | timeinfo->tm_mday, daysuffix, timeinfo->tm_year + 1900, | |
254 | timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | |
255 | writef (fp, "\n"); | |
256 | ||
257 | for (int i = 0; i < NUM_ConfigSections; i++) { | |
258 | if (i > 0) | |
259 | writef (fp, "\n"); | |
260 | ||
261 | writef (fp, "[%s]\n", sectionNames[i]); | |
262 | bool first = true; | |
263 | ||
264 | for (size_t j = 0; j < NUM_CONFIG; j++) { | |
265 | config* cfg = pointers[j]; | |
266 | ||
267 | if (cfg->sect != i) | |
268 | continue; | |
269 | ||
270 | if (!first) | |
271 | writef (fp, "\n"); | |
272 | ||
273 | str valstring; | |
274 | switch (cfg->getType()) { | |
275 | case CONFIG_int: | |
276 | valstring.format ("%d", static_cast<intconfig*> (cfg)->value); | |
277 | break; | |
278 | case CONFIG_str: | |
279 | valstring = static_cast<strconfig*> (cfg)->value; | |
280 | break; | |
281 | case CONFIG_float: | |
282 | valstring.format ("%f", static_cast<floatconfig*> (cfg)->value); | |
283 | ||
284 | // Trim any trailing zeros | |
285 | if (valstring.first (".") != -1) { | |
286 | while (valstring[~valstring - 1] == '0') | |
287 | valstring -= 1; | |
288 | ||
289 | // But don't trim the only one out... | |
290 | if (valstring[~valstring - 1] == '.') | |
291 | valstring += '0'; | |
292 | } | |
293 | ||
294 | break; | |
295 | case CONFIG_bool: | |
296 | valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false"; | |
297 | break; | |
298 | case CONFIG_color: | |
299 | valstring = (str)(static_cast<colorconfig*> (cfg)->value); | |
300 | break; | |
301 | default: | |
302 | break; | |
303 | } | |
304 | ||
305 | // Write the entry now. | |
306 | writef (fp, "# %s, default: %s\n", g_ConfigTypeNames[cfg->getType()], cfg->defaultstring); | |
307 | writef (fp, "# %s: %s\n", cfg->fullname, cfg->description); | |
308 | writef (fp, "%s=%s\n", cfg->name, valstring.chars()); | |
309 | first = false; | |
310 | } | |
311 | } | |
312 | ||
313 | fclose (fp); | |
314 | return true; | |
315 | } | |
316 | ||
317 | // ============================================================================= | |
318 | void config::reset () { | |
319 | for (size_t i = 0; i < NUM_CONFIG; i++) | |
320 | pointers[i]->resetValue (); | |
321 | } | |
322 | ||
323 | // ============================================================================= | |
324 | str config::filepath () { | |
325 | #ifdef APPNAME | |
326 | str path; | |
327 | path.format ("%s" APPNAME ".ini", dirpath().chars()); | |
328 | return path; | |
329 | #else // APPNAME | |
330 | return "config.ini"; | |
331 | #endif // APPNAME | |
332 | } | |
333 | ||
334 | // ============================================================================= | |
335 | str config::dirpath () { | |
336 | #ifdef APPNAME | |
337 | str path; | |
338 | ||
339 | #ifdef CONFIG_WITH_QT | |
340 | path = (QDir::homePath ().toStdString().c_str()); | |
341 | #else // CONFIG_WITH_QT | |
342 | path = "~"; | |
343 | #endif // CONFIG_WITH_QT | |
344 | ||
345 | path += "/." APPNAME "/"; | |
346 | #else | |
347 | path = "./"; | |
348 | #endif // APPNAME | |
349 | ||
350 | return path; | |
351 | } |