Debian Codegraph @

CodeLite/search_thread.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : search_thread.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #ifndef SEARCH_THREAD_H
26 #define SEARCH_THREAD_H
27
28 #include "JSON.h"
29 #include "clFilesCollector.h"
30 #include "codelite_exports.h"
31 #include "singleton.h"
32 #include "worker_thread.h"
33 #include "wx/event.h"
34 #include "wx/filename.h"
35 #include "wxStringHash.h"
36
37 #include <deque>
38 #include <list>
39 #include <map>
40 #include <vector>
41 #include <wx/regex.h>
42 #include <wx/stopwatch.h>
43 #include <wx/string.h>
44
45 class wxEvtHandler;
46 class SearchResult;
47 class SearchThread;
48
49 //----------------------------------------------------------
50 // The searched data class to be passed to the search thread
51 //----------------------------------------------------------
52 // Possible search data options:
53 enum {
54 wxSD_MATCHCASE = 0x00000001,
55 wxSD_MATCHWHOLEWORD = 0x00000002,
56 wxSD_REGULAREXPRESSION = 0x00000004,
57 wxSD_SEARCH_BACKWARD = 0x00000008,
58 wxSD_USE_EDITOR_ENCODING = 0x00000010,
59 wxSD_PRINT_SCOPE = 0x00000020,
60 wxSD_SKIP_COMMENTS = 0x00000040,
61 wxSD_SKIP_STRINGS = 0x00000080,
62 wxSD_COLOUR_COMMENTS = 0x00000100,
63 wxSD_WILDCARD = 0x00000200,
64 wxSD_ENABLE_PIPE_SUPPORT = 0x00000400,
65 };
66
67 class WXDLLIMPEXP_CL SearchData : public ThreadRequest
68 {
69 wxArrayString m_rootDirs;
70 wxString m_findString;
71 wxString m_replaceWith;
72 size_t m_flags;
73 wxString m_validExt;
74 wxArrayString m_files;
75 bool m_newTab;
76 wxEvtHandler* m_owner;
77 wxString m_encoding;
78 wxArrayString m_excludePatterns;
79 size_t m_file_scanner_flags = clFilesScanner::SF_DONT_FOLLOW_SYMLINKS | clFilesScanner::SF_EXCLUDE_HIDDEN_DIRS;
80 friend class SearchThread;
81
82 private:
83 // An internal helper function that set/remove an option bit
84 void SetOption(int option, bool set)
85 {
86 if(set) {
87 m_flags |= option;
88 } else {
89 m_flags &= ~(option);
90 }
91 }
92
93 public:
94 // Ctor-Dtor
95 SearchData()
96 : ThreadRequest()
97 , m_findString(wxEmptyString)
98 , m_flags(0)
99 , m_newTab(false)
100 , m_owner(NULL)
101 {
102 }
103
104 SearchData(const SearchData& rhs) { Copy(rhs); }
105 SearchData& operator=(const SearchData& rhs);
106
107 virtual ~SearchData() {}
108 SearchData& Copy(const SearchData& other);
109
110 public:
111 //------------------------------------------
112 // Setters / Getters
113 //------------------------------------------
114 size_t GetFileScannerFlags() const { return m_file_scanner_flags; }
115 void SetFileScannerFlags(size_t flags) { m_file_scanner_flags = flags; }
116 bool IsMatchCase() const { return m_flags & wxSD_MATCHCASE ? true : false; }
117 bool IsEnablePipeSupport() const { return m_flags & wxSD_ENABLE_PIPE_SUPPORT; }
118 void SetEnablePipeSupport(bool b) { SetOption(wxSD_ENABLE_PIPE_SUPPORT, b); }
119 bool IsMatchWholeWord() const { return m_flags & wxSD_MATCHWHOLEWORD ? true : false; }
120 bool IsRegularExpression() const { return m_flags & wxSD_REGULAREXPRESSION ? true : false; }
121 const wxArrayString& GetRootDirs() const { return m_rootDirs; }
122 void SetMatchCase(bool matchCase) { SetOption(wxSD_MATCHCASE, matchCase); }
123 void SetMatchWholeWord(bool matchWholeWord) { SetOption(wxSD_MATCHWHOLEWORD, matchWholeWord); }
124 void SetRegularExpression(bool re) { SetOption(wxSD_REGULAREXPRESSION, re); }
125 void SetExtensions(const wxString& exts) { m_validExt = exts; }
126 void SetRootDirs(const wxArrayString& rootDirs) { m_rootDirs = rootDirs; }
127 const wxString& GetExtensions() const;
128 const wxString& GetFindString() const { return m_findString; }
129 void SetFindString(const wxString& findString) { m_findString = findString; }
130 void SetFiles(const wxArrayString& files) { m_files = files; }
131 const wxArrayString& GetFiles() const { return m_files; }
132 void SetExcludePatterns(const wxArrayString& excludePatterns) { this->m_excludePatterns = excludePatterns; }
133 const wxArrayString& GetExcludePatterns() const { return m_excludePatterns; }
134 void UseNewTab(bool useNewTab) { m_newTab = useNewTab; }
135 bool UseNewTab() const { return m_newTab; }
136 void SetEncoding(const wxString& encoding) { this->m_encoding = encoding.c_str(); }
137 const wxString& GetEncoding() const { return this->m_encoding; }
138 bool GetDisplayScope() const { return m_flags & wxSD_PRINT_SCOPE ? true : false; }
139 void SetDisplayScope(bool d) { SetOption(wxSD_PRINT_SCOPE, d); }
140 void SetOwner(wxEvtHandler* owner) { this->m_owner = owner; }
141 wxEvtHandler* GetOwner() const { return m_owner; }
142 bool HasCppOptions() const
143 {
144 return (m_flags & wxSD_SKIP_COMMENTS) || (m_flags & wxSD_SKIP_STRINGS) || (m_flags & wxSD_COLOUR_COMMENTS);
145 }
146
147 void SetSkipComments(bool d) { SetOption(wxSD_SKIP_COMMENTS, d); }
148 void SetSkipStrings(bool d) { SetOption(wxSD_SKIP_STRINGS, d); }
149 void SetColourComments(bool d) { SetOption(wxSD_COLOUR_COMMENTS, d); }
150 bool GetSkipComments() const { return (m_flags & wxSD_SKIP_COMMENTS); }
151 bool GetSkipStrings() const { return (m_flags & wxSD_SKIP_STRINGS); }
152 bool GetColourComments() const { return (m_flags & wxSD_COLOUR_COMMENTS); }
153 const wxString& GetReplaceWith() const { return m_replaceWith; }
154 void SetReplaceWith(const wxString& replaceWith) { this->m_replaceWith = replaceWith; }
155 };
156
157 //------------------------------------------
158 // class containing the search result
159 //------------------------------------------
160 class WXDLLIMPEXP_CL SearchResult : public wxObject
161 {
162 wxString m_pattern;
163 int m_position;
164 int m_lineNumber;
165 int m_column;
166 wxString m_fileName;
167 int m_len;
168 wxString m_findWhat;
169 size_t m_flags;
170 int m_columnInChars;
171 int m_lenInChars;
172 wxString m_scope;
173 wxArrayString m_regexCaptures;
174
175 public:
176 // ctor-dtor, copy constructor and assignment operator
177 SearchResult() {}
178
179 virtual ~SearchResult() {}
180
181 SearchResult(const SearchResult& rhs) { *this = rhs; }
182
183 SearchResult& operator=(const SearchResult& rhs)
184 {
185 if(this == &rhs)
186 return *this;
187 m_position = rhs.m_position;
188 m_column = rhs.m_column;
189 m_lineNumber = rhs.m_lineNumber;
190 m_pattern = rhs.m_pattern.c_str();
191 m_fileName = rhs.m_fileName.c_str();
192 m_len = rhs.m_len;
193 m_findWhat = rhs.m_findWhat.c_str();
194 m_flags = rhs.m_flags;
195 m_columnInChars = rhs.m_columnInChars;
196 m_lenInChars = rhs.m_lenInChars;
197 m_scope = rhs.m_scope.c_str();
198 m_regexCaptures = rhs.m_regexCaptures;
199 return *this;
200 }
201
202 JSONItem ToJSON() const;
203 void FromJSON(const JSONItem& json);
204
205 //------------------------------------------------------
206 // Setters/getters
207
208 void SetFlags(const size_t& flags) { this->m_flags = flags; }
209
210 const size_t& GetFlags() const { return m_flags; }
211
212 void SetPattern(const wxString& pat) { m_pattern = pat.c_str(); }
213 void SetPosition(const int& position) { m_position = position; }
214 void SetLineNumber(const int& line) { m_lineNumber = line; }
215 void SetColumn(const int& col) { m_column = col; }
216 void SetFileName(const wxString& fileName) { m_fileName = fileName.c_str(); }
217
218 const int& GetPosition() const { return m_position; }
219 const int& GetLineNumber() const { return m_lineNumber; }
220 const int& GetColumn() const { return m_column; }
221 const wxString& GetPattern() const { return m_pattern; }
222 const wxString& GetFileName() const { return m_fileName; }
223
224 void SetLen(const int& len) { this->m_len = len; }
225 const int& GetLen() const { return m_len; }
226
227 // Setters
228 void SetFindWhat(const wxString& findWhat) { this->m_findWhat = findWhat.c_str(); }
229 // Getters
230 const wxString& GetFindWhat() const { return m_findWhat; }
231
232 void SetColumnInChars(const int& col) { this->m_columnInChars = col; }
233 const int& GetColumnInChars() const { return m_columnInChars; }
234
235 void SetLenInChars(const int& len) { this->m_lenInChars = len; }
236 const int& GetLenInChars() const { return m_lenInChars; }
237
238 void SetScope(const wxString& scope) { this->m_scope = scope.c_str(); }
239 const wxString& GetScope() const { return m_scope; }
240
241 void SetRegexCaptures(const wxArrayString& regexCaptures) { this->m_regexCaptures = regexCaptures; }
242 const wxArrayString& GetRegexCaptures() const { return m_regexCaptures; }
243 wxString GetRegexCapture(size_t backref) const
244 {
245 if(m_regexCaptures.size() > backref) {
246 return m_regexCaptures[backref];
247 } else {
248 return wxEmptyString;
249 }
250 }
251
252 // return a foramtted message
253 wxString GetMessage() const
254 {
255 wxString msg;
256 msg << GetFileName() << wxT("(") << GetLineNumber() << wxT(",") << GetColumn() << wxT(",") << GetLen()
257 << wxT("): ") << GetPattern();
258 return msg;
259 }
260 };
261
262 typedef std::vector<SearchResult> SearchResultList;
263
264 class WXDLLIMPEXP_CL SearchSummary : public wxObject
265 {
266 int m_fileScanned;
267 int m_matchesFound;
268 int m_elapsed;
269 wxArrayString m_failedFiles;
270 wxString m_findWhat;
271 wxString m_replaceWith;
272
273 public:
274 SearchSummary()
275 : m_fileScanned(0)
276 , m_matchesFound(0)
277 , m_elapsed(0)
278 {
279 }
280
281 virtual ~SearchSummary() {}
282
283 SearchSummary(const SearchSummary& rhs) { *this = rhs; }
284
285 SearchSummary& operator=(const SearchSummary& rhs)
286 {
287 if(this == &rhs)
288 return *this;
289
290 m_fileScanned = rhs.m_fileScanned;
291 m_matchesFound = rhs.m_matchesFound;
292 m_elapsed = rhs.m_elapsed;
293 m_failedFiles = rhs.m_failedFiles;
294 m_findWhat = rhs.m_findWhat;
295 m_replaceWith = rhs.m_replaceWith;
296 return *this;
297 }
298
299 JSONItem ToJSON() const;
300 void FromJSON(const JSONItem& json);
301
302 void SetFindWhat(const wxString& findWhat) { this->m_findWhat = findWhat; }
303 void SetReplaceWith(const wxString& replaceWith) { this->m_replaceWith = replaceWith; }
304 const wxString& GetFindWhat() const { return m_findWhat; }
305 const wxString& GetReplaceWith() const { return m_replaceWith; }
306 const wxArrayString& GetFailedFiles() const { return m_failedFiles; }
307 wxArrayString& GetFailedFiles() { return m_failedFiles; }
308
309 int GetNumFileScanned() const { return m_fileScanned; }
310 int GetNumMatchesFound() const { return m_matchesFound; }
311
312 void SetNumFileScanned(const int& num) { m_fileScanned = num; }
313 void SetNumMatchesFound(const int& num) { m_matchesFound = num; }
314 void SetElapsedTime(long elapsed) { m_elapsed = elapsed; }
315 wxString GetMessage() const
316 {
317 wxString msg(wxString(wxT("====== ")) + _("Number of files scanned: "));
318 msg << m_fileScanned << wxT(",");
319 msg << _(" Matches found: ");
320 msg << m_matchesFound;
321 int secs = m_elapsed / 1000;
322 int msecs = m_elapsed % 1000;
323
324 msg << _(", elapsed time: ") << secs << wxT(".") << msecs << _(" seconds") << wxT(" ======");
325 if(!m_failedFiles.IsEmpty()) {
326 msg << "\n";
327 msg << "====== " << _("Failed to open the following files for scan:") << "\n";
328 for(size_t i = 0; i < m_failedFiles.size(); ++i) {
329 msg << m_failedFiles.Item(i) << "\n";
330 }
331 }
332 return msg;
333 }
334 };
335
336 //----------------------------------------------------------
337 // The search thread
338 //----------------------------------------------------------
339
340 class WXDLLIMPEXP_CL SearchThread : public WorkerThread
341 {
342 friend class SearchThreadST;
343 wxString m_wordChars;
344 SearchResultList m_results;
345 bool m_stopSearch;
346 SearchSummary m_summary;
347 wxString m_reExpr;
348 wxRegEx m_regex;
349 bool m_matchCase;
350 wxCriticalSection m_cs;
351 wxStopWatch m_stopWatch;
352 long m_msPassed = 0;
353
354 public:
355 /**
356 * Default constructor.
357 */
358 SearchThread();
359
360 /**
361 * Destructor.
362 */
363 virtual ~SearchThread();
364
365 /**
366 * Process request from caller
367 */
368 void ProcessRequest(ThreadRequest* req);
369
370 /**
371 * Add a request to the search thread to start
372 * \param data SearchData class
373 */
374 void PerformSearch(const SearchData& data);
375
376 /**
377 * Stops the current search operation
378 * \note This call must be called from the context of other thread (e.g. main thread)
379 */
380 void StopSearch(bool stop = true);
381
382 private:
383 /**
384 * Return files to search
385 * \param files output
386 * \param data search data
387 */
388 void GetFiles(const SearchData* data, wxArrayString& files);
389
390 // Test to see if user asked to cancel the search
391 bool TestStopSearch();
392
393 /**
394 * Do the actual search operation
395 * \param data inpunt contains information about the search
396 */
397 void DoSearchFiles(ThreadRequest* data);
398
399 // Perform search on a single file
400 void DoSearchFile(const wxString& fileName, const SearchData* data);
401
402 // Perform search on a line
403 void DoSearchLine(const wxString& line, const int lineNum, const int lineOffset, const wxString& fileName,
404 const SearchData* data, const wxString& findWhat, const wxArrayString& filters);
405
406 // Perform search on a line using regular expression
407 void DoSearchLineRE(const wxString& line, const int lineNum, const int lineOffset, const wxString& fileName,
408 const SearchData* data);
409
410 // Send an event to the notified window
411 void SendEvent(wxEventType type, wxEvtHandler* owner);
412
413 // return a compiled regex object for the expression
414 wxRegEx& GetRegex(const wxString& expr, bool matchCase);
415
416 // Internal function
417 bool AdjustLine(wxString& line, int& pos, const wxString& findString);
418
419 // filter 'files' according to the files spec
420 void FilterFiles(wxArrayString& files, const SearchData* data);
421 };
422
423 class WXDLLIMPEXP_CL SearchThreadST
424 {
425 public:
426 static SearchThread* Get();
427 static void Free();
428 };
429
430 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SEARCH_THREAD_MATCHFOUND, wxCommandEvent);
431 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SEARCH_THREAD_SEARCHEND, wxCommandEvent);
432 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SEARCH_THREAD_SEARCHCANCELED, wxCommandEvent);
433 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SEARCH_THREAD_SEARCHSTARTED, wxCommandEvent);
434
435 #endif // SEARCH_THREAD_H