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

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmith/wxscoder.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 wxSmith 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 3 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, see <http://www.gnu.org/licenses/>.
 
17
*
 
18
* $Revision: 4850 $
 
19
* $Id: wxscoder.cpp 4850 2008-01-29 21:45:49Z byo $
 
20
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/wxSmith/wxscoder.cpp $
 
21
*/
 
22
 
 
23
#include "wxscoder.h"
 
24
 
 
25
#include <manager.h>
 
26
#include <editormanager.h>
 
27
#include <configmanager.h>
 
28
#include <logmanager.h>
 
29
#include <encodingdetector.h>
 
30
#include <globals.h>
 
31
#include <wx/file.h>
 
32
#include <wx/intl.h>
 
33
#include "cbstyledtextctrl.h"
 
34
 
 
35
namespace
 
36
{
 
37
    bool ReadFileContentWithProperEncoding(const wxString& FileName,wxString& Content,wxFontEncoding& Encoding,bool& UseBOM)
 
38
    {
 
39
        EncodingDetector Detector(FileName);
 
40
        if ( !Detector.IsOK() ) return false;
 
41
        Encoding = Detector.GetFontEncoding();
 
42
        if ( Encoding == wxFONTENCODING_ISO8859_1 )
 
43
        {
 
44
            wxString enc_name = Manager::Get()->GetConfigManager(_T("editor"))->Read(_T("/default_encoding"), wxLocale::GetSystemEncodingName());
 
45
            Encoding = wxFontMapper::GetEncodingFromName(enc_name);
 
46
        }
 
47
        UseBOM = Detector.UsesBOM();
 
48
        wxFile Fl(FileName,wxFile::read);
 
49
        if ( !Fl.IsOpened() ) return false;
 
50
        if ( !cbRead(Fl,Content,Encoding) ) return false;
 
51
        Content.Remove(0,Detector.GetBOMSizeInBytes() / sizeof(wxChar));
 
52
        return true;
 
53
    }
 
54
}
 
55
 
 
56
static wxsCoder SingletonObject;
 
57
wxsCoder* wxsCoder::Singleton = &SingletonObject;
 
58
 
 
59
wxsCoder::wxsCoder()
 
60
{
 
61
    FlushTimer.SetOwner(this,1);
 
62
    Connect(wxEVT_TIMER,(wxObjectEventFunction)&wxsCoder::FlushTimerEvent);
 
63
}
 
64
 
 
65
wxsCoder::~wxsCoder()
 
66
{
 
67
    FlushAll();
 
68
}
 
69
 
 
70
void wxsCoder::AddCode(const wxString& FileName,const wxString& Header,const wxString& End,const wxString& Code,bool Immediately,bool CodeHasHeader,bool CodeHasEnd)
 
71
{
 
72
    wxMutexLocker Lock(DataMutex);
 
73
 
 
74
    wxString FixedFileName = NormalizeFileName(FileName);
 
75
    if ( FixedFileName.IsEmpty() )
 
76
    {
 
77
        return;
 
78
    }
 
79
 
 
80
    // Find changing file
 
81
    int Index = CodeChangesFiles.Index(FileName);
 
82
    if ( Index==wxNOT_FOUND )
 
83
    {
 
84
        Index = CodeChangesFiles.Count();
 
85
        CodeChangesFiles.Add(FileName);
 
86
        CodeChanges.Add(0);
 
87
    }
 
88
 
 
89
    // Add entry to list of waiting changes
 
90
    CodeChange* Change = new CodeChange;
 
91
    Change->Header = Header;
 
92
    Change->End = End;
 
93
    Change->Code = Code;
 
94
    Change->CodeHasHeader = CodeHasHeader;
 
95
    Change->CodeHasEnd = CodeHasEnd;
 
96
    Change->Next = CodeChanges[Index];
 
97
    CodeChanges[Index] = Change;
 
98
 
 
99
    // If the change has already been put onto queue, delete it
 
100
    for ( CodeChange *Prev=Change, *This=Prev->Next; This; Prev=This, This=This->Next )
 
101
    {
 
102
        if ( This->Header==Header && This->End==End )
 
103
        {
 
104
            Prev->Next = This->Next;
 
105
            delete This;
 
106
            This = Prev;
 
107
        }
 
108
    }
 
109
 
 
110
    if ( Immediately )
 
111
    {
 
112
        FlushFile(FixedFileName);
 
113
    }
 
114
}
 
