1// Aseprite Code Generator
2// Copyright (c) 2016-2017 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#include "gen/strings_class.h"
8
9#include "base/fs.h"
10#include "base/replace_string.h"
11#include "base/string.h"
12#include "cfg/cfg.h"
13
14#include <iostream>
15
16static std::string to_cpp(std::string stringId)
17{
18 base::replace_string(stringId, ".", "_");
19 return stringId;
20}
21
22void gen_strings_class(const std::string& inputFn)
23{
24 cfg::CfgFile cfg;
25 cfg.load(inputFn);
26
27 std::cout
28 << "// Don't modify, generated file from " << inputFn << "\n"
29 << "\n";
30
31 std::cout
32 << "#ifndef GENERATED_STRINGS_INI_H_INCLUDED\n"
33 << "#define GENERATED_STRINGS_INI_H_INCLUDED\n"
34 << "#pragma once\n"
35 << "\n"
36 << "namespace app {\n"
37 << "namespace gen {\n"
38 << "\n"
39 << " template<typename T>\n"
40 << " class Strings {\n"
41 << " public:\n";
42
43 std::vector<std::string> sections;
44 std::vector<std::string> keys;
45 cfg.getAllSections(sections);
46 for (const auto& section : sections) {
47 keys.clear();
48 cfg.getAllKeys(section.c_str(), keys);
49
50 std::string textId = section;
51 textId.push_back('.');
52 for (auto key : keys) {
53 textId.append(key);
54
55 std::cout << " static const std::string& " << to_cpp(textId) << "() { return T::instance()->translate(\"" << textId << "\"); }\n";
56
57 textId.erase(section.size()+1);
58 }
59 }
60
61 std::cout
62 << " };\n"
63 << "\n"
64 << "} // namespace gen\n"
65 << "} // namespace app\n"
66 << "\n"
67 << "#endif\n";
68}
69
70void gen_command_ids(const std::string& inputFn)
71{
72 cfg::CfgFile cfg;
73 cfg.load(inputFn);
74
75 std::cout
76 << "// Don't modify, generated file from " << inputFn << "\n"
77 << "\n";
78
79 std::cout
80 << "#ifndef GENERATED_COMMAND_IDS_H_INCLUDED\n"
81 << "#define GENERATED_COMMAND_IDS_H_INCLUDED\n"
82 << "#pragma once\n"
83 << "\n"
84 << "namespace app {\n"
85 << "namespace gen {\n"
86 << "\n"
87 << " class CommandId {\n"
88 << " public:\n";
89
90 std::vector<std::string> keys;
91 cfg.getAllKeys("commands", keys);
92 for (auto key : keys) {
93 if (key.find('_') != std::string::npos)
94 continue;
95
96 std::cout << " static const char* "
97 << key << "() { return \""
98 << key << "\"; }\n";
99 }
100
101 std::cout
102 << " };\n"
103 << "\n"
104 << "} // namespace gen\n"
105 << "} // namespace app\n"
106 << "\n"
107 << "#endif\n";
108}
109