-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml.cpp
72 lines (65 loc) · 1.62 KB
/
toml.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
//
// Copyright 2018 Yoshinori Suzuki<wave.suzuki.z@gmail.com>
//
#include <cpptoml.h> // [cpptoml](https://github.com/skystrife/cpptoml)
#include <iostream>
#include <string>
int
main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << argv[0] << ": toml-file" << std::endl;
return 1;
}
auto config = cpptoml::parse_file(argv[1]);
auto struct_name = config->get_as<std::string>("name");
if (!struct_name)
{
std::cerr << "struct has no name" << std::endl;
return 1;
}
auto includes = config->get_array_of<std::string>("include");
for (const auto& inc : *includes)
{
std::cout << "#include <" << inc << ">" << std::endl;
}
std::cout << std::endl << "struct " << *struct_name << " {" << std::endl;
auto contents = config->get_table_array("contents");
for (const auto& table : *contents)
{
auto name = table->get_as<std::string>("name");
auto type = table->get_as<std::string>("type");
if (!name || !type)
{
std::cerr << "this content have no name." << std::endl;
continue;
}
std::string line;
auto bits = table->get_as<int64_t>("bits");
if (bits)
{
// bit field
line = *type + " " + *name + ": ";
line += std::to_string(*bits);
}
else
{
auto array = table->get_as<int64_t>("size");
if (array)
{
// array
line = *type + " " + *name + "[";
line += std::to_string(*array);
line += "]";
}
else
{
line = *type + " " + *name;
}
}
std::cout << "\t" << line << ";" << std::endl;
}
std::cout << "};" << std::endl;
return 0;
}