Debian Codegraph @

samples/codelite-sample/src/pptable.cpp

raw symbols SCIP
1 #include "pptable.h"
2 #include <wx/tokenzr.h>
3 #include <set>
4
5 bool IsWordChar(const wxString &s, int strSize)
6 {
7 if(strSize) {
8 return s.find_first_of(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_")) != wxString::npos;
9
10 } else {
11 return s.find_first_of(wxT("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) != wxString::npos;
12 }
13 }
14
15 bool IsWordCharA(char c, int strSize)
16 {
17 if(strSize) {
18 return ((c >= 97 && c <= 122) ||// a-z
19 (c >= 65 && c <= 90) ||// A-Z
20 (c >= 48 && c <= 57) ||// 0-9
21 (c == '_'));
22
23 } else {
24 return ((c >= 97 && c <= 122) ||// a-z
25 (c >= 65 && c <= 90) ||// A-Z
26 (c == '_'));
27 }
28 }
29
30 std::string ReplaceWordA(const std::string &str, const std::string &word, const std::string &replaceWith)
31 {
32 char currChar;
33 char nextChar;
34 std::string currentWord;
35 std::string output;
36
37 output.reserve( str.length() * 2 );
38
39 for(size_t i=0; i<str.length(); i++) {
40 // Look ahead
41 if( str.length() > i + 1 ) {
42 nextChar = str[i+1];
43 } else {
44 // we are at the end of buffer
45 nextChar = '\0';
46 }
47
48 currChar = str[i];
49 if(!IsWordCharA( currChar, currentWord.length() )) {
50 output += str[i];
51 currentWord.clear();
52
53 } else {
54
55 currentWord += currChar;
56 if(IsWordCharA(nextChar, currentWord.length())) {
57 // do nothing
58
59 } else if( !IsWordCharA(nextChar, currentWord.length()) && currentWord == word ) {
60 output += replaceWith;
61 currentWord.clear();
62
63 } else {
64 output += currentWord;
65 currentWord.clear();
66 }
67
68 }
69 }
70 return output;
71 }
72
73 // Helper string find metho
74 wxString ReplaceWord(const wxString &str, const wxString &word, const wxString &replaceWith)
75 {
76 wxString currChar;
77 wxString nextChar;
78 wxString currentWord;
79 wxString output;
80 for(size_t i=0; i<str.Length(); i++) {
81 // Look ahead
82 if( str.Length() > i + 1 ) {
83 nextChar = str[i+1];
84 } else {
85 // we are at the end of buffer
86 nextChar = wxT('\0');
87 }
88
89 currChar = str[i];
90 if(!IsWordChar( currChar, currentWord.Length() )) {
91 output << str[i];
92 currentWord.Clear();
93
94 } else {
95
96 currentWord << currChar;
97 if(IsWordChar(nextChar, currentWord.Length())) {
98 // do nothing
99
100 } else if( !IsWordChar(nextChar, currentWord.Length()) && currentWord == word ) {
101 output << replaceWith;
102 currentWord.Clear();
103
104 } else {
105 output << currentWord;
106 currentWord.Clear();
107 }
108
109 }
110 }
111 return output;
112 }
113
114 wxArrayString TokenizeWords(const wxString &str)
115 {
116 wxString currChar;
117 wxString nextChar;
118 wxString currentWord;
119 wxArrayString outputArr;
120
121 wxString::const_iterator iter = str.begin();
122 for(; iter != str.end(); iter++) {
123 // Look ahead
124 if( (iter + 1) != str.end() ) {
125 nextChar = *(iter+1);
126 } else {
127 // we are at the end of buffer
128 nextChar = wxT('\0');
129 }
130
131 currChar = *iter;
132 if(!IsWordChar( currChar, currentWord.Length() )) {
133 currentWord.Clear();
134
135 } else {
136
137 currentWord << currChar;
138 if(IsWordChar(nextChar, currentWord.Length())) {
139 // do nothing
140
141 } else {
142
143 outputArr.Add(currentWord);
144 currentWord.Clear();
145 }
146
147 }
148 }
149 return outputArr;
150 }
151
152 static PPTable* ms_instance = NULL;
153
154 void PPToken::processArgs(const wxString &argsList)
155 {
156 args = wxStringTokenize(argsList, wxT(","), wxTOKEN_STRTOK);
157
158 // replace all occurrences of 'arg' with %1, %2 etc
159 for(size_t i=0; i<args.GetCount(); i++) {
160 wxString replaceWith = wxString::Format(wxT("%%%d"), (int)i);
161 #if wxVERSION_NUMBER < 2900
162 replacement = ReplaceWord(replacement, args.Item(i), replaceWith);
163 #else
164 std::string res = ReplaceWordA(replacement.To8BitData().data(), args.Item(i).To8BitData().data(), replaceWith.To8BitData().data());
165 if(res.empty()) {
166 replacement.clear();
167
168 } else {
169 replacement = wxString::From8BitData(res.c_str());
170
171 }
172 #endif
173 }
174 }
175
176 void PPToken::print(wxFFile &fp)
177 {
178 #if 0
179 wxString buff;
180 buff << wxT("Name : ") << name << wxT("\n");
181 buff << wxT("replacement : ") << replacement << wxT("\n");
182 buff << wxT("isFunctionLike: ") << isFunctionLike << wxT("\n");
183 if(isFunctionLike) {
184 for(size_t i=0; i<args.size(); i++) {
185 buff << wxT("Arg: ") << args.Item(i) << wxT("\n");
186 }
187 }
188 buff << wxT(" ---- \n");
189 fp.Write(buff);
190 #else
191 wxString buff;
192 buff << name << wxT("(") << (flags & IsFunctionLike) << wxT(")") << wxT("=") << replacement << wxT("\n");
193 fp.Write(buff);
194 #endif
195 }
196
197 wxString PPToken::fullname() const
198 {
199 wxString fullname;
200 fullname << name;
201 if(flags & IsFunctionLike) {
202 fullname << wxT("(");
203 for(size_t i=0; i<args.size(); i++) {
204 fullname << wxT("%") << i << wxT(",");
205 }
206 if(args.size()) {
207 fullname.RemoveLast();
208 }
209 fullname << wxT(")");
210 }
211 return fullname;
212 }
213
214 void PPToken::squeeze()
215 {
216 std::set<wxString> alreadyReplacedMacros;
217
218 // perform the squeeze 5 times max
219 for(size_t count=0; count < 5; count++) {
220 bool modified(false);
221
222 // get list of possible macros in the replacement
223 wxArrayString tmpWords = TokenizeWords(replacement);
224 wxArrayString words;
225
226 // make sure that a word is not been replaced more than once
227 // this will avoid recursion
228 // an example (taken from qglobal.h of the Qt library):
229 //
230 // #define qDebug QT_NO_QDEBUG_MACRO
231 // #define QT_NO_QDEBUG_MACRO if(1); else qDebug
232 //
233 for(size_t i=0; i<tmpWords.size(); i++) {
234 if(alreadyReplacedMacros.find(tmpWords.Item(i)) == alreadyReplacedMacros.end()){
235 alreadyReplacedMacros.insert(tmpWords[i]);
236 words.Add(tmpWords[i]);
237 }
238 }
239
240 for(size_t i=0; i<words.size(); i++) {
241 PPToken tok = PPTable::Instance()->Token(words.Item(i));
242 if(tok.flags & IsValid) {
243 if(tok.flags & IsFunctionLike) {
244 int where = replacement.Find(words.Item(i));
245 if(where != wxNOT_FOUND) {
246 wxString initList;
247 wxArrayString initListArr;
248 if(readInitList( replacement, where + words.Item(i).Length(), initList, initListArr )) {
249 tok.expandOnce(initListArr);
250
251 replacement.Remove(where, words.Item(i).Length() + initList.Length());
252 tok.replacement.Replace(wxT("##"), wxT(""));
253 replacement.insert(where, tok.replacement);
254 modified = true;
255 }
256 }
257
258 } else {
259 if(replacement.Replace(words.Item(i), tok.replacement)) {
260 modified = true;
261 }
262 }
263 }
264 }
265
266 if(!modified)
267 break;
268 }
269 replacement.Replace(wxT("##"), wxT(""));
270 }
271
272 bool PPToken::readInitList(const std::string& in, size_t from, std::string& initList, std::vector<std::string>& initListArr)
273 {
274 if(in.length() < from) {
275 return false;
276 }
277
278 std::string tmpString = in.substr(from);
279 size_t start = tmpString.find('(');
280 if(start == std::string::npos ) {
281 return false;
282 }
283
284 // skip the open brace
285 tmpString = tmpString.substr(start+1);
286
287 for(size_t i=0; i<start; i++) {
288 initList += " ";
289 }
290
291 initList += "(";
292 std::string word;
293 int depth(1);
294
295 for(size_t i=0; i<tmpString.length(); i++) {
296 char ch = tmpString[i];
297 initList += ch;
298 switch(ch) {
299 case ')':
300 depth --;
301 if(depth == 0) {
302 initListArr.push_back(word);
303 return true;
304 } else {
305 word += ch;
306 }
307 break;
308 case '(':
309 depth++;
310 word += ch;
311 break;
312 case ',':
313 if(depth == 1) {
314 initListArr.push_back(word);
315 word.clear();
316 } else {
317 word += ch;
318 }
319 break;
320 default:
321 word += ch;
322 break;
323 }
324 }
325 return false;
326 }
327
328 bool PPToken::readInitList(const wxString &in, int from, wxString& initList, wxArrayString& initListArr)
329 {
330 if((int)in.Length() < from) {
331 return false;
332 }
333
334 wxString tmpString = in.Mid(from);
335 int start = tmpString.Find(wxT("("));
336 if(start == wxNOT_FOUND ) {
337 return false;
338 }
339 tmpString = tmpString.Mid(start+1);
340
341 for(size_t i=0; i<(size_t)start; i++) {
342 initList << wxT(" ");
343 }
344
345 initList << wxT("(");
346 wxString word;
347 int depth(1);
348
349 for(size_t i=0; i<tmpString.Length(); i++) {
350 wxChar ch = tmpString[i];
351 initList << ch;
352 switch(ch) {
353 case wxT(')'):
354 depth --;
355 if(depth == 0) {
356 initListArr.Add(word);
357 return true;
358 } else {
359 word << ch;
360 }
361 break;
362 case wxT('('):
363 depth++;
364 word << ch;
365 break;
366 case wxT(','):
367 if(depth == 1) {
368 initListArr.Add(word);
369 word.Clear();
370 } else {
371 word << ch;
372 }
373 break;
374 default:
375 word << ch;
376 break;
377 }
378 }
379 return false;
380 }
381
382 void PPToken::expandOnce(const wxArrayString& initList)
383 {
384 if(initList.size() != args.size())
385 return;
386
387 for(size_t i=0; i<args.size(); i++) {
388 wxString placeHolder;
389 placeHolder << wxT("%") << i;
390
391 wxString replaceWith = initList.Item(i);
392 replaceWith.Trim().Trim(false);
393
394 if(replaceWith.Contains(placeHolder))
395 continue;
396
397 replacement.Replace(placeHolder, initList.Item(i));
398 }
399 }
400
401 wxString PPToken::signature() const
402 {
403 wxString sig;
404 if(flags & IsFunctionLike) {
405 sig << wxT("(");
406 for(size_t i=0; i<args.size(); i++) {
407 sig << wxT("%") << i << wxT(",");
408 }
409 if(args.size()) {
410 sig.RemoveLast();
411 }
412 sig << wxT(")");
413 }
414 return sig;
415 }
416
417 ///////////////////////////////////////////////////
418
419 PPTable::PPTable()
420 {
421 }
422
423 PPTable::~PPTable()
424 {
425 }
426
427 PPTable* PPTable::Instance()
428 {
429 if(ms_instance == NULL) {
430 ms_instance = new PPTable();
431 }
432 return ms_instance;
433 }
434
435 void PPTable::Release()
436 {
437 if(ms_instance) {
438 delete ms_instance;
439 }
440 ms_instance = NULL;
441 }
442
443 PPToken PPTable::Token(const wxString& name)
444 {
445 std::map<wxString, PPToken>::iterator iter = m_table.find(name);
446 if(iter == m_table.end()) {
447 return PPToken();
448 }
449
450 return iter->second;
451 }
452
453 void PPTable::Add(const PPToken& token)
454 {
455 if(token.name.IsEmpty())
456 return;
457
458 wxString name = token.name;
459 name.Trim().Trim(false);
460 std::map<wxString, PPToken>::iterator iter = m_table.find(name);
461 if(iter == m_table.end())
462 m_table[name] = token;
463 else {
464 // if the new token's replacement is empty and the current one is NOT empty,
465 // replace the two (we prefer empty replacements)
466 if(iter->second.flags & PPToken::IsOverridable && !iter->second.replacement.IsEmpty() && token.replacement.IsEmpty()) {
467 m_table[name] = token;
468 }
469 }
470 }
471
472 void PPTable::AddUsed(const wxString& name)
473 {
474 if(name.IsEmpty()) {
475 return;
476 }
477 m_namesUsed.insert(name);
478 }
479
480 void PPTable::Print(wxFFile &fp)
481 {
482 std::map<wxString, PPToken>::iterator iter = m_table.begin();
483 for(; iter != m_table.end(); iter++) {
484 iter->second.print(fp);
485 }
486 }
487
488 bool PPTable::Contains(const wxString& name)
489 {
490 std::map<wxString, PPToken>::iterator iter = m_table.find(name);
491 return iter != m_table.end();
492 }
493
494 wxString PPTable::Export()
495 {
496 wxString table;
497 std::map<wxString, PPToken>::iterator iter = m_table.begin();
498 for(; iter != m_table.end(); iter++) {
499 iter->second.squeeze();
500 wxString replacement = iter->second.replacement;
501 replacement.Trim().Trim(false);
502
503 // remove extra whitespaces
504 while(replacement.Replace(wxT(" "), wxT(" "))){}
505
506 if(replacement.IsEmpty()) {
507 table << iter->second.fullname() << wxT("\n");
508
509 } else if(iter->second.flags & PPToken::IsFunctionLike) {
510 table << iter->second.fullname() << wxT("=") << replacement << wxT("\n");
511
512 } else {
513 // macros with replacement but they are not in a form of a function
514 // we take only macros that their replacement is not a number
515 long v(-1);
516 if(!replacement.ToLong(&v) && !replacement.ToLong(&v, 8) && !replacement.ToLong(&v, 16) && replacement.find(wxT('"')) == wxString::npos && !replacement.StartsWith(wxT("0x"))) {
517 table << iter->second.fullname() << wxT("=") << replacement << wxT("\n");
518 }
519 }
520 }
521 return table;
522 }
523
524 void PPTable::Squeeze()
525 {
526 std::map<wxString, PPToken>::iterator iter = m_table.begin();
527 for(; iter != m_table.end(); iter++) {
528 m_table[iter->first].squeeze();
529 }
530 }
531
532 void PPTable::Clear()
533 {
534 m_table.clear();
535 }
536
537 void PPTable::ClearNamesUsed()
538 {
539 m_namesUsed.clear();
540 }
541
542 bool CLReplacePattern(const wxString& in, const wxString& pattern, const wxString& replaceWith, wxString &outStr)
543 {
544 int where = pattern.Find(wxT("%0"));
545 if(where != wxNOT_FOUND) {
546 wxString replacement(replaceWith);
547
548 // a patterened expression
549 wxString searchFor = pattern.BeforeFirst(wxT('('));
550 where = in.Find(searchFor);
551 if(where == wxNOT_FOUND) {
552 return false;
553 }
554
555 wxString initList;
556 wxArrayString initListArr;
557 if(PPToken::readInitList(in, searchFor.Length() + where, initList, initListArr) == false)
558 return false;
559
560 outStr = in;
561 // update the 'replacement' with the actual values ( replace %0..%n)
562 for(size_t i=0; i<initListArr.size(); i++) {
563 wxString placeHolder;
564 placeHolder << wxT("%") << i;
565 replacement.Replace(placeHolder, initListArr.Item(i));
566 }
567
568 outStr.Remove(where, searchFor.Length() + initList.Length());
569 outStr.insert(where, replacement);
570 return true;
571
572 } else {
573 if(in.Find(pattern) == wxNOT_FOUND) {
574 return false;
575 }
576 // simple replacement
577 outStr = ReplaceWord(in, pattern, replaceWith);
578 return outStr != in;
579 }
580 }
581
582 std::string replacement;
583
584 bool CLReplacePatternA(const std::string& in, const CLReplacement& repl, std::string& outStr)
585 {
586 if(repl.is_compound) {
587 size_t where = in.find(repl.searchFor);
588 if(where == std::string::npos)
589 return false;
590
591 std::string initList;
592 std::vector<std::string> initListArr;
593 if(PPToken::readInitList(in, repl.searchFor.length() + where, initList, initListArr) == false)
594 return false;
595
596 // update the 'replacement' with the actual values ( replace %0..%n)
597 replacement = repl.replaceWith;
598 char placeHolder[4];
599 for(size_t i=0; i<initListArr.size(); i++) {
600
601 memset(placeHolder, 0, sizeof(placeHolder));
602 sprintf(placeHolder, "%%%d", (int)i);
603
604 size_t pos = replacement.find(placeHolder);
605 const std::string& init = initListArr[i];
606 while( pos != std::string::npos ) {
607 replacement.replace(pos, strlen(placeHolder), init.c_str());
608
609 // search for the next match
610 pos = replacement.find(placeHolder, pos + 1);
611 }
612 }
613
614 outStr = in;
615 where = outStr.find(repl.searchFor);
616 if(where == std::string::npos)
617 return false;
618
619 outStr.replace(where, repl.searchFor.length() + initList.length(), replacement);
620 return true;
621
622 } else {
623
624 size_t where = in.find(repl.searchFor);
625 if(where == std::string::npos) {
626 return false;
627 }
628
629 outStr = ReplaceWordA(in, repl.searchFor, repl.replaceWith);
630
631 // outStr = in;
632 // outStr.replace(where, repl.searchFor.length(), repl.replaceWith);
633
634 // simple replacement
635 return outStr != in;
636 }
637 }
638
639 void CLReplacement::construct(const std::string& pattern, const std::string& replacement)
640 {
641 is_ok = true;
642 full_pattern = pattern;
643 is_compound = full_pattern.find("%0") != std::string::npos;
644 if(is_compound) {
645 // a patterened expression
646 replaceWith = replacement;
647 size_t where = pattern.find('(');
648 if(where == std::string::npos) {
649 is_ok = false;
650 return;
651 }
652
653 searchFor = pattern.substr(0, where);
654 if(searchFor.empty()){
655 is_ok = false;
656 return;
657 }
658
659 } else {
660 // simple Key=Value pair
661 replaceWith = replacement;
662 searchFor = full_pattern;
663 }
664 }
665