~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/ThreadSearch/ThreadSearchThread.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************
 
2
 * Name:      ThreadSearchThread
 
3
 *
 
4
 * Purpose:   This class implements the search worker thread.
 
5
 *            It sends events to the view when one or more
 
6
 *            search pattern is (are) found in a file.
 
7
 *            It derives from wxDirTraverser to test thread
 
8
 *            cancel when searching in directory.
 
9
 *            One event/file is sent.
 
10
 *
 
11
 * Author:    Jerome ANTOINE
 
12
 * Created:   2007-10-08
 
13
 * Copyright: Jerome ANTOINE
 
14
 * License:   GPL
 
15
 **************************************************************/
 
16
 
 
17
#include "sdk.h"
 
18
#ifndef CB_PRECOMP
 
19
    #include "cbeditor.h"
 
20
    #include "configmanager.h"
 
21
    #include "projectbuildtarget.h"
 
22
#endif
 
23
 
 
24
#include "cbauibook.h"
 
25
 
 
26
#include "ThreadSearchThread.h"
 
27
#include "ThreadSearchEvent.h"
 
28
#include "TextFileSearcher.h"
 
29
 
 
30
ThreadSearchThread::ThreadSearchThread(ThreadSearchView*           pThreadSearchView,
 
31
                                       const ThreadSearchFindData& findData)
 
32
{
 
33
    m_pThreadSearchView = pThreadSearchView;
 
34
    m_FindData          = findData;
 
35
 
 
36
    // If wxDIR_IGNORE is used, we don't recurse in sub directories during directory search
 
37
    m_DefaultDirResult  = (findData.GetRecursiveSearch() == true) ? wxDIR_CONTINUE : wxDIR_IGNORE;
 
38
 
 
39
    // File patterns separator is ';'
 
40
    m_Masks             = GetArrayFromString(m_FindData.GetSearchMask());
 
41
    if ( m_Masks.GetCount() == 0 )
 
42
    {
 
43
        m_Masks.Add(_T("*"));
 
44
    }
 
45
    m_pTextFileSearcher = TextFileSearcher::BuildTextFileSearcher(findData.GetFindText(),
 
46
                                                                  findData.GetMatchCase(),
 
47
                                                                  findData.GetStartWord(),
 
48
                                                                  findData.GetMatchWord(),
 
49
                                                                  findData.GetRegEx());
 
50
    if (!m_pTextFileSearcher)
 
51
    {
 
52
        ThreadSearchEvent event(wxEVT_THREAD_SEARCH_ERROR, -1);
 
53
        event.SetString(_("TextFileSearcher could not be instantiated."));
 
54
 
 
55
        // Using wxPostEvent, we avoid multi-threaded memory violation.
 
56
        wxPostEvent( m_pThreadSearchView,event);
 
57
    }
 
58
    ConfigManager* pCfg = Manager::Get()->GetConfigManager(_T("ThreadSearch"));
 
59
    m_ShowFileMissingError=pCfg->ReadBool(wxT("/ShowFileMissingError"),true);
 
60
    m_ShowCantOpenFileError=pCfg->ReadBool(wxT("/ShowCantOpenFileError"),true);
 
61
 
 
62
}
 
63
 
 
64
 
 
65
ThreadSearchThread::~ThreadSearchThread()
 
66
{
 
67
    //dtor
 
68
    if ( m_pTextFileSearcher != NULL )
 
69
    {
 
70
        delete m_pTextFileSearcher;
 
71
    }
 
72
}
 
73
 
 
74
 
 
75
void *ThreadSearchThread::Entry()
 
