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

« back to all changes in this revision

Viewing changes to src/plugins/contrib/lib_finder/headersdetectordlg.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
//(*InternalHeaders(HeadersDetectorDlg)
 
3
#include <wx/gauge.h>
 
4
#include <wx/sizer.h>
 
5
#include <wx/button.h>
 
6
#include <wx/string.h>
 
7
#include <wx/intl.h>
 
8
#include <wx/stattext.h>
 
9
//*)
 
10
 
 
11
#include <encodingdetector.h>
 
12
#include "headersdetectordlg.h"
 
13
 
 
14
//(*IdInit(HeadersDetectorDlg)
 
15
const long HeadersDetectorDlg::ID_STATICTEXT1 = wxNewId();
 
16
const long HeadersDetectorDlg::ID_STATICTEXT3 = wxNewId();
 
17
const long HeadersDetectorDlg::ID_STATICTEXT2 = wxNewId();
 
18
const long HeadersDetectorDlg::ID_STATICTEXT4 = wxNewId();
 
19
const long HeadersDetectorDlg::ID_GAUGE1 = wxNewId();
 
20
const long HeadersDetectorDlg::ID_TIMER1 = wxNewId();
 
21
//*)
 
22
 
 
23
BEGIN_EVENT_TABLE(HeadersDetectorDlg,wxScrollingDialog)
 
24
        //(*EventTable(HeadersDetectorDlg)
 
25
        //*)
 
26
        EVT_BUTTON(wxID_CANCEL,HeadersDetectorDlg::Cancel)
 
27
END_EVENT_TABLE()
 
28
 
 
29
HeadersDetectorDlg::HeadersDetectorDlg(wxWindow* parent,cbProject* project,wxArrayString& headers)
 
30
    : m_Project(project)
 
31
    , m_Headers(headers)
 
32
    , m_Progress(0)
 
33
    , m_Finished(false)
 
34
    , m_Cancel(false)
 
