-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstyle_file.cpp
321 lines (259 loc) · 7.9 KB
/
style_file.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* SPDX-FileCopyrightText: 2005 Takuro Ashie
* SPDX-FileCopyrightText: 2012 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "style_file.h"
#include <fcitx-utils/charutils.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/stringutils.h>
#include <fstream>
namespace {
const int MAX_LINE_LENGTH = 4096;
std::string escape(const std::string &str) {
std::string dest = str;
for (unsigned int i = 0; i < dest.size(); i++) {
if (dest[i] == '#' || // for comment
dest[i] == '\\' || // for backslash itself
dest[i] == '=' || // for separatort
dest[i] == '[' || dest[i] == ']' || // for section
dest[i] == ',' || // for array
dest[i] == ' ' || dest[i] == '\t') // for space
{
dest.insert(i, "\\");
i++;
}
}
return dest;
}
std::string unescape(const std::string &str) {
std::string dest = str;
for (unsigned int i = 0; i < dest.size(); i++) {
if (dest[i] == '\\') {
dest.erase(i, 1);
if (i < dest.size() && dest[i] == '\\')
i++;
}
}
return dest;
}
} // namespace
StyleLine::StyleLine(StyleFile *style_file, std::string line)
: styleFile_(style_file), line_(std::move(line)),
type_(StyleLineType::UNKNOWN) {
auto trimmed = fcitx::stringutils::trimView(line_);
if (trimmed.length() == 0) {
type_ = StyleLineType::SPACE;
} else if (trimmed[0] == '#') {
type_ = StyleLineType::COMMENT;
} else if (trimmed.front() == '[' && trimmed.back() == ']') {
type_ = StyleLineType::SECTION;
} else {
type_ = StyleLineType::KEY;
}
}
StyleLine::~StyleLine() {}
StyleLineType StyleLine::type() const { return type_; }
bool StyleLine::get_section(std::string §ion) const {
if (type() != StyleLineType::SECTION) {
return false;
}
auto result = fcitx::stringutils::trim(line_);
// remove [ and ]
result.pop_back();
result = result.substr(1);
section = std::move(result);
return true;
}
bool StyleLine::get_key(std::string &key) const {
if (type() != StyleLineType::KEY)
return false;
// skip space ahead.
auto spos = line_.find_first_not_of(FCITX_WHITESPACE);
if (spos == std::string::npos) {
key = std::string();
return true;
}
size_t epos = spos;
for (epos = spos; epos < line_.length(); epos++) {
if (line_[epos] == '\\') {
epos++;
continue;
}
if (line_[epos] == '=') {
break;
}
}
for (--epos; epos >= spos && fcitx::charutils::isspace(line_[epos]); epos--)
;
if (!fcitx::charutils::isspace(line_[epos]))
epos++;
if (spos < epos && epos <= line_.length()) {
key = unescape(line_.substr(spos, epos - spos));
} else
key = std::string();
return true;
}
static int get_value_position(std::string_view str) {
unsigned int spos;
for (spos = 0; spos < str.length(); spos++) {
if (str[spos] == '\\') {
spos++;
continue;
}
if (str[spos] == '=') {
break;
}
}
if (spos >= str.length())
return true;
else
spos++;
for (; spos < str.length() && fcitx::charutils::isspace(str[spos]); spos++)
;
return spos;
}
bool StyleLine::get_value(std::string &value) const {
if (type() != StyleLineType::KEY)
return false;
unsigned int spos = get_value_position(line_);
unsigned int epos = line_.length();
value = unescape(line_.substr(spos, epos - spos));
return true;
}
bool StyleLine::get_value_array(std::vector<std::string> &value) const {
if (type() != StyleLineType::KEY)
return false;
unsigned int spos = get_value_position(line_);
unsigned int epos = line_.length();
unsigned int head_of_element = spos;
for (unsigned int i = spos; i <= epos; i++) {
if (i < epos && line_[i] == '\\') {
i++;
continue;
}
if (i == epos || line_[i] == ',') {
std::string str;
if (head_of_element == epos)
str = std::string();
else
str = unescape(
line_.substr(head_of_element, i - head_of_element));
value.push_back(str);
head_of_element = i + 1;
}
}
return true;
}
StyleFile::StyleFile() { setupDefaultEntries(); }
bool StyleFile::load(const std::string &filename) {
clear();
setupDefaultEntries();
std::ifstream in_file(filename);
if (!in_file)
return false;
clear();
sections_.push_back(StyleLines());
StyleLines *section = §ions_[0];
unsigned int section_id = 0;
char buf[MAX_LINE_LENGTH];
do {
in_file.getline(buf, MAX_LINE_LENGTH);
if (in_file.eof())
break;
std::string dest = buf;
StyleLine line(this, dest);
StyleLineType type = line.type();
if (type == StyleLineType::SECTION) {
sections_.push_back(StyleLines());
section = §ions_.back();
section_id++;
}
section->push_back(line);
if (section_id == 0) {
std::string key;
line.get_key(key);
if (key == "Title") {
line.get_value(title_);
}
}
} while (!in_file.eof());
in_file.close();
return true;
}
void StyleFile::clear() {
title_ = std::string();
sections_.clear();
}
const std::string &StyleFile::title() const { return title_; }
bool StyleFile::getString(std::string &value, std::string section,
std::string key) const {
for (auto it = sections_.begin(); it != sections_.end(); it++) {
if (it->size() <= 0)
continue;
std::string s, k;
(*it)[0].get_section(s);
if (s != section)
continue;
for (auto lit = it->begin(); lit != it->end(); lit++) {
lit->get_key(k);
if (k == key) {
lit->get_value(value);
return true;
}
}
}
return false;
}
bool StyleFile::getStringArray(std::vector<std::string> &value,
std::string section, std::string key) const {
const StyleLines *lines = findSection(section);
if (!lines)
return false;
// find entry
for (auto lit = lines->begin(); lit != lines->end(); lit++) {
std::string k;
lit->get_key(k);
if (k == key) {
lit->get_value_array(value);
return true;
}
}
return false;
}
bool StyleFile::getKeyList(std::vector<std::string> &keys,
std::string section) const {
const StyleLines *lines = findSection(section);
if (!lines)
return false;
for (auto lit = lines->begin(); lit != lines->end(); lit++) {
if (lit->type() != StyleLineType::KEY)
continue;
std::string key;
lit->get_key(key);
keys.push_back(key);
}
return true;
}
void StyleFile::setupDefaultEntries() {
title_ = "User defined";
sections_.push_back(StyleLines());
sections_.push_back(StyleLines());
StyleLines &newsec = sections_.back();
std::string str = std::string("Title") + std::string("=") + escape(title_);
newsec.push_back(StyleLine(this, str.c_str()));
}
const StyleLines *StyleFile::findSection(const std::string §ion) const {
// find section
for (auto it = sections_.begin(); it != sections_.end(); it++) {
if (it->size() <= 0)
continue;
std::string s;
(*it)[0].get_section(s);
if (s == section)
return &(*it);
}
return nullptr;
}