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 | #include "SubString.h" |
46 | #include <cstdio> |
47 | #include "Output.h" |
48 | |
49 | namespace orxonox |
50 | { |
51 | const std::string SubString::WhiteSpaces = " \n\t"; |
52 | const std::string SubString::WhiteSpacesWithComma = " \n\t,"; |
53 | const SubString SubString::NullSubString = SubString(); |
54 | |
55 | |
56 | |
57 | |
58 | SubString::SubString() |
59 | { |
60 | } |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | SubString::SubString(const std::string& line, |
78 | const std::string& delimiters, const std::string& delimiterNeighbours, bool bAllowEmptyEntries, |
79 | char escapeChar, bool bRemoveEscapeChar, char safemodeChar, bool bRemoveSafemodeChar, |
80 | char openparenthesisChar, char closeparenthesisChar, bool bRemoveParenthesisChars, char commentChar) |
81 | { |
82 | SubString::splitLine(this->tokens_, this->bTokenInSafemode_, line, delimiters, delimiterNeighbours, bAllowEmptyEntries, escapeChar, bRemoveEscapeChar, safemodeChar, bRemoveSafemodeChar, openparenthesisChar, closeparenthesisChar, bRemoveParenthesisChars, commentChar); |
83 | } |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | SubString::SubString(const SubString& other, size_t begin, size_t length) |
94 | { |
95 | for (size_t i = 0; i < length; ++i) |
96 | { |
97 | if (begin + i >= other.size()) |
98 | break; |
99 | |
100 | this->tokens_.push_back(other[begin + i]); |
101 | this->bTokenInSafemode_.push_back(other.isInSafemode(begin + i)); |
102 | } |
103 | } |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | SubString::SubString(size_t argc, const char** argv) |
111 | { |
112 | for (size_t i = 0; i < argc; ++i) |
113 | { |
114 | this->tokens_.emplace_back(argv[i]); |
115 | this->bTokenInSafemode_.push_back(false); |
116 | } |
117 | } |
118 | |
119 | |
120 | |
121 | |
122 | SubString::~SubString() |
123 | { } |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | SubString& SubString::operator=(const SubString& other) |
130 | { |
131 | this->tokens_ = other.tokens_; |
132 | this->bTokenInSafemode_ = other.bTokenInSafemode_; |
133 | return *this; |
134 | } |
135 | |
136 | |
137 | |
138 | |
139 | bool SubString::operator==(const SubString& other) const |
140 | { |
141 | return ((this->tokens_ == other.tokens_) && (this->bTokenInSafemode_ == other.bTokenInSafemode_)); |
142 | } |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | bool SubString::compare(const SubString& other, size_t length) const |
150 | { |
151 | if (std::min(length, this->size()) != std::min(length, other.size())) |
152 | return false; |
153 | |
154 | for (size_t i = 0; i < std::min(length, this->size()); ++i) |
155 | if ((this->tokens_[i] != other.tokens_[i]) || (this->bTokenInSafemode_[i] != other.bTokenInSafemode_[i])) |
156 | return false; |
157 | |
158 | return true; |
159 | } |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | SubString SubString::operator+(const SubString& other) const |
166 | { |
167 | return SubString(*this) += other; |
168 | } |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | SubString& SubString::operator+=(const SubString& other) |
175 | { |
176 | for (size_t i = 0; i < other.size(); ++i) |
177 | { |
178 | this->tokens_.push_back(other[i]); |
179 | this->bTokenInSafemode_.push_back(other.isInSafemode(i)); |
180 | } |
181 | return *this; |
182 | } |
183 | |
184 | |
185 | |
186 | |
187 | size_t SubString::split(const std::string& line, |
188 | const std::string& delimiters, const std::string& delimiterNeighbours, bool bAllowEmptyEntries, |
189 | char escapeChar, bool bRemoveEscapeChar, char safemodeChar, bool bRemoveSafemodeChar, |
190 | char openparenthesisChar, char closeparenthesisChar, bool bRemoveParenthesisChars, char commentChar) |
191 | { |
192 | this->tokens_.clear(); |
193 | this->bTokenInSafemode_.clear(); |
194 | SubString::splitLine(this->tokens_, this->bTokenInSafemode_, line, delimiters, delimiterNeighbours, bAllowEmptyEntries, escapeChar, bRemoveEscapeChar, safemodeChar, bRemoveSafemodeChar, openparenthesisChar, closeparenthesisChar, bRemoveParenthesisChars, commentChar); |
195 | return this->tokens_.size(); |
196 | } |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | std::string SubString::join(const std::string& delimiter) const |
204 | { |
205 | if (!this->tokens_.empty()) |
206 | { |
207 | std::string retVal = this->tokens_[0]; |
208 | for (size_t i = 1; i < this->tokens_.size(); ++i) |
209 | retVal += delimiter + this->tokens_[i]; |
210 | return retVal; |
211 | } |
212 | else |
213 | return ""; |
214 | } |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | SubString SubString::subSet(size_t begin, size_t length) const |
228 | { |
229 | return SubString(*this, begin, length); |
230 | } |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | SubString::SPLIT_LINE_STATE |
244 | SubString::splitLine(std::vector<std::string>& tokens, |
245 | std::vector<bool>& bTokenInSafemode, |
246 | const std::string& line, |
247 | const std::string& delimiters, |
248 | const std::string& delimiterNeighbours, |
249 | bool bAllowEmptyEntries, |
250 | char escapeChar, |
251 | bool bRemoveEscapeChar, |
252 | char safemodeChar, |
253 | bool bRemoveSafemodeChar, |
254 | char openparenthesisChar, |
255 | char closeparenthesisChar, |
256 | bool bRemoveParenthesisChars, |
257 | char commentChar, |
258 | SPLIT_LINE_STATE start_state) |
259 | { |
260 | SPLIT_LINE_STATE state = start_state; |
261 | size_t i = 0; |
262 | size_t fallBackNeighbours = 0; |
263 | |
264 | std::string token; |
265 | bool inSafemode = false; |
266 | |
267 | if(start_state != SPLIT_LINE_STATE::NORMAL && tokens.size() > 0) |
268 | { |
269 | token = tokens[tokens.size()-1]; |
270 | tokens.pop_back(); |
271 | } |
272 | if(start_state != SPLIT_LINE_STATE::NORMAL && bTokenInSafemode.size() > 0) |
273 | { |
274 | inSafemode = bTokenInSafemode[bTokenInSafemode.size()-1]; |
275 | bTokenInSafemode.pop_back(); |
276 | } |
277 | |
278 | while(i < line.size()) |
279 | { |
280 | switch(state) |
281 | { |
282 | case SPLIT_LINE_STATE::NORMAL: |
283 | if(line[i] == escapeChar) |
284 | { |
285 | state = SPLIT_LINE_STATE::ESCAPE; |
286 | if (!bRemoveEscapeChar) |
287 | token += line[i]; |
288 | fallBackNeighbours = 0; |
289 | } |
290 | else if(line[i] == safemodeChar) |
291 | { |
292 | state = SPLIT_LINE_STATE::SAFEMODE; |
293 | inSafemode = true; |
294 | if (!bRemoveSafemodeChar) |
295 | token += line[i]; |
296 | fallBackNeighbours = 0; |
297 | } |
298 | else if(line[i] == openparenthesisChar) |
299 | { |
300 | state = SPLIT_LINE_STATE::PARENTHESES; |
301 | inSafemode = true; |
302 | if (!bRemoveParenthesisChars) |
303 | token += line[i]; |
304 | fallBackNeighbours = 0; |
305 | } |
306 | else if(line[i] == commentChar) |
307 | { |
308 | if (fallBackNeighbours > 0) |
309 | token = token.substr(0, token.size() - fallBackNeighbours); |
310 | fallBackNeighbours = 0; |
311 | |
312 | if(bAllowEmptyEntries || token.size() > 0) |
313 | { |
314 | tokens.push_back(token); |
315 | token.clear(); |
316 | bTokenInSafemode.push_back(inSafemode); |
317 | inSafemode = false; |
318 | } |
319 | token += line[i]; |
320 | state = SPLIT_LINE_STATE::COMMENT; |
321 | } |
322 | else if(delimiters.find(line[i]) != std::string::npos) |
323 | { |
324 | |
325 | if (fallBackNeighbours > 0) |
326 | token = token.substr(0, token.size() - fallBackNeighbours); |
327 | fallBackNeighbours = 0; |
328 | |
329 | if(bAllowEmptyEntries || token.size() > 0) |
330 | { |
331 | tokens.push_back(token); |
332 | token.clear(); |
333 | bTokenInSafemode.push_back(inSafemode); |
334 | inSafemode = false; |
335 | } |
336 | state = SPLIT_LINE_STATE::NORMAL; |
337 | } |
338 | else |
339 | { |
340 | if (delimiterNeighbours.find(line[i]) != std::string::npos) |
341 | { |
342 | if (token.size() > 0) |
343 | ++fallBackNeighbours; |
344 | else |
345 | { |
346 | ++i; |
347 | continue; |
348 | } |
349 | } |
350 | else |
351 | fallBackNeighbours = 0; |
352 | token += line[i]; |
353 | } |
354 | break; |
355 | case SPLIT_LINE_STATE::ESCAPE: |
356 | if (!bRemoveSafemodeChar) |
357 | token += line[i]; |
358 | else |
359 | { |
360 | if(line[i] == 'n') token += '\n'; |
361 | else if(line[i] == 't') token += '\t'; |
362 | else if(line[i] == 'v') token += '\v'; |
363 | else if(line[i] == 'b') token += '\b'; |
364 | else if(line[i] == 'r') token += '\r'; |
365 | else if(line[i] == 'f') token += '\f'; |
366 | else if(line[i] == 'a') token += '\a'; |
367 | else if(line[i] == '?') token += '\?'; |
368 | else token += line[i]; |
369 | } |
370 | state = SPLIT_LINE_STATE::NORMAL; |
371 | break; |
372 | case SPLIT_LINE_STATE::SAFEMODE: |
373 | if(line[i] == safemodeChar) |
374 | { |
375 | state = SPLIT_LINE_STATE::NORMAL; |
376 | if (!bRemoveSafemodeChar) |
377 | token += line[i]; |
378 | } |
379 | else if(line[i] == escapeChar) |
380 | { |
381 | state = SPLIT_LINE_STATE::SAFEESCAPE; |
382 | } |
383 | else |
384 | { |
385 | token += line[i]; |
386 | } |
387 | break; |
388 | |
389 | case SPLIT_LINE_STATE::SAFEESCAPE: |
390 | if(line[i] == 'n') token += '\n'; |
391 | else if(line[i] == 't') token += '\t'; |
392 | else if(line[i] == 'v') token += '\v'; |
393 | else if(line[i] == 'b') token += '\b'; |
394 | else if(line[i] == 'r') token += '\r'; |
395 | else if(line[i] == 'f') token += '\f'; |
396 | else if(line[i] == 'a') token += '\a'; |
397 | else if(line[i] == '?') token += '\?'; |
398 | else token += line[i]; |
399 | state = SPLIT_LINE_STATE::SAFEMODE; |
400 | break; |
401 | |
402 | case SPLIT_LINE_STATE::PARENTHESES: |
403 | if(line[i] == closeparenthesisChar) |
404 | { |
405 | state = SPLIT_LINE_STATE::NORMAL; |
406 | if (!bRemoveParenthesisChars) |
407 | token += line[i]; |
408 | } |
409 | else if(line[i] == escapeChar) |
410 | { |
411 | state = SPLIT_LINE_STATE::PARENTHESESESCAPE; |
412 | } |
413 | else |
414 | { |
415 | token += line[i]; |
416 | } |
417 | break; |
418 | |
419 | case SPLIT_LINE_STATE::PARENTHESESESCAPE: |
420 | if(line[i] == 'n') token += '\n'; |
421 | else if(line[i] == 't') token += '\t'; |
422 | else if(line[i] == 'v') token += '\v'; |
423 | else if(line[i] == 'b') token += '\b'; |
424 | else if(line[i] == 'r') token += '\r'; |
425 | else if(line[i] == 'f') token += '\f'; |
426 | else if(line[i] == 'a') token += '\a'; |
427 | else if(line[i] == '?') token += '\?'; |
428 | else token += line[i]; |
429 | state = SPLIT_LINE_STATE::PARENTHESES; |
430 | break; |
431 | |
432 | case SPLIT_LINE_STATE::COMMENT: |
433 | if(line[i] == '\n') |
434 | { |
435 | |
436 | if(token.size() > 0) |
437 | { |
438 | tokens.push_back(token); |
439 | token.clear(); |
440 | bTokenInSafemode.push_back(inSafemode); |
441 | inSafemode = false; |
442 | } |
443 | state = SPLIT_LINE_STATE::NORMAL; |
444 | } |
445 | else |
446 | { |
447 | token += line[i]; |
448 | } |
449 | break; |
450 | |
451 | default: |
452 | |
453 | break; |
454 | } |
455 | ++i; |
456 | } |
457 | |
458 | |
459 | if (fallBackNeighbours > 0) |
460 | token = token.substr(0, token.size() - fallBackNeighbours); |
461 | if(bAllowEmptyEntries || token.size() > 0) |
462 | { |
463 | tokens.push_back(token); |
464 | token.clear(); |
465 | bTokenInSafemode.push_back(inSafemode); |
466 | inSafemode = false; |
| Value stored to 'inSafemode' is never read |
467 | } |
468 | return(state); |
469 | } |
470 | |
471 | |
472 | |
473 | |
474 | void SubString::debug() const |
475 | { |
476 | orxout(debug_output) << "Substring-information::count=" << this->tokens_.size() << " ::"; |
477 | for (size_t i = 0; i < this->tokens_.size(); ++i) |
478 | orxout(debug_output) << "s" << i << "='" << this->tokens_[i].c_str() << "'::"; |
479 | orxout(debug_output) << endl; |
480 | } |
481 | } |