-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoml_make_cpp.cpp
More file actions
250 lines (226 loc) · 9.61 KB
/
toml_make_cpp.cpp
File metadata and controls
250 lines (226 loc) · 9.61 KB
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
//
// Created by lp on 2019/12/7.
//
#define CPPTOML_NO_RTTI
#include "cpptoml.h"
#include <iostream>
#include <memory>
#include <algorithm>
const char *kCommit = "/*********************************************************\n"
"*\n"
"* 文件自动生成. tool: https://github.com/liuping001/toml_cpp\n"
"*\n"
"**********************************************************/\n\n";
const char *kTomlBase = "\n#ifndef TOML_BASE_STRUCT\n"
"#define TOML_BASE_STRUCT\n"
"struct TomlBase {\n"
" TomlBase(){}\n"
" TomlBase(std::shared_ptr<cpptoml::base> ptr) : ptr_(ptr) {}\n"
" std::string operator ()() const { return ptr_->as<std::string>()->get(); }\n"
" std::string S() const { return ptr_->as<std::string>()->get(); }\n"
" int64_t I() const { return ptr_->as<int64_t>()->get(); }\n"
" double D() const { return ptr_->as<double>()->get(); }\n"
" bool B() const { return ptr_->as<bool>()->get(); }\n"
"#if defined(TOML_DATE)\n"
" cpptoml::local_date LocalDate() const { return ptr_->as<cpptoml::local_date>()->get(); }\n"
" cpptoml::local_time LocalTime() const { return ptr_->as<cpptoml::local_time>()->get(); }\n"
" cpptoml::local_datetime LocalDatetime() const { return ptr_->as<cpptoml::local_datetime>()->get(); }\n"
" cpptoml::offset_datetime OffsetDatetime() const { return ptr_->as<cpptoml::offset_datetime>()->get(); }\n"
"#endif\n"
" private:\n"
" std::shared_ptr<cpptoml::base> ptr_;\n"
"};\n"
"#endif\n\n";
namespace toml_cpp {
static std::string Tab;
struct CppOut {
std::ostringstream make_struct;
};
std::string Type(std::shared_ptr<cpptoml::base> ptr) {
using namespace cpptoml;
switch (ptr->type()) {
case base_type::STRING: return "std::string";
case base_type::LOCAL_TIME: return "cpptoml::local_time";
case base_type::LOCAL_DATE: return "cpptoml::local_date";
case base_type::LOCAL_DATETIME: return "cpptoml::local_datetime";
case base_type::OFFSET_DATETIME: return "cpptoml::offset_datetime";
case base_type::INT: return "int64_t";
case base_type::FLOAT: return "double";
case base_type::BOOL: return "bool";
}
return "error";
}
inline std::string BigWord(const std::string &org_word) {
auto to_up = [](char c) -> char {
if (c >= 'a' && c <= 'z') {
return 'A' + (c - 'a');
}
return c;
};
auto word = org_word;
for (size_t i = 0; i < word.size(); i++) {
if (i==0 || (i > 0 && word[i - 1] == '_')) {
word[i] = to_up(word[i]);
}
}
word.erase(std::remove_if(word.begin(), word.end(), [](char c) { return c == '_';}), word.end());
return word;
}
void StructDefineArray(std::shared_ptr<cpptoml::base> ptr, CppOut &ss) {
if (ptr->is_array()) {
ss.make_struct << "std::vector<";
StructDefineArray(*ptr->as_array()->begin(), ss);
ss.make_struct << ">";
} else {
ss.make_struct << "TomlBase";
}
}
void MakeStructDefine(std::shared_ptr<cpptoml::base> ptr,
const std::string &key,
CppOut &ss,
const std::string &depth) {
auto next_depth = depth + Tab;
if (ptr->is_array()) {
ss.make_struct << depth;
StructDefineArray(ptr, ss);
ss.make_struct << " " << key << ";\n";
} else if (ptr->is_table()) {
ss.make_struct << depth << BigWord(key) << " " << key << "; \n";
} else if (ptr->is_table_array()) {
ss.make_struct << depth << "std::vector<" << BigWord(key) << "> " << key << ";\n";
} else {
ss.make_struct << depth << Type(ptr) <<" " << key << ";\n";
}
}
void MakeInitArray(std::shared_ptr<cpptoml::base> ptr, std::ostringstream &init_func, const std::string &depth,
const std::string &key, int32_t d) {
std::string item1 = "arr_" + key + "_item" + std::to_string(d - 1);
std::string item2 = "arr_" + key + "_item" + std::to_string(d);
auto n_depth = depth + Tab;
auto nn_depth = n_depth + Tab;
auto first_item = ptr->is_array() ? *ptr->as_array()->begin() : nullptr;
if (first_item && first_item->is_array()) {
init_func << n_depth << "for (auto item : *arr_" << key << "->as_array()) {\n";
init_func << nn_depth << "auto arr_data = item;\n";
init_func << nn_depth << "decltype(" << item1 << ")::value_type " << item2 << ";\n";
MakeInitArray(first_item, init_func, n_depth, key, d + 1);
init_func << nn_depth << item1 << ".push_back(" << item2 << ");\n";
init_func << n_depth << "}\n";
} else {
auto real_key = d > 2 ? "data" : key;
if (ptr->is_table_array()) {
init_func << n_depth << "for (auto item : *arr_" << real_key << "->as_table_array()) {\n";
init_func << nn_depth << item1 << ".emplace_back();\n";
init_func << nn_depth << item1 << ".back().FromToml(item);\n";
init_func << n_depth << "}\n";
} else {
init_func << n_depth << "for (auto item : *arr_" << real_key << "->as_array()) {\n";
init_func << nn_depth << item1 << ".push_back(item);\n";
init_func << n_depth << "}\n";
}
}
}
void MakeInitFunc(std::shared_ptr<cpptoml::base> ptr,
const std::string &key,
std::ostringstream &init_func,
const std::string &depth) {
auto n_depth = depth + Tab;
if (ptr->is_array() || ptr->is_table_array()) {
auto d = 1;
init_func << n_depth << "auto arr_" << key << " = ptr->as_table()->get(\"" << key << "\");\n";
init_func << n_depth << "decltype(" << key << ") arr_" << key << "_item" << d << ";\n";
MakeInitArray(ptr, init_func, depth, key, d + 1);
init_func << n_depth << key << " = " << "std::move(arr_" << key << "_item" << d << ");\n";
} else if (ptr->is_table()) {
init_func << n_depth << key << ".FromToml(ptr->as_table()->get(\"" << key << "\"));\n";
} else {
init_func << n_depth << key << " = ptr->as_table()->get(\"" << key << "\")->as<"<< Type(ptr) <<">()->get();\n";
}
}
// 由于依赖关系,所以先遍历到最低层数据结构 然后才分析结构
void PrintStruct(std::shared_ptr<cpptoml::base> ptr, const std::string &key, CppOut &ss, const std::string &depth) {
auto n_depth = depth + Tab;
auto nn_depth = n_depth + Tab;
std::vector<std::pair<std::string, std::shared_ptr<cpptoml::base>>> depend;
if (ptr->is_table() || ptr->is_table_array()) {
auto &type_table = ptr->is_table() ? *ptr->as_table() : **ptr->as_table_array()->begin();
for (auto &item : type_table) {
if (item.second->is_table() || item.second->is_table_array()) {
depend.emplace_back(item.first, item.second);
}
}
}
for (auto item : depend) {
PrintStruct(item.second, item.first, ss, depth);
}
std::ostringstream init_func;
if (ptr->is_table() || ptr->is_table_array()) {
auto &type_table = ptr->is_table() ? *ptr->as_table() : **ptr->as_table_array()->begin();
ss.make_struct << "\n" << depth << "struct " << BigWord(key) << " {\n";
init_func << "\n" << n_depth << "void FromToml(std::shared_ptr<cpptoml::base> ptr){\n";
for (auto &item : type_table) {
MakeStructDefine(item.second, item.first, ss, n_depth);
MakeInitFunc(item.second, item.first, init_func, n_depth);
}
init_func << n_depth << "}\n";
ss.make_struct << init_func.str();
ss.make_struct << depth << "};\n";
}
}
} // end namespace toml_cpp
const std::string basename(const std::string &path) {
auto const pos = path.find_last_of('/');
return pos == path.npos ? path : path.substr(pos + 1);
}
const std::string dirname(const std::string &path) {
auto const pos = path.find_last_of('/');
return pos == path.npos ? "" : path.substr(0, pos);
}
using namespace toml_cpp;
int main(int argc, char *argv[]) {
std::string file;
std::string toml_date_flag;
auto tab = 2;
if (argc < 3) {
std::cout << "toml_cpp -file file [-tab 2] [-date 1]\n";
return -1;
}
for (int i = 2; i < argc; i = i + 2) {
if (strcmp(argv[i - 1], "-file") == 0) {
file = argv[i];
} else if (strcmp(argv[i - 1], "-tab") == 0) {
tab = std::stoi(argv[i]);
} else if (strcmp(argv[i - 1], "-date") == 0) {
toml_date_flag = argv[i];
} else {
std::cout << "not find option: " << argv[i - 1] << std::endl;
return -2;
}
}
for (auto i = 0; i < tab; i++) {
Tab.push_back(' ');
}
try {
auto root = cpptoml::parse_file(file);
auto file_name = std::string(basename(file));
auto hpp = file_name.substr(0, file_name.find("."));
CppOut out;
PrintStruct(root, "root", out, "");
std::ofstream fout(hpp + "_toml.hpp", std::ios::out);
fout << kCommit;
fout << "#pragma once\n";
fout << "#include \"thirdparty/cpptoml/cpptoml.h\"\n\n";
if (toml_date_flag=="1") {
fout <<"#define TOML_DATE 1";
}
fout << kTomlBase;
fout << "namespace " << hpp << "_toml {\n";
fout << out.make_struct.str() << "\n";
fout << "} // end " << hpp << "_toml\n";
fout.close();
std::cout << "finish toml cpp:" << file_name << std::endl;
} catch (std::exception e) {
std::cout << "parse file failed :" << argv[1] << " e:"<<e.what();
}
return 0;
}