76
{
 
77
    // Tests if we have a working searcher object.
 
78
    // Cancel search if it is not the case
 
79
    if ( m_pTextFileSearcher == NULL )
 
80
        return 0;
 
81
 
 
82
    size_t i = 0;
 
83
 
 
84
    // For now, we look for all paths for the different search scopes
 
85
    // and store them in a sorted array to avoid pasing several times
 
86
    // the same file.
 
87
    // This will be changed to avoid consuming a lot of memory (parsing
 
88
    // form C:\ and storing all paths...). Aim is to avoid the use of the
 
89
    // array for storing items.
 
90
 
 
91
    // Search in directory files ?
 
92
    if ( m_FindData.MustSearchInDirectory() == true )
 
93
    {
 
94
        int flags = wxDIR_FILES | wxDIR_DIRS | wxDIR_DOTDOT;
 
95
        flags    |= m_FindData.GetHiddenSearch() ? wxDIR_HIDDEN : 0;
 
96
 
 
97
        const wxString &path = m_FindData.GetSearchPath(true);
 
98
        if (!wxDir::Exists(path))
 
99
        {
 
100
            ThreadSearchEvent event(wxEVT_THREAD_SEARCH_ERROR, -1);
 
101
            event.SetString(_("Cannot open folder ") + path);
 
102
 
 
103
            // Using wxPostEvent, we avoid multi-threaded memory violation.
 
104
            wxPostEvent(m_pThreadSearchView,event);
 
105
            return 0;
 
106
        }
 
107
        else
 
108
        {
 
109
            wxDir Dir(path);
 
110
            Dir.Traverse(*(static_cast<wxDirTraverser*>(this)), wxEmptyString, flags);
 
111
        }
 
112
 
 
113
        // Tests thread stop (cancel search, app shutdown)
 
114
        if ( TestDestroy() == true ) return 0;
 
115
    }
 
116
 
 
117
    // Search in workspace files ?
 
118
    if ( m_FindData.MustSearchInWorkspace() == true )
 
119
    {
 
120
        ProjectsArray* pProjectsArray = Manager::Get()->GetProjectManager()->GetProjects();
 
121
        for ( size_t j=0; j < pProjectsArray->GetCount(); ++j )
 
122
        {
 
123
            AddProjectFiles(m_FilePaths, *pProjectsArray->Item(j));
 
124
            if ( TestDestroy() == true ) return 0;
 
125
        }
 
126
    }
 
127
    else if ( m_FindData.MustSearchInProject() == true )
 
128
    {
 
129
        // Search in project files ?
 
130
        // Necessary only if not already parsed in worspace part
 
131
        cbProject* pProject = Manager::Get()->GetProjectManager()->GetActiveProject();
 
132
        if ( pProject != NULL )
 
133
        {
 
134
            AddProjectFiles(m_FilePaths, *pProject);
 
135
            if ( TestDestroy() == true ) return 0;
 
136
        }
 
137
    }
 
138
    else if ( m_FindData.MustSearchInTarget() == true )
 
139
    {
 
140
        // Search in target files ?
 
141
        // Necessary only if not already parsed in project part
 
142
        cbProject* pProject = Manager::Get()->GetProjectManager()->GetActiveProject();
 
143
        if ( pProject != NULL )
 
144
        {
 
145
            ProjectBuildTarget *pTarget = pProject->GetBuildTarget(pProject->GetActiveBuildTarget());
 
146
            if ( pTarget != 0 )
 
147
            {
 
148
                AddTargetFiles(m_FilePaths, *pTarget);
 
149
                if ( TestDestroy() == true ) return 0;
 
150
            }
 
151
        }
 
152
    }
 
153
 
 
154
    // Tests thread stop (cancel search, app shutdown)
 
155
    if ( TestDestroy() == true ) return 0;
 
156
 
 
157
    // Open files
 
158
    if ( m_FindData.MustSearchInOpenFiles() == true )
 
159
    {
 
160
        EditorManager* pEdManager = Manager::Get()->GetEditorManager();
 
161
        for (i = 0; i < (size_t)pEdManager->GetNotebook()->GetPageCount(); ++i)
 
162
        {
 
163
            cbEditor* pEditor = pEdManager->GetBuiltinEditor(i);
 
164
            if ( pEditor != NULL )
 
165
            {
 
166
                AddNewItem(m_FilePaths, pEditor->GetFilename());
 
167
            }
 
168
        }
 
169
    }
 
170
 
 
171
    // Tests thread stop (cancel search, app shutdown)
 
172
    if ( TestDestroy() == true ) return 0;
 
173
 
 
174
    // if the list is empty, leave
 
175
    if (m_FilePaths.GetCount() == 0)
 
176
    {
 
177
        //-cbMessageBox(wxT("No files to search in!"), wxT("Error"), wxICON_WARNING);
 
178
        ////(pecan 2008/4/26)
 
179
        // DO NOT issue graphics calls from this thread !!!!!!
 
180
        ThreadSearchEvent event(wxEVT_THREAD_SEARCH_ERROR, -1);
 
181
        event.SetString(_("No files to search.\nCheck options "));
 
182
        // Using wxPostEvent, we avoid multi-threaded memory violation.
 
183
        wxPostEvent(m_pThreadSearchView,event);
 
184
        return 0;
 
185
    }
 
186
 
 
187
    for ( i = 0; i < m_FilePaths.GetCount(); ++i )
 
188
    {
 
189
        FindInFile(m_FilePaths[i]);
 
190
 
 
191
        // Tests thread stop (cancel search, app shutdown)
 
192
        if ( TestDestroy() == true ) return 0;
 
193
    }
 
194
 
 
195
    return 0;
 
196
}
 
197
 
 
198
 
 
199
void ThreadSearchThread::OnExit()
 
200
{
 
201
    // Method is called automatically by wxWidgets framework
 
202
    // We inform thread caller about its termination.
 
203
    m_pThreadSearchView->OnThreadExit();
 
204
}
 
205
 
 
206
 
 
207
wxDirTraverseResult ThreadSearchThread::OnDir(const wxString& WXUNUSED(dirName))
 
