~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/textbuf.h
// Purpose:     class wxTextBuffer to work with text buffers of _small_ size
//              (buffer is fully loaded in memory) and which understands CR/LF
//              differences between platforms.
// Created:     14.11.01
// Author:      Morten Hanssen, Vadim Zeitlin
// Copyright:   (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_TEXTBUFFER_H
#define _WX_TEXTBUFFER_H

#include "wx/defs.h"
#include "wx/arrstr.h"
#include "wx/convauto.h"

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// the line termination type (kept wxTextFileType name for compability)
enum wxTextFileType
{
    wxTextFileType_None,  // incomplete (the last line of the file only)
    wxTextFileType_Unix,  // line is terminated with 'LF' = 0xA = 10 = '\n'
    wxTextFileType_Dos,   //                         'CR' 'LF'
    wxTextFileType_Mac,   //                         'CR' = 0xD = 13 = '\r'
    wxTextFileType_Os2    //                         'CR' 'LF'
};

#include "wx/string.h"

#if wxUSE_TEXTBUFFER

#include "wx/dynarray.h"

// ----------------------------------------------------------------------------
// wxTextBuffer
// ----------------------------------------------------------------------------

WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
                                  wxArrayLinesType,
                                  class WXDLLIMPEXP_BASE);

#endif // wxUSE_TEXTBUFFER

class WXDLLIMPEXP_BASE wxTextBuffer
{
public:
    // constants and static functions
    // default type for current platform (determined at compile time)
    static const wxTextFileType typeDefault;

    // this function returns a string which is identical to "text" passed in
    // except that the line terminator characters are changed to correspond the
    // given type. Called with the default argument, the function translates
    // the string to the native format (Unix for Unix, DOS for Windows, ...).
    static wxString Translate(const wxString& text,
                            wxTextFileType type = typeDefault);

    // get the buffer termination string
    static const wxChar *GetEOL(wxTextFileType type = typeDefault);

    // the static methods of this class are compiled in even when
    // !wxUSE_TEXTBUFFER because they are used by the library itself, but the
    // rest can be left out
#if wxUSE_TEXTBUFFER

    // buffer operations
    // -----------------

    // buffer exists?
    bool Exists() const;

    // create the buffer if it doesn't already exist
    bool Create();

    // same as Create() but with (another) buffer name
    bool Create(const wxString& strBufferName);

    // Open() also loads buffer in memory on success
    bool Open(const wxMBConv& conv = wxConvAuto());

    // same as Open() but with (another) buffer name
    bool Open(const wxString& strBufferName, const wxMBConv& conv = wxConvAuto());

    // closes the buffer and frees memory, losing all changes
    bool Close();

    // is buffer currently opened?
    bool IsOpened() const { return m_isOpened; }

    // accessors
    // ---------

    // get the number of lines in the buffer
    size_t GetLineCount() const { return m_aLines.size(); }

    // the returned line may be modified (but don't add CR/LF at the end!)
          wxString& GetLine(size_t n)          { return m_aLines[n]; }
    const wxString& GetLine(size_t n)    const { return m_aLines[n]; }
          wxString& operator[](size_t n)       { return m_aLines[n]; }
    const wxString& operator[](size_t n) const { return m_aLines[n]; }

    // the current line has meaning only when you're using
    // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
    // you're using "direct access" i.e. GetLine()
    size_t GetCurrentLine() const { return m_nCurLine; }
    void GoToLine(size_t n) { m_nCurLine = n; }
    bool Eof() const { return m_nCurLine == m_aLines.size(); }

    // these methods allow more "iterator-like" traversal of the list of
    // lines, i.e. you may write something like:
    //  for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }

    // NB: const is commented out because not all compilers understand
    //     'mutable' keyword yet (m_nCurLine should be mutable)
    wxString& GetFirstLine() /* const */
        { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = 0]; }
    wxString& GetNextLine()  /* const */
        { return ++m_nCurLine == m_aLines.size() ? ms_eof
                                                 : m_aLines[m_nCurLine]; }
    wxString& GetPrevLine()  /* const */
        { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
    wxString& GetLastLine() /* const */
        { m_nCurLine = m_aLines.size() - 1; return m_aLines.Last(); }

    // get the type of the line (see also GetEOL)
    wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }

    // guess the type of buffer
    wxTextFileType GuessType() const;

    // get the name of the buffer
    const wxString& GetName() const { return m_strBufferName; }

    // add/remove lines
    // ----------------

    // add a line to the end
    void AddLine(const wxString& str, wxTextFileType type = typeDefault)
        { m_aLines.push_back(str); m_aTypes.push_back(type); }
    // insert a line before the line number n
    void InsertLine(const wxString& str,
                  size_t n,
                  wxTextFileType type = typeDefault)
    {
        m_aLines.insert(m_aLines.begin() + n, str);
        m_aTypes.insert(m_aTypes.begin()+n, type);
    }

    // delete one line
    void RemoveLine(size_t n)
    {
        m_aLines.erase(m_aLines.begin() + n);
        m_aTypes.erase(m_aTypes.begin() + n);
    }

    // remove all lines
    void Clear() { m_aLines.clear(); m_aTypes.clear(); m_nCurLine = 0; }

    // change the buffer (default argument means "don't change type")
    // possibly in another format
    bool Write(wxTextFileType typeNew = wxTextFileType_None,
               const wxMBConv& conv = wxConvAuto());

    // dtor
    virtual ~wxTextBuffer();

protected:
    // ctors
    // -----

    // default ctor, use Open(string)
    wxTextBuffer() { m_nCurLine = 0; m_isOpened = false; }

    // ctor from filename
    wxTextBuffer(const wxString& strBufferName);

    enum wxTextBufferOpenMode { ReadAccess, WriteAccess };

    // Must implement these in derived classes.
    virtual bool OnExists() const = 0;
    virtual bool OnOpen(const wxString &strBufferName,
                        wxTextBufferOpenMode openmode) = 0;
    virtual bool OnClose() = 0;
    virtual bool OnRead(const wxMBConv& conv) = 0;
    virtual bool OnWrite(wxTextFileType typeNew, const wxMBConv& conv) = 0;

    static wxString ms_eof;     // dummy string returned at EOF
    wxString m_strBufferName;   // name of the buffer

private:
    wxArrayLinesType m_aTypes;   // type of each line
    wxArrayString    m_aLines;   // lines of file

    size_t        m_nCurLine; // number of current line in the buffer

    bool          m_isOpened; // was the buffer successfully opened the last time?
#endif // wxUSE_TEXTBUFFER

    // copy ctor/assignment operator not implemented
    wxTextBuffer(const wxTextBuffer&);
    wxTextBuffer& operator=(const wxTextBuffer&);
};

#endif // _WX_TEXTBUFFER_H