]> git.saurik.com Git - wxWidgets.git/blame - include/wx/textbuf.h
Warning fix for wxOS2 night build.
[wxWidgets.git] / include / wx / textbuf.h
CommitLineData
a3a584a7
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/textbuf.h
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.
6// Created: 14.11.01
7// Author: Morten Hanssen, Vadim Zeitlin
99d80019 8// Copyright: (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
65571936 9// Licence: wxWindows licence
a3a584a7
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_TEXTBUFFER_H
13#define _WX_TEXTBUFFER_H
14
a3a584a7 15#include "wx/defs.h"
df5168c4 16#include "wx/arrstr.h"
a3a584a7
VZ
17
18// ----------------------------------------------------------------------------
19// constants
20// ----------------------------------------------------------------------------
21
22// the line termination type (kept wxTextFileType name for compability)
23enum wxTextFileType
24{
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'
30};
31
32#include "wx/string.h"
33
34#if wxUSE_TEXTBUFFER
35
36#include "wx/dynarray.h"
37
38// ----------------------------------------------------------------------------
39// wxTextBuffer
40// ----------------------------------------------------------------------------
41
4b9d8a9c
VZ
42WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
43 wxArrayLinesType,
44 class WXDLLIMPEXP_BASE);
a3a584a7
VZ
45
46#endif // wxUSE_TEXTBUFFER
47
bddd7a8d 48class WXDLLIMPEXP_BASE wxTextBuffer
a3a584a7
VZ
49{
50public:
51 // constants and static functions
52 // default type for current platform (determined at compile time)
53 static const wxTextFileType typeDefault;
54
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);
61
62 // get the buffer termination string
63 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
64
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
68#if wxUSE_TEXTBUFFER
69
70 // buffer operations
71 // -----------------
72
73 // buffer exists?
74 bool Exists() const;
75
76 // create the buffer if it doesn't already exist
77 bool Create();
78
79 // same as Create() but with (another) buffer name
80 bool Create(const wxString& strBufferName);
81
82 // Open() also loads buffer in memory on success
d3c0ce34 83 bool Open(wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
84
85 // same as Open() but with (another) buffer name
d3c0ce34 86 bool Open(const wxString& strBufferName, wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
87
88 // closes the buffer and frees memory, losing all changes
89 bool Close();
90
91 // is buffer currently opened?
92 bool IsOpened() const { return m_isOpened; }
93
94 // accessors
95 // ---------
96
97 // get the number of lines in the buffer
df5168c4 98 size_t GetLineCount() const { return m_aLines.size(); }
a3a584a7
VZ
99
100 // the returned line may be modified (but don't add CR/LF at the end!)
df5168c4
MB
101 wxString& GetLine(size_t n) const { return (wxString&)m_aLines[n]; }
102 wxString& operator[](size_t n) const { return (wxString&)m_aLines[n]; }
a3a584a7
VZ
103
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; }
2dfa1d9e 109 bool Eof() const { return m_nCurLine == m_aLines.size(); }
a3a584a7
VZ
110
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() ) { ... }
114
115 // NB: const is commented out because not all compilers understand
116 // 'mutable' keyword yet (m_nCurLine should be mutable)
4ae776b7
VZ
117 wxString& GetFirstLine() /* const */
118 { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = 0]; }
119 wxString& GetNextLine() /* const */
120 { return ++m_nCurLine == m_aLines.size() ? ms_eof
121 : m_aLines[m_nCurLine]; }
a3a584a7 122 wxString& GetPrevLine() /* const */
4ae776b7 123 { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
a3a584a7 124 wxString& GetLastLine() /* const */
4ae776b7 125 { m_nCurLine = m_aLines.size() - 1; return m_aLines.Last(); }
a3a584a7
VZ
126
127 // get the type of the line (see also GetEOL)
128 wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
129
130 // guess the type of buffer
131 wxTextFileType GuessType() const;
132
133 // get the name of the buffer
134 const wxChar *GetName() const { return m_strBufferName.c_str(); }
135
136 // add/remove lines
137 // ----------------
138
139 // add a line to the end
140 void AddLine(const wxString& str, wxTextFileType type = typeDefault)
df5168c4 141 { m_aLines.push_back(str); m_aTypes.push_back(type); }
a3a584a7
VZ
142 // insert a line before the line number n
143 void InsertLine(const wxString& str,
144 size_t n,
145 wxTextFileType type = typeDefault)
df5168c4 146 {
cb719f2e
WS
147 m_aLines.insert(m_aLines.begin() + n, str);
148 m_aTypes.insert(m_aTypes.begin()+n, type);
df5168c4
MB
149 }
150
a3a584a7 151 // delete one line
df5168c4
MB
152 void RemoveLine(size_t n)
153 {
154 m_aLines.erase(m_aLines.begin() + n);
cb719f2e 155 m_aTypes.erase(m_aTypes.begin() + n);
df5168c4 156 }
a3a584a7 157
0bcd7416 158 // remove all lines
8397cab4 159 void Clear() { m_aLines.clear(); m_aTypes.clear(); m_nCurLine = 0; }
51e25780 160
a3a584a7
VZ
161 // change the buffer (default argument means "don't change type")
162 // possibly in another format
163 bool Write(wxTextFileType typeNew = wxTextFileType_None,
d3c0ce34 164 wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
165
166 // dtor
5e233068 167 virtual ~wxTextBuffer();
a3a584a7
VZ
168
169protected:
170 // ctors
171 // -----
172
173 // default ctor, use Open(string)
51e25780 174 wxTextBuffer() { m_isOpened = false; }
a3a584a7
VZ
175
176 // ctor from filename
177 wxTextBuffer(const wxString& strBufferName);
178
179 enum wxTextBufferOpenMode { ReadAccess, WriteAccess };
180
181 // Must implement these in derived classes.
182 virtual bool OnExists() const = 0;
183 virtual bool OnOpen(const wxString &strBufferName,
184 wxTextBufferOpenMode openmode) = 0;
185 virtual bool OnClose() = 0;
186 virtual bool OnRead(wxMBConv& conv) = 0;
2b5f62a0 187 virtual bool OnWrite(wxTextFileType typeNew, wxMBConv& conv) = 0;
a3a584a7 188
4ae776b7
VZ
189 static wxString ms_eof; // dummy string returned at EOF
190 wxString m_strBufferName; // name of the buffer
a3a584a7
VZ
191
192private:
4b9d8a9c
VZ
193 wxArrayLinesType m_aTypes; // type of each line
194 wxArrayString m_aLines; // lines of file
a3a584a7
VZ
195
196 size_t m_nCurLine; // number of current line in the buffer
197
198 bool m_isOpened; // was the buffer successfully opened the last time?
199#endif // wxUSE_TEXTBUFFER
200
201 // copy ctor/assignment operator not implemented
202 wxTextBuffer(const wxTextBuffer&);
203 wxTextBuffer& operator=(const wxTextBuffer&);
204};
205
206#endif // _WX_TEXTBUFFER_H
207