115
 
 
116
wxString wxsCoder::GetCode(const wxString& FileName,const wxString& Header,const wxString& End,bool IncludeHeader,bool IncludeEnd)
 
117
{
 
118
    wxMutexLocker Lock(DataMutex);
 
119
 
 
120
    wxString FixedFileName = NormalizeFileName(FileName);
 
121
    FlushFile(FixedFileName);
 
122
 
 
123
    int TabSize = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/tab_size"), 4);
 
124
 
 
125
    // Checking if editor is opened
 
126
        EditorManager* EM = Manager::Get()->GetEditorManager();
 
127
        assert ( EM != 0 );
 
128
    cbEditor* Editor = EM->GetBuiltinEditor(FixedFileName);
 
129
 
 
130
    if ( Editor )
 
131
    {
 
132
        cbStyledTextCtrl* Ctrl = Editor->GetControl();
 
133
        Ctrl->SetSearchFlags(wxSCI_FIND_MATCHCASE);
 
134
        Ctrl->SetTargetStart(0);
 
135
        Ctrl->SetTargetEnd(Ctrl->GetLength());
 
136
        int Position = Ctrl->SearchInTarget(Header);
 
137
        if ( Position == -1 ) return _T("");
 
138
 
 
139
        // Counting number of indentation spaces which will be removed at
 
140
        // the beginning of each line
 
141
        int SpacesCut = 0;
 
142
        int SpacesPos = Position;
 
143
        while ( --SpacesPos >= 0 )
 
144
        {
 
145
            wxChar ch = Ctrl->GetCharAt(SpacesPos);
 
146
            if ( ch == _T('\t') ) SpacesCut += TabSize;
 
147
            else if ( (ch==_T('\n')) || (ch==_T('\r')) ) break;
 
148
            else SpacesCut++;
 
149
        }
 
150
 
 
151
        Ctrl->SetTargetStart(Position);
 
152
        Ctrl->SetTargetEnd(Ctrl->GetLength());
 
153
        int EndPosition = Ctrl->SearchInTarget(End);
 
154
        if ( EndPosition == -1 ) return _T("");
 
155
 
 
156
        // Fixing up positions to include / exclude header and/or ending sequence
 
157
        if ( !IncludeHeader ) Position += Header.Length();
 
158
        if ( IncludeEnd ) EndPosition += End.Length();
 
159
        return CutSpaces(Ctrl->GetTextRange(Position,EndPosition),SpacesCut);
 
160
    }
 
161
    else
 
162
    {
 
163
        wxString Content;
 
164
        wxFontEncoding Encoding;
 
165
        bool UseBOM;
 
166
        if ( !ReadFileContentWithProperEncoding(FixedFileName,Content,Encoding,UseBOM) ) return _T("");
 
167
 
 
168
        int Position = Content.First(Header);
 
169
        if ( Position == -1 ) return _T("");
 
170
        int SpacesCut = 0;
 
171
        int SpacesPos = Position;
 
172
        while ( --SpacesPos >= 0 )
 
173
        {
 
174
            wxChar ch = Content.GetChar(SpacesPos);
 
175
            if ( ch == _T('\t') ) SpacesCut += TabSize;
 
176
            else if ( (ch==_T('\n')) || (ch==_T('\r')) ) break;
 
177
            else SpacesCut++;
 
178
        }
 
179
 
 
180
        if ( !IncludeHeader ) Position += Header.Length();
 
181
        Content.Remove(0,Position);
 
182
        int EndPosition = Content.First(End);
 
183
        if ( EndPosition == -1 ) return _T("");
 
184
        if ( IncludeEnd ) EndPosition += End.Length();
 
185
        Content.Remove(EndPosition);
 
186
        return CutSpaces(Content,SpacesCut);
 
187
    }
 
188
}
 
189
 
 
190
wxString wxsCoder::GetFullCode(const wxString& FileName,wxFontEncoding& Encoding,bool &UseBOM)
 
