-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathStringTemplate.h
436 lines (413 loc) · 10.8 KB
/
StringTemplate.h
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
#ifndef _STRINGTEMPLATE_H_
#define _STRINGTEMPLATE_H_
#include <string>
#include <vector>
#include <map>
#include <stdio.h>
/**
* For ANNO, support keywords are:
* TOP_GENE_NAME1
* TOP_GENE_NAME
* TOP_TYPE
* GENE_NAME can be vector
* GENE_STRAND ..
* TYPE
* TYPE1
*/
/**
* Specify a template format
* e.g.
* int n;
* std::string result;
* StringTemplate s("Hello $(NAME).");
* s.add("NAME", "world");
* s.translate(&result);
* printf("%s\n", result.c_str());
*
* Output:
* Hello world.
*/
class StringTemplate{
public:
struct KEY;
struct VALUE;
class Array {
public:
int translate(std::string* str, const std::map<std::string, VALUE>& dict) const;
/**
* Parse s[beg...end] beg: inclusive end: exclusive
*/
int parse(const char* s, int param_beg, int param_end);
void clear() {
this->data.clear();
this->dict.clear();
this->delim.clear();
};
private:
std::vector<KEY> data;
std::map<std::string, VALUE> dict; // store key->value pair
std::string delim;
};
typedef enum {
UNDEFINED_KEY = 0,
TEXT = 1,
KEYWORD = 2,
ARRAY = 3 // will be expanded recursively
} KEY_TYPE;
typedef enum {
UNDEFINED_TYPE = 0,
STRING = 1,
STRING_ARRAY = 2
} VALUE_TYPE;
struct VALUE {
VALUE_TYPE type;
std::string string;
std::vector<std::string> array;
} ;
struct KEY{
KEY_TYPE type;
std::string text;
std::string keyword;
Array array;
void clear() {
type = UNDEFINED_KEY;
text.clear();
keyword.clear();
array.clear();
}
};
explicit StringTemplate() {};
explicit StringTemplate(const char* s) {
this->parse(s);
};
explicit StringTemplate(const std::string& s) {
this->parse(s);
};
private:
// forbid two default copy-constructor
StringTemplate(const StringTemplate& s) {
if (this != &s){
this->data = s.data;
this->dict = s.dict;
}
};
StringTemplate& operator= (const StringTemplate& s) {
this->clear();
this->data = s.data;
this->dict = s.dict;
return (*this);
};
public:
StringTemplate& operator= (const char* s) {
this->clear();
this->parse(s);
return *this;
};
StringTemplate& operator= (const std::string& s) {
this->clear();
this->parse(s);
return *this;
};
std::vector<KEY> data;
std::map<std::string, VALUE> dict; // store key->value pair
static bool isValidKeyword(const char c){
if (!isalnum(c) && c != '_')
return false;
return true;
};
static bool isValidKeyword(const char* s){
while ( *s != '\0') {
if (! isValidKeyword(*s))
return false;
s++;
}
return true;
}
void add(const char* key, const char* value) {
VALUE v;
v.type = STRING;
v.string = value;
this->dict[key] = v;
};
void add(const char* key, const std::string& value) {
this->add(key, value.c_str());
};
void add(const char* key, const std::vector<std::string>& value) {
VALUE v;
v.type = STRING_ARRAY;
v.array = value;
this->dict[key] = v;
};
/**
* @param str: store translated result in str
* @return 0: if success;
* when translation is unsuccessful, the @param str is not reliable.
*/
int translate(std::string* str) const {
std::string& s = *str;
s.clear();
for (unsigned int i = 0; i < data.size(); i++) {
const KEY& k = data[i];
switch(k.type) {
case TEXT:
s += k.text;
break;
case KEYWORD:
if (dict.find(k.keyword) == dict.end()) {
fprintf(stderr, "Data translation error for key %s!\n", k.keyword.c_str());
} else {
std::map<std::string, VALUE>::const_iterator iter;
iter = dict.find(k.keyword);
const VALUE& v = iter->second;
if (v.type == STRING) {
s += v.string;
} else {
if (v.array.size() > 0) {
s += v.array[0];
}
};
};
break;
case ARRAY:
{
int ret = k.array.translate(str, this->dict);
if (ret) {
return ret;
};
}
break;
case UNDEFINED_KEY:
fprintf(stderr, "UNDEFINED_KEY not handled!\n");
break;
};
}
return 0;
};
/**
* Split template into chunks of 3 types: text, keyword, array
* @return 0: when parse succeeds
*/
int parse(const std::string& s) {
return this->parse(s.c_str());
};
int parse(const char* s) {
int beg = 0; // mark the begin of array
int end = 0;
KEY key;
key.type = TEXT;
while (true) {
if (s[end] == '\0') {
this->data.push_back(key);
key.clear();
/* if (mode != 0) { */
/* fprintf(stderr, "Malformated data %s!", s); */
/* return -1; */
/* } */
break;
}
switch (key.type) {
case TEXT:
if (s[end] == '$' && (end == 0 || s[end-1] != '\\')) { // change mode
if (key.text.size()) {
key.type = TEXT;
this->data.push_back(key);
}
key.clear();
if (s[end + 1] == '[') {
beg = end + 2;
key.type = ARRAY;
} else if (s[end + 1] == '(') {
beg = end + 2;
key.type = KEYWORD;
} else {
fprintf(stderr, "Wrong usage of $ in %s!\n", s);
return -1;
};
end ++ ;
} else {
key.text.push_back(s[end]);
};
break;
case KEYWORD:
if (s[end] == ')' && s[end-1] != '\\'){ // change mode
key.type = KEYWORD;
key.keyword = std::string(s+beg, s+end);
if (key.keyword.size() == 0) {
fprintf(stderr, "Empty keyword in %s!\n", s);
return -1;
};
this->data.push_back(key);
if (!isValidKeyword(key.keyword.c_str())) {
fprintf(stderr, "Wrong keyword %s!\n", s);
}
key.clear();
key.type = TEXT;
} else {
if ( !isValidKeyword(s[end]) ) {
fprintf(stderr, "Wrong keyword name: %c in %s\n", s[end], s);
return -1;
}
};
break;
case ARRAY:
if (s[end] == ']' && s[end-1] != '\\') { // change mode
if (key.array.parse(s, beg, end) != 0) {
fprintf(stderr, "Cannot parse this part: %s!\n", s+beg);
}
this->data.push_back(key);
key.clear();
key.type = TEXT;
} else {
}
break;
case UNDEFINED_KEY:
fprintf(stderr, "UNDEFINED_KEY not handled!\n");
break;
}
end ++ ;
};
return 0;
};
void clear() {
this->data.clear();
this->dict.clear();
}
};
int StringTemplate::Array::translate(std::string* str, const std::map<std::string, VALUE>& dict) const{
std::string& s = *str;
std::map<std::string, VALUE>::const_iterator iter;
size_t idx = 0;
int maxStringArraySize = -1; // less than zero for initialization
do {
if (idx)
s += this->delim;
for (unsigned int i = 0; i < data.size(); i++) {
const KEY& k = data[i];
switch(k.type) {
case TEXT:
s += k.text;
break;
case KEYWORD:
iter = dict.find(k.keyword);
if (iter == dict.end()) {
fprintf(stderr, "Data translation error: not found key %s!\n", k.keyword.c_str());
} else {
const VALUE& v = iter->second;
if (v.type == STRING) {
s += v.string;
} else {
if (maxStringArraySize < 0) {
maxStringArraySize = v.array.size();
} else {
if (v.array.size() != (size_t) maxStringArraySize) {
fprintf(stderr, "Unbalanced vector size. Stopped when tranlating %s!\n", k.keyword.c_str());
}
}
if (idx < v.array.size()) {
s += v.array[idx];
}
};
};
break;
case ARRAY:
{
int ret = k.array.translate(str, this->dict);
if (ret) {
return ret;
};
}
break;
case UNDEFINED_KEY:
fprintf(stderr, "UNDEFINED_KEY not handled!\n");
break;
};
}
idx ++;
} while (maxStringArraySize >= 0 && idx < (size_t)maxStringArraySize);
return 0;
};
int StringTemplate::Array::parse(const char* s, int param_beg, int param_end){
int real_beg = param_beg;
int real_end = param_end;
for (int i = param_end; i >= param_beg; i--){
if (s[i] == ' ') {
this->delim = std::string(s + i + 1, s + param_end);
real_end = i;
break;
}
};
if (real_beg == real_end) {
fprintf(stderr, "Cannot parse array: %s", s+real_beg);
return -1;
};
int beg = real_beg;
int end = real_beg;
KEY* pKey = new KEY;
KEY& key = *pKey;
key.type = TEXT;
while (true) {
if (end == real_end) {
this->data.push_back(key);
key.clear();
break;
}
switch (key.type) {
case TEXT:
if (s[end] == '$' && s[end-1] != '\\') { // change mode
if (key.text.size()) {
key.type = TEXT;
this->data.push_back(key);
}
key.clear();
if (s[end + 1] == '[') {
beg = end + 2;
key.type = ARRAY;
} else if (s[end + 1] == '(') {
beg = end + 2;
key.type = KEYWORD;
} else {
fprintf(stderr, "Wrong usage of $ in %s!\n", s);
return -1;
};
end ++ ;
} else {
key.text.push_back(s[end]);
};
break;
case KEYWORD:
if (s[end] == ')' && s[end-1] != '\\'){ // change mode
key.type = KEYWORD;
key.keyword = std::string(s+beg, s+end);
this->data.push_back(key);
key.clear();
key.type = TEXT;
if (!isValidKeyword(key.keyword.c_str())) {
fprintf(stderr, "Wrong keyword %s!\n", s);
}
} else {
if ( !isValidKeyword(s[end]) ) {
fprintf(stderr, "Wrong keyword name: %c in %s", s[end], s);
return -1;
}
};
break;
case ARRAY:
if (s[end] == ']' && s[end-1] != '\\') { // change mode
this->data.push_back(key);
key.clear();
key.type = TEXT;
if (key.array.parse(s, beg, end) != 0) {
fprintf(stderr, "Cannot parse this part: %s!\n", s+beg);
}
} else {
}
break;
case UNDEFINED_KEY:
fprintf(stderr, "UNDEFINED_KEY not handled!\n");
break;
}
end ++ ;
};
return 0;
};
#endif /* _STRINGTEMPLATE_H_ */