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

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmith/wxscoder.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:
15
15
* You should have received a copy of the GNU General Public License
16
16
* along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
17
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 $
 
18
* $Revision: 5796 $
 
19
* $Id: wxscoder.cpp 5796 2009-09-16 12:09:22Z mortenmacfly $
 
20
* $HeadURL: svn+ssh://jenslody@svn.berlios.de/svnroot/repos/codeblocks/trunk/src/plugins/contrib/wxSmith/wxscoder.cpp $
21
21
*/
22
22
 
23
23
#include "wxscoder.h"
26
26
#include <editormanager.h>
27
27
#include <configmanager.h>
28
28
#include <logmanager.h>
 
29
#include <filemanager.h>
29
30
#include <encodingdetector.h>
30
31
#include <globals.h>
31
32
#include <wx/file.h>
32
33
#include <wx/intl.h>
33
34
#include "cbstyledtextctrl.h"
34
35
 
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
 
}
 
36
//namespace
 
37
//{
 
38
//    EncodingDetector* LoadFile(const wxString& FileName)
 
39
//    {
 
40
//        if ( !wxFileExists(FileName) ) return nullptr;
 
41
//
 
42
//        LoaderBase* Loader = FileManager::Get()->Load(FileName,false);
 
43
//        if ( !Loader ) return nullptr;
 
44
//
 
45
//        EncodingDetector* Detector = new EncodingDetector(Loader);
 
46
//        delete Loader;
 
47
//
 
48
//        if ( !Detector->IsOK() )
 
49
//        {
 
50
//            delete Detector;
 
51
//            return nullptr;
 
52
//        }
 
53
//
 
54
//        return Detector;
 
55
//    }
 
56
//}
55
57
 
56
58
static wxsCoder SingletonObject;
57
59
wxsCoder* wxsCoder::Singleton = &SingletonObject;
160
162
    }
161
163
    else
162
164
    {
163
 
        wxString Content;
164
 
        wxFontEncoding Encoding;
165
 
        bool UseBOM;
166
 
        if ( !ReadFileContentWithProperEncoding(FixedFileName,Content,Encoding,UseBOM) ) return _T("");
 
165
        EncodingDetector Detector(FixedFileName);
 
166
        if ( !Detector.IsOK() ) return _T("");
 
167
 
 
168
        wxString Content = Detector.GetWxStr();
167
169
 
168
170
        int Position = Content.First(Header);
169
171
        if ( Position == -1 ) return _T("");
208
210
    }
209
211
    else
210
212
    {
211
 
        wxString Content;
212
 
        if ( !ReadFileContentWithProperEncoding(FixedFileName,Content,Encoding,UseBOM) ) return _T("");
213
 
        return Content;
 
213
        EncodingDetector Detector(FixedFileName);
 
214
        Encoding = Detector.GetFontEncoding();
 
215
        UseBOM = Detector.GetBOMSizeInBytes() > 0;
 
216
        return Detector.IsOK() ? Detector.GetWxStr() : _T("");
214
217
    }
215
218
}
216
219
 
242
245
    }
243
246
    else
244
247
    {
245
 
        cbSaveToFile(FixedFileName,Code,Encoding,UseBOM);
 
248
        if ( !cbSaveToFile(FixedFileName,Code,Encoding,UseBOM) )
 
249
        {
 
250
            #if wxCHECK_VERSION(2, 9, 0)
 
251
            Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't write file '%s'"),FixedFileName.wx_str()));
 
252
            #else
 
253
            Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't write file '%s'"),FixedFileName.c_str()));
 
254
            #endif
 
255
        }
246
256
    }
247
257
}
248
258
 
273
283
    else
