~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/contrib/lib_finder/processingdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of lib_finder plugin for Code::Blocks Studio
 
3
* Copyright (C) 2006-2007  Bartlomiej Swiecki
 
4
*
 
5
* wxSmith is free software; you can redistribute it and/or modify
 
6
* it under the terms of the GNU General Public License as published by
 
7
* the Free Software Foundation; either version 2 of the License, or
 
8
* (at your option) any later version.
 
9
*
 
10
* wxSmith is distributed in the hope that it will be useful,
 
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
* GNU General Public License for more details.
 
14
*
 
15
* You should have received a copy of the GNU General Public License
 
16
* along with wxSmith; if not, write to the Free Software
 
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
18
*
 
19
* $Revision: 4504 $
 
20
* $Id: wxsmithpluginregistrants.cpp 4504 2007-10-02 21:52:30Z byo $
 
21
* $HeadURL: svn+ssh://byo@svn.berlios.de/svnroot/repos/codeblocks/trunk/src/plugins/contrib/wxSmith/plugin/wxsmithpluginregistrants.cpp $
 
22
*/
 
23
 
 
24
#include "processingdlg.h"
 
25
 
 
26
#include <wx/arrstr.h>
 
27
#include <wx/dir.h>
 
28
#include <wx/filename.h>
 
29
#include <wx/tokenzr.h>
 
30
#include <wx/filename.h>
 
31
#include <prep.h>
 
32
 
 
33
//(*InternalHeaders(ProcessingDlg)
 
34
#include <wx/intl.h>
 
35
#include <wx/string.h>
 
36
//*)
 
37
 
 
38
#include "libraryconfigmanager.h"
 
39
#include "resultmap.h"
 
40
#include "lib_finder.h"
 
41
 
 
42
//(*IdInit(ProcessingDlg)
 
43
const long ProcessingDlg::ID_STATICTEXT1 = wxNewId();
 
44
const long ProcessingDlg::ID_GAUGE1 = wxNewId();
 
45
const long ProcessingDlg::ID_BUTTON1 = wxNewId();
 
46
//*)
 
47
 
 
48
BEGIN_EVENT_TABLE(ProcessingDlg,wxDialog)
 
49
        //(*EventTable(ProcessingDlg)
 
50
        //*)
 
51
END_EVENT_TABLE()
 
52
 
 
53
ProcessingDlg::ProcessingDlg(wxWindow* parent,LibraryConfigManager& Manager,TypedResults& KnownResults,ResultMap& FoundResults,wxWindowID id):
 
54
    StopFlag(false),
 
55
    m_Manager(Manager),
 
56
    m_KnownResults(KnownResults),
 
57
    m_FoundResults(FoundResults)
 
58
{
 
59
        //(*Initialize(ProcessingDlg)
 
60
        Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxCAPTION, _T("id"));
 
61
        FlexGridSizer1 = new wxFlexGridSizer(0, 1, 0, 0);
 
62
        StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, this, _("Processing"));
 
