~ubuntu-branches/ubuntu/raring/codeblocks/raring-proposed

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-08-09 04:38:38 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20100809043838-a59ygguym4eg0jgw
Tags: 10.05-0ubuntu1
* New upstream release. Closes (LP: #322350)
 - Switch to dpkg-source 3.0 (quilt) format
 - Remove unneeded README.source
 - Add debian/get-source-orig script that removes all
   Windows prebuilt binaries
* Bump Standards-Version to 3.9.1
 - Stop shipping *.la files
* debian/control
 - Add cdbs package as Build-Depend
 - Add libbz2-dev and zlib1g-dev packages as
   Build-Depends (needed by libhelp_plugin.so)
 - Remove dpatch package of Build-Depends
 - Add codeblocks-contrib-debug package
 - Split architecture-independent files of codeblocks
   package in codeblocks-common package
* debian/rules
 - Switch to CDBS rules system
 - Add parallel build support
 - Add a call to debian/get-source-orig script
 - Use lzma compression (saves 23,5 MB of free space)
* debian/patches
 - Refresh 01_codeblocks_plugin_path
 - Add 02_no_Makefiles_in_debian_dir to remove any link
   in codeblocks build system to deleted Makefiles of debian directory
 - Drop 02_ftbfs_gcc44 and 03_ftbfs_glib221 (merged in upstream)
* debian/watch
 - Update to use the new host (berlios.de)

Show diffs side-by-side

added added

removed removed

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