274
284
    {
275
285
        // Reading file content
276
 
        wxString Content;
277
 
        wxFontEncoding Encoding;
278
286
        wxString EOL;
279
 
        bool UseBOM;
280
287
        bool HasChanged = false;
281
288
 
282
289
        //wxStopWatch SW;
283
 
        if ( !ReadFileContentWithProperEncoding(FileName,Content,Encoding,UseBOM) )
 
290
        EncodingDetector Detector(FileName);
 
291
        if ( !Detector.IsOK() )
284
292
        {
285
 
            Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't open file '%s'"),FileName.c_str()));
 
293
            #if wxCHECK_VERSION(2, 9, 0)
 
294
            Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't open and properly read file '%s'"),FileName.wx_str()));
 
295
            #else
 
296
            Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't open and properly read file '%s'"),FileName.c_str()));
 
297
            #endif
286
298
            return;
287
299
        }
288
300
        //Manager::Get()->GetLogManager()->DebugLog(F(_T("File read time: %d ms"),SW.Time()));
289
301
 
 
302
        wxString Content = Detector.GetWxStr();
290
303
        while ( Changes )
291
304
        {
292
305
            CodeChange* Next = Changes->Next;
299
312
        {
300
313
            // Storing the result
301
314
            //wxStopWatch SW;
302
 
            cbSaveToFile(FileName,Content,Encoding,UseBOM);
 
315
            if ( !cbSaveToFile(FileName,Content,Detector.GetFontEncoding(),Detector.GetBOMSizeInBytes()>0) )
 
316
            {
 
317
                #if wxCHECK_VERSION(2, 9, 0)
 
318
                Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't write data to file '%s'"),FileName.wx_str()));
 
319
                #else
 
320
                Manager::Get()->GetLogManager()->Log(F(_("wxSmith: Couldn't write data to file '%s'"),FileName.c_str()));
 
321
                #endif
 
322
            }
303
323
            //Manager::Get()->GetLogManager()->DebugLog(F(_T("File write time: %d ms"),SW.Time()));
304
324
        }
305
325
    }
343
363
        if ( Position == -1 )
344
364
        {
345
365
            Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't find code with header:\n\t\"%s\"\nin file '%s'"),
 
366
                        #if wxCHECK_VERSION(2, 9, 0)
 
367
                        Header.wx_str(),
 
368
                        Editor->GetFilename().wx_str()));
 
369
                        #else
346
370
                        Header.c_str(),
347
371
                        Editor->GetFilename().c_str()));
 
372
                        #endif
348
373
                return false;
349
374
        }
350
375
 
355
380
    if ( EndPosition == -1 )
356
381
    {
357
382
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Unfinished block of auto-generated code with header:\n\t\"%s\"\nin file '%s'"),
 
383
            #if wxCHECK_VERSION(2, 9, 0)
 
384
            Header.wx_str(),
 
385
            Editor->GetFilename().wx_str()));
 
386
            #else
358
387
            Header.c_str(),
359
388
            Editor->GetFilename().c_str()));
 
389
            #endif
360
390
        return false;
361
391
    }
362
392
 
387
417
        return true;
388
418
    }
389
419
 
 
420
    // Make sure that the code we're replacing is not folded. Otherwise scintilla
 
421
    // does some weird things
 
422
 
 
423
    int lineEnd = Ctrl->LineFromPosition(EndPosition);
 
424
    for ( int line = Ctrl->LineFromPosition(Position); line <= lineEnd; line++ )
 
425
    {
 
426
        Ctrl->EnsureVisible(line);
 
427
    }
 
428
 
390
429
    // Replacing code
391
430
    Ctrl->SetTargetStart(Position);
392
431
    Ctrl->SetTargetEnd(EndPosition);
393
432
    Ctrl->ReplaceTarget(Code);
394
433
    Editor->SetModified();
395
 
 
396
 
    // TODO: Update fooldings
397
 
        return true;
 
434
    return true;
398
435
}
399
436
 
400
437
bool wxsCoder::ApplyChangesString(wxString& BaseContent,const wxString& Header,const wxString& End,wxString& Code,bool CodeHasHeader,bool CodeHasEnd,bool& HasChanged,wxString& EOL)
427
464
 
428
465
    if ( Position == -1 )
429
466
    {
 
467
        #if wxCHECK_VERSION(2, 9, 0)
 
468
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't find code with header:\n\t\"%s\""),Header.wx_str()));
 
469
        #else
430
470
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Couldn't find code with header:\n\t\"%s\""),Header.c_str()));
 
471
        #endif
431
472
                return false;
432
473
    }
433
474
 
442
483
    int EndPosition = Content.First(End);
443
484
    if ( EndPosition == -1 )
444
485
    {
 
486
        #if wxCHECK_VERSION(2, 9, 0)
 
487
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Unfinished block of auto-generated code with header:\n\t\"%s\""),Header.wx_str()));
 
488
        #else
445
489
        Manager::Get()->GetLogManager()->DebugLog(F(_("wxSmith: Unfinished block of auto-generated code with header:\n\t\"%s\""),Header.c_str()));
 
490
        #endif
446
491
        return false;
447
492
    }
448
493
 
479
524
    return true;
480
525
}
481
526
 
482
 
wxString wxsCoder::RebuildCode(wxString& BaseIndentation,const wchar_t* Code,int CodeLen,wxString& EOL)
 
527
wxString wxsCoder::RebuildCode(wxString& BaseIndentation,const wxChar* Code,int CodeLen,wxString& EOL)
483
528
{
484
529
    wxString Tab;
485
530
    bool UseTab = Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/use_tab"), false);
586
631
 
587
632
void wxsCoder::FlushAll()
588
633
{
589
 
    wxStopWatch SW;
 
634
    //wxStopWatch SW;
590
635
    for ( int i=0; i<(int)CodeChangesFiles.Count(); i++ )
591
636
    {
592
637
        FlushFile(CodeChangesFiles[i]);