forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_class.cpp
191 lines (166 loc) · 4.79 KB
/
strings_class.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
// Aseprite Code Generator
// Copyright (c) 2024 Igara Studio S.A.
// Copyright (c) 2016-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "gen/strings_class.h"
#include "base/fs.h"
#include "base/replace_string.h"
#include "base/string.h"
#include "cfg/cfg.h"
#include <algorithm>
#include <cctype>
#include <iostream>
static std::string to_cpp(std::string stringId)
{
base::replace_string(stringId, ".", "_");
return stringId;
}
static size_t count_fmt_args(const std::string& format)
{
size_t n = 0;
for (size_t i=0; i<format.size(); ++i) {
if (format[i] == '{') {
if (format[i+1] == '}') {
++n;
}
else if (std::isdigit(format[i+1]) &&
format[i+2] == '}') {
n = std::max<size_t>(n, (format[i+1]-'0')+1);
}
}
}
return n;
}
// Strings with raw "{something}" that are not a fmt format string.
//
// TODO This is a super hacky way to filter out these strings, find
// another way.
static bool force_simple_string(const std::string& stringId)
{
return (stringId == "data_filename_format_tooltip" ||
stringId == "data_tagname_format_tooltip");
}
void gen_strings_class(const std::string& inputFn)
{
cfg::CfgFile cfg;
cfg.load(inputFn);
std::cout
<< "// Don't modify, generated file from " << inputFn << "\n"
<< "\n";
std::cout
<< "#ifndef GENERATED_STRINGS_INI_H_INCLUDED\n"
<< "#define GENERATED_STRINGS_INI_H_INCLUDED\n"
<< "#pragma once\n"
<< "\n"
<< "namespace app {\n"
<< "namespace gen {\n"
<< "\n"
<< " template<typename T>\n"
<< " class Strings {\n"
<< " public:\n"
<< " class ID {\n"
<< " public:\n";
std::vector<std::string> sections;
std::vector<std::string> keys;
cfg.getAllSections(sections);
for (const auto& section : sections) {
keys.clear();
cfg.getAllKeys(section.c_str(), keys);
std::string textId = section;
textId.push_back('.');
for (const auto& key : keys) {
textId.append(key);
std::cout << " static constexpr const char* " << to_cpp(textId) << " = \"" << textId << "\";\n";
textId.erase(section.size()+1);
}
}
std::cout
<< " };\n"
<< "\n";
for (const auto& section : sections) {
keys.clear();
cfg.getAllKeys(section.c_str(), keys);
std::string textId = section;
textId.push_back('.');
for (const auto& key : keys) {
textId.append(key);
std::string value = cfg.getValue(section.c_str(), key.c_str(), "");
std::string cppId = to_cpp(textId);
size_t nargs = count_fmt_args(value);
// Create just a function to get the translated string (it
// doesn't have arguments).
if (nargs == 0 || force_simple_string(cppId)) {
std::cout << " static const std::string& " << cppId << "() { return T::Translate(ID::" << cppId << "); }\n";
}
// Create a function to format the translated string with a
// specific number of arguments (the good part is that we can
// check the number of required arguments at compile-time).
else {
std::cout << " template<";
for (int i=1; i<=nargs; ++i) {
std::cout << "typename T" << i;
if (i < nargs)
std::cout << ", ";
}
std::cout << ">\n";
std::cout << " static std::string " << cppId << "(";
for (int i=1; i<=nargs; ++i) {
std::cout << "T" << i << "&& arg" << i;
if (i < nargs)
std::cout << ", ";
}
std::cout << ") { return T::Format(ID::" << cppId << ", ";
for (int i=1; i<=nargs; ++i) {
std::cout << "arg" << i;
if (i < nargs)
std::cout << ", ";
}
std::cout << "); }\n";
}
textId.erase(section.size()+1);
}
}
std::cout
<< " };\n"
<< "\n"
<< "} // namespace gen\n"
<< "} // namespace app\n"
<< "\n"
<< "#endif\n";
}
void gen_command_ids(const std::string& inputFn)
{
cfg::CfgFile cfg;
cfg.load(inputFn);
std::cout
<< "// Don't modify, generated file from " << inputFn << "\n"
<< "\n";
std::cout
<< "#ifndef GENERATED_COMMAND_IDS_H_INCLUDED\n"
<< "#define GENERATED_COMMAND_IDS_H_INCLUDED\n"
<< "#pragma once\n"
<< "\n"
<< "namespace app {\n"
<< "namespace gen {\n"
<< "\n"
<< " class CommandId {\n"
<< " public:\n";
std::vector<std::string> keys;
cfg.getAllKeys("commands", keys);
for (auto key : keys) {
if (key.find('_') != std::string::npos)
continue;
std::cout << " static const char* "
<< key << "() { return \""
<< key << "\"; }\n";
}
std::cout
<< " };\n"
<< "\n"
<< "} // namespace gen\n"
<< "} // namespace app\n"
<< "\n"
<< "#endif\n";
}