]> git.saurik.com Git - wxWidgets.git/blob - samples/regextest/regextest.cpp
1cf722538575b4e3b551eb26b8294f4ad6665896
[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 // 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 //
29 // This is the required header for wxRegEx
30 //
31 #include <wx/regex.h>
32
33 //===========================================================================
34 // IMPLEMENTATION
35 //===========================================================================
36
37 //---------------------------------------------------------------------------
38 // MyDialog
39 //---------------------------------------------------------------------------
40
41 class MyDialog : public wxFrame
42 {
43 public:
44
45 //IDs for our controls
46 enum
47 {
48 PatternTextID,
49 SearchTextID,
50 OkButtonID
51 };
52
53 //Menu IDs
54 enum
55 {
56 ExtendedID,
57 ICaseID,
58 NewLineID,
59 NotBolID,
60 NotEolID
61 };
62
63
64 MyDialog() : wxFrame( NULL, -1, _("regextest - wxRegEx Testing App"),
65 wxPoint(20,20), wxSize(300,300) )
66 {
67 //Set the background to something light gray-ish
68 SetBackgroundColour(wxColour(150,150,150));
69
70 //
71 // Create the menus (Exit & About)
72 //
73 #if wxUSE_MENUS
74 wxMenu *FileMenu = new wxMenu;
75 OptionsMenu = new wxMenu;
76 wxMenu *HelpMenu = new wxMenu;
77
78 FileMenu->Append(wxID_EXIT, _T("&Exit"), _("Quit this program"));
79
80 OptionsMenu->AppendCheckItem(ExtendedID, _T("wxRE_EXTENDED"), _("Extended Regular Expressions?"));
81 OptionsMenu->AppendCheckItem(ICaseID, _T("wxRE_ICASE"), _("Enable case-insensitive matching?"));
82 OptionsMenu->AppendCheckItem(NewLineID, _T("wxRE_NEWLINE"), _("Treat \n as special?"));
83 OptionsMenu->AppendSeparator();
84 OptionsMenu->AppendCheckItem(NotBolID, _T("wxRE_NOTBOL"), _("Use functionality of ^ character?"));
85 OptionsMenu->AppendCheckItem(NotEolID, _T("wxRE_NOTEOL"), _("Use functionality of $ character?"));
86 OptionsMenu->Check(ExtendedID, true);
87
88 HelpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _("Show about dialog"));
89
90 wxMenuBar *MenuBar = new wxMenuBar();
91 MenuBar->Append(FileMenu, _T("&File"));
92 MenuBar->Append(OptionsMenu, _T("&Options"));
93 MenuBar->Append(HelpMenu, _T("&Help"));
94
95 SetMenuBar(MenuBar);
96 #endif // wxUSE_MENUS
97
98 // Text controls
99 PatternText.Create(this, PatternTextID, _(""), wxPoint(5, 30));
100 SearchText.Create(this, SearchTextID, _(""), wxPoint(5, 75));
101 //reset size of text controls
102 wxSize TextSize = PatternText.GetSize();
103 TextSize.SetWidth(200);
104 PatternText.SetSize(TextSize);
105 SearchText.SetSize(TextSize);
106
107 // StaticText
108 PatternHeader.Create(this, -1, _("Regular Expression:"), wxPoint(5, 10));
109 SearchHeader.Create(this, -1, _("String to Search:"), wxPoint(5, 55));
110 ResultText.Create(this, -1, _(""), wxPoint(5, 150));
111
112 // Button
113 OkButton.Create(this, OkButtonID, _("OK"), wxPoint(20, 120));
114 }
115
116 void OnAbout(wxCommandEvent& WXUNUSED(evt))
117 {
118 wxMessageBox(wxString::Format(
119 _("%s%s%s"),
120 _("Regular Expression Test Application\n"),
121 _("(c) 2003 Ryan Norton <wxprojects@comcast.net>\n"),
122 wxVERSION_STRING
123 )
124 );
125 }
126
127 void OnMatch(wxCommandEvent& WXUNUSED(evt))
128 {
129 wxString szPattern = PatternText.GetValue();
130 wxString szSearch = SearchText.GetValue();
131 wxString szResult = _("Result:\n"); //Will be displayed in ResultText
132
133 int nCompileFlags = 0;
134 int nMatchFlags = 0;
135
136 if (!(OptionsMenu->IsChecked(ExtendedID)))
137 nCompileFlags |= wxRE_BASIC;
138
139 if (OptionsMenu->IsChecked(ICaseID))
140 nCompileFlags |= wxRE_ICASE;
141
142 if (OptionsMenu->IsChecked(NewLineID))
143 nCompileFlags |= wxRE_NEWLINE;
144
145 if (OptionsMenu->IsChecked(NotBolID))
146 nMatchFlags |= wxRE_NOTBOL;
147
148 if (OptionsMenu->IsChecked(NotEolID))
149 nMatchFlags |= wxRE_NOTEOL;
150
151 //Our regular expression object
152 wxRegEx Regex;
153
154 //We're going to go ahead and time the match
155 //for fun (clock() is a c library routine that
156 //returns the current time since a certian point
157 //in milliseconds
158 time_t dwStartTime = clock();
159
160 //Regular Expressions must be compiled before
161 //you can search a string with them, or
162 //at least most implementations do.
163 //(Boost and Microsoft have templated
164 //versions that don't require compilation)
165 //Basically compilation breaks it down into
166 //something that's easier to parse, due
167 //to the syntax of regular expressions
168 if (!Regex.Compile(szPattern, nCompileFlags))
169 szResult += _("\nCompile Failed!\n");
170 else
171 {
172 //Here's where we actually search our string
173 if (!Regex.Matches(szSearch, nMatchFlags))
174 szResult += _("\nExecution/Matching Failed!\n");
175 else
176 {
177 //Success! Here we'll display some
178 //information to the user
179 size_t dwStartIndex, dwEndIndex;
180 Regex.GetMatch(&dwStartIndex, &dwEndIndex);
181
182 szResult += wxString::Format(
183 _("Start Index:%i\nMatch Length:%i\nString Matched:%s\n"),
184 dwStartIndex, dwEndIndex,
185 szSearch.Mid(dwStartIndex, dwEndIndex)
186 );
187 }
188 }
189
190 //This line gets the difference in time between now
191 //and when we first initialized dwStartTime.
192 time_t dwEndTime = clock() - dwStartTime;
193
194 szResult += wxString::Format(_("Match Time:%u Milliseconds"), dwEndTime);
195
196 ResultText.SetLabel(szResult);
197 }
198
199 void OnQuit(wxCommandEvent& WXUNUSED(evt))
200 {
201 Close(TRUE);
202 }
203
204 wxTextCtrl PatternText, SearchText;
205 wxStaticText PatternHeader, SearchHeader, ResultText;
206 wxButton OkButton;
207
208 wxMenu *OptionsMenu;
209
210 DECLARE_EVENT_TABLE()
211 };
212
213 BEGIN_EVENT_TABLE(MyDialog, wxFrame)
214
215 //menu
216 EVT_MENU(wxID_EXIT, MyDialog::OnQuit)
217 EVT_MENU(wxID_ABOUT, MyDialog::OnAbout)
218
219 //text
220 EVT_TEXT_ENTER(MyDialog::PatternTextID, MyDialog::OnMatch)
221 EVT_TEXT_ENTER(MyDialog::SearchTextID, MyDialog::OnMatch)
222
223 //button
224 EVT_BUTTON(MyDialog::OkButtonID, MyDialog::OnMatch)
225
226 END_EVENT_TABLE()
227
228 //---------------------------------------------------------------------------
229 // MyApp
230 //---------------------------------------------------------------------------
231
232 class MyApp : public wxApp
233 {
234 public:
235 bool OnInit()
236 {
237 MyDialog* dialog = new MyDialog();
238 dialog->Show();
239 return true;
240 }
241 };
242
243 IMPLEMENT_APP(MyApp)
244