OpenASIP 2.2
Loading...
Searching...
No Matches
FindWindow.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2017 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file FindWindow.cc
26 *
27 * Definition of FindWindow class.
28 *
29 * @author Alex Hirvonen 2017 (alex.hirvonen-no.spam-gmail.com)
30 * @note rating: red
31 */
32
33
34#include <algorithm>
35#include <wx/textctrl.h>
36#include <wx/stattext.h>
37
38#include "FindWindow.hh"
39#include "ProximToolbox.hh"
40#include "WxConversion.hh"
42#include "Program.hh"
43#include "Instruction.hh"
44#include "Move.hh"
45
46
47BEGIN_EVENT_TABLE(FindWindow, ProximSimulatorWindow)
48 EVT_TEXT(ID_OP_INPUT, FindWindow::onInputText)
50 EVT_TEXT_ENTER(ID_OP_INPUT, FindWindow::onFindNext)
53 EVT_CHECKBOX(ID_MATCH_CASE, FindWindow::onInputText)
55
56
58 ProximSimulatorWindow(parent, id) {
59
60 createContents(this, true, true);
61 opInput_->SetFocus();
62 matchCase_->SetValue(false);
63 findPrevBtn_->Disable();
64 findNextBtn_->Disable();
65}
66
67
70
71
72/**
73 * Called when the simulator program, memory and machine models are reset.
74 */
75void
77 // Do nothing.
78}
79
80/**
81 * Called when the input text changes or match case checkbox changes state.
82 */
83void
84FindWindow::onInputText(wxCommandEvent&) {
85 wxString inputwxString = opInput_->GetValue();
86
87 if (inputwxString.Length() > 2) {
88 std::string pattern = WxConversion::toString(inputwxString);
89
90 bool found = find(pattern);
91 if (found) {
92 findPrevBtn_->Enable();
93 findNextBtn_->Enable();
94
95 int total = matchedLines.size();
96 // update label
97 infoLabel_->SetLabel(wxT("1 of ") + WxConversion::toWxString(total)+
98 wxT(" matched lines"));
99 // jump to first matched line
101
102 return;
103
104 } else {
105 infoLabel_->SetLabel(wxT("Pattern not found"));
106 return;
107 }
108
109 }
110 infoLabel_->SetLabel(wxT(""));
111 findPrevBtn_->Disable();
112 findNextBtn_->Disable();
113}
114
115
116/*
117 * Called when the [Previous] button is pressed.
118 */
119void
120FindWindow::onFindPrev(wxCommandEvent&) {
121
122 int matchedSize = matchedLines.size();
123 if (matchedSize == 0) {
124 return;
125 }
126
127 if (matchedIndex == 0) {
128 matchedIndex = matchedSize - 1;
129 } else {
130 matchedIndex--;
131 }
132 infoLabel_->SetLabel(WxConversion::toWxString(matchedIndex+1) + wxT(" of ") +
133 WxConversion::toWxString(matchedSize) + wxT(" matched lines"));
135}
136
137
138/*
139 * Called when the [Find Next] button is pressed.
140 */
141void
142FindWindow::onFindNext(wxCommandEvent&) {
143
144 int matchedSize = matchedLines.size();
145 if (matchedSize == 0) {
146 return;
147 }
148
149 if (matchedIndex == matchedSize - 1) {
150 matchedIndex = 0;
151 } else {
152 matchedIndex++;
153 }
154 // update label
155 infoLabel_->SetLabel(WxConversion::toWxString(matchedIndex+1) + wxT(" of ") +
156 WxConversion::toWxString(matchedSize) + wxT(" matched lines"));
157
159}
160
161
162/*
163 * Searches through program's assembly instructions and collects information
164 * on which lines the pattern text appears.
165 *
166 * @param pattern Text string to be searched.
167 */
168bool
169FindWindow::find(std::string pattern) {
170
172 std::size_t found;
173 matchedIndex = 0;
174 matchedLines.clear();
175
176 for (int i = 0; i < program.instructionCount(); i++) {
177 const TTAProgram::Instruction& instruction = program.instructionAt(i);
178
179 std::string instrString = "";
180
181 for (int j = 0; j < instruction.moveCount(); j++) {
182 const TTAProgram::Move& move = instruction.move(j);
183 instrString += move.toString();
184 }
185 // remove spaces in instruction and search pattern
186 instrString.erase(remove_if(instrString.begin(), instrString.end(),
187 isspace), instrString.end());
188 pattern.erase(remove_if(pattern.begin(), pattern.end(),
189 isspace), pattern.end());
190 // case insensitive search
191 if (!matchCase_->IsChecked()) {
192 std::transform(instrString.begin(), instrString.end(),
193 instrString.begin(), ::tolower);
194 std::transform(pattern.begin(), pattern.end(), pattern.begin(),
195 ::tolower);
196 }
197
198 found = instrString.find(pattern);
199 if (found != std::string::npos) {
200 matchedLines.push_back(i);
201 }
202 }
203 return matchedLines.size() > 0;
204}
205
206
207/**
208 * Creates the dialog widgets.
209 */
210wxSizer*
212 wxWindow *parent, bool call_fit, bool set_sizer) {
213
214 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
215 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
216
217 opInput_ = new wxTextCtrl(parent, ID_OP_INPUT, wxT(""),
218 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
219 mainSizer->Add(opInput_, 0, wxALL|wxEXPAND, 5);
220
221 matchCase_ = new wxCheckBox(parent, ID_MATCH_CASE, wxT("Case sensitive"),
222 wxDefaultPosition, wxDefaultSize);
223 mainSizer->Add(matchCase_, 0, wxALL|wxEXPAND, 5);
224
225 infoLabel_ = new wxStaticText(parent, ID_INFO_LABEL, wxT(""),
226 wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
227 mainSizer->Add(infoLabel_, 0, wxALL|wxEXPAND, 5);
228
229 findPrevBtn_ = new wxButton(parent, ID_FIND_PREV, wxT("Previous"),
230 wxDefaultPosition, wxDefaultSize, 0);
231 findNextBtn_ = new wxButton(parent, ID_FIND_NEXT, wxT("Find next"),
232 wxDefaultPosition, wxDefaultSize, 0);
233
234 buttonSizer->Add(findPrevBtn_, 0, wxALL, 5);
235 buttonSizer->Add(findNextBtn_, 0, wxALL, 5);
236
237 mainSizer->Add(buttonSizer, 0, wxALIGN_CENTER|wxALL, 5);
238
239 if (set_sizer) {
240 parent->SetSizer(mainSizer);
241 if (call_fit) {
242 mainSizer->SetSizeHints(parent);
243 }
244 }
245
246 return mainSizer;
247}
END_EVENT_TABLE() using namespace IDF
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
find Finds info of the inner loops in the program
#define EVT_SIMULATOR_PROGRAM_LOADED(id, fn)
int matchedIndex
Currently displayed codeline index in matchedLines.
Definition FindWindow.hh:72
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
wxTextCtrl * opInput_
Definition FindWindow.hh:63
void onFindNext(wxCommandEvent &event)
void onFindPrev(wxCommandEvent &event)
wxStaticText * infoLabel_
Definition FindWindow.hh:65
std::vector< int > matchedLines
List of code linenumbers where serached text was found.
Definition FindWindow.hh:70
virtual void reset()
Definition FindWindow.cc:76
virtual ~FindWindow()
Definition FindWindow.cc:68
wxButton * findNextBtn_
Definition FindWindow.hh:67
void onInputText(wxCommandEvent &event)
Definition FindWindow.cc:84
wxButton * findPrevBtn_
Definition FindWindow.hh:66
wxCheckBox * matchCase_
Definition FindWindow.hh:64
bool find(std::string searchString)
void showAddress(unsigned address)
static const TTAProgram::Program & program()
static ProximDisassemblyWindow * disassemblyWindow()
Move & move(int i) const
std::string toString() const
Definition Move.cc:436
static wxString toWxString(const std::string &source)
static std::string toString(const wxString &source)