Mon, 18 Mar 2013 18:29:02 +0200
Added LDraw path setting dialog
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 | }; | |
84 | ||
85 | // ============================================================================= | |
86 | // Load the configuration from file | |
87 | bool config::load () { | |
88 | FILE* fp = fopen (filepath().chars(), "r"); | |
89 | char linedata[MAX_INI_LINE]; | |
90 | char* line; | |
91 | size_t ln = 0; | |
92 | configsection_e section = NO_CONFIG_SECTION; | |
93 | ||
94 | if (!fp) | |
95 | return false; // can't open for reading | |
96 | ||
97 | // Read the values. | |
98 | while (fgets (linedata, MAX_INI_LINE, fp)) { | |
99 | ln++; | |
100 | line = linedata; | |
101 | ||
102 | while (*line != 0 && (*line <= 32 || *line >= 127)) | |
103 | line++; // Skip junk | |
104 | ||
105 | if (*line == '\0' || line[0] == '#') | |
106 | continue; // Empty line or comment. | |
107 | ||
108 | if (line[0] == '[') { | |
109 | // Section | |
110 | char* endbracket = strchr (line, ']'); | |
111 | ||
112 | if (!endbracket) { | |
113 | fprintf (stderr, "badly formed section: %s", line); | |
114 | continue; | |
115 | } | |
116 | ||
117 | str sectionName = str (line).substr (1, endbracket - line); | |
118 | const configsection_e oldsection = section; | |
119 | section = NO_CONFIG_SECTION; | |
120 | ||
121 | // Find the section | |
122 | for (unsigned i = 0; i < NUM_ConfigSections && section == NO_CONFIG_SECTION; i++) | |
123 | if (sectionName.compare (sectionNames[i]) == 0) | |
124 | section = (configsection_e)i; | |
125 | ||
126 | if (section == NO_CONFIG_SECTION) { | |
127 | fprintf (stderr, "unknown config section `%s`\n", sectionName.chars()); | |
128 | section = oldsection; | |
129 | } | |
130 | ||
131 | continue; | |
132 | } | |
133 | ||
134 | // Find the equals sign. | |
135 | char* equals = strchr (line, '='); | |
136 | if (!equals) { | |
137 | fprintf (stderr, "couldn't find `=` sign in entry `%s`\n", line); | |
138 | continue; | |
139 | } | |
140 | ||
141 | str entry = str (line).substr (0, equals - line); | |
142 | ||
143 | str configname; | |
144 | configname.format ("%s_%s", sections[section], entry.chars ()); | |
145 | ||
146 | // Find the config entry for this. | |
147 | config* cfg = NULL; | |
148 | for (size_t i = 0; i < NUM_CONFIG && !cfg; i++) | |
149 | if (configname.compare (pointers[i]->fullname) == 0) | |
150 | cfg = pointers[i]; | |
151 | ||
152 | if (!cfg) { | |
153 | fprintf (stderr, "unknown config `%s`\n", configname.chars()); | |
154 | continue; | |
155 | } | |
156 | ||
157 | str valstring = str (line).substr (equals - line + 1, -1); | |
158 | ||
159 | // Trim the crap off the end | |
160 | while (~valstring) { | |
161 | char c = valstring[~valstring - 1]; | |
162 | if (c <= 32 || c >= 127) | |
163 | valstring -= 1; | |
164 | else | |
165 | break; | |
166 | } | |
167 | ||
168 | switch (const_cast<config*> (cfg)->getType()) { | |
169 | case CONFIG_int: | |
170 | static_cast<intconfig*> (cfg)->value = atoi (valstring.chars()); | |
171 | break; | |
172 | case CONFIG_str: | |
173 | static_cast<strconfig*> (cfg)->value = valstring; | |
174 | break; | |
175 | case CONFIG_float: | |
176 | static_cast<floatconfig*> (cfg)->value = atof (valstring.chars()); | |
177 | break; | |
178 | case CONFIG_bool: | |
179 | { | |
180 | bool& val = static_cast<boolconfig*> (cfg)->value; | |
181 | ||
182 | if (+valstring == "TRUE" || valstring == "1") | |
183 | val = true; | |
184 | else if (+valstring == "FALSE" || valstring == "0") | |
185 | val = false; | |
186 | break; | |
187 | } | |
188 | default: | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | fclose (fp); | |
194 | return true; | |
195 | } | |
196 | ||
197 | // ============================================================================= | |
198 | // Write a given formatted string to the given file stream | |
199 | static size_t writef (FILE* fp, const char* fmt, ...) { | |
200 | va_list va; | |
201 | ||
202 | va_start (va, fmt); | |
203 | char* buf = vdynformat (fmt, va, 256); | |
204 | va_end (va); | |
205 | ||
206 | size_t len = fwrite (buf, 1, strlen (buf), fp); | |
207 | delete[] buf; | |
208 | ||
209 | return len; | |
210 | } | |
211 | ||
212 | // ============================================================================= | |
213 | // Save the configuration to disk | |
214 | bool config::save () { | |
215 | #ifdef APPNAME | |
216 | #ifdef CONFIG_WITH_QT | |
217 | // If the directory doesn't exist, create it now. | |
218 | if (!QDir (dirpath().chars()).exists ()) { | |
219 | fprintf (stderr, "Creating config path %s...\n", dirpath().chars()); | |
220 | if (!QDir ().mkpath (dirpath().chars())) { | |
221 | fprintf (stderr, "Failed to create the directory. Configuration cannot be saved!\n"); | |
222 | return false; // Couldn't create directory | |
223 | } | |
224 | } | |
225 | #else | |
226 | #warning Need QT to check for directories. Config will not be able | |
227 | #warning to save properly if ~/.APPNAME/ does not exist. | |
228 | #endif // CONFIG_WITH_QT | |
229 | #endif // CONFIG_DIRECTORY | |
230 | ||
231 | FILE* fp = fopen (filepath().chars(), "w"); | |
232 | printf ("writing cfg to %s\n", filepath().chars()); | |
233 | ||
234 | if (!fp) { | |
235 | printf ("Couldn't open %s for writing\n", filepath().chars()); | |
236 | return false; | |
237 | } | |
238 | ||
239 | const time_t curtime = time (NULL); | |
240 | const struct tm* timeinfo = localtime (&curtime); | |
241 | const char* daysuffix = | |
242 | (timeinfo->tm_mday % 10 == 1) ? "st" : | |
243 | (timeinfo->tm_mday % 10 == 2) ? "nd" : | |
244 | (timeinfo->tm_mday % 10 == 3) ? "rd" : "th"; | |
245 | ||
246 | writef (fp, "# Configuration file for " APPNAME "\n"); | |
247 | writef (fp, "# Written on %s, %s %d%s %d %.2d:%.2d:%.2d\n", | |
248 | g_WeekdayNames[timeinfo->tm_wday], g_MonthNames[timeinfo->tm_mon], | |
249 | timeinfo->tm_mday, daysuffix, timeinfo->tm_year + 1900, | |
250 | timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | |
251 | writef (fp, "\n"); | |
252 | ||
253 | for (int i = 0; i < NUM_ConfigSections; i++) { | |
254 | if (i > 0) | |
255 | writef (fp, "\n"); | |
256 | ||
257 | writef (fp, "[%s]\n", sectionNames[i]); | |
258 | bool first = true; | |
259 | ||
260 | for (size_t j = 0; j < NUM_CONFIG; j++) { | |
261 | config* cfg = pointers[j]; | |
262 | ||
263 | if (cfg->sect != i) | |
264 | continue; | |
265 | ||
266 | if (!first) | |
267 | writef (fp, "\n"); | |
268 | ||
269 | str valstring; | |
270 | switch (cfg->getType()) { | |
271 | case CONFIG_int: | |
272 | valstring.format ("%d", static_cast<intconfig*> (cfg)->value); | |
273 | break; | |
274 | case CONFIG_str: | |
275 | valstring = static_cast<strconfig*> (cfg)->value; | |
276 | break; | |
277 | case CONFIG_float: | |
278 | valstring.format ("%f", static_cast<floatconfig*> (cfg)->value); | |
279 | ||
280 | // Trim any trailing zeros | |
281 | if (valstring.first (".") != -1) { | |
282 | while (valstring[~valstring - 1] == '0') | |
283 | valstring -= 1; | |
284 | ||
285 | // But don't trim the only one out... | |
286 | if (valstring[~valstring - 1] == '.') | |
287 | valstring += '0'; | |
288 | } | |
289 | ||
290 | break; | |
291 | case CONFIG_bool: | |
292 | valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false"; | |
293 | break; | |
294 | default: | |
295 | break; | |
296 | } | |
297 | ||
298 | // Write the entry now. | |
299 | writef (fp, "# %s, default: %s\n", g_ConfigTypeNames[cfg->getType()], cfg->defaultstring); | |
300 | writef (fp, "# %s: %s\n", cfg->fullname, cfg->description); | |
301 | writef (fp, "%s=%s\n", cfg->name, valstring.chars()); | |
302 | first = false; | |
303 | } | |
304 | } | |
305 | ||
306 | fclose (fp); | |
307 | return true; | |
308 | } | |
309 | ||
310 | // ============================================================================= | |
311 | void config::reset () { | |
312 | for (size_t i = 0; i < NUM_CONFIG; i++) | |
313 | pointers[i]->resetValue (); | |
314 | } | |
315 | ||
316 | // ============================================================================= | |
317 | str config::filepath () { | |
318 | #ifdef APPNAME | |
319 | str path; | |
320 | path.format ("%s" APPNAME ".ini", dirpath().chars()); | |
321 | return path; | |
322 | #else // APPNAME | |
323 | return "config.ini"; | |
324 | #endif // APPNAME | |
325 | } | |
326 | ||
327 | // ============================================================================= | |
328 | str config::dirpath () { | |
329 | #ifdef APPNAME | |
330 | str path; | |
331 | ||
332 | #ifdef CONFIG_WITH_QT | |
333 | path = (QDir::homePath ().toStdString().c_str()); | |
334 | #else // CONFIG_WITH_QT | |
335 | path = "~"; | |
336 | #endif // CONFIG_WITH_QT | |
337 | ||
338 | path += "/." APPNAME "/"; | |
339 | #else | |
340 | path = "./"; | |
341 | #endif // APPNAME | |
342 | ||
343 | return path; | |
344 | } |