35
{
 
36
        //(*Initialize(HeadersDetectorDlg)
 
37
        wxFlexGridSizer* FlexGridSizer1;
 
38
        wxBoxSizer* BoxSizer1;
 
39
        wxStdDialogButtonSizer* StdDialogButtonSizer1;
 
40
        wxStaticBoxSizer* StaticBoxSizer1;
 
41
        
 
42
        Create(parent, wxID_ANY, _("Detecting missing libraries..."), wxDefaultPosition, wxDefaultSize, wxCAPTION, _T("wxID_ANY"));
 
43
        BoxSizer1 = new wxBoxSizer(wxVERTICAL);
 
44
        StaticBoxSizer1 = new wxStaticBoxSizer(wxVERTICAL, this, _("Scanning:"));
 
45
        FlexGridSizer1 = new wxFlexGridSizer(0, 2, 5, 5);
 
46
        FlexGridSizer1->AddGrowableCol(1);
 
47
        StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("Project:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
 
48
        FlexGridSizer1->Add(StaticText1, 1, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
 
49
        m_ProjectName = new wxStaticText(this, ID_STATICTEXT3, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
 
50
        FlexGridSizer1->Add(m_ProjectName, 1, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
51
        StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("File:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
 
52
        FlexGridSizer1->Add(StaticText2, 1, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
 
53
        m_FileNameTxt = new wxStaticText(this, ID_STATICTEXT4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
 
54
        FlexGridSizer1->Add(m_FileNameTxt, 1, wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
55
        StaticBoxSizer1->Add(FlexGridSizer1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
56
        m_ProgressBar = new wxGauge(this, ID_GAUGE1, 100, wxDefaultPosition, wxSize(341,15), 0, wxDefaultValidator, _T("ID_GAUGE1"));
 
57
        StaticBoxSizer1->Add(m_ProgressBar, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
58
        BoxSizer1->Add(StaticBoxSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
59
        StdDialogButtonSizer1 = new wxStdDialogButtonSizer();
 
60
        StdDialogButtonSizer1->AddButton(new wxButton(this, wxID_CANCEL, wxEmptyString));
 
61
        StdDialogButtonSizer1->Realize();
 
62
        BoxSizer1->Add(StdDialogButtonSizer1, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
 
63
        SetSizer(BoxSizer1);
 
64
        Timer1.SetOwner(this, ID_TIMER1);
 
65
        Timer1.Start(50, false);
 
66
        BoxSizer1->Fit(this);
 
67
        BoxSizer1->SetSizeHints(this);
 
68
        Center();
 
69
        
 
70
        Connect(ID_TIMER1,wxEVT_TIMER,(wxObjectEventFunction)&HeadersDetectorDlg::OnTimer1Trigger);
 
71
        //*)
 
72
 
 
73
    m_Thread.m_Dlg = this;
 
74
    m_Thread.Create();
 
75
    m_Thread.Run();
 
76
 
 
77
    m_ProgressBar->SetRange( m_Project->GetFilesCount() );
 
78
    m_ProjectName->SetLabel( m_Project->GetTitle() );
 
79
}
 
80
 
 
81
HeadersDetectorDlg::~HeadersDetectorDlg()
 
82
{
 
83
    m_Thread.Wait();
 
84
 
 
85
        //(*Destroy(HeadersDetectorDlg)
 
86
        //*)
 
87
}
 
88
 
 
89
 
 
90
void HeadersDetectorDlg::OnTimer1Trigger(wxTimerEvent& event)
 
91
{
 
92
    wxCriticalSectionLocker lock(m_Section);
 
93
    Freeze();
 
94
    m_FileNameTxt->SetLabel( m_FileName );
 
95
    m_ProgressBar->SetValue( m_Progress );
 
96
    if ( m_Finished )
 
97
    {
 
98
        EndModal( m_Cancel ? wxID_CANCEL : wxID_OK );
 
99
    }
 
100
    Thaw();
 
101
}
 
102
 
 
103
void HeadersDetectorDlg::Cancel(wxCommandEvent& event)
 
104
{
 
105
    m_Cancel = true;
 
106
}
 
107
 
 
108
void HeadersDetectorDlg::ThreadProc()
 
109
{
 
110
    for ( int i=0; i<m_Project->GetFilesCount(); i++ )
 
111
    {
 
112
        if ( m_Thread.TestDestroy() || m_Cancel )
 
113
        {
 
114
            break;
 
115
        }
 
116
 
 
117
        ProjectFile* file = m_Project->GetFile(i);
 
118
 
 
119
        {
 
120
            wxCriticalSectionLocker lock(m_Section);
 
121
            m_Progress = i;
 
122
            m_FileName = file ? file->relativeFilename : _T("");
 
123
        }
 
124
 
 
125
        ProcessFile( file, m_Headers );
 
126
    }
 
127
    m_Finished = true;
 
128
}
 
129
 
 
130
void HeadersDetectorDlg::ProcessFile( ProjectFile* file, wxArrayString& includes )
 
131
{
 
132
    // We do not care about proper encoding right now.
 
133
    // Libraries should never use any native characters in names
 
134
    // of their includes and in case of any multibyte encoding
 
135
    // multibyte charcters shouldn't hurt us
 
136
    // Encoding detector tends to work really slow in some cases
 
137
 
 
138
    wxString Ext = file->file.GetExt();
 
139
    Ext.MakeLower();
 
140
 
 
141
    static const wxChar* Exts[] =
 
142
    {
 
143
        _T("h"), _T("hxx"), _T("hpp"),
 
144
        _T("c"), _T("cpp"), _T("cxx"),
 
145
        0
 
146
    };
 
147
 
 
148
    bool validExt = false;
 
149
    for ( const wxChar** ptr = Exts; *ptr; ptr++ )
 
150
    {
 
151
        if ( Ext == *ptr )
 
152
        {
 
153
            validExt = true;
 
154
            break;
 
155
        }
 
156
    }
 
157
    if ( !validExt )
 
158
    {
 
159
        return;
 
160
    }
 
161
 
 
162
 
 
163
    wxFile fl( file->file.GetFullPath() );
 
164
    if ( !fl.IsOpened() ) return;
 
165
    wxFileOffset contentLength = fl.Length();
 
166
    if ( contentLength <= 0 )
 
167
    {
 
168
        return;
 
169
    }
 
170
    char* content = new char[contentLength+1];
 
171
    char* line = new char[contentLength+1];
 
172
    if ( fl.Read(content,contentLength) != contentLength )
 
173
    {
 
174
        delete[] line;
 
175
        delete[] content;
 
176
        return;
 
177
    }
 
178
    content[contentLength] = 0;
 
179
 
 
180
    bool blockComment = false;
 
181
    for ( size_t pos = 0; pos < static_cast<size_t>(contentLength); )
 
182
    {
 
183
        // Fetching next line
 
184
        char last = 0;
 
185
        bool lineEnd = false;
 
186
        int lineLength = 0;
 
187
        bool lineComment = false;
 
188
        bool inStr = false;
 
189
        bool inChar = false;
 
190
        bool lastCharAdded = false;
 
191
        do
 
192
        {
 
193
            char ch = content[pos++];
 
194
            bool thisCharAdded = false;
 
195
            switch ( ch )
 
196
            {
 
197
                case '\n':
 
198
                    if ( content[pos] == '\r' )
 
199
                    {
 
200
                        pos++;
 
201
                    }
 
202
                    // Continue to \r
 
203
                case '\r':
 
204
                    if ( last != '\\' )
 
205
                    {
 
206
                        lineEnd = true;
 
207
                        break;
 
208
                    }
 
209
                    else if ( lastCharAdded )
 
210
                    {
 
211
                        // Removing last char since it was '\'
 
212
                        // which is removed in the
 
213
                        // preprocessor level
 
214
                        lineLength--;
 
215
                    }
 
216
                    break;
 
217
 
 
218
                case '*':
 
219
                    if ( blockComment )
 
220
                    {
 
221
                        if ( content[pos] == '/' )
 
222
                        {
 
223
                            pos++;
 
224
                            blockComment = false;
 
225
                            break;
 
226
                        }
 
227
                    }
 
228
                    else if ( !lineComment )
 
229
                    {
 
230
                        thisCharAdded = true;
 
231
                        line[lineLength++] = ch;
 
232
                    }
 
233
                    break;
 
234
 
 
235
                case '"':
 
236
                    if ( !blockComment && !lineComment )
 
237
                    {
 
238
                        if ( !inChar )
 
239
                        {
 
240
                            if ( !inStr )
 
241
                            {
 
242
                                inStr = true;
 
243
                            }
 
244
                            else if ( last != '\\' )
 
245
                            {
 
246
                                inStr = false;
 
247
                            }
 
248
                        }
 
249
                        thisCharAdded = true;
 
250
                        line[lineLength++] = ch;
 
251
                    }
 
252
                    break;
 
253
 
 
254
                case '\'':
 
255
                    if ( !blockComment && !lineComment )
 
256
                    {
 
257
                        if ( !inStr )
 
258
                        {
 
259
                            if ( !inChar )
 
260
                            {
 
261
                                inChar = true;
 
262
                            }
 
263
                            else if ( last != '\\' )
 
264
                            {
 
265
                                inChar = false;
 
266
                            }
 
267
                        }
 
268
                        thisCharAdded = true;
 
269
                        line[lineLength++] = ch;
 
270
                    }
 
271
                    break;
 
272
 
 
273
                case '/':
 
274
                    if ( !blockComment && !lineComment && !inStr && !inChar )
 
275
                    {
 
276
                        if ( content[pos] == '/' )
 
277
                        {
 
278
                            pos++;
 
279
                            lineComment = true;
 
280
                            break;
 
281
                        }
 
282
 
 
283
                        if ( content[pos] == '*' )
 
284
                        {
 
285
                            pos++;
 
286
                            blockComment = true;
 
287
                            break;
 
288
                        }
 
289
                    }
 
290
 
 
291
                    // Contnue to default case
 
292
                default:
 
293
                    if ( !blockComment && !lineComment )
 
294
                    {
 
295
                        thisCharAdded = true;
 
296
                        line[lineLength++] = ch;
 
297
                    }
 
298
            }
 
299
            last = ch;
 
300
            lastCharAdded = thisCharAdded;
 
301
        }
 
302
        while ( !lineEnd && pos < static_cast<size_t>(contentLength) );
 
303
        line[lineLength] = 0;
 
304
 
 
305
        // Searching for include
 
306
 
 
307
        int i=0;
 
308
        while ( line[i]==' ' || line[i]=='\t' ) i++;
 
309
        if ( line[i++] == '#' )
 
310
        {
 
311
            while ( line[i]==' ' || line[i]=='\t' ) i++;
 
312
            if ( !strncmp( line+i, "include", 7 ) )
 
313
            {
 
314
                i += 7;
 
315
                while ( line[i]==' ' || line[i]=='\t' ) i++;
 
316
 
 
317
                wxString include;
 
318
 
 
319
                char readTill =
 
320
                    ( line[i] == '<' ) ? '>' :
 
321
                    ( line[i] == '"' ) ? '"' :
 
322
                      0;
 
323
 
 
324
                if ( readTill )
 
325
                {
 
326
                    i++;
 
327
                    while ( line[i] && line[i]!=readTill )
 
328
                    {
 
329
                        include += (wxChar)line[i++];
 
330
                    }
 
331
                    if ( line[i] == readTill )
 
332
                    {
 
333
                        includes.Add( include );
 
334
                    }
 
335
                }
 
336
            }
 
337
        }
 
338
    }
 
339
 
 
340
    delete[] line;
 
341
    delete[] content;
 
342
}