/
main.cpp
204 lines (166 loc) · 4.53 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "common.hpp"
#include "mapcss_parse.hpp"
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <sstream>
#include <string>
#include <unordered_map>
using TCoefficientTable = std::unordered_map<std::string, double>;
void InitCoefficientTable(TCoefficientTable & table, int argc, const char * argv[])
{
table["width"] = 1.0;
table["dashes"] = 1.0;
table["casing-width"] = 1.0;
table["casing-dashes"] = 1.0;
table["font-size"] = 1.0;
table["text-offset"] = 1.0;
table["text-halo-radius"] = 1.0;
table["shield-font-size"] = 1.0;
table["shield-text-halo-radius"] = 1.0;
for (int i = 2; i < argc; ++i)
{
std::string const s = argv[i];
size_t const d = s.find('=');
if (d == std::string::npos)
continue;
std::string s1(s.begin(), s.begin() + d);
std::string s2(s.begin() + d + 1, s.end());
Trim(s1);
Trim(s2);
table[s1] = std::stod(s2);
}
}
class MapcssConverter
{
public:
MapcssConverter(TCoefficientTable && table)
: m_table(move(table))
{}
void Process(std::string const & filePath, std::string && fileContent)
{
bool const touched = ProcessFile(fileContent);
if (touched)
m_files[filePath] = move(fileContent);
}
void Flush()
{
for (auto const & fc : m_files)
WriteFile(fc.first, fc.second);
m_files.clear();
}
std::list<std::string> GetAffectedFiles() const
{
std::list<std::string> files;
for (auto const & fc : m_files)
files.emplace_back(fc.first);
return files;
}
private:
bool ProcessFile(std::string & content)
{
bool touched = false;
for (auto const & cc : m_table)
{
if (cc.second == 1.0)
continue;
size_t pos = 0;
while (true)
{
std::string value;
auto const ppos = mapcss::FindProperty(content, pos, cc.first, value);
if (ppos.first == std::string::npos)
break;
value = CorrectValue(value, cc.second);
std::string newProperty = cc.first + ": " + value + ";";
content.replace(ppos.first, ppos.second, newProperty);
pos = ppos.first + newProperty.length();
touched = true;
}
}
return touched;
}
static std::string CorrectValue(std::string const & str, double ratio)
{
// Value can be:
// <double>
// or
// <double> , <double>
// or
// eval(formula)
// or
// <string> (like "butt")
if (str.find("eval") != std::string::npos)
return str;
try
{
std::ostringstream o;
size_t const d = str.find(',');
if (d == std::string::npos)
{
std::string s = str;
Trim(s);
double v = std::stod(s);
v *= ratio;
o << v;
}
else
{
std::string s1(str.begin(), str.begin() + d);
std::string s2(str.begin() + d + 1, str.end());
Trim(s1);
Trim(s2);
double v1 = std::stod(s1);
double v2 = std::stod(s2);
v1 *= ratio;
v2 *= ratio;
o << v1 << "," << v2;
}
return o.str();
}
catch (std::invalid_argument & e)
{
std::cout << "WARNING. Value \"" << str << "\" cannot be processed. " << e.what() << std::endl;
return str;
}
}
static void WriteFile(std::string const & file, std::string const & content)
{
std::ofstream o(file);
o << content;
o.close();
}
TCoefficientTable const m_table;
std::unordered_map<std::string, std::string> m_files;
};
int main(int argc, const char * argv [])
{
if (argc < 2)
{
std::cout << "Specify path to mapcss and correction parameters" << std::endl;
return -1;
}
try
{
std::string const file = argv[1];
std::cout << "Input mapcss project: " << file << std::endl;
TCoefficientTable table;
InitCoefficientTable(table, argc, argv);
for (auto const & kv : table)
std::cout << "Correction: " << kv.first << " x " << kv.second << std::endl;
MapcssConverter converter(move(table));
mapcss::ReadProject(file, std::bind(&MapcssConverter::Process, &converter, std::placeholders::_1, std::placeholders::_2));
auto const & affectedFiles = converter.GetAffectedFiles();
std::cout << affectedFiles.size() << " files has been affected" << (affectedFiles.empty() ? "" : ":") << std::endl;
for (auto const & f : affectedFiles)
std::cout << " " << f << std::endl;
converter.Flush();
std::cout << "Done." << std::endl;
}
catch (std::exception & e)
{
std::cout << "Something went wrong. " << e.what() << std::endl;
}
return 0;
}