]> git.saurik.com Git - wxWidgets.git/blob - samples/regextest/regextest.cpp
Changed main frame style to wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL
[wxWidgets.git] / samples / regextest / regextest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: regextest.cpp
3 // Purpose: Application to test regular expression (wxRegEx)
4 // Author: Ryan Norton
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) 2003 Ryan Norton <wxprojects@comcast.net>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 //===========================================================================
12 // REGEX LIBS
13 //===========================================================================
14
15 //wxWindows regular expression library
16 #define wxUSE_WXREGEX
17
18 //RN (Ryan Norton's) regular expression library
19 //#define wxUSE_RNWXRE
20
21 //GRETA, Microsoft Research's templated regex library
22 //[http://research.microsoft.com/projects/greta/]
23 //Install - Get it from .net powertools, put the directory in this directory
24 //#define wxUSE_GRETA
25
26 //PCRE (Perl Compatible Regular Expressions) [sourceforge.net/projects/pcre]
27 //Install - Get the GnuWin32 version and put the files in this directory
28 //#define wxUSE_PCRE
29
30 //===========================================================================
31 // HEADERS
32 //===========================================================================
33
34 // For compilers that support precompilation, includes "wx/wx.h".
35 #include "wx/wxprec.h"
36
37 #ifdef __BORLANDC__
38 #pragma hdrstop
39 #endif
40
41 // for all others, include the necessary headers (this file is usually all you
42 // need because it includes almost all "standard" wxWindows headers)
43 #ifndef WX_PRECOMP
44 #include "wx/wx.h"
45 #endif
46
47 #include "wx/file.h"
48
49 #ifdef wxUSE_WXREGEX
50 //
51 // This is the required header for wxRegEx
52 //
53 # include <wx/regex.h>
54 #endif
55
56 #ifdef wxUSE_RNWXRE
57 # include "re.h"
58 #endif
59
60 #ifdef wxUSE_GRETA
61 # ifdef _MSC_VER
62 # pragma warning(disable:4018)
63 # pragma warning(disable:4100)
64 # pragma warning(disable:4146)
65 # pragma warning(disable:4244)
66 # pragma warning(disable:4663)
67 extern "C" {
68 int __cdecl _resetstkoflw(void) {return 0;}
69 }
70 # endif //_MSC_VER
71 # include "greta/regexpr2.h"
72 using namespace regex;
73 #endif //wxUSE_GRETA
74
75 #ifdef wxUSE_PCRE
76 # include "pcre.h"
77 # ifdef _MSC_VER
78 # pragma comment(lib, "libpcre.a")
79 # endif
80 #endif
81
82 //===========================================================================
83 // IMPLEMENTATION
84 //===========================================================================
85
86 //
87 // One of the best sources for regex info is IEEE document/standard 1003.2
88 // From the open group.
89 // A current link - (http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap09.html).
90 //
91
92 //---------------------------------------------------------------------------
93 // MyDialog
94 //---------------------------------------------------------------------------
95
96 class MyDialog : public wxFrame
97 {
98 public:
99
100 //IDs for our controls
101 enum
102 {
103 PatternTextID,
104 SearchTextID,
105 OkButtonID
106 };
107
108 //Menu IDs
109 enum
110 {
111 ExtendedID,
112 ICaseID,
113 NewLineID,
114 NotBolID,
115 NotEolID,
116 CompID
117 };
118
119
120 MyDialog() : wxFrame( NULL, -1, _("regextest - wxRegEx Testing App"),
121 wxPoint(20,20), wxSize(300,400), wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL )
122 {
123 //Set the background to something light gray-ish
124 SetBackgroundColour(wxColour(150,150,150));
125
126 //
127 // Create the menus (Exit & About)
128 //
129 #if wxUSE_MENUS
130 wxMenu *FileMenu = new wxMenu;
131 OptionsMenu = new wxMenu;
132 wxMenu *HelpMenu = new wxMenu;
133
134 FileMenu->Append(wxID_EXIT, _T("&Exit"), _("Quit this program"));
135
136 OptionsMenu->AppendCheckItem(ExtendedID, _T("wxRE_EXTENDED"), _("Extended Regular Expressions?"));
137 OptionsMenu->AppendCheckItem(ICaseID, _T("wxRE_ICASE"), _("Enable case-insensitive matching?"));
138 OptionsMenu->AppendCheckItem(NewLineID, _T("wxRE_NEWLINE"), _("Treat \n as special?"));
139 OptionsMenu->AppendSeparator();
140 OptionsMenu->AppendCheckItem(NotBolID, _T("wxRE_NOTBOL"), _("Use functionality of ^ character?"));
141 OptionsMenu->AppendCheckItem(NotEolID, _T("wxRE_NOTEOL"), _("Use functionality of $ character?"));
142 OptionsMenu->AppendSeparator();
143 OptionsMenu->AppendSeparator();
144 OptionsMenu->AppendCheckItem(CompID, _T("Test Compile"), _("Added Compiling to Match Time?"));
145 OptionsMenu->Check(ExtendedID, true);
146
147 HelpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _("Show about dialog"));
148
149 wxMenuBar *MenuBar = new wxMenuBar();
150 MenuBar->Append(FileMenu, _T("&File"));
151 MenuBar->Append(OptionsMenu, _T("&Options"));
152 MenuBar->Append(HelpMenu, _T("&Help"));
153
154 SetMenuBar(MenuBar);
155 #endif // wxUSE_MENUS
156
157 // Text controls
158 PatternText.Create(this, PatternTextID, _(""), wxPoint(5, 30));
159 SearchText.Create(this, SearchTextID, _(""), wxPoint(5, 75));
160 //reset size of text controls
161 wxSize TextSize = PatternText.GetSize();
162 TextSize.SetWidth(200);
163 PatternText.SetSize(TextSize);
164 SearchText.SetSize(TextSize);
165
166 // StaticText
167 PatternHeader.Create(this, -1, _("Regular Expression:"), wxPoint(5, 10));
168 SearchHeader.Create(this, -1, _("String to Search:"), wxPoint(5, 55));
169 IterHeader.Create(this, -1, _("Iterations (Match Time):"), wxPoint(100, 100));
170
171 ResultText.Create(this, -1, _(""), wxPoint(5, 150), wxSize(100,200), wxST_NO_AUTORESIZE);
172 ResultText2.Create(this, -1, _(""), wxPoint(115, 150), wxSize(100,200), wxST_NO_AUTORESIZE);
173
174 // Button
175 OkButton.Create(this, OkButtonID, _("OK"), wxPoint(20, 120));
176
177 NumIters.Create(this, -1, _("15000"), wxPoint(100, 120));
178 }
179
180 void OnAbout(wxCommandEvent& WXUNUSED(evt))
181 {
182 wxChar* szBuffer;
183 wxFile f;
184
185 f.Open(_("README.txt"), wxFile::read);
186 szBuffer = new wxChar[f.Length() + 1];
187 f.Read(szBuffer, f.Length());
188 f.Close();
189
190 wxMessageBox(wxString::Format(
191 _("%s%s%s%s"),
192 _("-----------Regular Expression Test Application-----------\n"),
193 szBuffer,
194 _("\n\n\n(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
195 wxVERSION_STRING
196 )
197 );
198
199 delete szBuffer;
200 }
201
202 void OnMatch(wxCommandEvent& WXUNUSED(evt))
203 {
204 wxString szPattern = PatternText.GetValue();
205 wxString szSearch = SearchText.GetValue();
206 wxString szResult, szResult2, szResult3, szResult4; //Will be displayed in ResultText
207 wxString szStatus, szStatus2, szStatus3, szStatus4;
208
209 int nCompileFlags = 0, nCompileFlags2 = 0, nCompileFlags3 = 0, nCompileFlags4 = 0;
210 int nMatchFlags = 0, nMatchFlags2 = 0, nMatchFlags3 = 0, nMatchFlags4 = 0;
211
212 if (!(OptionsMenu->IsChecked(ExtendedID)))
213 {
214 #ifdef wxUSE_WXREGEX
215 nCompileFlags |= wxRE_BASIC;
216 #endif
217 }
218 else
219 {
220 #ifdef wxUSE_RNWXRE
221 nCompileFlags2 |= wxRe::wxRE_EXTENDED;
222 #endif
223 // nCompileFlags3 |= EXTENDED;
224 }
225
226 if (OptionsMenu->IsChecked(ICaseID))
227 {
228 #ifdef wxUSE_WXREGEX
229 nCompileFlags |= wxRE_ICASE;
230 #endif
231 #ifdef wxUSE_RNWXRE
232 nCompileFlags2 |= wxRe::wxRE_ICASE;
233 #endif
234 #ifdef wxUSE_GRETA
235 nCompileFlags3 |= NOCASE;
236 #endif
237 }
238
239 if (OptionsMenu->IsChecked(NewLineID))
240 {
241 #ifdef wxUSE_WXREGEX
242 nCompileFlags |= wxRE_NEWLINE;
243 #endif
244 #ifdef wxUSE_RNWXRE
245 nCompileFlags2 |= wxRe::wxRE_NEWLINE;
246 #endif
247 #ifdef wxUSE_GRETA
248 nCompileFlags3 |= MULTILINE;
249 #endif
250 }
251
252 if (OptionsMenu->IsChecked(NotBolID))
253 {
254 #ifdef wxUSE_WXREGEX
255 nMatchFlags |= wxRE_NOTBOL;
256 #endif
257 #ifdef wxUSE_RNWXRE
258 nMatchFlags2 |= wxRe::wxRE_NOTBOL;
259 #endif
260 }
261
262 if (OptionsMenu->IsChecked(NotEolID))
263 {
264 #ifdef wxUSE_WXREGEX
265 nMatchFlags |= wxRE_NOTEOL;
266 #endif
267 #ifdef wxUSE_RNWXRE
268 nMatchFlags2 |= wxRe::wxRE_NOTEOL;
269 #endif
270 }
271
272 //Our regular expression object
273
274 //Success! Here we'll display some
275 //information to the user
276 size_t dwStartIndex = 0, dwEndIndex = 0,
277 dwStartIndex2= 0, dwEndIndex2= 0,
278 dwStartIndex3= 0, dwEndIndex3= 0,
279 dwStartIndex4= 0, dwEndIndex4= 0;
280
281 time_t dwStartTime = 0, dwEndTime = 0,
282 dwStartTime2= 0, dwEndTime2= 0,
283 dwStartTime3= 0, dwEndTime3= 0,
284 dwStartTime4= 0, dwEndTime4= 0;
285
286 int i = 0;
287 long n;
288 if (!NumIters.GetValue().ToLong(&n))
289 n = 0;
290
291
292 #ifdef wxUSE_WXREGEX
293 wxRegEx Regex;
294 //Regular Expressions must be compiled before
295 //you can search a string with them, or
296 //at least most implementations do.
297 //(Boost and Microsoft have templated
298 //versions that don't require compilation)
299 //Basically compilation breaks it down into
300 //something that's easier to parse, due
301 //to the syntax of regular expressions
302 if (!Regex.Compile(szPattern, nCompileFlags))
303 szStatus += _("\nCompile Failed!\n");
304 else
305 {
306 //Here's where we actually search our string
307 if (!Regex.Matches(szSearch, nMatchFlags))
308 szStatus += _("\nExecution/Matching Failed!\n");
309 else
310 {
311 Regex.GetMatch(&dwStartIndex, &dwEndIndex);
312
313 szStatus = _("Success");
314
315 //We're going to go ahead and time the match
316 //for fun (clock() is a c library routine that
317 //returns the current time since a certian point
318 //in milliseconds
319 dwStartTime = clock();
320
321 if (OptionsMenu->IsChecked(CompID))
322 {
323 for(i = 0; i < n; ++i)
324 {
325 Regex.Compile(szPattern, nCompileFlags);
326 Regex.Matches(szSearch, nMatchFlags);
327 }
328 }
329 else
330 {
331 for(i = 0; i < n; ++i)
332 {
333 Regex.Matches(szSearch, nMatchFlags);
334 }
335 }
336
337 //This line gets the difference in time between now
338 //and when we first initialized dwStartTime.
339 dwEndTime = clock() - dwStartTime;
340 }
341 }
342
343
344 szResult = wxString::Format(
345 _("--wxRegEx--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
346 dwStartIndex, dwEndIndex+dwStartIndex,
347 szSearch.Mid(dwStartIndex, dwEndIndex),
348 dwEndTime,
349 szStatus
350 );
351
352 #endif //wxUSE_WXREGEX
353
354 #ifdef wxUSE_RNWXRE
355 wxRe Re;
356 wxRe::wxReError e;
357 if ((e = Re.Comp(szPattern, nCompileFlags2)) != wxRe::wxRE_OK)
358 szStatus2 = wxString::Format(_("\nCompile Failed!\n%s\n"), wxRe::ErrorToString(e));
359 else
360 {
361 //Here's where we actually search our string
362 if ((e = Re.Exec(szSearch, nMatchFlags2)) != wxRe::wxRE_OK)
363 szStatus2 = wxString::Format(_("\nExecution/Matching Failed!\n%s\n"), wxRe::ErrorToString(e));
364 else
365 {
366 dwStartIndex2 = Re.GetMatch(0).first;
367 dwEndIndex2 = Re.GetMatch(0).second;
368
369 szStatus2 = _("Success");
370
371 dwStartTime2 = clock();
372
373 if (OptionsMenu->IsChecked(CompID))
374 {
375 for(i = 0; i < n; ++i)
376 {
377 Re.Comp(szPattern, nCompileFlags2);
378 Re.Exec(szSearch, nMatchFlags2);
379 }
380 }
381 else
382 {
383 for(i = 0; i < n; ++i)
384 {
385 Re.Exec(szSearch, nMatchFlags2);
386 }
387 }
388
389 dwEndTime2 = clock() - dwStartTime2;
390 }
391 }
392 szResult2 = wxString::Format(
393 _("--Ryan's wxRe--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
394 dwStartIndex2, dwEndIndex2+dwStartIndex2,
395 szSearch.Mid(dwStartIndex2, dwEndIndex2),
396 dwEndTime2,
397 szStatus2
398 );
399 #endif //wxUSE_RNWXRE
400
401 #ifdef wxUSE_GRETA
402 std::string stdszPattern(szPattern);
403 rpattern Greta (stdszPattern,EXTENDED,MODE_MIXED);
404 match_results r;
405 std::string stdszSearch(szSearch);
406
407 //Here's where we actually search our string
408 if (!Greta.match(stdszSearch, r).matched)
409 szStatus3 += _("\nExecution/Matching Failed!\n");
410 else
411 {
412 szStatus3 = _("Success");
413
414 dwStartTime3 = clock();
415
416 if (OptionsMenu->IsChecked(CompID))
417 {
418 for(i = 0; i < n; ++i)
419 {
420 Greta = rpattern(stdszPattern,EXTENDED,MODE_MIXED);
421 Greta.match(stdszSearch, r);
422 }
423 }
424 else
425 {
426 for(i = 0; i < n; ++i)
427 {
428 Greta.match(stdszSearch, r);
429 }
430 }
431
432 dwEndTime3 = clock() - dwStartTime3;
433 }
434
435 szResult3 = wxString::Format(
436 _("--Greta--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
437 r.rstart(), r.rlength() + r.rstart(),
438 szSearch.Mid(r.rstart(), r.rlength()),
439 dwEndTime3,
440 szStatus3);
441 #endif //wxUSE_GRETA
442
443 #ifdef wxUSE_PCRE
444 pcre* pPcre;
445 const wxChar* szError;
446 int nErrOff;
447
448 if ((pPcre = pcre_compile(szPattern, nCompileFlags4, &szError, &nErrOff, 0)) == NULL)
449 szStatus4 = wxString::Format(_("\nCompile Failed!\nError:%s\nOffset:%i\n"), szError, nErrOff);
450 else
451 {
452 size_t msize;
453 pcre_fullinfo(pPcre, 0, PCRE_INFO_CAPTURECOUNT, &msize);
454 msize = 3*(msize+1);
455 int *m = new int[msize];
456
457 //Here's where we actually search our string
458 if (!pcre_exec(pPcre, 0, szSearch, szSearch.Length(), 0, 0, m, msize))
459 szStatus4 = wxString::Format(_("\nExecution/Matching Failed!\n"));
460 else
461 {
462 dwStartIndex4 = m[0];
463 dwEndIndex4 = m[1] - m[0];
464
465 szStatus4 = _("Success");
466
467 dwStartTime4 = clock();
468
469 if (OptionsMenu->IsChecked(CompID))
470 {
471 for(i = 0; i < n; ++i)
472 {
473 pPcre = pcre_compile(szPattern, nCompileFlags4, &szError, &nErrOff, 0);
474 pcre_exec(pPcre, 0, szSearch, szSearch.Length(), 0, 0, m, msize);
475 }
476 }
477 else
478 {
479 for(i = 0; i < n; ++i)
480 {
481 pcre_exec(pPcre, 0, szSearch, szSearch.Length(), 0, 0, m, msize);
482 }
483 }
484
485 dwEndTime4 = clock() - dwStartTime4;
486 }
487 }
488 szResult4 = wxString::Format(
489 _("--PCRE--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
490 dwStartIndex4, dwEndIndex4+dwStartIndex4,
491 szSearch.Mid(dwStartIndex4, dwEndIndex4),
492 dwEndTime4,
493 szStatus4
494 );
495 #endif //wxUSE_PCRE
496
497 ResultText.SetLabel(szResult + _("\n\n") + szResult2);
498 ResultText2.SetLabel(szResult3 + _("\n\n") + szResult4);
499 }
500
501 void OnQuit(wxCommandEvent& WXUNUSED(evt))
502 {
503 Close(TRUE);
504 }
505
506 wxTextCtrl PatternText, SearchText, NumIters;
507 wxStaticText PatternHeader, SearchHeader, IterHeader, ResultText, ResultText2;
508 wxButton OkButton;
509
510 wxMenu *OptionsMenu;
511
512 DECLARE_EVENT_TABLE()
513 };
514
515 BEGIN_EVENT_TABLE(MyDialog, wxFrame)
516
517 //menu
518 EVT_MENU(wxID_EXIT, MyDialog::OnQuit)
519 EVT_MENU(wxID_ABOUT, MyDialog::OnAbout)
520
521 //text
522 EVT_TEXT_ENTER(MyDialog::PatternTextID, MyDialog::OnMatch)
523 EVT_TEXT_ENTER(MyDialog::SearchTextID, MyDialog::OnMatch)
524
525 //button
526 EVT_BUTTON(MyDialog::OkButtonID, MyDialog::OnMatch)
527
528 END_EVENT_TABLE()
529
530 //---------------------------------------------------------------------------
531 // MyApp
532 //---------------------------------------------------------------------------
533
534 class MyApp : public wxApp
535 {
536 public:
537 bool OnInit()
538 {
539 MyDialog* dialog = new MyDialog();
540 dialog->Show();
541 return true;
542 }
543 };
544
545 IMPLEMENT_APP(MyApp)
546