191
{
 
192
    wxMutexLocker Lock(DataMutex);
 
193
 
 
194
    wxString FixedFileName = NormalizeFileName(FileName);
 
195
    FlushFile(FixedFileName);
 
196
 
 
197
    // Checking if editor is opened
 
198
        EditorManager* EM = Manager::Get()->GetEditorManager();
 
199
        assert ( EM != 0 );
 
200
    cbEditor* Editor = EM->GetBuiltinEditor(FixedFileName);
 
201
 
 
202
    if ( Editor )
 
203
    {
 
204
        Encoding = Editor->GetEncoding();
 
205
        UseBOM = Editor->GetUseBom();
 
206
        cbStyledTextCtrl* Ctrl = Editor->GetControl();
 
207
        return Ctrl->GetText();
 
208
    }
 
209
    else
 
210
    {
 
211
        wxString Content;
 
212
        if ( !ReadFileContentWithProperEncoding(FixedFileName,Content,Encoding,UseBOM) ) return _T("");
 
213
        return Content;
 
214
    }
 
215
}
 
216
 
 
217
void wxsCoder::PutFullCode(const wxString& FileName,const wxString& Code,wxFontEncoding Encoding,bool UseBOM)
 
218
{
 
219
    wxMutexLocker Lock(DataMutex);
 
220
 
 
221
    wxString FixedFileName = NormalizeFileName(FileName);
 
222
    int Index = CodeChangesFiles.Index(FixedFileName);
 
223
    if ( Index != wxNOT_FOUND )
 
224
    {
 
225
        for ( CodeChange* Change=CodeChanges[Index]; Change; )
 
226
        {
 
227
            CodeChange* Next = Change->Next;
 
228
            delete Change;
 
229
            Change = Next;
 
230
        }
 
231
        CodeChanges[Index] = 0;
 
232
    }
 
233
 
 
234
    // Searching for file in opened file list
 
235
        EditorManager* EM = Manager::Get()->GetEditorManager();
 
236
        assert ( EM != 0 );
 
237
    cbEditor* Editor = EM->GetBuiltinEditor(FixedFileName);
 
238
 
 
239
    if ( Editor )
 
240
    {
 
241
        Editor->GetControl()->SetText(Code);
 
242
    }
 
243
    else
 
244
    {
 
245
        cbSaveToFile(FixedFileName,Code,Encoding,UseBOM);
 
246
    }
 
247
}
 
248
 
 
249
void wxsCoder::FlushFile(const wxString& FileName)
 
250
{
 
251
    int Index = CodeChangesFiles.Index(FileName);
 
252
    if ( Index == wxNOT_FOUND ) return;
 
253
 
 
254
    CodeChange* Changes = CodeChanges[Index];
 
255
    if ( !Changes ) return;
 
256
 
 
257
    // Searching for file in opened file list
 
258
        EditorManager* EM = Manager::Get()->GetEditorManager();
 
259
        assert ( EM != 0 );
 
260
    cbEditor* Editor = EM->GetBuiltinEditor(FileName);
 
261
 
 
262
    if ( Editor )
 
263
    {
 
264
        wxString EOL;
 
265
        while ( Changes )
 
266
        {
 
267
            CodeChange* Next = Changes->Next;
 
268
            ApplyChangesEditor(Editor,Changes->Header,Changes->End,Changes->Code,Changes->CodeHasHeader,Changes->CodeHasEnd,EOL);
 
269
            delete Changes;
 
270
            Changes = Next;
 
271
        }
 
272
    }
 
273
    else
 
274
    {
 
275
        // Reading file content
 
276
        wxString Content;
 
277
        wxFontEncoding Encoding;
 
278
        wxString EOL;
 
279
        bool UseBOM;
 
280
        bool HasChanged = false;
 
281
 
 
282
        //wxStopWatch SW;
 
283
        if ( !ReadFileContentWithProperEncoding(FileName,Content,Encoding,UseBOM) )
 
284
        {
 
285
            Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't open file '%s'"),FileName.c_str()));
 
286
            return;
 
287
        }
 
288
        //Manager::Get()->GetLogManager()->DebugLog(F(_T("File read time: %d ms"),SW.Time()));
 
289
 
 
290
        while ( Changes )
 
291
        {
 
292
            CodeChange* Next = Changes->Next;
 
293
            ApplyChangesString(Content,Changes->Header,Changes->End,Changes->Code,Changes->CodeHasHeader,Changes->CodeHasEnd,HasChanged,EOL);
 
294
            delete Changes;
 
295
            Changes = Next;
 
296
        }
 
297
 
 
298
        if ( HasChanged )
 
299
        {
 
300
            // Storing the result
 
301
            //wxStopWatch SW;
 
302
            cbSaveToFile(FileName,Content,Encoding,UseBOM);
 
303
            //Manager::Get()->GetLogManager()->DebugLog(F(_T("File write time: %d ms"),SW.Time()));
 
304
        }
 
305
    }
 
306
 
 
307
    CodeChanges[Index] = 0;
 
308
}
 
