]> git.saurik.com Git - wxWidgets.git/blame - include/wx/textbuf.h
Warning fixes found under hardest mode of OpenWatcom. Seems clean in Borland, MinGW...
[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
77ffb593 8// Copyright: (c) 1998-2001 wxWidgets team
65571936 9// Licence: wxWindows licence
a3a584a7
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_TEXTBUFFER_H
13#define _WX_TEXTBUFFER_H
14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a3a584a7
VZ
16 #pragma interface "textbuf.h"
17#endif
18
19#include "wx/defs.h"
df5168c4 20#include "wx/arrstr.h"
a3a584a7
VZ
21
22// ----------------------------------------------------------------------------
23// constants
24// ----------------------------------------------------------------------------
25
26// the line termination type (kept wxTextFileType name for compability)
27enum wxTextFileType
28{
29 wxTextFileType_None, // incomplete (the last line of the file only)
30 wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
31 wxTextFileType_Dos, // 'CR' 'LF'
32 wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r'
33 wxTextFileType_Os2 // 'CR' 'LF'
34};
35
36#include "wx/string.h"
37
38#if wxUSE_TEXTBUFFER
39
40#include "wx/dynarray.h"
41
42// ----------------------------------------------------------------------------
43// wxTextBuffer
44// ----------------------------------------------------------------------------
45
4b9d8a9c
VZ
46WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
47 wxArrayLinesType,
48 class WXDLLIMPEXP_BASE);
a3a584a7
VZ
49
50#endif // wxUSE_TEXTBUFFER
51
bddd7a8d 52class WXDLLIMPEXP_BASE wxTextBuffer
a3a584a7
VZ
53{
54public:
55 // constants and static functions
56 // default type for current platform (determined at compile time)
57 static const wxTextFileType typeDefault;
58
59 // this function returns a string which is identical to "text" passed in
60 // except that the line terminator characters are changed to correspond the
61 // given type. Called with the default argument, the function translates
62 // the string to the native format (Unix for Unix, DOS for Windows, ...).
63 static wxString Translate(const wxString& text,
64 wxTextFileType type = typeDefault);
65
66 // get the buffer termination string
67 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
68
69 // the static methods of this class are compiled in even when
70 // !wxUSE_TEXTBUFFER because they are used by the library itself, but the
71 // rest can be left out
72#if wxUSE_TEXTBUFFER
73
74 // buffer operations
75 // -----------------
76
77 // buffer exists?
78 bool Exists() const;
79
80 // create the buffer if it doesn't already exist
81 bool Create();
82
83 // same as Create() but with (another) buffer name
84 bool Create(const wxString& strBufferName);
85
86 // Open() also loads buffer in memory on success
d3c0ce34 87 bool Open(wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
88
89 // same as Open() but with (another) buffer name
d3c0ce34 90 bool Open(const wxString& strBufferName, wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
91
92 // closes the buffer and frees memory, losing all changes
93 bool Close();
94
95 // is buffer currently opened?
96 bool IsOpened() const { return m_isOpened; }
97
98 // accessors
99 // ---------
100
101 // get the number of lines in the buffer
df5168c4 102 size_t GetLineCount() const { return m_aLines.size(); }
a3a584a7
VZ
103
104 // the returned line may be modified (but don't add CR/LF at the end!)
df5168c4
MB
105 wxString& GetLine(size_t n) const { return (wxString&)m_aLines[n]; }
106 wxString& operator[](size_t n) const { return (wxString&)m_aLines[n]; }
a3a584a7
VZ
107
108 // the current line has meaning only when you're using
109 // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
110 // you're using "direct access" i.e. GetLine()
111 size_t GetCurrentLine() const { return m_nCurLine; }
112 void GoToLine(size_t n) { m_nCurLine = n; }
df5168c4 113 bool Eof() const { return (m_aLines.size() == 0 || m_nCurLine == m_aLines.size() - 1); }
a3a584a7
VZ
114
115 // these methods allow more "iterator-like" traversal of the list of
116 // lines, i.e. you may write something like:
117 // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
118
119 // NB: const is commented out because not all compilers understand
120 // 'mutable' keyword yet (m_nCurLine should be mutable)
121 wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; }
122 wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; }
123 wxString& GetPrevLine() /* const */
124 { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
125 wxString& GetLastLine() /* const */
df5168c4 126 { return m_aLines[m_nCurLine = m_aLines.size() - 1]; }
a3a584a7
VZ
127
128 // get the type of the line (see also GetEOL)
129 wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
130
131 // guess the type of buffer
132 wxTextFileType GuessType() const;
133
134 // get the name of the buffer
135 const wxChar *GetName() const { return m_strBufferName.c_str(); }
136
137 // add/remove lines
138 // ----------------
139
140 // add a line to the end
141 void AddLine(const wxString& str, wxTextFileType type = typeDefault)
df5168c4 142 { m_aLines.push_back(str); m_aTypes.push_back(type); }
a3a584a7
VZ
143 // insert a line before the line number n
144 void InsertLine(const wxString& str,
145 size_t n,
146 wxTextFileType type = typeDefault)
df5168c4 147 {
cb719f2e
WS
148 m_aLines.insert(m_aLines.begin() + n, str);
149 m_aTypes.insert(m_aTypes.begin()+n, type);
df5168c4
MB
150 }
151
a3a584a7 152 // delete one line
df5168c4
MB
153 void RemoveLine(size_t n)
154 {
155 m_aLines.erase(m_aLines.begin() + n);
cb719f2e 156 m_aTypes.erase(m_aTypes.begin() + n);
df5168c4 157 }
a3a584a7 158
0bcd7416 159 // remove all lines
df5168c4 160 void Clear() { m_aLines.clear(); m_nCurLine = 0; }
51e25780 161
a3a584a7
VZ
162 // change the buffer (default argument means "don't change type")
163 // possibly in another format
164 bool Write(wxTextFileType typeNew = wxTextFileType_None,
d3c0ce34 165 wxMBConv& conv = wxConvUTF8);
a3a584a7
VZ
166
167 // dtor
42841dfc 168 virtual ~wxTextBuffer(){};
a3a584a7
VZ
169
170protected:
171 // ctors
172 // -----
173
174 // default ctor, use Open(string)
51e25780 175 wxTextBuffer() { m_isOpened = false; }
a3a584a7
VZ
176
177 // ctor from filename
178 wxTextBuffer(const wxString& strBufferName);
179
180 enum wxTextBufferOpenMode { ReadAccess, WriteAccess };
181
182 // Must implement these in derived classes.
183 virtual bool OnExists() const = 0;
184 virtual bool OnOpen(const wxString &strBufferName,
185 wxTextBufferOpenMode openmode) = 0;
186 virtual bool OnClose() = 0;
187 virtual bool OnRead(wxMBConv& conv) = 0;
2b5f62a0 188 virtual bool OnWrite(wxTextFileType typeNew, wxMBConv& conv) = 0;
a3a584a7
VZ
189
190 wxString m_strBufferName; // name of the buffer
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