Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

wxString wxsCoder::GetCode ( const wxString &  FileName,
const wxString &  Header,
const wxString &  End,
bool  IncludeHeader = false,
bool  IncludeEnd = false 
)

Function reading block of code from given source file.

Parameters:
FileName Full path for processed file
Header Header beginning code block
End Sequence ending code block
IncludeHeader if true, returned code will also contain header
IncludeEnd if true, returned code will also contain ending sequence

Definition at line 116 of file wxscoder.cpp.

References CutSpaces(), DataMutex, FlushFile(), cbEditor::GetControl(), and NormalizeFileName().

Referenced by wxsEventsEditor::CreateNewFunction(), wxsEventsEditor::FindFunctions(), and wxWidgetsGUI::IsAppSourceManaged().

{
    wxMutexLocker Lock(DataMutex);

    wxString FixedFileName = NormalizeFileName(FileName);
    FlushFile(FixedFileName);

    int TabSize = Manager::Get()->GetConfigManager(_T("editor"))->ReadInt(_T("/tab_size"), 4);

    // Checking if editor is opened
      EditorManager* EM = Manager::Get()->GetEditorManager();
      assert ( EM != 0 );
    cbEditor* Editor = EM->GetBuiltinEditor(FixedFileName);

    if ( Editor )
    {
        cbStyledTextCtrl* Ctrl = Editor->GetControl();
        Ctrl->SetSearchFlags(wxSCI_FIND_MATCHCASE);
        Ctrl->SetTargetStart(0);
        Ctrl->SetTargetEnd(Ctrl->GetLength());
        int Position = Ctrl->SearchInTarget(Header);
        if ( Position == -1 ) return _T("");

        // Counting number of indentation spaces which will be removed at
        // the beginning of each line
        int SpacesCut = 0;
        int SpacesPos = Position;
        while ( --SpacesPos >= 0 )
        {
            wxChar ch = Ctrl->GetCharAt(SpacesPos);
            if ( ch == _T('\t') ) SpacesCut += TabSize;
            else if ( (ch==_T('\n')) || (ch==_T('\r')) ) break;
            else SpacesCut++;
        }

        Ctrl->SetTargetStart(Position);
        Ctrl->SetTargetEnd(Ctrl->GetLength());
        int EndPosition = Ctrl->SearchInTarget(End);
        if ( EndPosition == -1 ) return _T("");

        // Fixing up positions to include / exclude header and/or ending sequence
        if ( !IncludeHeader ) Position += Header.Length();
        if ( IncludeEnd ) EndPosition += End.Length();
        return CutSpaces(Ctrl->GetTextRange(Position,EndPosition),SpacesCut);
    }
    else
    {
        wxString Content;
        wxFontEncoding Encoding;
        bool UseBOM;
        if ( !ReadFileContentWithProperEncoding(FixedFileName,Content,Encoding,UseBOM) ) return _T("");

        int Position = Content.First(Header);
        if ( Position == -1 ) return _T("");
        int SpacesCut = 0;
        int SpacesPos = Position;
        while ( --SpacesPos >= 0 )
        {
            wxChar ch = Content.GetChar(SpacesPos);
            if ( ch == _T('\t') ) SpacesCut += TabSize;
            else if ( (ch==_T('\n')) || (ch==_T('\r')) ) break;
            else SpacesCut++;
        }

        if ( !IncludeHeader ) Position += Header.Length();
        Content.Remove(0,Position);
        int EndPosition = Content.First(End);
        if ( EndPosition == -1 ) return _T("");
        if ( IncludeEnd ) EndPosition += End.Length();
        Content.Remove(EndPosition);
        return CutSpaces(Content,SpacesCut);
    }
}


Generated by  Doxygen 1.6.0   Back to index