309
 
 
310
bool wxsCoder::ApplyChangesEditor(cbEditor* Editor,const wxString& Header,const wxString& End,wxString& Code,bool CodeHasHeader,bool CodeHasEnd,wxString& EOL)
 
311
{
 
312
        cbStyledTextCtrl* Ctrl = Editor->GetControl();
 
313
        int FullLength = Ctrl->GetLength();
 
314
 
 
315
    if ( EOL.IsEmpty() )
 
316
    {
 
317
        // Detecting EOL style in source
 
318
        for ( int i=0; i<FullLength; i++ )
 
319
        {
 
320
            wxChar ch = Ctrl->GetCharAt(i);
 
321
            if ( ch==_T('\n') || ch==_T('\r') )
 
322
            {
 
323
                EOL = ch;
 
324
                if ( ++i < FullLength )
 
325
                {
 
326
                    wxChar ch2 = Ctrl->GetCharAt(i);
 
327
                    if ( (ch2==_T('\n') || ch2==_T('\r')) && ch!=ch2 )
 
328
                    {
 
329
                        EOL.Append(ch2);
 
330
                    }
 
331
                }
 
332
                break;
 
333
            }
 
334
        }
 
335
    }
 
336
 
 
337
    // Searching for beginning of section to replace
 
338
        Ctrl->SetSearchFlags(wxSCI_FIND_MATCHCASE);
 
339
        Ctrl->SetTargetStart(0);
 
340
        Ctrl->SetTargetEnd(FullLength);
 
341
        int Position = Ctrl->SearchInTarget(Header);
 
342
 
 
343
        if ( Position == -1 )
 
344
        {
 
345
            Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't find code with header:\n\t\"%s\"\nin file '%s'"),
 
346
                        Header.c_str(),
 
347
                        Editor->GetFilename().c_str()));
 
348
                return false;
 
349
        }
 
350
 
 
351
    // Beginning of this code block is in Position, now searching for end
 
352
    Ctrl->SetTargetStart(Position);
 
353
    Ctrl->SetTargetEnd(FullLength);
 
354
    int EndPosition = Ctrl->SearchInTarget(End);
 
355
    if ( EndPosition == -1 )
 
356
    {
 
357
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Unfinished block of auto-generated code with header:\n\t\"%s\"\nin file '%s'"),
 
358
            Header.c_str(),
 
359
            Editor->GetFilename().c_str()));
 
360
        return false;
 
361
    }
 
362
 
 
363
    // Fetching indentation
 
364
    wxString BaseIndentation;
 
365
    int IndentPos = Position;
 
366
    while ( --IndentPos >= 0 )
 
367
    {
 
368
        wxChar ch = Ctrl->GetCharAt(IndentPos);
 
369
        if ( (ch == _T('\n')) || (ch == _T('\r')) ) break;
 
370
    }
 
371
    while ( ++IndentPos < Position )
 
372
    {
 
373
        wxChar ch = Ctrl->GetCharAt(IndentPos);
 
374
        BaseIndentation.Append(
 
375
            ( ch == _T('\t') ) ? _T('\t') : _T(' '));
 
376
    }
 
377
 
 
378
    Code = RebuildCode(BaseIndentation,Code.c_str(),(int)Code.Length(),EOL);
 
379
 
 
380
    // Fixing up positions to contain or not header / ending sequence
 
381
    if ( !CodeHasHeader ) Position += Header.Length();
 
382
    if ( CodeHasEnd ) EndPosition += End.Length();
 
383
 
 
384
    // Checking of code has really changed
 
385
    if ( Ctrl->GetTextRange(Position,EndPosition) == Code )
 
