2
* This file is part of lib_finder plugin for Code::Blocks Studio
3
* Copyright (C) 2006-2007 Bartlomiej Swiecki
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.
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.
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
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 $
24
#include "processingdlg.h"
26
#include <wx/arrstr.h>
28
#include <wx/filename.h>
29
#include <wx/tokenzr.h>
30
#include <wx/filename.h>
33
//(*InternalHeaders(ProcessingDlg)
35
#include <wx/string.h>
38
#include "libraryconfigmanager.h"
39
#include "resultmap.h"
40
#include "lib_finder.h"
42
//(*IdInit(ProcessingDlg)
43
const long ProcessingDlg::ID_STATICTEXT1 = wxNewId();
44
const long ProcessingDlg::ID_GAUGE1 = wxNewId();
45
const long ProcessingDlg::ID_BUTTON1 = wxNewId();
48
BEGIN_EVENT_TABLE(ProcessingDlg,wxDialog)
49
//(*EventTable(ProcessingDlg)
53
ProcessingDlg::ProcessingDlg(wxWindow* parent,LibraryConfigManager& Manager,TypedResults& KnownResults,ResultMap& FoundResults,wxWindowID id):
56
m_KnownResults(KnownResults),
57
m_FoundResults(FoundResults)
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);
74
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ProcessingDlg::OnButton1Click);
78
ProcessingDlg::~ProcessingDlg()
82
void ProcessingDlg::OnButton1Click(wxCommandEvent& event)
88
bool ProcessingDlg::ReadDirs(const wxArrayString& Dirs)
90
Gauge1->SetRange(Dirs.Count());
91
for ( size_t i = 0; i<Dirs.Count(); i++ )
93
if ( StopFlag ) return false;
96
wxString DirName = Dirs[i];
97
if ( DirName.empty() ) continue;
99
// Cutting off last character if it is path separator
100
wxChar LastChar = DirName[DirName.Len()-1];
101
if ( wxFileName::GetPathSeparators().Find(LastChar) != -1 )
103
DirName.RemoveLast();
106
// Reading dir content
112
void ProcessingDlg::ReadDir(const wxString& DirName)
116
if ( !Dir.IsOpened() ) return;
118
Status->SetLabel(_T("Reading dir: ") + DirName);
120
if ( StopFlag ) return;
124
if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_FILES|wxDIR_HIDDEN) )
128
Map[Name].Add(DirName + wxFileName::GetPathSeparator() + Name);
130
while ( Dir.GetNext(&Name) );
133
if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_DIRS|wxDIR_HIDDEN) )
137
Map[Name].Add(DirName + wxFileName::GetPathSeparator() + Name);
138
ReadDir(DirName + wxFileName::GetPathSeparator() + Name);
140
while ( Dir.GetNext(&Name) );
144
bool ProcessingDlg::ProcessLibs()
146
Gauge1->SetRange(m_Manager.GetLibraryCount());
148
for ( int i=0; i<m_Manager.GetLibraryCount(); ++i )
150
if ( StopFlag ) return false;
151
ProcessLibrary(m_Manager.GetLibrary(i));
157
void ProcessingDlg::ProcessLibrary(const LibraryConfig* Config)
161
_("Searching library \"%s\""),
162
Config->ShortCode.c_str()));
164
CheckFilter(_T(""),wxStringStringMap(),wxArrayString(),Config,0);
167
void ProcessingDlg::CheckFilter(
168
const wxString& OldBasePath,
169
const wxStringStringMap& OldVars,
170
const wxArrayString& OldCompilers,
171
const LibraryConfig* Config,
174
if ( (int)Config->Filters.size() <= WhichFilter )
176
FoundLibrary(OldBasePath,OldVars,OldCompilers,Config);
180
const LibraryFilter& Filter = Config->Filters[WhichFilter];
182
switch ( Filter.Type )
184
case LibraryFilter::File:
187
wxArrayString Pattern;
188
SplitPath(Filter.Value,Pattern);
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;
194
// Process those files
195
for ( size_t i=0; i<PathArray.Count(); i++ )
198
wxStringStringMap Vars = OldVars;
199
SplitPath(PathArray[i],Path);
201
int path_index = (int)Path.Count() - 1;
202
int pattern_index = (int)Pattern.Count() - 1;
204
// Check if patterns do match
205
while ( ( path_index >= 0 ) && ( pattern_index >= 0 ) )
207
wxString& PatternPart = Pattern[pattern_index];
208
if ( IsVariable(PatternPart) )
210
wxString VarName = PatternPart.Mid(3,PatternPart.Len()-4);
211
if ( Vars[VarName].empty() )
213
Vars[VarName] = Path[path_index];
217
if ( Vars[VarName] != Path[path_index] ) break;
222
if ( PatternPart != Path[path_index] ) break;
228
// This is when patterns did not match
229
if ( pattern_index >= 0 ) continue;
231
// Construct base path from the rest of file's name
233
for ( int j=0; j<=path_index; j++ )
235
BasePath += Path[j] + wxFileName::GetPathSeparator();
238
// And check if base path match the previous one
239
if ( !OldBasePath.IsEmpty() )
241
if ( BasePath != OldBasePath ) continue;
244
// Ok, this filter matches, let's advance to next filet
245
CheckFilter(BasePath,Vars,OldCompilers,Config,WhichFilter+1);
250
case LibraryFilter::Platform:
252
wxStringTokenizer Tokenizer(Filter.Value,_T("| \t"));
253
bool IsPlatform = false;
254
while ( Tokenizer.HasMoreTokens() )
256
wxString Platform = Tokenizer.GetNextToken();
258
if ( platform::windows )
260
if ( Platform==_T("win") || Platform==_T("windows") )
267
if ( platform::macosx )
269
if ( Platform==_T("mac") || Platform==_T("macosx") )
276
if ( platform::linux )
278
if ( Platform==_T("lin") || Platform==_T("linux") )
285
if ( platform::freebsd )
287
if ( Platform==_T("bsd") || Platform==_T("freebsd") )
294
if ( platform::netbsd )
296
if ( Platform==_T("bsd") || Platform==_T("netbsd") )
303
if ( platform::openbsd )
305
if ( Platform==_T("bsd") || Platform==_T("openbsd") )
312
if ( platform::darwin )
314
if ( Platform==_T("darwin") )
321
if ( platform::solaris )
323
if ( Platform==_T("solaris") )
330
if ( platform::unix )
332
if ( Platform==_T("unix") || Platform==_T("un*x") )
342
CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
347
case LibraryFilter::Exec:
350
if ( wxIsAbsolutePath(Filter.Value) )
352
// If this is absolute path, we don't search in PATH evironment var
353
IsExec = wxFileName::IsFileExecutable(Filter.Value);
357
// Let's search for the name in search paths
359
if ( wxGetEnv(_T("PATH"),&Path) )
361
wxString Splitter = _T(":");
362
if ( platform::windows ) Splitter = _T(";");
363
wxStringTokenizer Tokenizer(Path,Splitter);
364
while ( Tokenizer.HasMoreTokens() )
366
wxString OnePath = Tokenizer.GetNextToken();
368
// Let's skip relative paths (f.ex. ".")
369
if ( !wxIsAbsolutePath(OnePath) ) continue;
371
OnePath += wxFileName::GetPathSeparator()+Filter.Value;
372
if ( wxFileName::IsFileExecutable(OnePath) )
383
CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
388
case LibraryFilter::PkgConfig:
390
if ( m_KnownResults[rtPkgConfig].IsShortCode(Filter.Value) )
392
CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
397
case LibraryFilter::Compiler:
399
if ( OldCompilers.IsEmpty() )
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);
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() )
412
wxString Comp = Tokenizer.GetNextToken();
413
if ( OldCompilers.Index(Comp) != wxNOT_FOUND )
419
if ( !Compilers.IsEmpty() )
421
CheckFilter(OldBasePath,OldVars,Compilers,Config,WhichFilter+1);
427
case LibraryFilter::None:
429
CheckFilter(OldBasePath,OldVars,OldCompilers,Config,WhichFilter+1);
435
void ProcessingDlg::SplitPath(const wxString& FileName,wxArrayString& Split)
437
wxStringTokenizer Tknz(FileName,_T("\\/"));
438
while (Tknz.HasMoreTokens()) Split.Add(Tknz.GetNextToken());
441
bool ProcessingDlg::IsVariable(const wxString& NamePart) const
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;
451
void ProcessingDlg::FoundLibrary(const wxString& OldBasePath,const wxStringStringMap& OldVars,const wxArrayString& Compilers,const LibraryConfig* Config)
453
wxStringStringMap Vars = OldVars;
454
wxString BasePath = OldBasePath;
456
BasePath.RemoveLast();
457
Vars[_T("BASE_DIR")] = BasePath;
458
LibraryResult* Result = new LibraryResult();
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);
467
Result->Compilers = Compilers;
468
Result->Categories = Config->Categories;
470
for ( size_t i=0; i<Config->IncludePaths.Count(); i++ )
472
Result->IncludePath.Add(FixPath(FixVars(Config->IncludePaths[i],Vars)));
475
for ( size_t i=0; i<Config->LibPaths.Count(); i++ )
477
Result->LibPath.Add(FixPath(FixVars(Config->LibPaths[i],Vars)));
480
for ( size_t i=0; i<Config->ObjPaths.Count(); i++ )
482
Result->ObjPath.Add(FixPath(FixVars(Config->ObjPaths[i],Vars)));
485
for ( size_t i=0; i<Config->Libs.Count(); i++ )
487
Result->Libs.Add(FixVars(Config->Libs[i],Vars));
490
for ( size_t i=0; i<Config->Defines.Count(); i++ )
492
Result->Defines.Add(FixVars(Config->Defines[i],Vars));
495
for ( size_t i=0; i<Config->CFlags.Count(); i++ )
497
Result->CFlags.Add(FixVars(Config->CFlags[i],Vars));
500
for ( size_t i=0; i<Config->LFlags.Count(); i++ )
502
Result->LFlags.Add(FixVars(Config->LFlags[i],Vars));
505
ResultArray& Array = m_FoundResults.GetShortCode(Config->ShortCode);
509
wxString ProcessingDlg::FixVars(wxString Original,const wxStringStringMap& Vars)
511
for ( wxStringStringMap::const_iterator it = Vars.begin();
515
wxString SearchString = _T("$(") + it->first + _T(")");
516
wxString ReplaceWith = it->second;
517
Original.Replace(SearchString,ReplaceWith);
523
wxString ProcessingDlg::FixPath(wxString Original)
525
return wxFileName(Original).GetFullPath();