OpenASIP  2.0
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 
47 BEGIN_EVENT_TABLE(FindWindow, ProximSimulatorWindow)
48  EVT_TEXT(ID_OP_INPUT, FindWindow::onInputText)
50  EVT_TEXT_ENTER(ID_OP_INPUT, FindWindow::onFindNext)
51  EVT_BUTTON(ID_FIND_PREV, FindWindow::onFindPrev)
52  EVT_BUTTON(ID_FIND_NEXT, 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 
69 }
70 
71 
72 /**
73  * Called when the simulator program, memory and machine models are reset.
74  */
75 void
77  // Do nothing.
78 }
79 
80 /**
81  * Called when the input text changes or match case checkbox changes state.
82  */
83 void
84 FindWindow::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  */
119 void
120 FindWindow::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  */
141 void
142 FindWindow::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  */
168 bool
169 FindWindow::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  */
210 wxSizer*
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 }
ProximMainFrame
Definition: ProximMainFrame.hh:58
TTAProgram::Program
Definition: Program.hh:63
WxConversion::toWxString
static wxString toWxString(const std::string &source)
FindWindow::matchedLines
std::vector< int > matchedLines
List of code linenumbers where serached text was found.
Definition: FindWindow.hh:70
TTAProgram::Instruction::move
Move & move(int i) const
Definition: Instruction.cc:193
FindWindow::findNextBtn_
wxButton * findNextBtn_
Definition: FindWindow.hh:67
TTAProgram::Instruction
Definition: Instruction.hh:57
FindWindow::matchedIndex
int matchedIndex
Currently displayed codeline index in matchedLines.
Definition: FindWindow.hh:72
ProximToolbox::program
static const TTAProgram::Program & program()
Definition: ProximToolbox.cc:88
TTAProgram::Move::toString
std::string toString() const
Definition: Move.cc:436
FindWindow
Definition: FindWindow.hh:49
ProximDisassemblyWindow.hh
ProximSimulatorWindow
Definition: ProximSimulatorWindow.hh:47
ProximToolbox.hh
FindWindow::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: FindWindow.cc:211
Instruction.hh
FindWindow::onFindPrev
void onFindPrev(wxCommandEvent &event)
Definition: FindWindow.cc:120
TTAProgram::Move
Definition: Move.hh:55
FindWindow::ID_MATCH_CASE
@ ID_MATCH_CASE
Definition: FindWindow.hh:77
FindWindow::infoLabel_
wxStaticText * infoLabel_
Definition: FindWindow.hh:65
FindWindow::opInput_
wxTextCtrl * opInput_
Definition: FindWindow.hh:63
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
FindWindow::onFindNext
void onFindNext(wxCommandEvent &event)
Definition: FindWindow.cc:142
FindWindow::ID_FIND_NEXT
@ ID_FIND_NEXT
Definition: FindWindow.hh:80
Program.hh
FindWindow::onInputText
void onInputText(wxCommandEvent &event)
Definition: FindWindow.cc:84
WxConversion.hh
FindWindow::reset
virtual void reset()
Definition: FindWindow.cc:76
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
ProximToolbox::disassemblyWindow
static ProximDisassemblyWindow * disassemblyWindow()
Definition: ProximToolbox.cc:150
FindWindow::ID_FIND_PREV
@ ID_FIND_PREV
Definition: FindWindow.hh:79
EVT_SIMULATOR_PROGRAM_LOADED
#define EVT_SIMULATOR_PROGRAM_LOADED(id, fn)
Definition: SimulatorEvent.hh:151
ProximDisassemblyWindow::showAddress
void showAddress(unsigned address)
Definition: ProximDisassemblyWindow.cc:359
Move.hh
FindWindow::findPrevBtn_
wxButton * findPrevBtn_
Definition: FindWindow.hh:66
FindWindow::ID_OP_INPUT
@ ID_OP_INPUT
Definition: FindWindow.hh:76
WxConversion::toString
static std::string toString(const wxString &source)
FindWindow::find
bool find(std::string searchString)
Definition: FindWindow.cc:169
TTAProgram::Instruction::moveCount
int moveCount() const
Definition: Instruction.cc:176
FindWindow::ID_INFO_LABEL
@ ID_INFO_LABEL
Definition: FindWindow.hh:78
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
FindWindow::matchCase_
wxCheckBox * matchCase_
Definition: FindWindow.hh:64
FindWindow::~FindWindow
virtual ~FindWindow()
Definition: FindWindow.cc:68
FindWindow.hh