386
    {
 
387
        return true;
 
388
    }
 
389
 
 
390
    // Replacing code
 
391
    Ctrl->SetTargetStart(Position);
 
392
    Ctrl->SetTargetEnd(EndPosition);
 
393
    Ctrl->ReplaceTarget(Code);
 
394
    Editor->SetModified();
 
395
 
 
396
    // TODO: Update fooldings
 
397
        return true;
 
398
}
 
399
 
 
400
bool wxsCoder::ApplyChangesString(wxString& BaseContent,const wxString& Header,const wxString& End,wxString& Code,bool CodeHasHeader,bool CodeHasEnd,bool& HasChanged,wxString& EOL)
 
401
{
 
402
    wxString Content = BaseContent;
 
403
    if ( EOL.IsEmpty() )
 
404
    {
 
405
        // Detecting EOL in this sources
 
406
        for ( size_t i=0; i<Content.Length(); i++ )
 
407
        {
 
408
            wxChar ch = Content.GetChar(i);
 
409
            if ( ch==_T('\n') || ch==_T('\r') )
 
410
            {
 
411
                EOL = ch;
 
412
                if ( ++i < Content.Length() )
 
413
                {
 
414
                    wxChar ch2 = Content.GetChar(i);
 
415
                    if ( (ch2==_T('\n') || ch2==_T('\r')) && ch!=ch2 )
 
416
                    {
 
417
                        EOL.Append(ch2);
 
418
                    }
 
419
                }
 
420
                break;
 
421
            }
 
422
        }
 
423
    }
 
424
 
 
425
    // Search for header
 
426
    int Position = Content.First(Header);
 
427
 
 
428
    if ( Position == -1 )
 
429
    {
 
430
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't find code with header:\n\t\"%s\""),Header.c_str()));
 
431
                return false;
 
432
    }
 
433
 
 
434
    // Skipping header if necessary
 
435
    int IndentPos = Position;
 
436
    int IndentMax = Position;
 
437
    if ( !CodeHasHeader ) Position += Header.Length();
 
438
 
 
439
    wxString Result = Content.Left(Position);
 
440
    Content.Remove(0,Position);
 
441
 
 
442
    int EndPosition = Content.First(End);
 
443
    if ( EndPosition == -1 )
 
444
    {
 
445
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Unfinished block of auto-generated code with header:\n\t\"%s\""),Header.c_str()));
 
446
        return false;
 
447
    }
 
448
 
 
449
    // Including ending sequence if necessary
 
450
    if ( CodeHasEnd ) EndPosition += End.Length();
 
451
 
 
452
    // Fetching indentation
 
453
    wxString BaseIndentation;
 
454
    while ( --IndentPos >= 0 )
 
455
    {
 
456
        wxChar ch = Result.GetChar(IndentPos);
 
457
        if ( (ch == _T('\n')) || (ch == _T('\r')) ) break;
 
458
    }
 
459
    while ( ++IndentPos < IndentMax )
 
460
    {
 
461
        wxChar ch = Result.GetChar(IndentPos);
 
462
        BaseIndentation.Append(
 
463
            ( ch == _T('\t') ) ? _T('\t') : _T(' '));
 
464
    }
 
465
 
 
466
    Code = RebuildCode(BaseIndentation,Code.c_str(),Code.Length(),EOL);
 
467
 
 
468
    // Checking if code has really changed
 
469
    if ( Content.Mid(0,EndPosition) == Code )
 
470
    {
 
471
        return true;
 
472
    }
 
473
 
 
474
    HasChanged = true;
 
475
    Result += Code;
 
476
    Result += Content.Remove(0,EndPosition);
 
477
    BaseContent = Result;
 
478
 
 
479
    return true;
 
480
}
 
481
 
 
482
wxString wxsCoder::RebuildCode(wxString& BaseIndentation,const wchar_t* Code,int CodeLen,wxString& EOL)
 
