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

« back to all changes in this revision

Viewing changes to src/plugins/contrib/lib_finder/librarydetectionmanager.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:
 
1
/*
 
2
* This file is part of lib_finder plugin for Code::Blocks Studio
 
3
* Copyright (C) 2006-2008  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 <tinyxml/tinyxml.h>
 
25
#include <tinyxml/tinywxuni.h>
 
26
 
 
27
#include <wx/arrstr.h>
 
28
#include <wx/dir.h>
 
29
#include <wx/filename.h>
 
30
#include <wx/string.h>
 
31
 
 
32
#include <manager.h>
 
33
#include <configmanager.h>
 
34
 
 
35
#include "librarydetectionmanager.h"
 
36
 
 
37
LibraryDetectionManager::LibraryDetectionManager(TypedResults& CurrentResults): m_CurrentResults(CurrentResults)
 
38
{
 
39
}
 
40
 
 
41
LibraryDetectionManager::~LibraryDetectionManager()
 
42
{
 
43
    Clear();
 
44
}
 
45
 
 
46
void LibraryDetectionManager::Clear()
 
47
{
 
48
    for ( size_t i=0; i<Libraries.Count(); ++i )
 
49
    {
 
50
        delete Libraries[i];
 
51
    }
 
52
    Libraries.Clear();
 
53
}
 
54
 
 
55
int LibraryDetectionManager::LoadXmlConfig(const wxString& Path)
 
56
{
 
57
    wxDir Dir(Path);
 
58
    wxString Name;
 
59
    if ( !Dir.IsOpened() ) return 0;
 
60
 
 
61
    int loaded = 0;
 
62
    if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_DIRS|wxDIR_HIDDEN) )
 
63
    {
 
64
        do
 
65
        {
 
66
            loaded += LoadXmlConfig(Path+wxFileName::GetPathSeparator()+Name);
 
67
        }
 
68
        while ( Dir.GetNext(&Name) );
 
69
    }
 
70
 
 
71
    if ( Dir.GetFirst(&Name,wxEmptyString,wxDIR_FILES|wxDIR_HIDDEN) )
 
72
    {
 
73
        do
 
74
        {
 
75
            loaded += LoadXmlFile(Path+wxFileName::GetPathSeparator()+Name) ? 1 : 0;
 
76
        }
 
77
        while ( Dir.GetNext(&Name) );
 
78
    }
 
79
 
 
80
    return loaded;
 
81
}
 
82
 
 
83
int LibraryDetectionManager::LoadXmlFile(const wxString& Name)
 
84
{
 
85
    TiXmlDocument Doc;
 
86
    if ( !TinyXML::LoadDocument( Name, &Doc ) || Doc.Error() ) return 0;
 
87
    return LoadXmlDoc( Doc );
 
88
}
 
89
 
 
90
int LibraryDetectionManager::LoadXmlDoc( TiXmlDocument& Doc )
 
91
{
 
92
    int loaded = 0;
 
93
    for ( TiXmlElement* Elem = Doc.FirstChildElement("library");
 
94
          Elem;
 
95
          Elem = Elem->NextSiblingElement("library") )
 
96
    {
 
97
        // Load the version of this set
 
98
        int version = 0;
 
99
        if ( Elem->QueryIntAttribute( "version", &version ) != TIXML_SUCCESS )
 
100
        {
 
101
            version = 0;
 
102
        }
 
103
 
 
104
        // Load shortcode
 
105
        wxString ShortCode = wxString(Elem->Attribute("short_code"),wxConvUTF8);
 
106
        if ( ShortCode.IsEmpty() )
 
107
        {
 
108
            continue;
 
109
        }
 
110
 
 
111
        // Load name
 
112
        wxString Name = wxString( Elem->Attribute("name"), wxConvUTF8 );
 
113
        if ( Name.IsEmpty() )
 
114
        {
 
115
            continue;
 
116
        }
 
117
 
 
118
        // Check if we already have setting of this library
 
119
        // I'm to lazy to firbid const_cast here ;)
 
120
        LibraryDetectionConfigSet* OldSet = const_cast< LibraryDetectionConfigSet* > ( GetLibrary( ShortCode ) );
 
121
        LibraryDetectionConfigSet* NewSet = 0;
 
122
 
 
123
        if ( OldSet )
 
124
        {
 
125
            // There are detection settings yet, we override only when there's newer
 
126
            // or same version already
 
127
            if ( OldSet->Version > version )
 
128
            {
 
129
                // We do not upgrade
 
130
                continue;
 
131
            }
 
132
 
 
133
            OldSet->Categories.Clear();
 
134
            OldSet->Configurations.clear();
 
135
            OldSet->LibraryName.Clear();
 
136
            NewSet = OldSet;
 
137
        }
 
138
        else
 
139
        {
 
140
            NewSet = new LibraryDetectionConfigSet;
 
141
            Libraries.Add( NewSet );
 
142
        }
 
143
 
 
144
        // Setup configuration set
 
145
        NewSet->ShortCode = ShortCode;
 
146
        NewSet->Version = version;
 
147
        NewSet->LibraryName = Name;
 
148
 
 
149
        // Read categories of library
 
150
        for ( TiXmlAttribute* attr = Elem->FirstAttribute();
 
151
              attr;
 
152
              attr = attr->Next() )
 
153
        {
 
154
//            if ( !strncasecmp(attr->Name(),"category",8) )
 
155
            if ( !strncmp(attr->Name(),"category",8) )
 
156
            {
 
157
                NewSet->Categories.Add( wxString( attr->Value(),wxConvUTF8 ) );
 
158
            }
 
159
        }
 
160
 
 
161
        // Check if there's corresponding pkg-config entry
 
162
        if ( IsPkgConfigEntry(ShortCode) )
 
163
        {
 
164
            LibraryDetectionConfig Config;
 
165
            Config.PkgConfigVar = ShortCode;
 
166
            Config.Description = NewSet->LibraryName + _T(" (pkg-config)");
 
167
            LibraryDetectionFilter Filter;
 
168
            Filter.Type = LibraryDetectionFilter::PkgConfig;
 
169
            Filter.Value = ShortCode;
 
170
            Config.Filters.push_back(Filter);
 
171
            loaded += AddConfig(Config,NewSet) ? 1 : 0;
 
172
        }
 
173
 
 
174
        // Load libraries
 
175
        LibraryDetectionConfig Initial;
 
176
        loaded += LoadXml( Elem, Initial, NewSet );
 
177
    }
 
178
    return loaded;
 
179
}
 
180
 
 
181
int LibraryDetectionManager::LoadXml(TiXmlElement* Elem,LibraryDetectionConfig& Config,LibraryDetectionConfigSet* ConfigSet,bool Filters,bool Settings)
 
182
{
 
183
    wxString Description = wxString(Elem->Attribute("description"),wxConvUTF8);
 
184
    if ( !Description.empty() ) Config.Description = Description;
 
185
 
 
186
    int loaded = 0;
 
187
    for ( TiXmlElement* Data = Elem->FirstChildElement();
 
188
          Data;
 
189
          Data = Data->NextSiblingElement() )
 
190
    {
 
191
        wxString Node = wxString(Data->Value(),wxConvUTF8).MakeLower();
 
192
 
 
193
        if ( Filters && Settings )
 
194
        {
 
195
            // Load subnodes
 
196
            if ( Node == _T("filters") )
 
197
            {
 
198
                loaded += LoadXml(Data,Config,ConfigSet,true,false);
 
199
                continue;
 
200
            }
 
201
 
 
202
            if ( Node == _T("settings") )
 
203
            {
 
204
                loaded += LoadXml(Data,Config,ConfigSet,false,true);
 
205
                continue;
 
206
            }
 
207
 
 
208
            // pkgconfig does define both filter and setting
 
209
            if ( Node == _T("pkgconfig") )
 
210
            {
 
211
                Config.PkgConfigVar = wxString(Data->Attribute("name"),wxConvUTF8);
 
212
                LibraryDetectionFilter Filter;
 
213
                Filter.Type = LibraryDetectionFilter::PkgConfig;
 
214
                Filter.Value = Config.PkgConfigVar;
 
215
                Config.Filters.push_back(Filter);
 
216
                continue;
 
217
            }
 
218
        }
 
219
 
 
220
        if ( Filters )
 
221
        {
 
222
            // Load filter
 
223
            LibraryDetectionFilter::FilterType Type = LibraryDetectionFilter::None;
 
224
 
 
225
            if ( Node == _T("platform") ) Type = LibraryDetectionFilter::Platform; else
 
226
            if ( Node == _T("file") )     Type = LibraryDetectionFilter::File;     else
 
227
            if ( Node == _T("exec") )     Type = LibraryDetectionFilter::Exec;     else
 
228
            if ( Node == _T("compiler") ) Type = LibraryDetectionFilter::Compiler;
 
229
 
 
230
            if ( Type != LibraryDetectionFilter::None )
 
231
            {
 
232
                LibraryDetectionFilter Filter;
 
233
                Filter.Type = Type;
 
234
                Filter.Value = wxString(Data->Attribute("name"),wxConvUTF8);
 
235
                if ( !Filter.Value.IsEmpty() )
 
236
                {
 
237
                    Config.Filters.push_back(Filter);
 
238
                }
 
239
                continue;
 
240
            }
 
241
        }
 
242
 
 
243
        if ( Settings )
 
244
        {
 
245
            // Load setting
 
246
            if ( Node==_T("path") )
 
247
            {
 
248
                wxString Include = wxString(Data->Attribute("include"),wxConvUTF8);
 
249
                wxString Lib = wxString(Data->Attribute("lib"),wxConvUTF8);
 
250
                wxString Obj = wxString(Data->Attribute("obj"),wxConvUTF8);
 
251
                if ( !Include.empty() ) Config.IncludePaths.Add(Include);
 
252
                if ( !Lib.empty()     ) Config.LibPaths.Add(Lib);
 
253
                if ( !Obj.empty()     ) Config.ObjPaths.Add(Obj);
 
254
                continue;
 
255
            }
 
256
 
 
257
            if ( Node==_T("flags") )
 
258
            {
 
259
                wxString cFlags = wxString(Data->Attribute("cflags"),wxConvUTF8);
 
260
                wxString lFlags = wxString(Data->Attribute("lflags"),wxConvUTF8);
 
261
                if ( !cFlags.empty() ) Config.CFlags.Add(cFlags);
 
262
                if ( !lFlags.empty() ) Config.LFlags.Add(lFlags);
 
263
                continue;
 
264
            }
 
265
 
 
266
            if ( Node==_T("add") )
 
267
            {
 
268
                wxString cFlags = wxString(Data->Attribute("cflags"),wxConvUTF8);
 
269
                wxString lFlags = wxString(Data->Attribute("lflags"),wxConvUTF8);
 
270
                wxString Lib    = wxString(Data->Attribute("lib")   ,wxConvUTF8);
 
271
                wxString Define = wxString(Data->Attribute("define"),wxConvUTF8);
 
272
                if ( !cFlags.empty() ) Config.CFlags.Add(cFlags);
 
273
                if ( !lFlags.empty() ) Config.LFlags.Add(lFlags);
 
274
                if ( !Lib.empty()    ) Config.Libs.Add(Lib);
 
275
                if ( !Define.empty() ) Config.Defines.Add(Define);
 
276
            }
 
277
 
 
278
            if ( Node==_T("header") )
 
279
            {
 
280
                wxString file = wxString(Data->Attribute("file"),wxConvUTF8);
 
281
                if ( !file.empty() ) Config.Headers.Add(file);
 
282
            }
 
283
 
 
284
            if ( Node==_T("require") )
 
285
            {
 
286
                wxString lib = wxString(Data->Attribute("library"),wxConvUTF8);
 
287
                if ( !lib.empty() ) Config.Require.Add(lib);
 
288
            }
 
289
        }
 
290
    }
 
291
 
 
292
    if ( Settings && Filters )
 
293
    {
 
294
        TiXmlElement* Cfg = Elem->FirstChildElement("config");
 
295
        if ( Cfg )
 
296
        {
 
297
            // If there are any sub-configurations, let's
 
298
            // iterate through them and load config-specific settings
 
299
            for ( ;Cfg; Cfg = Cfg->NextSiblingElement("config") )
 
300
            {
 
301
                // Append sub-configuration data
 
302
                LibraryDetectionConfig Copy(Config);
 
303
                loaded += LoadXml(Cfg,Copy,ConfigSet);
 
304
            }
 
305
        }
 
306
        else
 
307
        {
 
308
            // No sub-config entry, so let's add this one
 
309
            loaded += AddConfig(Config,ConfigSet) ? 1 : 0;
 
310
        }
 
311
    }
 
312
    return loaded;
 
313
}
 
314
 
 
315
bool LibraryDetectionManager::CheckConfig(const LibraryDetectionConfig& Cfg) const
 
316
{
 
317
    if ( Cfg.Filters.empty() ) return false;
 
318
    return true;
 
319
}
 
320
 
 
321
const LibraryDetectionConfigSet* LibraryDetectionManager::GetLibrary(int Index)
 
322
{
 
323
    if ( Index < 0 ) return NULL;
 
324
    if ( Index >= GetLibraryCount() ) return NULL;
 
325
    return Libraries[Index];
 
326
}
 
327
 
 
328
const LibraryDetectionConfigSet* LibraryDetectionManager::GetLibrary(const wxString& ShortCode)
 
329
{
 
330
    for ( int i=0; i<GetLibraryCount(); i++ )
 
331
    {
 
332
        if ( Libraries[i]->ShortCode == ShortCode )
 
333
        {
 
334
            return Libraries[i];
 
335
        }
 
336
    }
 
337
    return 0;
 
338
}
 
339
 
 
340
bool LibraryDetectionManager::IsPkgConfigEntry(const wxString& Name)
 
341
{
 
342
    return m_CurrentResults[rtPkgConfig].IsShortCode(Name);
 
343
}
 
344
 
 
345
bool LibraryDetectionManager::AddConfig(LibraryDetectionConfig& Cfg,LibraryDetectionConfigSet* Set)
 
346
{
 
347
    if ( CheckConfig(Cfg) )
 
348
    {
 
349
        Set->Configurations.push_back(Cfg);
 
350
        return true;
 
351
    }
 
352
    return false;
 
353
}
 
354
 
 
355
bool LibraryDetectionManager::LoadSearchFilters()
 
356
{
 
357
    wxString Sep = wxFileName::GetPathSeparator();
 
358
 
 
359
    int loaded = 0;
 
360
    loaded += LoadXmlConfig(ConfigManager::GetFolder(sdDataGlobal) + Sep + _T("lib_finder"));
 
361
    loaded += LoadXmlConfig(ConfigManager::GetFolder(sdDataUser)   + Sep + _T("lib_finder"));
 
362
 
 
363
    return loaded>0;
 
364
}
 
365
 
 
366
int LibraryDetectionManager::StoreNewSettingsFile( const wxString& shortcut, const std::vector< char >& content )
 
367
{
 
368
    // Try to parse file's content
 
369
    TiXmlDocument doc;
 
370
    if ( !doc.Parse( &content[0] ) ) return -1;
 
371
 
 
372
    // Ensure that this file contains required shortcut
 
373
    if ( !doc.RootElement() ) return -1;
 
374
    if ( !doc.RootElement()->Attribute("short_code") ) return -1;
 
375
    if ( strcmp( doc.RootElement()->Attribute("short_code"), cbU2C(shortcut) ) ) return -1;
 
376
 
 
377
    // Finally load new data - this will make sure that we have valid xml structure
 
378
    int AddedConfigs = LoadXmlDoc( doc );
 
379
    if ( !AddedConfigs ) return -1;
 
380
 
 
381
    // Search for not-yet existing file name
 
382
    int i=0;
 
383
    wxString BaseName = ConfigManager::GetFolder(sdDataUser) + wxFileName::GetPathSeparator() + _T("lib_finder") + wxFileName::GetPathSeparator();
 
384
    if ( !wxFileName::Mkdir( BaseName, 0777, wxPATH_MKDIR_FULL ) )
 
385
    {
 
386
        return -2;
 
387
    }
 
388
    wxString FileName = BaseName + shortcut + _T(".xml");
 
389
    while ( wxFileName::FileExists( FileName ) || wxFileName::DirExists( FileName ) )
 
390
    {
 
391
        FileName = BaseName + shortcut + wxString::Format(_T("%d.xml"),i++);
 
392
    }
 
393
 
 
394
    // Store data
 
395
    wxFile fl( FileName, wxFile::write_excl );
 
396
    if ( !fl.IsOpened() )
 
397
    {
 
398
        return -2;
 
399
    }
 
400
    const char* ptr = &content[0];
 
401
    size_t len = strlen(ptr);
 
402
    if ( fl.Write( ptr, len ) != len )
 
403
    {
 
404
        return -2;
 
405
    }
 
406
 
 
407
    return AddedConfigs;
 
408
}