]> git.saurik.com Git - wxWidgets.git/blob - samples/regextest/regextest.cpp
84e9ce7fc36a62cc1f13d198a0324dd993f1637e
[wxWidgets.git] / samples / regextest / regextest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: regextest.cpp
3 // Purpose: Test regex libs and some gui components
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 // HEADERS
13 //===========================================================================
14
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 // for all others, include the necessary headers (this file is usually all you
23 // need because it includes almost all "standard" wxWindows headers)
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/file.h"
29
30 #if !wxUSE_REGEX
31 # error wxUSE_REGEX needs to be enabled in setup.h to use wxRegEx
32 #endif
33
34 //
35 // This is the required header for wxRegEx
36 //
37 #include <wx/regex.h>
38
39 //===========================================================================
40 // IMPLEMENTATION
41 //===========================================================================
42
43 //
44 // One of the best sources for regex info is IEEE document/standard 1003.2
45 // From the open group.
46 // A current link - (http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap09.html).
47 //
48
49 //---------------------------------------------------------------------------
50 // MyFrame
51 //---------------------------------------------------------------------------
52
53 class MyFrame : public wxFrame
54 {
55 public:
56
57 //IDs for our controls
58 enum
59 {
60 PatternTextID,
61 SearchTextID,
62 OkButtonID
63 };
64
65 //Menu IDs
66 enum
67 {
68 ExtendedID,
69 ICaseID,
70 NewLineID,
71 NotBolID,
72 NotEolID,
73 CompID,
74 MatchID
75 };
76
77
78 //
79 // Adds an item to a menu (long way, easier to do wxMenu::AppendCheckItem())
80 //
81 void AddMenuItem(wxMenu* pMenu, int nID = wxID_SEPARATOR, const wxChar* szTitle = _(""),
82 const wxChar* szToolTip = _(""))
83 {
84 wxMenuItem* pItem;
85
86 if (nID == wxID_SEPARATOR)
87 pItem = new wxMenuItem (NULL, wxID_SEPARATOR, szTitle, szToolTip, wxITEM_SEPARATOR);
88 else
89 pItem = new wxMenuItem (NULL, nID, szTitle, szToolTip, wxITEM_CHECK);
90
91 #ifdef __WXMSW__
92 pItem->SetBackgroundColour(wxColour(115, 113, 115));
93 pItem->SetTextColour(*wxBLACK);
94 #endif
95 pMenu->Append(pItem);
96 }
97
98 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
99 void OnContextMenu(wxContextMenuEvent& event)
100 { PopupMenu(GetMenuBar()->GetMenu(1), ScreenToClient(event.GetPosition())); }
101 #else
102 void OnRightUp(wxMouseEvent& event)
103 { PopupMenu(GetMenuBar()->GetMenu(1), event.GetPosition()); }
104 #endif
105
106 MyFrame() : wxFrame( NULL, -1, _("regextest - wxRegEx Testing App"),
107 wxPoint(20,20), wxSize(300,400), wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL )
108 {
109 //Set the background to something light gray-ish
110 SetBackgroundColour(wxColour(150,150,150));
111
112 //
113 // Create the menus
114 //
115 #if wxUSE_MENUS
116
117 wxMenu *FileMenu = new wxMenu;
118 wxMenu *OptionsMenu = new wxMenu;
119 wxMenu *HelpMenu = new wxMenu;
120
121 AddMenuItem(FileMenu, wxID_EXIT, _("&Exit"), _("Quit this program"));
122
123 AddMenuItem(OptionsMenu, ExtendedID, _T("wxRE_EXTENDED"), _("Extended Regular Expressions?"));
124 AddMenuItem(OptionsMenu, ICaseID, _T("wxRE_ICASE"), _("Enable case-insensitive matching?"));
125 AddMenuItem(OptionsMenu, NewLineID, _T("wxRE_NEWLINE"), _("Treat \n as special?"));
126 AddMenuItem(OptionsMenu);
127 AddMenuItem(OptionsMenu, NotBolID, _T("wxRE_NOTBOL"), _("Use functionality of ^ character?"));
128 AddMenuItem(OptionsMenu, NotEolID, _T("wxRE_NOTEOL"), _("Use functionality of $ character?"));
129 AddMenuItem(OptionsMenu);
130 AddMenuItem(OptionsMenu);
131 AddMenuItem(OptionsMenu, CompID, _("Test Compile"), _("Added Compiling to Match Time?"));
132 AddMenuItem(OptionsMenu, MatchID, _("Test Match"), _("Added Matching to Match Time?"));
133
134 AddMenuItem(HelpMenu, wxID_ABOUT, _T("&About...\tF1"), _("Show about dialog"));
135
136 OptionsMenu->Check(ExtendedID, true);
137
138 OptionsMenu->Check(CompID, true);
139 OptionsMenu->Check(MatchID, true);
140
141 wxMenuBar *MenuBar = new wxMenuBar();
142 MenuBar->Append(FileMenu, _T("&File"));
143 MenuBar->Append(OptionsMenu, _T("&Options"));
144 MenuBar->Append(HelpMenu, _T("&Help"));
145
146 SetMenuBar(MenuBar);
147 #endif // wxUSE_MENUS
148
149 // Text controls
150 PatternText.Create(this, PatternTextID, _(""), wxPoint(5, 30));
151 SearchText.Create(this, SearchTextID, _(""), wxPoint(5, 75), PatternText.GetSize(), wxTE_MULTILINE);
152 NumIters.Create(this, -1, _("5000"), wxPoint(100, 190));
153 //reset size of the 2 main text controls
154 wxSize TextSize = PatternText.GetSize();
155 TextSize.SetWidth(200);
156 PatternText.SetSize(TextSize);
157 TextSize.SetHeight(TextSize.GetHeight() * 4);
158 SearchText.SetSize(TextSize);
159
160 // StaticText
161 PatternHeader.Create(this, -1, _("Regular Expression:"), wxPoint(5, 10));
162 SearchHeader.Create(this, -1, _("String to Search:"), wxPoint(5, 55));
163 IterHeader.Create(this, -1, _("Iterations (Match Time):"), wxPoint(100, 170));
164
165 ResultText.Create(this, -1, _(""), wxPoint(5, 220), wxSize(100,110), wxST_NO_AUTORESIZE);
166
167 // Button
168 OkButton.Create(this, OkButtonID, _("OK"), wxPoint(20, 190));
169
170
171 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
172 // create a status bar just for fun (by default with 1 pane only)
173 CreateStatusBar(1);
174 SetStatusText(_("Enter Some Values and Press the OK Button or Enter"));
175 #endif // wxUSE_STATUSBAR
176 }
177
178 void OnAbout(wxCommandEvent& WXUNUSED(evt))
179 {
180 wxChar* szBuffer;
181 wxFile f;
182
183 f.Open(_("README.txt"), wxFile::read);
184 szBuffer = new wxChar[f.Length() + 1];
185 f.Read(szBuffer, f.Length());
186 f.Close();
187
188 wxMessageBox(wxString::Format(
189 _("%s%s%s%s"),
190 _("-----------Regular Expression Test Application-----------\n"),
191 szBuffer,
192 _("\n\n\n(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
193 wxVERSION_STRING
194 )
195 );
196
197 delete szBuffer;
198 }
199
200 void OnMatch(wxCommandEvent& WXUNUSED(evt))
201 {
202 wxString szPattern = PatternText.GetValue();
203 wxString szSearch = SearchText.GetValue();
204 wxString szResult; //Will be displayed in ResultText
205 wxString szStatus;
206
207 int nCompileFlags = 0;
208 int nMatchFlags = 0;
209
210 wxMenu* OptionsMenu = GetMenuBar()->GetMenu(1);
211
212 if (!(OptionsMenu->IsChecked(ExtendedID)))
213 {
214 nCompileFlags |= wxRE_BASIC;
215 }
216
217 if (OptionsMenu->IsChecked(ICaseID))
218 {
219 nCompileFlags |= wxRE_ICASE;
220 }
221
222 if (OptionsMenu->IsChecked(NotBolID))
223 {
224 nMatchFlags |= wxRE_NOTBOL;
225 }
226
227 if (OptionsMenu->IsChecked(NotEolID))
228 {
229 nMatchFlags |= wxRE_NOTEOL;
230 }
231
232
233 //Success! Here we'll display some
234 //information to the user
235 size_t dwStartIndex = 0, dwEndIndex = 0;
236
237 time_t dwStartTime = 0, dwEndTime = 0;
238
239 int i = 0;
240 long n;
241 if (!NumIters.GetValue().ToLong(&n))
242 n = 0;
243
244
245 SetStatusText("Testing wxRegEx...");
246
247 //Our regular expression object
248 wxRegEx Regex;
249
250 //Regular Expressions must be compiled before
251 //you can search a string with them, or
252 //at least most implementations do.
253 //(Boost and Microsoft have templated
254 //versions that don't require compilation)
255 //Basically compilation breaks it down into
256 //something that's easier to parse, due
257 //to the syntax of regular expressions
258 //
259 //Note that you can use a constructor of wxRegEx
260 //that compiles it automatically, i.e.
261 //wxRegEx Regex(szPattern, nCompileFlags)
262 if (!Regex.Compile(szPattern, nCompileFlags))
263 szStatus += _("\nCompile Failed!\n");
264 else
265 {
266 //Here's where we actually search our string
267 if (!Regex.Matches(szSearch, nMatchFlags))
268 szStatus += _("\nExecution/Matching Failed!\n");
269 else
270 {
271 Regex.GetMatch(&dwStartIndex, &dwEndIndex);
272
273 szStatus = _("Success");
274
275 //We're going to go ahead and time the match
276 //for fun (clock() is a c library routine that
277 //returns the current time since a certian point
278 //in milliseconds
279 dwStartTime = clock();
280
281 if (OptionsMenu->IsChecked(CompID))
282 {
283 for(i = 0; i < n; ++i)
284 {
285 SetStatusText(wxString::Format(_("wxRegEx Compile #%i"), i));
286 Regex.Compile(szPattern, nCompileFlags);
287 }
288 }
289 if (OptionsMenu->IsChecked(MatchID))
290 {
291 for(i = 0; i < n; ++i)
292 {
293 SetStatusText(wxString::Format(_("wxRegEx Match #%i"), i));
294 Regex.Matches(szSearch, nMatchFlags);
295 }
296 }
297
298 //This line gets the difference in time between now
299 //and when we first initialized dwStartTime.
300 dwEndTime = clock() - dwStartTime;
301 }
302 }
303
304
305 szResult = wxString::Format(
306 _("--wxRegEx--\nIndex:[%i]-[%i]\nString:%s\nMatch Time:%ums\nStatus:%s"),
307 dwStartIndex, dwEndIndex+dwStartIndex,
308 szSearch.Mid(dwStartIndex, dwEndIndex),
309 dwEndTime,
310 szStatus
311 );
312
313 SetStatusText("Regex Run Complete");
314
315 ResultText.SetLabel(szResult);
316 }
317
318 void OnQuit(wxCommandEvent& WXUNUSED(evt))
319 {
320 Close(TRUE);
321 }
322
323 wxTextCtrl PatternText, SearchText, NumIters;
324 wxStaticText PatternHeader, SearchHeader, IterHeader, ResultText;
325 wxButton OkButton;
326
327 DECLARE_EVENT_TABLE()
328 };
329
330 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
331
332 //menu
333 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
334 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
335
336 //text
337 EVT_TEXT_ENTER(MyFrame::PatternTextID, MyFrame::OnMatch)
338 EVT_TEXT_ENTER(MyFrame::SearchTextID, MyFrame::OnMatch)
339
340 //button
341 EVT_BUTTON(MyFrame::OkButtonID, MyFrame::OnMatch)
342
343 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
344 EVT_CONTEXT_MENU(MyFrame::OnContextMenu)
345 #else
346 EVT_RIGHT_UP(MyFrame::OnRightUp)
347 #endif
348
349 END_EVENT_TABLE()
350
351 //---------------------------------------------------------------------------
352 // MyApp
353 //---------------------------------------------------------------------------
354
355 class MyApp : public wxApp
356 {
357 public:
358 bool OnInit()
359 {
360 MyFrame* dialog = new MyFrame();
361 dialog->Show();
362 return true;
363 }
364 };
365
366 IMPLEMENT_APP(MyApp)
367