forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pref_types.cpp
396 lines (339 loc) · 11.6 KB
/
pref_types.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Aseprite Code Generator
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2014-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "base/exception.h"
#include "base/file_handle.h"
#include "base/fs.h"
#include "base/split_string.h"
#include "base/string.h"
#include "gen/common.h"
#include "gen/pref_types.h"
#include <iostream>
#include <stdexcept>
#include <vector>
typedef std::vector<TiXmlElement*> XmlElements;
static void print_pref_class_def(TiXmlElement* elem, const std::string& className, const char* section, int indentSpaces)
{
std::string indent(indentSpaces, ' ');
std::cout
<< "\n"
<< indent << "class " << className << " : public Section {\n"
<< indent << "public:\n"
<< indent << " " << className << "(const std::string& name);\n";
if (elem->Attribute("canforce"))
std::cout << indent << " void forceSection();\n";
if (elem->Attribute("canclear"))
std::cout << indent << " void clearSection();\n";
std::cout
<< indent << " void load();\n"
<< indent << " void save();\n"
<< indent << " Section* section(const char* id) override;\n"
<< indent << " OptionBase* option(const char* id) override;\n";
TiXmlElement* child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
if (!child->Attribute("type")) throw std::runtime_error("missing 'type' attr in <option>");
if (!childId) throw std::runtime_error("missing 'id' attr in <option>");
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout
<< indent << " Option<" << child->Attribute("type") << "> " << memberName << ";\n";
}
else if (name == "section") {
if (!childId) throw std::runtime_error("missing 'id' attr in <section>");
std::string childClassName = convert_xmlid_to_cppid(childId, true);
std::string memberName = convert_xmlid_to_cppid(childId, false);
print_pref_class_def(child, childClassName, childId, indentSpaces+2);
std::cout
<< indent << " " << childClassName << " " << memberName << ";\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< indent << "};\n";
}
static void print_pref_class_impl(TiXmlElement* elem, const std::string& prefix, const std::string& className, const char* section)
{
std::cout
<< "\n"
<< prefix << className << "::" << className << "(const std::string& name)\n";
if (section)
std::cout << " : Section(std::string(!name.empty() ? name + \".\": \"\") + \"" << section << "\")\n";
else
std::cout << " : Section(name)\n";
TiXmlElement* child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
if (!child->Attribute("type")) throw std::runtime_error("missing 'type' attr in <option>");
if (!childId) throw std::runtime_error("missing 'id' attr in <option>");
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " , "
<< memberName << "(this, \"" << childId << "\"";
if (child->Attribute("default"))
std::cout << ", " << child->Attribute("default");
std::cout << ")\n";
}
else if (name == "section") {
if (!childId) throw std::runtime_error("missing 'id' attr in <option>");
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " , " << memberName << "(name)\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< "{\n"
<< "}\n";
// Section::forceSection()
if (elem->Attribute("canforce")) {
std::cout
<< "\n"
<< "void " << prefix << className << "::forceSection()\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): nullptr);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " " << memberName << ".forceDirtyFlag();\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< "}\n";
}
// Section::clearSection()
if (elem->Attribute("canclear")) {
std::cout
<< "\n"
<< "void " << prefix << className << "::clearSection()\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): nullptr);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " " << memberName << ".clearValue();\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< "}\n";
}
// Section::load()
std::cout
<< "\n"
<< "void " << prefix << className << "::load()\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
const char* migrate = child->Attribute("migrate");
if (migrate) {
std::vector<std::string> parts;
base::split_string(migrate, parts, ".");
std::cout << " load_option_with_migration(" << memberName
<< ", \"" << parts[0]
<< "\", \"" << parts[1] << "\");\n";
}
else
std::cout << " load_option(" << memberName << ");\n";
}
else if (name == "section") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " " << memberName << ".load();\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< "}\n"
<< "\n";
// Section::save()
std::cout
<< "void " << prefix << className << "::save()\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
if (name == "option") {
std::string memberName = convert_xmlid_to_cppid(child->Attribute("id"), false);
std::cout << " save_option(" << memberName << ");\n";
}
else if (name == "section") {
std::string memberName = convert_xmlid_to_cppid(child->Attribute("id"), false);
std::cout << " " << memberName << ".save();\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< "}\n"
<< "\n";
// Section::section(id)
std::cout
<< "Section* " << prefix << className << "::section(const char* id)\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "section") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " if (std::strcmp(id, " << memberName << ".name()) == 0) return &" << memberName << ";\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< " return nullptr;\n"
<< "}\n"
<< "\n";
// Section::option(id)
std::cout
<< "OptionBase* " << prefix << className << "::option(const char* id)\n"
<< "{\n";
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
const char* childId = child->Attribute("id");
if (name == "option") {
std::string memberName = convert_xmlid_to_cppid(childId, false);
std::cout << " if (std::strcmp(id, " << memberName << ".id()) == 0) return &" << memberName << ";\n";
}
}
child = child->NextSiblingElement();
}
std::cout
<< " return nullptr;\n"
<< "}\n";
// Sub-sections
child = (elem->FirstChild() ? elem->FirstChild()->ToElement(): NULL);
while (child) {
if (child->Value()) {
std::string name = child->Value();
if (name == "section") {
std::string childClassName = convert_xmlid_to_cppid(child->Attribute("id"), true);
print_pref_class_impl(child, className + "::", childClassName, child->Attribute("id"));
}
}
child = child->NextSiblingElement();
}
}
void gen_pref_header(TiXmlDocument* doc, const std::string& inputFn)
{
std::cout
<< "// Don't modify, generated file from " << inputFn << "\n"
<< "\n";
std::cout
<< "#ifndef GENERATED_PREF_TYPES_H_INCLUDED\n"
<< "#define GENERATED_PREF_TYPES_H_INCLUDED\n"
<< "#pragma once\n"
<< "\n"
<< "#include <string>\n"
<< "\n"
<< "namespace app {\n"
<< "namespace gen {\n";
TiXmlHandle handle(doc);
TiXmlElement* elem = handle
.FirstChild("preferences")
.FirstChild("types")
.FirstChild("enum").ToElement();
while (elem) {
if (!elem->Attribute("id")) throw std::runtime_error("missing 'id' attr in <enum>");
std::cout
<< "\n"
<< " enum class " << elem->Attribute("id") << " {\n";
TiXmlElement* child = elem->FirstChildElement("value");
while (child) {
if (!child->Attribute("id")) throw std::runtime_error("missing 'id' attr in <value>");
if (!child->Attribute("value")) throw std::runtime_error("missing 'value' attr in <value>");
std::cout << " " << child->Attribute("id") << " = "
<< child->Attribute("value") << ",\n";
child = child->NextSiblingElement("value");
}
std::cout
<< " };\n";
elem = elem->NextSiblingElement("enum");
}
elem = handle
.FirstChild("preferences")
.FirstChild("global").ToElement();
if (elem)
print_pref_class_def(elem, "GlobalPref", NULL, 2);
elem = handle
.FirstChild("preferences")
.FirstChild("tool").ToElement();
if (elem)
print_pref_class_def(elem, "ToolPref", NULL, 2);
elem = handle
.FirstChild("preferences")
.FirstChild("document").ToElement();
if (elem)
print_pref_class_def(elem, "DocPref", NULL, 2);
std::cout
<< "\n"
<< "} // namespace gen\n"
<< "} // namespace app\n"
<< "\n"
<< "#endif\n";
}
void gen_pref_impl(TiXmlDocument* doc, const std::string& inputFn)
{
std::cout
<< "// Don't modify, generated file from " << inputFn << "\n"
<< "\n"
<< "#ifdef HAVE_CONFIG_H\n"
<< "#include \"config.h\"\n"
<< "#endif\n"
<< "\n"
<< "#include \"app/pref/option_io.h\"\n"
<< "#include \"app/pref/preferences.h\"\n"
<< "\n"
<< "#include <cstring>\n"
<< "\n"
<< "namespace app {\n"
<< "namespace gen {\n";
TiXmlHandle handle(doc);
TiXmlElement* elem = handle
.FirstChild("preferences")
.FirstChild("global").ToElement();
if (elem)
print_pref_class_impl(elem, "", "GlobalPref", NULL);
elem = handle
.FirstChild("preferences")
.FirstChild("tool").ToElement();
if (elem)
print_pref_class_impl(elem, "", "ToolPref", NULL);
elem = handle
.FirstChild("preferences")
.FirstChild("document").ToElement();
if (elem)
print_pref_class_impl(elem, "", "DocPref", NULL);
std::cout
<< "\n"
<< "} // namespace gen\n"
<< "} // namespace app\n";
}