Debian Codegraph @

farlng/src/lng.inserter.cpp

raw
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <list>
5 #include <string.h>
6
7 int main_inserter(int argc, char **argv)
8 {
9 if (argc != 4) {
10 const auto NamePtr = strrchr(argv[0], '/');
11 std::cout << "Usage:\n"
12 << (NamePtr ? NamePtr + 1 : argv[0])
13 << " input_template_file output_template_file new_lng_file" << std::endl;
14 return -1;
15 }
16
17 const std::string InFeedName = argv[1], OutFeedName = argv[2], LngName = argv[3];
18
19 std::ifstream Feed(InFeedName), Lng(LngName);
20
21 std::cout << "Reading " << LngName << std::endl;
22
23 std::string LngHeader;
24 std::getline(Lng, LngHeader);
25
26 std::list<std::string> LngLines;
27
28 std::string Buffer;
29 while (!Lng.eof()) {
30 std::getline(Lng, Buffer);
31 if (!Buffer.compare(0, 1, "\"")) {
32 LngLines.push_back(Buffer);
33 }
34 }
35
36 std::cout << "Reading " << InFeedName << std::endl;
37
38 std::list<std::string> FeedLines;
39
40 while (!Feed.eof()) {
41 getline(Feed, Buffer);
42 FeedLines.push_back(Buffer);
43 }
44
45 size_t ConstsCount = 0;
46 for (auto i = FeedLines.begin(); i != FeedLines.end(); ++i) {
47 // assume that all constants starts with 'M'.
48 if (!i->compare(0, 1, "M")) {
49 ++ConstsCount;
50 }
51 }
52
53 if (ConstsCount != LngLines.size()) {
54 std::cerr << "Error: lines count mismatch: " << InFeedName << " - " << ConstsCount << ", " << LngName
55 << " - " << LngLines.size() << std::endl;
56 return -1;
57 }
58
59 if (FeedLines.back().empty()) {
60 FeedLines.pop_back();
61 }
62
63 auto Ptr = FeedLines.begin();
64
65 if (!Ptr->compare(0, 14, "\xef\xbb\xbfm4_include(")) {
66 ++Ptr;
67 }
68
69 #define SKIP_EMPTY_LINES_AND_COMMENTS \
70 while (Ptr->empty() || !Ptr->compare(0, 1, "#")) { \
71 ++Ptr; \
72 }
73
74 SKIP_EMPTY_LINES_AND_COMMENTS
75
76 // skip header name
77 ++Ptr;
78
79 SKIP_EMPTY_LINES_AND_COMMENTS
80
81 std::stringstream strStream(*Ptr);
82 size_t Num;
83 strStream >> Num;
84 std::cout << Num << " languages found." << std::endl;
85
86 auto NumPtr = Ptr;
87 ++Ptr;
88
89 bool Update = false;
90 size_t UpdateIndex = Num;
91
92 for (size_t i = 0; i < Num; ++i, ++Ptr) {
93 SKIP_EMPTY_LINES_AND_COMMENTS
94 if (!Ptr->compare(0, LngName.length(), LngName)) {
95 Update = true;
96 UpdateIndex = i;
97 break;
98 }
99 }
100
101 if (Update) {
102 std::cout << LngName << " already exist (id == " << UpdateIndex << "). Updating." << std::endl;
103 } else {
104 std::cout << "Inserting new language (id == " << UpdateIndex << ") from " << LngName << std::endl;
105 strStream.clear();
106 strStream << Num + 1;
107 *NumPtr = strStream.str();
108
109 std::string ShortLngName = LngHeader.substr(LngHeader.find(L'=', 0) + 1);
110 ShortLngName.resize(ShortLngName.find(L','), 0);
111
112 std::string FullLngName = LngHeader.substr(LngHeader.find(L',', 0) + 1);
113 FeedLines.insert(Ptr, LngName + " " + ShortLngName + " \"" + FullLngName + "\"");
114 }
115
116 for (auto i = LngLines.begin(); i != LngLines.end(); ++i) {
117 // assume that all constants start with 'M'.
118 while (Ptr->compare(0, 1, "M")) {
119 ++Ptr;
120 }
121 ++Ptr;
122
123 for (size_t j = 0; j < UpdateIndex || !UpdateIndex; ++j) {
124 while (Ptr != FeedLines.end() && Ptr->compare(0, 1, "\"") && Ptr->compare(0, 5, "upd:\"")) {
125 ++Ptr;
126 }
127 if (!UpdateIndex) {
128 break;
129 }
130 ++Ptr;
131 }
132
133 if (Update) {
134 const char *Str = Ptr->c_str();
135 size_t l = Ptr->length();
136 if (!Ptr->compare(0, 4, "upd:")) {
137 Str+= 4;
138 l-= 4;
139 }
140 if (i->compare(0, l, Str)) {
141 *Ptr = *i;
142 }
143 } else {
144 FeedLines.insert(Ptr, *i);
145 }
146 }
147
148 std::cout << "Writing to " << OutFeedName << std::endl;
149
150 std::ofstream oFeed(OutFeedName);
151
152 for (auto i = FeedLines.begin(); i != FeedLines.end(); ++i) {
153 oFeed << *i << L'\n';
154 }
155
156 std::cout << "Done." << std::endl;
157
158 return 0;
159 }