1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Test regex libs and some gui components
7 // Copyright: (c) 2003 Ryan Norton <wxprojects@comcast.net>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 //===========================================================================
13 //===========================================================================
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers (this file is usually all you
23 // need because it includes almost all "standard" wxWindows headers)
31 # error wxUSE_REGEX needs to be enabled in setup.h to use wxRegEx
35 // This is the required header for wxRegEx
39 //===========================================================================
41 //===========================================================================
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).
49 //---------------------------------------------------------------------------
51 //---------------------------------------------------------------------------
53 class MyFrame
: public wxFrame
57 //IDs for our controls
79 // Adds an item to a menu (long way, easier to do wxMenu::AppendCheckItem())
81 void AddMenuItem(wxMenu
* pMenu
, int nID
= wxID_SEPARATOR
, const wxChar
* szTitle
= _(""),
82 const wxChar
* szToolTip
= _(""))
86 if (nID
== wxID_SEPARATOR
)
87 pItem
= new wxMenuItem (NULL
, wxID_SEPARATOR
, szTitle
, szToolTip
, wxITEM_SEPARATOR
);
89 pItem
= new wxMenuItem (NULL
, nID
, szTitle
, szToolTip
, wxITEM_CHECK
);
92 pItem
->SetBackgroundColour(wxColour(115, 113, 115));
93 pItem
->SetTextColour(*wxBLACK
);
98 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
99 void OnContextMenu(wxContextMenuEvent
& event
)
100 { PopupMenu(GetMenuBar()->GetMenu(1), ScreenToClient(event
.GetPosition())); }
102 void OnRightUp(wxMouseEvent
& event
)
103 { PopupMenu(GetMenuBar()->GetMenu(1), event
.GetPosition()); }
106 MyFrame() : wxFrame( NULL
, -1, _("regextest - wxRegEx Testing App"),
107 wxPoint(20,20), wxSize(300,400), wxDEFAULT_FRAME_STYLE
| wxTAB_TRAVERSAL
)
109 //Set the background to something light gray-ish
110 SetBackgroundColour(wxColour(150,150,150));
117 wxMenu
*FileMenu
= new wxMenu
;
118 wxMenu
*OptionsMenu
= new wxMenu
;
119 wxMenu
*HelpMenu
= new wxMenu
;
121 AddMenuItem(FileMenu
, wxID_EXIT
, _("&Exit"), _("Quit this program"));
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?"));
134 AddMenuItem(HelpMenu
, wxID_ABOUT
, _T("&About...\tF1"), _("Show about dialog"));
136 OptionsMenu
->Check(ExtendedID
, true);
138 OptionsMenu
->Check(CompID
, true);
139 OptionsMenu
->Check(MatchID
, true);
141 wxMenuBar
*MenuBar
= new wxMenuBar();
142 MenuBar
->Append(FileMenu
, _T("&File"));
143 MenuBar
->Append(OptionsMenu
, _T("&Options"));
144 MenuBar
->Append(HelpMenu
, _T("&Help"));
147 #endif // wxUSE_MENUS
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
);
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));
165 ResultText
.Create(this, -1, _(""), wxPoint(5, 220), wxSize(100,110), wxST_NO_AUTORESIZE
);
168 OkButton
.Create(this, OkButtonID
, _("OK"), wxPoint(20, 190));
171 #if wxUSE_STATUSBAR && !defined(__WXWINCE__)
172 // create a status bar just for fun (by default with 1 pane only)
174 SetStatusText(_("Enter Some Values and Press the OK Button or Enter"));
175 #endif // wxUSE_STATUSBAR
178 void OnAbout(wxCommandEvent
& WXUNUSED(evt
))
183 f
.Open(_("README.txt"), wxFile::read
);
184 szBuffer
= new wxChar
[f
.Length() + 1];
185 f
.Read(szBuffer
, f
.Length());
188 wxMessageBox(wxString::Format(
190 _("-----------Regular Expression Test Application-----------\n"),
192 _("\n\n\n(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
200 void OnMatch(wxCommandEvent
& WXUNUSED(evt
))
202 wxString szPattern
= PatternText
.GetValue();
203 wxString szSearch
= SearchText
.GetValue();
204 wxString szResult
; //Will be displayed in ResultText
207 int nCompileFlags
= 0;
210 wxMenu
* OptionsMenu
= GetMenuBar()->GetMenu(1);
212 if (!(OptionsMenu
->IsChecked(ExtendedID
)))
214 nCompileFlags
|= wxRE_BASIC
;
217 if (OptionsMenu
->IsChecked(ICaseID
))
219 nCompileFlags
|= wxRE_ICASE
;
222 if (OptionsMenu
->IsChecked(NotBolID
))
224 nMatchFlags
|= wxRE_NOTBOL
;
227 if (OptionsMenu
->IsChecked(NotEolID
))
229 nMatchFlags
|= wxRE_NOTEOL
;
233 //Success! Here we'll display some
234 //information to the user
235 size_t dwStartIndex
= 0, dwEndIndex
= 0;
237 time_t dwStartTime
= 0, dwEndTime
= 0;
241 if (!NumIters
.GetValue().ToLong(&n
))
245 SetStatusText("Testing wxRegEx...");
247 //Our regular expression object
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
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");
266 //Here's where we actually search our string
267 if (!Regex
.Matches(szSearch
, nMatchFlags
))
268 szStatus
+= _("\nExecution/Matching Failed!\n");
271 Regex
.GetMatch(&dwStartIndex
, &dwEndIndex
);
273 szStatus
= _("Success");
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
279 dwStartTime
= clock();
281 if (OptionsMenu
->IsChecked(CompID
))
283 for(i
= 0; i
< n
; ++i
)
285 SetStatusText(wxString::Format(_("wxRegEx Compile #%i"), i
));
286 Regex
.Compile(szPattern
, nCompileFlags
);
289 if (OptionsMenu
->IsChecked(MatchID
))
291 for(i
= 0; i
< n
; ++i
)
293 SetStatusText(wxString::Format(_("wxRegEx Match #%i"), i
));
294 Regex
.Matches(szSearch
, nMatchFlags
);
298 //This line gets the difference in time between now
299 //and when we first initialized dwStartTime.
300 dwEndTime
= clock() - dwStartTime
;
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
),
313 SetStatusText("Regex Run Complete");
315 ResultText
.SetLabel(szResult
);
318 void OnQuit(wxCommandEvent
& WXUNUSED(evt
))
323 wxTextCtrl PatternText
, SearchText
, NumIters
;
324 wxStaticText PatternHeader
, SearchHeader
, IterHeader
, ResultText
;
327 DECLARE_EVENT_TABLE()
330 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
333 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
334 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
337 EVT_TEXT_ENTER(MyFrame::PatternTextID
, MyFrame::OnMatch
)
338 EVT_TEXT_ENTER(MyFrame::SearchTextID
, MyFrame::OnMatch
)
341 EVT_BUTTON(MyFrame::OkButtonID
, MyFrame::OnMatch
)
343 #if defined( __WXMSW__ ) || defined( __WXMAC__ )
344 EVT_CONTEXT_MENU(MyFrame::OnContextMenu
)
346 EVT_RIGHT_UP(MyFrame::OnRightUp
)
351 //---------------------------------------------------------------------------
353 //---------------------------------------------------------------------------
355 class MyApp
: public wxApp
360 MyFrame
* dialog
= new MyFrame();