63
        Status = new wxStaticText(this, ID_STATICTEXT1, _("Waiting"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
 
64
        StaticBoxSizer1->Add(Status, 0, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
 
65
        Gauge1 = new wxGauge(this, ID_GAUGE1, 100, wxDefaultPosition, wxSize(402,12), 0, wxDefaultValidator, _T("ID_GAUGE1"));
 
66
        StaticBoxSizer1->Add(Gauge1, 1, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
67
        FlexGridSizer1->Add(StaticBoxSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
68
        StopBtn = new wxButton(this, ID_BUTTON1, _("Stop"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
 
69
        FlexGridSizer1->Add(StopBtn, 1, wxBOTTOM|wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
70
        SetSizer(FlexGridSizer1);
 
71
        FlexGridSizer1->Fit(this);
 
72
        FlexGridSizer1->SetSizeHints(this);
 
73
 
 
74
        Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ProcessingDlg::OnButton1Click);
 
75
        //*)
 
76
}
 
77
 
 
78
ProcessingDlg::~ProcessingDlg()
 
79
{
 
80
}
 
81
 
 
82
void ProcessingDlg::OnButton1Click(wxCommandEvent& event)
 
83
{
 
84
    StopBtn->Disable();
 
85
    StopFlag = true;
 
86
}
 
87
 
 
88
bool ProcessingDlg::ReadDirs(const wxArrayString& Dirs)
 
89
{
 
90
    Gauge1->SetRange(Dirs.Count());
 
91
    for ( size_t i = 0; i<Dirs.Count(); i++ )
 
92
    {
 
93
        if ( StopFlag ) return false;
 
94
        Gauge1->SetValue(i);
 
95
 
 
96
        wxString DirName = Dirs[i];
 
97
        if ( DirName.empty() ) continue;
 
98
 
 
99
        // Cutting off last character if it is path separator
 
100
        wxChar LastChar = DirName[DirName.Len()-1];
 
101
        if ( wxFileName::GetPathSeparators().Find(LastChar) != -1 )
 
102
        {
 
103
            DirName.RemoveLast();
 
104
        }
 
105
 
 
106
        // Reading dir content
 
107
        ReadDir(DirName);
 
108
    }
 
109
    return !StopFlag;
 
110
}
 
111
 
 
112
void ProcessingDlg::ReadDir(const wxString& DirName)
 
113
{
 
114
    wxDir Dir(DirName);
 
115
 
 
116
    if ( !Dir.IsOpened() ) return;
 
117
 
 
118
    Status->SetLabel(_T("Reading dir: ") + DirName);
 
119
    ::wxYield();
 
120
    if ( StopFlag ) return;
 
121
 
 
122
    wxString Name;
 
123
 
 
124
    if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_FILES|wxDIR_HIDDEN) )
 
125
    {
 
126
        do
 
127
        {
 
128
            Map[Name].Add(DirName + wxFileName::GetPathSeparator() + Name);
 
129
        }
 
130
        while ( Dir.GetNext(&Name) );
 
131
    }
 
132
 
 
133
    if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_DIRS|wxDIR_HIDDEN) )
 
134
    {
 
135
        do
 
136
        {
 
137
            Map[Name].Add(DirName + wxFileName::GetPathSeparator() + Name);
 
138
            ReadDir(DirName + wxFileName::GetPathSeparator() + Name);
 
139
        }
 
140
        while ( Dir.GetNext(&Name) );
 
141
    }
 
142
}
 
143
 
 
144
bool ProcessingDlg::ProcessLibs()
 
145
{
 
146
    Gauge1->SetRange(m_Manager.GetLibraryCount());
 
147
 
 
148
    for ( int i=0; i<m_Manager.GetLibraryCount(); ++i )
 
149
    {
 
150
        if ( StopFlag ) return false;
 
151
        ProcessLibrary(m_Manager.GetLibrary(i));
 
152
    }
 
153
 
 
154
    return !StopFlag;
 
155
}
 
156
 
 
157
void ProcessingDlg::ProcessLibrary(const LibraryConfig* Config)
 
158
{
 
159
    Status->SetLabel(
 
160
        wxString::Format(
 
161
            _("Searching library \"%s\""),
 
162
            Config->ShortCode.c_str()));
 
163
 
 
164
    CheckFilter(_T(""),wxStringStringMap(),wxArrayString(),Config,0);
 
165
}
 
166
 
 
167
void ProcessingDlg::CheckFilter(
 
168
    const wxString& OldBasePath,
 
169
    const wxStringStringMap& OldVars,
 
170
    const wxArrayString& OldCompilers,
 
171
    const LibraryConfig* Config,
 
172
    int WhichFilter)
 
173
{
 
174
    if ( (int)Config->Filters.size() <= WhichFilter )
 
175
    {
 
176
        FoundLibrary(OldBasePath,OldVars,OldCompilers,Config);
 
177
        return;
 
178
    }
 
179
 
 
180
    const LibraryFilter& Filter = Config->Filters[WhichFilter];
 
181
 
 
182
    switch ( Filter.Type )
 
183
    {
 
184
        case LibraryFilter::File:
 
185
        {
 
186
            // Split path
 
187
            wxArrayString Pattern;
 
188
            SplitPath(Filter.Value,Pattern);
 
189
 
 
190
            // Fetch list of files with filename matching last pattern's element
 
191
            const wxArrayString& PathArray = Map[Pattern[Pattern.Count()-1]];
 
192
            if ( PathArray.empty() ) return;
 
193
 
 
194
            // Process those files
 
195
            for ( size_t i=0; i<PathArray.Count(); i++ )
 
196
            {
 
197
                wxArrayString Path;
 
198
                wxStringStringMap Vars = OldVars;
 
199
                SplitPath(PathArray[i],Path);
 
200
 
 
201
                int path_index = (int)Path.Count() - 1;
 
202
                int pattern_index = (int)Pattern.Count() - 1;
 
203
 
 
204
                // Check if patterns do match
 
205
                while ( ( path_index >= 0 ) && ( pattern_index >= 0 ) )
 
206
                {
 
207
                    wxString& PatternPart = Pattern[pattern_index];
 
208
                    if ( IsVariable(PatternPart) )
 
209
                    {
 
210
                        wxString VarName = PatternPart.Mid(3,PatternPart.Len()-4);
 
211
                        if ( Vars[VarName].empty() )
 
212
                        {
 
213
                            Vars[VarName] = Path[path_index];
 
214
                        }
 
215
                        else
 
216
                        {
 
217
                            if ( Vars[VarName] != Path[path_index] ) break;
 
218
                        }
 
219
                    }
 
220
                    else
 
221
                    {
 
222
                        if ( PatternPart != Path[path_index] ) break;
 
223
                    }
 
224
                    path_index--;
 
225
                    pattern_index--;
 
226
                }
 
227
 
 
228
                // This is when patterns did not match
 
229
                if ( pattern_index >= 0 ) continue;
 
230
 
 
231
                // Construct base path from the rest of file's name
 
232
                wxString BasePath;
 
233
                for ( int j=0; j<=path_index; j++ )
 
234
                {
 
235
                    BasePath += Path[j] + wxFileName::GetPathSeparator();
 
236
                }
 
237
 
 
238
                // And check if base path match the previous one
 
239
                if ( !OldBasePath.IsEmpty() )
 
240
                {
 
241
                    if ( BasePath != OldBasePath ) continue;
 
242
                }
 
243
 
 
244
                // Ok, this filter matches, let's advance to next filet
 
245
                CheckFilter(BasePath,Vars,OldCompilers,Config,WhichFilter+1);
 
246
            }
 
247
            break;
 
248
        }
 
249
 
 
250
        case LibraryFilter::Platform:
 
251
        {
 
252
            wxStringTokenizer Tokenizer(Filter.Value,_T("| \t"));
 
253
            bool IsPlatform = false;
 
254
            while ( Tokenizer.HasMoreTokens() )
 
255
            {
 
256
                wxString Platform = Tokenizer.GetNextToken();
 
257
 
 
258
                if ( platform::windows )
 
259
                {
 
260
                    if ( Platform==_T("win") || Platform==_T("windows") )
 
261
                    {
 
262
                        IsPlatform = true;
 
263
                        break;
 
264
                    }
 
265
                }
 
266
 
 
267
                if ( platform::macosx )
 
268
                {
 
269
                    if ( Platform==_T("mac") || Platform==_T("macosx") )
 
270
                    {
 
271
                        IsPlatform = true;
 
272
                        break;
 
273
                    }
 
274
                }
 
275
 
 
276
                if ( platform::linux )
 
277
                {
 
278
                    if ( Platform==_T("lin") || Platform==_T("linux") )
 
279
                    {
 
280
                        IsPlatform = true;
 
281
                        break;
 
282
                    }
 
283
                }
 
284
 
 
285
                if ( platform::freebsd )
 
286
                {
 
287
                    if ( Platform==_T("bsd") || Platform==_T("freebsd") )
 
288
                    {
 
289
                        IsPlatform = true;
 
290
                        break;
 
291
                    }
 
292
                }
 
293
 
 
294
                if ( platform::netbsd )
 
295
                {
 
296
                    if ( Platform==_T("bsd") || Platform==_T("netbsd") )
 
297
                    {
 
298
                        IsPlatform = true;
 
299
                        break;
 
300
                    }
 
301
                }
 
302
 
 
303
                if ( platform::openbsd )
 
304
                {
 
305
                    if ( Platform==_T("bsd") || Platform==_T("openbsd") )
 
306
                    {
 
307
                        IsPlatform = true;
 
308
                        break;
 
309
                    }
 
310
                }
 
311
 
 
312
                if ( platform::darwin )
 
313
                {
 
314
                    if ( Platform==_T("darwin") )
 
315
                    {
 
316
                        IsPlatform = true;
 
317
                        break;
 
318
                    }
 
319
                }
 
320
 
 
321
                if ( platform::solaris )
 
322
                {
 
323
                    if ( Platform==_T("solaris") )
 
324
                    {
 
325
                        IsPlatform = true;
 
326
                        break;
 
327
                    }
 
328
                }
 
329
 
 
330
                if ( platform::unix )
 
331
                {
 
332
                    if ( Platform==_T("unix") || Platform==_T("un*x") )
 
333
                    {
 
334
                        IsPlatform = true;
 
335
                        break;
 
336
                    }
 
337
                }
 
338
            }
 
339
 
 
340
            if ( IsPlatform )
 
341
            {
 
342
                CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
 
343
            }
 
344
            break;
 
345
        }
 
346
 
 
347
        case LibraryFilter::Exec:
 
348
        {
 
349
            bool IsExec = false;
 
350
            if ( wxIsAbsolutePath(Filter.Value) )
 
351
            {
 
352
                // If this is absolute path, we don't search in PATH evironment var
 
353
                IsExec = wxFileName::IsFileExecutable(Filter.Value);
 
354
            }
 
355
            else
 
356
            {
 
357
                // Let's search for the name in search paths
 
358
                wxString Path;
 
359
                if ( wxGetEnv(_T("PATH"),&Path) )
 
360
                {
 
361
                    wxString Splitter = _T(":");
 
362
                    if ( platform::windows ) Splitter = _T(";");
 
363
                    wxStringTokenizer Tokenizer(Path,Splitter);
 
364
                    while ( Tokenizer.HasMoreTokens() )
 
365
                    {
 
366
                        wxString OnePath = Tokenizer.GetNextToken();
 
367
 
 
368
                        // Let's skip relative paths (f.ex. ".")
 
369
                        if ( !wxIsAbsolutePath(OnePath) ) continue;
 
370
 
 
371
                        OnePath += wxFileName::GetPathSeparator()+Filter.Value;
 
372
                        if ( wxFileName::IsFileExecutable(OnePath) )
 
373
                        {
 
374
                            IsExec = true;
 
375
                            break;
 
376
                        }
 
377
                    }
 
378
                }
 
379
            }
 
380
 
 
381
            if ( IsExec )
 
382
            {
 
383
                CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
 
384
            }
 
385
            break;
 
386
        }
 
387
 
 
388
        case LibraryFilter::PkgConfig:
 
389
        {
 
390
            if ( m_KnownResults[rtPkgConfig].IsShortCode(Filter.Value) )
 
391
            {
 
392
                CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
 
393
            }
 
394
            break;
 
395
        }
 
396
 
 
397
        case LibraryFilter::Compiler:
 
398
        {
 
399
            if ( OldCompilers.IsEmpty() )
 
400
            {
 
401
                // If this is the first compiler filter, let's build new list and continue
 
402
                CheckFilter(OldBasePath,OldVars,wxStringTokenize(Filter.Value,_T("| \t")),Config,WhichFilter+1);
 
403
            }
 
404
            else
 
405
            {
 
406
                // We've set compiler list before, leave only the intersection
 
407
                // of previous and current list
 
408
                wxArrayString Compilers;
 
409
                wxStringTokenizer Tokenizer(Filter.Value,_T("| \t"));
 
410
                while ( Tokenizer.HasMoreTokens() )
 
411
                {
 
412
                    wxString Comp = Tokenizer.GetNextToken();
 
413
                    if ( OldCompilers.Index(Comp) != wxNOT_FOUND )
 
414
                    {
 
415
                        Compilers.Add(Comp);
 
416
                    }
 
417
                }
 
418
 
 
419
                if ( !Compilers.IsEmpty() )
 
420
                {
 
421
                    CheckFilter(OldBasePath,OldVars,Compilers,Config,WhichFilter+1);
 
422
                }
 
423
            }
 
424
            break;
 
425
        }
 
426
 
 
427
        case LibraryFilter::None:
 
428
        {
 
429
            CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
 
430
            break;
 
431
        }
 
432
    }
 
433
}
 
434
 
 
435
void ProcessingDlg::SplitPath(const wxString& FileName,wxArrayString& Split)
 
436
{
 
437
    wxStringTokenizer Tknz(FileName,_T("\\/"));
 
438
    while (Tknz.HasMoreTokens()) Split.Add(Tknz.GetNextToken());
 
439
}
 
440
 
 
441
bool ProcessingDlg::IsVariable(const wxString& NamePart) const
 
442
{
 
443
    if ( NamePart.Len() < 5 ) return false;
 
444
    if ( NamePart[0] != _T('*') ) return false;
 
445
    if ( NamePart[1] != _T('$') ) return false;
 
446
    if ( NamePart[2] != _T('(') ) return false;
 
447
    if ( NamePart[NamePart.Len()-1] != _T(')') ) return false;
 
448
    return true;
 
449
}
 
450
 
 
451
void ProcessingDlg::FoundLibrary(const wxString& OldBasePath,const wxStringStringMap& OldVars,const wxArrayString& Compilers,const LibraryConfig* Config)
 
452
{
 
453
    wxStringStringMap Vars = OldVars;
 
454
    wxString BasePath = OldBasePath;
 
455
 
 
456
    BasePath.RemoveLast();
 
457
    Vars[_T("BASE_DIR")] = BasePath;
 
458
    LibraryResult* Result = new LibraryResult();
 
459
 
 
460
    Result->Type = rtDetected;
 
461
    Result->ShortCode = Config->ShortCode;
 
462
    Result->LibraryName = FixVars(Config->LibraryName,Vars);
 
463
    Result->BasePath = FixPath(BasePath);
 
464
    Result->PkgConfigVar = Config->PkgConfigVar;
 
465
    Result->Description = FixVars(Config->Description,Vars);
 
466
 
 
467
    Result->Compilers = Compilers;
 
468
    Result->Categories = Config->Categories;
 
469
 
 
470
    for ( size_t i=0; i<Config->IncludePaths.Count(); i++ )
 
471
    {
 
472
        Result->IncludePath.Add(FixPath(FixVars(Config->IncludePaths[i],Vars)));
 
473
    }
 
474
 
 
475
    for ( size_t i=0; i<Config->LibPaths.Count(); i++ )
 
476
    {
 
477
        Result->LibPath.Add(FixPath(FixVars(Config->LibPaths[i],Vars)));
 
478
    }
 
479
 
 
480
    for ( size_t i=0; i<Config->ObjPaths.Count(); i++ )
 
481
    {
 
482
        Result->ObjPath.Add(FixPath(FixVars(Config->ObjPaths[i],Vars)));
 
483
    }
 
484
 
 
485
    for ( size_t i=0; i<Config->Libs.Count(); i++ )
 
486
    {
 
487
        Result->Libs.Add(FixVars(Config->Libs[i],Vars));
 
488
    }
 
489
 
 
490
    for ( size_t i=0; i<Config->Defines.Count(); i++ )
 
491
    {
 
492
        Result->Defines.Add(FixVars(Config->Defines[i],Vars));
 
493
    }
 
494
 
 
495
    for ( size_t i=0; i<Config->CFlags.Count(); i++ )
 
496
    {
 
497
        Result->CFlags.Add(FixVars(Config->CFlags[i],Vars));
 
498
    }
 
499
 
 
500
    for ( size_t i=0; i<Config->LFlags.Count(); i++ )
 
501
    {
 
502
        Result->LFlags.Add(FixVars(Config->LFlags[i],Vars));
 
503
    }
 
504
 
 
505
    ResultArray& Array = m_FoundResults.GetShortCode(Config->ShortCode);
 
506
    Array.Add(Result);
 
507
}
 
508
 
 
509
wxString ProcessingDlg::FixVars(wxString Original,const wxStringStringMap& Vars)
 
510
{
 
511
    for ( wxStringStringMap::const_iterator it = Vars.begin();
 
512
          it != Vars.end();
 
513
          ++it )
 
514
    {
 
515
        wxString SearchString = _T("$(") + it->first + _T(")");
 
516
        wxString ReplaceWith = it->second;
 
517
        Original.Replace(SearchString,ReplaceWith);
 
518
    }
 
519
 
 
520
    return Original;
 
521
}
 
522
 
 
523
wxString ProcessingDlg::FixPath(wxString Original)
 
524
{
 
525
    return wxFileName(Original).GetFullPath();
 
526
}