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
8 // Copyright:   (c) 1998-2001 Morten Hanssen, Vadim Zeitlin
9 // Licence:     wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef _WX_TEXTBUFFER_H
13 #define _WX_TEXTBUFFER_H
14 
15 #include "wx/defs.h"
16 #include "wx/arrstr.h"
17 #include "wx/convauto.h"
18 
19 // ----------------------------------------------------------------------------
20 // constants
21 // ----------------------------------------------------------------------------
22 
23 // the line termination type (kept wxTextFileType name for compatibility)
24 enum 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 
43 WX_DEFINE_USER_EXPORTED_ARRAY_INT(wxTextFileType,
44                                   wxArrayLinesType,
45                                   class WXDLLIMPEXP_BASE);
46 
47 #endif // wxUSE_TEXTBUFFER
48 
49 class WXDLLIMPEXP_BASE wxTextBuffer
50 {
51 public:
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
84     bool Open(const wxMBConv& conv = wxConvAuto());
85 
86     // same as Open() but with (another) buffer name
87     bool Open(const wxString& strBufferName, const wxMBConv& conv = wxConvAuto());
88 
89     // closes the buffer and frees memory, losing all changes
90     bool Close();
91 
92     // is buffer currently opened?
IsOpened()93     bool IsOpened() const { return m_isOpened; }
94 
95     // accessors
96     // ---------
97 
98     // get the number of lines in the buffer
GetLineCount()99     size_t GetLineCount() const { return m_aLines.size(); }
100 
101     // the returned line may be modified (but don't add CR/LF at the end!)
GetLine(size_t n)102           wxString& GetLine(size_t n)          { return m_aLines[n]; }
GetLine(size_t 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]; }
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()
GetCurrentLine()110     size_t GetCurrentLine() const { return m_nCurLine; }
GoToLine(size_t n)111     void GoToLine(size_t n) { m_nCurLine = n; }
Eof()112     bool Eof() const { return m_nCurLine == m_aLines.size(); }
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 
GetFirstLine()118     wxString& GetFirstLine()
119         { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = 0]; }
GetNextLine()120     wxString& GetNextLine()
121         { return ++m_nCurLine == m_aLines.size() ? ms_eof
122                                                  : m_aLines[m_nCurLine]; }
GetPrevLine()123     wxString& GetPrevLine()
124         { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
GetLastLine()125     wxString& GetLastLine()
126         { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = m_aLines.size() - 1]; }
127 
128     // get the type of the line (see also GetEOL)
GetLineType(size_t n)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
GetName()135     const wxString& GetName() const { return m_strBufferName; }
136 
137     // add/remove lines
138     // ----------------
139 
140     // add a line to the end
141     void AddLine(const wxString& str, wxTextFileType type = typeDefault)
142         { m_aLines.push_back(str); m_aTypes.push_back(type); }
143     // insert a line before the line number n
144     void InsertLine(const wxString& str,
145                   size_t n,
146                   wxTextFileType type = typeDefault)
147     {
148         m_aLines.insert(m_aLines.begin() + n, str);
149         m_aTypes.insert(m_aTypes.begin()+n, type);
150     }
151 
152     // delete one line
RemoveLine(size_t n)153     void RemoveLine(size_t n)
154     {
155         m_aLines.erase(m_aLines.begin() + n);
156         m_aTypes.erase(m_aTypes.begin() + n);
157     }
158 
159     // remove all lines
Clear()160     void Clear() { m_aLines.clear(); m_aTypes.clear(); m_nCurLine = 0; }
161 
162     // change the buffer (default argument means "don't change type")
163     // possibly in another format
164     bool Write(wxTextFileType typeNew = wxTextFileType_None,
165                const wxMBConv& conv = wxConvAuto());
166 
167     // dtor
168     virtual ~wxTextBuffer();
169 
170 protected:
171     // ctors
172     // -----
173 
174     // default ctor, use Open(string)
wxTextBuffer()175     wxTextBuffer() { m_nCurLine = 0; m_isOpened = false; }
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(const wxMBConv& conv) = 0;
188     virtual bool OnWrite(wxTextFileType typeNew, const wxMBConv& conv) = 0;
189 
190     static wxString ms_eof;     // dummy string returned at EOF
191     wxString m_strBufferName;   // name of the buffer
192 
193 private:
194     wxArrayLinesType m_aTypes;   // type of each line
195     wxArrayString    m_aLines;   // lines of file
196 
197     size_t        m_nCurLine; // number of current line in the buffer
198 
199     bool          m_isOpened; // was the buffer successfully opened the last time?
200 #endif // wxUSE_TEXTBUFFER
201 
202     // copy ctor/assignment operator not implemented
203     wxTextBuffer(const wxTextBuffer&);
204     wxTextBuffer& operator=(const wxTextBuffer&);
205 };
206 
207 #endif // _WX_TEXTBUFFER_H
208 
209