1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: class wxTextBuffer to work with text buffers of _small_ size
4 // (buffer is fully loaded in memory) and which understands CR/LF
5 // differences between platforms.
7 // Author: Morten Hanssen, Vadim Zeitlin
8 // Copyright: (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TEXTBUFFER_H
13 #define _WX_TEXTBUFFER_H
16 #include "wx/arrstr.h"
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
22 // the line termination type (kept wxTextFileType name for compability)
25 wxTextFileType_None
, // incomplete (the last line of the file only)
26 wxTextFileType_Unix
, // line is terminated with 'LF' = 0xA = 10 = '\n'
27 wxTextFileType_Dos
, // 'CR' 'LF'
28 wxTextFileType_Mac
, // 'CR' = 0xD = 13 = '\r'
29 wxTextFileType_Os2
// 'CR' 'LF'
32 #include "wx/string.h"
36 #include "wx/dynarray.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType
,
44 class WXDLLIMPEXP_BASE
);
46 #endif // wxUSE_TEXTBUFFER
48 class WXDLLIMPEXP_BASE wxTextBuffer
51 // constants and static functions
52 // default type for current platform (determined at compile time)
53 static const wxTextFileType typeDefault
;
55 // this function returns a string which is identical to "text" passed in
56 // except that the line terminator characters are changed to correspond the
57 // given type. Called with the default argument, the function translates
58 // the string to the native format (Unix for Unix, DOS for Windows, ...).
59 static wxString
Translate(const wxString
& text
,
60 wxTextFileType type
= typeDefault
);
62 // get the buffer termination string
63 static const wxChar
*GetEOL(wxTextFileType type
= typeDefault
);
65 // the static methods of this class are compiled in even when
66 // !wxUSE_TEXTBUFFER because they are used by the library itself, but the
67 // rest can be left out
76 // create the buffer if it doesn't already exist
79 // same as Create() but with (another) buffer name
80 bool Create(const wxString
& strBufferName
);
82 // Open() also loads buffer in memory on success
83 bool Open(wxMBConv
& conv
= wxConvUTF8
);
85 // same as Open() but with (another) buffer name
86 bool Open(const wxString
& strBufferName
, wxMBConv
& conv
= wxConvUTF8
);
88 // closes the buffer and frees memory, losing all changes
91 // is buffer currently opened?
92 bool IsOpened() const { return m_isOpened
; }
97 // get the number of lines in the buffer
98 size_t GetLineCount() const { return m_aLines
.size(); }
100 // the returned line may be modified (but don't add CR/LF at the end!)
101 wxString
& GetLine(size_t n
) const { return (wxString
&)m_aLines
[n
]; }
102 wxString
& operator[](size_t n
) const { return (wxString
&)m_aLines
[n
]; }
104 // the current line has meaning only when you're using
105 // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
106 // you're using "direct access" i.e. GetLine()
107 size_t GetCurrentLine() const { return m_nCurLine
; }
108 void GoToLine(size_t n
) { m_nCurLine
= n
; }
109 bool Eof() const { return m_nCurLine
== m_aLines
.size(); }
111 // these methods allow more "iterator-like" traversal of the list of
112 // lines, i.e. you may write something like:
113 // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
115 // NB: const is commented out because not all compilers understand
116 // 'mutable' keyword yet (m_nCurLine should be mutable)
117 wxString
& GetFirstLine() /* const */ { return m_aLines
[m_nCurLine
= 0]; }
118 wxString
& GetNextLine() /* const */ { return m_aLines
[++m_nCurLine
]; }
119 wxString
& GetPrevLine() /* const */
120 { wxASSERT(m_nCurLine
> 0); return m_aLines
[--m_nCurLine
]; }
121 wxString
& GetLastLine() /* const */
122 { return m_aLines
[m_nCurLine
= m_aLines
.size() - 1]; }
124 // get the type of the line (see also GetEOL)
125 wxTextFileType
GetLineType(size_t n
) const { return m_aTypes
[n
]; }
127 // guess the type of buffer
128 wxTextFileType
GuessType() const;
130 // get the name of the buffer
131 const wxChar
*GetName() const { return m_strBufferName
.c_str(); }
136 // add a line to the end
137 void AddLine(const wxString
& str
, wxTextFileType type
= typeDefault
)
138 { m_aLines
.push_back(str
); m_aTypes
.push_back(type
); }
139 // insert a line before the line number n
140 void InsertLine(const wxString
& str
,
142 wxTextFileType type
= typeDefault
)
144 m_aLines
.insert(m_aLines
.begin() + n
, str
);
145 m_aTypes
.insert(m_aTypes
.begin()+n
, type
);
149 void RemoveLine(size_t n
)
151 m_aLines
.erase(m_aLines
.begin() + n
);
152 m_aTypes
.erase(m_aTypes
.begin() + n
);
156 void Clear() { m_aLines
.clear(); m_aTypes
.clear(); m_nCurLine
= 0; }
158 // change the buffer (default argument means "don't change type")
159 // possibly in another format
160 bool Write(wxTextFileType typeNew
= wxTextFileType_None
,
161 wxMBConv
& conv
= wxConvUTF8
);
164 virtual ~wxTextBuffer();
170 // default ctor, use Open(string)
171 wxTextBuffer() { m_isOpened
= false; }
173 // ctor from filename
174 wxTextBuffer(const wxString
& strBufferName
);
176 enum wxTextBufferOpenMode
{ ReadAccess
, WriteAccess
};
178 // Must implement these in derived classes.
179 virtual bool OnExists() const = 0;
180 virtual bool OnOpen(const wxString
&strBufferName
,
181 wxTextBufferOpenMode openmode
) = 0;
182 virtual bool OnClose() = 0;
183 virtual bool OnRead(wxMBConv
& conv
) = 0;
184 virtual bool OnWrite(wxTextFileType typeNew
, wxMBConv
& conv
) = 0;
186 wxString m_strBufferName
; // name of the buffer
189 wxArrayLinesType m_aTypes
; // type of each line
190 wxArrayString m_aLines
; // lines of file
192 size_t m_nCurLine
; // number of current line in the buffer
194 bool m_isOpened
; // was the buffer successfully opened the last time?
195 #endif // wxUSE_TEXTBUFFER
197 // copy ctor/assignment operator not implemented
198 wxTextBuffer(const wxTextBuffer
&);
199 wxTextBuffer
& operator=(const wxTextBuffer
&);
202 #endif // _WX_TEXTBUFFER_H