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