-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceHandler.cpp
441 lines (375 loc) · 8.12 KB
/
ReplaceHandler.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// -----------------------------------------------------------------------------
// ReplaceHandler.cpp ReplaceHandler.cpp
// -----------------------------------------------------------------------------
/**
* @file
* @brief This file holds the implementation of the @ref ReplaceHandler class.
* @author Col. Walter E. Kurtz
* @version 2016-11-08
* @copyright GNU General Public License - Version 3.0
*/
// -----------------------------------------------------------------------------
// Includes Includes
// -----------------------------------------------------------------------------
#include "message.h"
#include "ReplaceHandler.h"
// -----------------------------------------------------------------------------
// Construction Construction
// -----------------------------------------------------------------------------
// --------------
// ReplaceHandler
// --------------
/*
*
*/
ReplaceHandler::ReplaceHandler(KVHandler* next)
: ChainHandler(next)
{
// nothing
}
// ---------------------------------------------------------------------------
// Callback handler Callback handler
// ---------------------------------------------------------------------------
// --------------
// OnBeginParsing
// --------------
/*
*
*/
void ReplaceHandler::OnBeginParsing(const string& filename)
{
// reset healthy flag
setHealthy();
// reset stacks
m_keys.clear();
m_values.clear();
// notify next handler
if (m_next)
{
m_next->OnBeginParsing(filename);
}
}
// ------------
// OnEndParsing
// ------------
/*
*
*/
void ReplaceHandler::OnEndParsing(bool healthy)
{
// empty stacks
m_keys.clear();
m_values.clear();
// notify next handler
if (m_next)
{
m_next->OnEndParsing(healthy);
}
}
// ------
// OnData
// ------
/*
*
*/
void ReplaceHandler::OnData(const string& key, const string& value)
{
// no handler set
if (m_next == 0) return;
// invalid state
if ( !healthy() ) return;
// buffer tags
m_keys.push_back(key);
m_values.push_back(value);
// COMPILATIONINDEX tag found
if (key == "COMPILATIONINDEX")
{
// try to replace all IDs
if ( replaceAll() )
{
// flush all buffered values
for(unsigned i = 0; i < m_keys.size(); i++)
{
// notify next handler
m_next->OnData(m_keys[i], m_values[i]);
// check healthy state
if ( !(m_next->healthy()) )
{
// update healthy flag
setHealthy(false);
// exit loop
break;
}
}
}
else
{
// update healthy flag
setHealthy(false);
}
// empty stacks
m_keys.clear();
m_values.clear();
}
}
// -----------------------------------------------------------------------------
// Internal methods Internal methods
// -----------------------------------------------------------------------------
// ----------
// replaceAll
// ----------
/*
*
*/
bool ReplaceHandler::replaceAll()
{
bool pending = true;
bool updated = true;
// while replacements can (and need to) be done
while (pending && updated)
{
// reset flags
pending = false;
updated = false;
// replace all IDs
for(unsigned i = 0; i < m_values.size(); i++)
{
// get current value
const string& value = m_values[i];
// parts of a value
string a, id, b;
// split value
if ( !split(value, a, id, b) )
{
// invalid syntax
return false;
}
// value is already final
if ( id.empty() ) continue;
// still work to be done
pending = true;
// get value to insert
string paste = getValue(id);
// empty value specified
if ( paste.empty() )
{
// notify user
msg::wrn( msg::cat("empty substitution: $", id) );
}
// requested value can be inserted
if ( isFinal(paste) )
{
// update value
m_values[i] = a + paste + b;
// set flag
updated = true;
}
}
}
// unable to finish all values
if (pending)
{
// notify user
msg::err("unable to finish some values");
// signalize trouble
return false;
}
// signalize success
return true;
}
// -----
// split
// -----
/*
*
*/
bool ReplaceHandler::split(const string& s, string& a, string& id, string& b) const
{
// reset return values
a = "";
id = "";
b = "";
// parser states
enum
{
READ_PRE,
READ_ESC,
CHECK_ID,
READ_PLAIN_ID,
READ_CURLY_ID,
READ_POST
}
state = READ_PRE;
// parse s
for(string::size_type i = 0; i < s.size(); i++)
{
// get current character
unsigned char uc = static_cast<unsigned char>(s[i]);
// READ_PRE
if (state == READ_PRE)
{
// escape sequence found
if (uc == '\\')
{
// don't replace escape sequences yet
a += uc;
// set next state
state = READ_ESC;
}
// ID started
else if (uc == '$')
{
// set next state
state = CHECK_ID;
}
// verbatim character found
else
{
// append character
a += uc;
}
}
// READ_ESC
else if (state == READ_ESC)
{
// append this character unparsed
a += uc;
// back to initial state
state = READ_PRE;
}
// CHECK_ID
else if (state == CHECK_ID)
{
// curly ID found
if (uc == '{')
{
// set next state
state = READ_CURLY_ID;
}
// plain ID found
else if ( (uc == '_')
|| ((uc >= 'A') && (uc <= 'Z'))
|| ((uc >= 'a') && (uc <= 'z'))
|| ((uc >= '0') && (uc <= '9')) )
{
// append character
id += uc;
// set next state
state = READ_PLAIN_ID;
}
// invalid syntax
else
{
// notify user
msg::err( msg::catq("invalid syntax: ", s) );
// signalize trouble
return false;
}
}
// READ_PLAIN_ID
else if (state == READ_PLAIN_ID)
{
// ID continued
if ( (uc == '_')
|| ((uc >= 'A') && (uc <= 'Z'))
|| ((uc >= 'a') && (uc <= 'z'))
|| ((uc >= '0') && (uc <= '9')) )
{
// append character
id += uc;
}
// ID finished
else
{
// append character
b += uc;
// set next state
state = READ_POST;
}
}
// READ_CURLY_ID
else if (state == READ_CURLY_ID)
{
// ID continued
if ( (uc == '_')
|| ((uc >= 'A') && (uc <= 'Z'))
|| ((uc >= 'a') && (uc <= 'z'))
|| ((uc >= '0') && (uc <= '9')) )
{
// append character
id += uc;
}
// ID finished
else if (uc == '}')
{
// set next state
state = READ_POST;
}
// invalid syntax
else
{
// notify user
msg::err( msg::catq("invalid syntax: ", s) );
// signalize trouble
return false;
}
}
// READ_POST
else if (state == READ_POST)
{
// don't parse any characters in this state
b += uc;
}
}
// check final state
if ( (state == READ_ESC)
|| (state == CHECK_ID)
|| (state == READ_CURLY_ID) )
{
// notify user
msg::err( msg::catq("invalid syntax: ", s) );
// signalize trouble
return false;
}
// signalize success
return true;
}
// --------
// getValue
// --------
/*
*
*/
string ReplaceHandler::getValue(const string& key) const
{
// search key
for(unsigned i = 0; i < m_keys.size(); i++)
{
// key found
if (m_keys[i] == key)
{
return m_values[i];
}
}
// key not found
return "";
}
// -------
// isFinal
// -------
/*
*
*/
bool ReplaceHandler::isFinal(const string& value) const
{
// parts of the given value
string a, id, b;
// split given value
if ( !split(value, a, id, b) )
{
// invalid syntax
return false;
}
// value is final
return id.empty();
}