Tue, 19 Mar 2013 18:12:39 +0200
Temporarily set the locale to C when using ftoa, or the resulting string is subject to the locale and gets unexpected symbols (e.g. commas for the decimal dots while the function expects periods)
30
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
1 | /* |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
3 | * Copyright (C) 2013 Santeri `arezey` Piippo |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
4 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
8 | * (at your option) any later version. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
9 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
13 | * GNU General Public License for more details. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
14 | * |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
17 | */ |
31ff9aabd506
Licensed LDForge GPL3, added some more icons
Santeri Piippo <crimsondusk64@gmail.com>
parents:
29
diff
changeset
|
18 | |
0 | 19 | #ifndef __OPTIONS_H__ |
20 | #define __OPTIONS_H__ | |
21 | ||
22 | #include "common.h" | |
23 | #include "str.h" | |
24 | ||
25 | // ============================================================================= | |
26 | // Determine configuration file. Use APPNAME if given. | |
27 | #ifdef APPNAME | |
28 | #define CONFIGFILE APPNAME ".ini" | |
29 | #else // APPNAME | |
30 | #define APPNAME "(unnamed application)" | |
31 | #define CONFIGFILE "config.ini" | |
32 | #endif // APPNAME | |
33 | ||
34 | #ifdef CONFIG_WITH_QT | |
35 | #include <QString> | |
36 | #endif // CONFIG_WITH_QT | |
37 | ||
38 | // ------------------------------- | |
39 | #define CFGSECTNAME(X) CFGSECT_##X | |
40 | ||
41 | #define MAX_INI_LINE 512 | |
42 | #define NUM_CONFIG (sizeof config::pointers / sizeof *config::pointers) | |
43 | ||
44 | // ============================================================================= | |
45 | enum configsection_e { | |
46 | #define CFG(...) | |
47 | #define SECT(A,B) CFGSECTNAME (A), | |
48 | #include "cfgdef.h" | |
49 | #undef CFG | |
50 | #undef SECT | |
51 | NUM_ConfigSections, | |
52 | NO_CONFIG_SECTION = -1 | |
53 | }; | |
54 | ||
55 | // ============================================================================= | |
56 | enum configtype_e { | |
57 | CONFIG_none, | |
58 | CONFIG_int, | |
59 | CONFIG_str, | |
60 | CONFIG_float, | |
61 | CONFIG_bool, | |
62 | }; | |
63 | ||
64 | // ========================================================= | |
65 | class config { | |
66 | public: | |
67 | configsection_e sect; | |
68 | const char* description, *name, *fullname, *typestring, *defaultstring; | |
69 | ||
70 | virtual configtype_e getType () { | |
71 | return CONFIG_none; | |
72 | } | |
73 | ||
74 | virtual void resetValue () {} | |
75 | ||
76 | // ------------------------------------------ | |
77 | static bool load (); | |
78 | static bool save (); | |
79 | static void reset (); | |
80 | static config* pointers[]; | |
81 | static const char* sections[]; | |
82 | static const char* sectionNames[]; | |
83 | static str dirpath (); | |
84 | static str filepath (); | |
85 | }; | |
86 | ||
87 | // ============================================================================= | |
88 | #define DEFINE_UNARY_OPERATOR(T, OP) \ | |
89 | T operator OP () { \ | |
90 | return (OP value); \ | |
91 | } \ | |
92 | ||
93 | #define DEFINE_BINARY_OPERATOR(T, OP) \ | |
94 | T operator OP (const T other) { \ | |
95 | return (value OP other); \ | |
96 | } \ | |
97 | ||
98 | #define DEFINE_ASSIGN_OPERATOR(T, OP) \ | |
99 | T& operator OP (const T other) { \ | |
100 | return (value OP other); \ | |
101 | } \ | |
102 | ||
103 | #define DEFINE_COMPARE_OPERATOR(T, OP) \ | |
104 | bool operator OP (const T other) { \ | |
105 | return (value OP other); \ | |
106 | } \ | |
107 | ||
108 | #define DEFINE_CAST_OPERATOR(T) \ | |
109 | operator T () { \ | |
110 | return (T) value; \ | |
111 | } \ | |
112 | ||
113 | #define DEFINE_ALL_COMPARE_OPERATORS(T) \ | |
114 | DEFINE_COMPARE_OPERATOR (T, ==) \ | |
115 | DEFINE_COMPARE_OPERATOR (T, !=) \ | |
116 | DEFINE_COMPARE_OPERATOR (T, >) \ | |
117 | DEFINE_COMPARE_OPERATOR (T, <) \ | |
118 | DEFINE_COMPARE_OPERATOR (T, >=) \ | |
119 | DEFINE_COMPARE_OPERATOR (T, <=) \ | |
120 | ||
121 | #define DEFINE_INCREMENT_OPERATORS(T) \ | |
122 | T operator++ () {return ++value;} \ | |
123 | T operator++ (int) {return value++;} \ | |
124 | T operator-- () {return --value;} \ | |
125 | T operator-- (int) {return value--;} | |
126 | ||
127 | #define CONFIGTYPE(T) \ | |
128 | class T##config : public config | |
129 | ||
130 | #define IMPLEMENT_CONFIG(T) \ | |
131 | T value, defval; \ | |
132 | \ | |
133 | T##config (const configsection_e _sect, const char* _description, \ | |
134 | T _defval, const char* _name, const char* _fullname, const char* _typestring, \ | |
135 | const char* _defaultstring) \ | |
136 | { \ | |
137 | sect = _sect; \ | |
138 | description = _description; \ | |
139 | value = defval = _defval; \ | |
140 | name = _name; \ | |
141 | fullname = _fullname; \ | |
142 | typestring = _typestring; \ | |
143 | defaultstring = _defaultstring; \ | |
144 | } \ | |
145 | operator T () { \ | |
146 | return value; \ | |
147 | } \ | |
148 | configtype_e getType () { \ | |
149 | return CONFIG_##T; \ | |
150 | } \ | |
151 | void resetValue () { \ | |
152 | value = defval; \ | |
153 | } | |
154 | ||
155 | // ============================================================================= | |
156 | CONFIGTYPE (int) { | |
157 | public: | |
158 | IMPLEMENT_CONFIG (int) | |
159 | ||
160 | // Int-specific operators | |
161 | DEFINE_ALL_COMPARE_OPERATORS (int) | |
162 | DEFINE_INCREMENT_OPERATORS (int) | |
163 | DEFINE_BINARY_OPERATOR (int, +) | |
164 | DEFINE_BINARY_OPERATOR (int, -) | |
165 | DEFINE_BINARY_OPERATOR (int, *) | |
166 | DEFINE_BINARY_OPERATOR (int, /) | |
167 | DEFINE_BINARY_OPERATOR (int, %) | |
168 | DEFINE_BINARY_OPERATOR (int, ^) | |
169 | DEFINE_BINARY_OPERATOR (int, |) | |
170 | DEFINE_BINARY_OPERATOR (int, &) | |
171 | DEFINE_BINARY_OPERATOR (int, >>) | |
172 | DEFINE_BINARY_OPERATOR (int, <<) | |
173 | DEFINE_UNARY_OPERATOR (int, !) | |
174 | DEFINE_UNARY_OPERATOR (int, ~) | |
175 | DEFINE_UNARY_OPERATOR (int, -) | |
176 | DEFINE_UNARY_OPERATOR (int, +) | |
177 | DEFINE_ASSIGN_OPERATOR (int, =) | |
178 | DEFINE_ASSIGN_OPERATOR (int, +=) | |
179 | DEFINE_ASSIGN_OPERATOR (int, -=) | |
180 | DEFINE_ASSIGN_OPERATOR (int, *=) | |
181 | DEFINE_ASSIGN_OPERATOR (int, /=) | |
182 | DEFINE_ASSIGN_OPERATOR (int, %=) | |
183 | DEFINE_ASSIGN_OPERATOR (int, >>=) | |
184 | DEFINE_ASSIGN_OPERATOR (int, <<=) | |
185 | }; | |
186 | ||
187 | // ============================================================================= | |
188 | CONFIGTYPE (str) { | |
189 | public: | |
190 | IMPLEMENT_CONFIG (str) | |
191 | ||
192 | DEFINE_ALL_COMPARE_OPERATORS (str) | |
193 | DEFINE_BINARY_OPERATOR (str, -) | |
194 | DEFINE_BINARY_OPERATOR (str, *) | |
195 | DEFINE_UNARY_OPERATOR (str, !) | |
196 | DEFINE_ASSIGN_OPERATOR (str, =) | |
197 | DEFINE_ASSIGN_OPERATOR (str, +=) | |
198 | DEFINE_ASSIGN_OPERATOR (str, -=) | |
199 | DEFINE_ASSIGN_OPERATOR (str, *=) | |
200 | DEFINE_CAST_OPERATOR (char*) | |
201 | ||
202 | char operator[] (size_t n) { | |
203 | return value[n]; | |
204 | } | |
205 | ||
206 | #ifdef CONFIG_WITH_QT | |
207 | operator QString () { | |
208 | return QString (value.chars()); | |
209 | } | |
210 | #endif // CONFIG_WITH_QT | |
211 | }; | |
212 | ||
213 | // ============================================================================= | |
214 | CONFIGTYPE (float) { | |
215 | public: | |
216 | IMPLEMENT_CONFIG (float) | |
217 | ||
218 | DEFINE_ALL_COMPARE_OPERATORS (float) | |
219 | DEFINE_INCREMENT_OPERATORS (float) | |
220 | DEFINE_BINARY_OPERATOR (float, +) | |
221 | DEFINE_BINARY_OPERATOR (float, -) | |
222 | DEFINE_BINARY_OPERATOR (float, *) | |
223 | DEFINE_UNARY_OPERATOR (float, !) | |
224 | DEFINE_ASSIGN_OPERATOR (float, =) | |
225 | DEFINE_ASSIGN_OPERATOR (float, +=) | |
226 | DEFINE_ASSIGN_OPERATOR (float, -=) | |
227 | DEFINE_ASSIGN_OPERATOR (float, *=) | |
228 | }; | |
229 | ||
230 | // ============================================================================= | |
231 | CONFIGTYPE (bool) { | |
232 | public: | |
233 | IMPLEMENT_CONFIG (bool) | |
234 | DEFINE_ALL_COMPARE_OPERATORS (bool) | |
235 | DEFINE_ASSIGN_OPERATOR (bool, =) | |
236 | }; | |
237 | ||
238 | // ============================================================================= | |
239 | // Extern the configurations now | |
240 | #define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) extern TYPE##config SECT##_##NAME; | |
241 | #define SECT(...) | |
242 | #include "cfgdef.h" | |
243 | #undef CFG | |
244 | #undef SECT | |
245 | ||
246 | #endif // __OPTIONS_H__ |