208
{
 
209
    // Method is just used to test thread termination (user cancelled) and
 
210
    // stop recursive dir traversing if it is not required.
 
211
    if ( TestDestroy() == true )
 
212
    {
 
213
        return wxDIR_STOP;
 
214
    }
 
215
    return m_DefaultDirResult;
 
216
}
 
217
 
 
218
 
 
219
wxDirTraverseResult ThreadSearchThread::OnFile(const wxString& fileName)
 
220
{
 
221
    // Tests thread termination (user cancelled)
 
222
    if ( TestDestroy() == true )
 
223
    {
 
224
        return wxDIR_STOP;
 
225
    }
 
226
 
 
227
    // Looks if current file matches one of the file patterns
 
228
    for (size_t i = 0; i < m_Masks.GetCount(); ++i)
 
229
    {
 
230
        if ( fileName.Matches(m_Masks[i].c_str() ) )
 
231
        {
 
232
            // Adds it to list of files to parse
 
233
            m_FilePaths.Add(fileName);
 
234
            break;
 
235
        }
 
236
    }
 
237
 
 
238
    return wxDIR_CONTINUE;
 
239
}
 
240
 
 
241
 
 
242
void ThreadSearchThread::FindInFile(const wxString& path)
 
243
{
 
244
    m_LineTextArray.Empty();
 
245
 
 
246
    switch ( m_pTextFileSearcher->FindInFile(path, m_LineTextArray) )
 
247
    {
 
248
        case TextFileSearcher::idStringFound:
 
249
        {
 
250
            ThreadSearchEvent event(wxEVT_THREAD_SEARCH, -1);
 
251
            event.SetString(path);
 
252
            event.SetLineTextArray(m_LineTextArray);
 
253
 
 
254
            // Using wxPostEvent, we avoid multi-threaded memory violation.
 
255
            m_pThreadSearchView->PostThreadSearchEvent(event);
 
256
            break;
 
257
        }
 
258
        case TextFileSearcher::idStringNotFound:
 
259
        {
 
260
            break;
 
261
        }
 
262
        case TextFileSearcher::idFileNotFound:
 
263
        {
 
264
            if(m_ShowFileMissingError)
 
265
            {
 
266
                ThreadSearchEvent event(wxEVT_THREAD_SEARCH_ERROR, -1);
 
267
                event.SetString(path + _(" does not exist."));
 
268
 
 
269
                // Using wxPostEvent, we avoid multi-threaded memory violation.
 
270
                wxPostEvent( m_pThreadSearchView,event);
 
271
            }
 
272
            break;
 
273
        }
 
274
        case TextFileSearcher::idFileOpenError:
 
275
        {
 
276
            if(m_ShowCantOpenFileError)
 
277
            {
 
278
                ThreadSearchEvent event(wxEVT_THREAD_SEARCH_ERROR, -1);
 
279
                event.SetString(_("Failed to open ") + path);
 
280
 
 
281
                // Using wxPostEvent, we avoid multi-threaded memory violation.
 
282
                wxPostEvent( m_pThreadSearchView,event);
 
283
            }
 
284
            break;
 
285
        }
 
286
        default:
 
287
        {
 
288
        }
 
289
    }
 
290
}
 
291
 
 
292
 
 
293
bool ThreadSearchThread::AddNewItem(wxSortedArrayString& sortedArrayString, const wxString& newItem)
 
294
{
 
295
    // Adds item to array only if it does not exist
 
296
    bool added = false;
 
297
    if ( sortedArrayString.Index(newItem.c_str()) == wxNOT_FOUND )
 
298
    {
 
299
        sortedArrayString.Add(newItem);
 
300
        added = true;
 
301
    }
 
302
    return added;
 
303
}
 
304
 
 
305
 
 
306
void ThreadSearchThread::AddProjectFiles(wxSortedArrayString& sortedArrayString, cbProject& project)
 
307
{
 
308
    // Adds project file paths to array only if they do not already exist.
 
309
    // Same path may exist if we parse both open files and project files
 
310
    // for examle.
 
311
    for (FilesList::iterator it = project.GetFilesList().begin(); it != project.GetFilesList().end(); ++it)
 
312
    {
 
313
        AddNewItem(sortedArrayString, (*it)->file.GetFullPath());
 
314
        if ( TestDestroy() == true ) return;
 
315
    }
 
316
}
 
317
 
 
318
 
 
319
void ThreadSearchThread::AddTargetFiles(wxSortedArrayString& sortedArrayString, ProjectBuildTarget& target)
 
320
{
 
321
    // Adds target file paths to array only if they do not already exist.
 
322
    // Same path may exist if we parse both open files and target files
 
323
    // for examle.
 
324
    for (FilesList::iterator it = target.GetFilesList().begin(); it != target.GetFilesList().end(); it++)
 
325
    {
 
326
        ProjectFile* pf = *it;
 
327
        AddNewItem(sortedArrayString, pf->file.GetFullPath());
 
328
        if ( TestDestroy() == true ) return;
 
329
    }
 
330
}
 
331
 
 
332
 
 
333