src/config.cpp

Fri, 14 Jun 2013 19:34:30 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 14 Jun 2013 19:34:30 +0300
changeset 287
3fcccd8c3357
parent 286
7a562bf3d829
child 290
be0c367e7420
permissions
-rw-r--r--

Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating

183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <errno.h>
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <time.h>
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include <QDir>
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
22 #include <qtextstream.h>
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "common.h"
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include "config.h"
198
f246725199dc Split some stuff into separate files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 189
diff changeset
25 #include "misc.h"
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
26 #include "gui.h"
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
28 config* g_configPointers[MAX_CONFIG];
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
29 static ushort g_cfgPointerCursor = 0;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 static const char* g_ConfigTypeNames[] = {
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 "None",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 "Integer",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 "String",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 "Float",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 "Boolean",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 "Key sequence",
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 };
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 // Load the configuration from file
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 bool config::load () {
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
43 print ("config::load: Loading configuration file...\n");
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
44 print ("config::load: Path to configuration is %1\n", filepath ());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 // Locale must be disabled for atof
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 setlocale (LC_NUMERIC, "C");
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
287
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
49 File f (filepath (), File::Read);
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
50 if (!f)
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
51 return false;
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
52
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 size_t ln = 0;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 // Read the values.
287
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
56 for (str line : f) {
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 ln++;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
59 if (line.isEmpty () || line[0] == '#')
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 continue; // Empty line or comment.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 // Find the equals sign.
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
63 int equals = line.indexOf ('=');
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
64 if (equals == -1) {
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
65 fprint (stderr, "couldn't find `=` sign in entry `%1`\n", line);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 continue;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
69 str entry = line.left (equals);
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
70 str valstring = line.right (line.length () - equals - 1);
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
71
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
72 print ("config: `%1` -> %2 == %3 (%4)\n", line, entry, valstring, equals);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 // Find the config entry for this.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 config* cfg = null;
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
76 for (config* i : g_configPointers) {
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
77 if (!i)
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
78 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
79
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 if (entry == i->name)
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 cfg = i;
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
82 }
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 if (!cfg) {
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
85 fprint (stderr, "unknown config `%1`\n", entry);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 continue;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 switch (cfg->getType()) {
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 case CONFIG_int:
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
91 static_cast<intconfig*> (cfg)->value = valstring.toInt ();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 case CONFIG_str:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 static_cast<strconfig*> (cfg)->value = valstring;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 case CONFIG_float:
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
99 static_cast<floatconfig*> (cfg)->value = valstring.toFloat ();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 case CONFIG_bool:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 {
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 bool& val = static_cast<boolconfig*> (cfg)->value;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
106 if (valstring.toUpper () == "TRUE" || valstring == "1")
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 val = true;
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
108 else if (valstring.toUpper () == "FALSE" || valstring == "0")
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 val = false;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 case CONFIG_keyseq:
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
114 static_cast<keyseqconfig*> (cfg)->value = keyseq::fromString (valstring);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 default:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
122 f.close ();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 return true;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 // Save the configuration to disk
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 bool config::save () {
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 // The function will write floats, disable the locale now so that they
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 // are written properly.
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 setlocale (LC_NUMERIC, "C");
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 // If the directory doesn't exist, create it now.
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
134 if (QDir (dirpath ()).exists () == false) {
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
135 fprint (stderr, "Creating config path %1...\n", dirpath ());
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
136 if (!QDir ().mkpath (dirpath ())) {
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
137 critical ("Failed to create the configuration directory. Configuration cannot be saved!\n");
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
138 return false;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141
287
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
142 File f (filepath (), File::Write);
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
143 print ("writing cfg to %1\n", filepath ());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144
287
3fcccd8c3357 Added a File type that wraps around QFile and provides stuff like a null file and range-for-iterating
Santeri Piippo <crimsondusk64@gmail.com>
parents: 286
diff changeset
145 if (!f) {
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
146 critical (fmt ("Cannot save configuration, cannot open %1 for writing\n", filepath ()));
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 return false;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
150 fprint (f, "# Configuration file for " APPNAME "\n");
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 for (config* cfg : g_configPointers) {
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
153 if (!cfg)
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
154 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
155
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 str valstring;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 switch (cfg->getType()) {
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 case CONFIG_int:
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
159 valstring = fmt ("%1", static_cast<intconfig*> (cfg)->value);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
161
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 case CONFIG_str:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 valstring = static_cast<strconfig*> (cfg)->value;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
165
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 case CONFIG_float:
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
167 valstring = fmt ("%1", static_cast<floatconfig*> (cfg)->value);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
169
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 case CONFIG_bool:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false";
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
173
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 case CONFIG_keyseq:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 valstring = static_cast<keyseqconfig*> (cfg)->value.toString ();
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 break;
189
ac2d3e8dd110 Rewrote the string class with a simpler version. The old one was more than probably leaking water like a boat with an elephant on board...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 183
diff changeset
177
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 default:
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 break;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
182 const char* defstr = (cfg->getType() != CONFIG_keyseq) ? cfg->defaultstring :
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
183 qchars (static_cast<keyseqconfig*> (cfg)->defval.toString ());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 // Write the entry now.
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
186 fprint (f, "\n# [%1] default: %2\n", g_ConfigTypeNames[cfg->getType()], defstr);
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
187 fprint (f, "%1=%2\n", cfg->name, valstring);
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
190 f.close ();
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 return true;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 void config::reset () {
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
196 for (config* cfg : g_configPointers) {
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
197 if (!cfg)
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
198 break;
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
199
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
200 cfg->resetValue ();
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
201 }
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 str config::filepath () {
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
206 str path = fmt ("%1%2.cfg", dirpath (),
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
207 str (APPNAME).toLower ());
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 return path;
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 // =============================================================================
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 str config::dirpath () {
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
213 #ifndef _WIN32
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
214 return fmt ("%1" DIRSLASH ".%2" DIRSLASH,
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
215 QDir::homePath (), str (APPNAME).toLower ());
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
216 #else
286
7a562bf3d829 Converted to 16-bit strings.. again
Santeri Piippo <crimsondusk64@gmail.com>
parents: 239
diff changeset
217 return fmt ("%1" DIRSLASH APPNAME DIRSLASH, QDir::homePath ());
238
3b35f502a6c7 Use a more Windows-like config dir name under Windows; warn when unable to read it
Santeri Piippo <crimsondusk64@gmail.com>
parents: 199
diff changeset
218 #endif // _WIN32
239
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
219 }
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
220
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
221 void addConfig (config* ptr) {
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
222 if (g_cfgPointerCursor == 0)
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
223 memset (g_configPointers, 0, sizeof g_configPointers);
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
224
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
225 assert (g_cfgPointerCursor < MAX_CONFIG);
ea09eeba1c2b Use a C-array for config too for the same reasons as with actions
Santeri Piippo <crimsondusk64@gmail.com>
parents: 238
diff changeset
226 g_configPointers[g_cfgPointerCursor++] = ptr;
183
f1b8cb53d2a2 Moved source files to src/, removed zz_ prefix off dialog files.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 }

mercurial