483
{
 
484
    wxString Tab;
 
485
    bool UseTab = Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/use_tab"), false);
 
486
    int TabSize = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/tab_size"), 4);
 
487
    if ( !UseTab )
 
488
    {
 
489
        Tab.Append(_T(' '),TabSize);
 
490
    }
 
491
 
 
492
    if ( EOL.IsEmpty() )
 
493
    {
 
494
        int EolMode = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/eol/eolmode"), 0);
 
495
        switch ( EolMode )
 
496
        {
 
497
            case 1:  EOL = _T("\r"); break;
 
498
            case 2:  EOL = _T("\n"); break;
 
499
            default: EOL = _T("\r\n");
 
500
        }
 
501
    }
 
502
 
 
503
    BaseIndentation.Prepend(EOL);
 
504
 
 
505
    wxString Result;
 
506
    Result.reserve(CodeLen+10);
 
507
 
 
508
    while ( *Code )
 
509
    {
 
510
        switch ( *Code )
 
511
        {
 
512
            case _T('\n'): Result << BaseIndentation; break;
 
513
            case _T('\t'): if ( UseTab ) { Result << Tab; break; }
 
514
            default:       Result << *Code;
 
515
        }
 
516
        Code++;
 
517
    }
 
518
 
 
519
    return Result;
 
520
}
 
521
 
 
522
wxString wxsCoder::CutSpaces(wxString Code,int Count)
 
523
{
 
524
    int TabSize = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/tab_size"), 4);
 
525
    if ( TabSize < 1 ) TabSize = 4;
 
526
 
 
527
    // Changing to \n line end mode
 
528
    wxString Result;
 
529
 
 
530
    for(;;)
 
531
    {
 
532
        int PosN = Code.Find(_T("\n"));
 
533
        int PosR = Code.Find(_T("\r"));
 
534
 
 
535
        if ( ( PosN < 0 ) && ( PosR < 0 ) ) break;
 
536
 
 
537
        int Pos;
 
538
        if ( PosN < 0 ) Pos = PosR;
 
539
        else if ( PosR < 0 ) Pos = PosN;
 
540
        else Pos = (PosN < PosR) ? PosN : PosR;
 
541
 
 
542
        Result.Append(Code.Left(Pos));
 
543
        Code.Remove(0,Pos);
 
544
        while ( Code.Length() )
 
545
        {
 
546
            if ( ( Code[0] != _T('\n') ) &&
 
547
                 ( Code[0] != _T('\r') ) ) break;
 
548
            Code.Remove(0,1);
 
549
        }
 
550
        int LeftSpaces = Count;
 
551
        while ( Code.Length() && LeftSpaces > 0 )
 
552
        {
 
553
            if ( Code[0] == _T(' ') ) LeftSpaces--;
 
554
            else if ( Code[0] == _T('\t') ) LeftSpaces -= TabSize;
 
555
            else break;
 
556
            Code.Remove(0,1);
 
557
        }
 
558
        Result.Append(_T('\n'));
 
559
    }
 
560
 
 
561
    Result.Append(Code);
 
562
    return Result;
 
563
}
 
564
 
 
565
wxString wxsCoder::NormalizeFileName(const wxString& FileName)
 
566
{
 
567
    // Updating the file name in case there are some ".." or "." enteries which prevents from finding
 
568
    // opened editor for file
 
569
    wxFileName FixedNameObject(FileName);
 
570
    FixedNameObject.Normalize(wxPATH_NORM_DOTS);
 
571
    return FixedNameObject.GetFullPath();
 
572
}
 
573
 
 
574
void wxsCoder::Flush(int Delay)
 
575
{
 
576
    if ( Delay<=0 )
 
577
    {
 
578
        FlushTimer.Stop();
 
579
        FlushAll();
 
580
    }
 
581
    else
 
582
    {
 
583
        FlushTimer.Start(Delay,true);
 
584
    }
 
585
}
 
586
 
 
587
void wxsCoder::FlushAll()
 
588
{
 
589
    wxStopWatch SW;
 
590
    for ( int i=0; i<(int)CodeChangesFiles.Count(); i++ )
 
591
    {
 
592
        FlushFile(CodeChangesFiles[i]);
 
593
    }
 
594
    CodeChanges.Clear();
 
595
    CodeChangesFiles.Clear();
 
596
    //Manager::Get()->GetLogManager()->DebugLog(F(_T("wxSmith: Flushing of code done in %d ms"),SW.Time()));
 
597
}
 
598
 
 
599
void wxsCoder::FlushTimerEvent(wxTimerEvent& event)
 
600
{
 
601
    FlushAll();
 
602
}