]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: textfile.h | |
3 | // Purpose: class wxTextFile to work with text files of _small_ size | |
4 | // (file is fully loaded in memory) and which understands CR/LF | |
5 | // differences between platforms. | |
6 | // Author: Vadim Zeitlin | |
f42d2aba | 7 | // Modified by: |
c801d85f KB |
8 | // Created: 03.04.98 |
9 | // RCS-ID: $Id$ | |
10 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
11 | // Licence: wxWindows license | |
12 | /////////////////////////////////////////////////////////////////////////////// | |
13 | ||
a1b82138 VZ |
14 | #ifndef _WX_TEXTFILE_H |
15 | #define _WX_TEXTFILE_H | |
c801d85f KB |
16 | |
17 | #ifdef __GNUG__ | |
a1b82138 | 18 | #pragma interface "textfile.h" |
c801d85f KB |
19 | #endif |
20 | ||
21 | #include "wx/defs.h" | |
ce4169a4 | 22 | |
a1b82138 VZ |
23 | #if !wxUSE_FILE |
24 | #undef wxUSE_TEXTFILE | |
25 | #define wxUSE_TEXTFILE 0 | |
26 | #endif // wxUSE_FILE | |
27 | ||
c801d85f | 28 | // ---------------------------------------------------------------------------- |
d8edb385 | 29 | // constants |
c801d85f | 30 | // ---------------------------------------------------------------------------- |
c2389495 | 31 | |
d8edb385 VZ |
32 | // NB: this is always defined, even if !wxUSE_TEXTFILE |
33 | ||
c2389495 VZ |
34 | // the line termination type |
35 | enum wxTextFileType | |
36 | { | |
37 | wxTextFileType_None, // incomplete (the last line of the file only) | |
38 | wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n' | |
39 | wxTextFileType_Dos, // 'CR' 'LF' | |
717b9bf2 DW |
40 | wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r' |
41 | wxTextFileType_Os2 // 'CR' 'LF' | |
c2389495 VZ |
42 | }; |
43 | ||
d8edb385 VZ |
44 | #if wxUSE_TEXTFILE |
45 | ||
46 | #include "wx/string.h" | |
47 | #include "wx/file.h" | |
48 | #include "wx/dynarray.h" | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxTextFile | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
a497618a | 54 | WX_DEFINE_EXPORTED_ARRAY(wxTextFileType, ArrayFileType); |
c2389495 | 55 | |
9f04ccb1 | 56 | class WXDLLEXPORT wxTextFile |
c801d85f KB |
57 | { |
58 | public: | |
a1b82138 VZ |
59 | // constants and static functions |
60 | // default type for current platform (determined at compile time) | |
c2389495 | 61 | static const wxTextFileType typeDefault; |
c801d85f | 62 | |
a1b82138 VZ |
63 | // this function returns a string which is identical to "text" passed in |
64 | // except that the line terminator characters are changed to correspond the | |
65 | // given type. Called with the default argument, the function translates | |
66 | // the string to the native format (Unix for Unix, DOS for Windows, ...). | |
67 | static wxString Translate(const wxString& text, | |
68 | wxTextFileType type = typeDefault); | |
69 | ||
70 | // get the file termination string | |
71 | static const wxChar *GetEOL(wxTextFileType type = typeDefault); | |
72 | ||
c801d85f KB |
73 | // ctors |
74 | // def ctor, use Open(string) | |
75 | wxTextFile() { } | |
f42d2aba | 76 | // |
c801d85f KB |
77 | wxTextFile(const wxString& strFile); |
78 | ||
79 | // file operations | |
80 | // file exists? | |
81 | bool Exists() const; | |
1b6dea5d MB |
82 | // create the file if it doesn't already exist |
83 | bool Create(); | |
84 | // same as Create() but with (another) file name | |
85 | bool Create(const wxString& strFile); | |
c801d85f KB |
86 | // Open() also loads file in memory on success |
87 | bool Open(); | |
88 | // same as Open() but with (another) file name | |
89 | bool Open(const wxString& strFile); | |
90 | // closes the file and frees memory, losing all changes | |
91 | bool Close(); | |
92 | // is file currently opened? | |
7cc98b3e | 93 | bool IsOpened() const { return m_isOpened; } |
c801d85f KB |
94 | |
95 | // accessors | |
96 | // get the number of lines in the file | |
be6bf94b | 97 | size_t GetLineCount() const { return m_aLines.Count(); } |
c801d85f | 98 | // the returned line may be modified (but don't add CR/LF at the end!) |
c86f1403 VZ |
99 | wxString& GetLine(size_t n) const { return m_aLines[n]; } |
100 | wxString& operator[](size_t n) const { return m_aLines[n]; } | |
be6bf94b VZ |
101 | |
102 | // the current line has meaning only when you're using | |
103 | // GetFirstLine()/GetNextLine() functions, it doesn't get updated when | |
104 | // you're using "direct access" i.e. GetLine() | |
105 | size_t GetCurrentLine() const { return m_nCurLine; } | |
106 | void GoToLine(size_t n) { m_nCurLine = n; } | |
5e0e6ceb | 107 | bool Eof() const { return (m_aLines.Count() == 0 || m_nCurLine == m_aLines.Count() - 1); } |
be6bf94b VZ |
108 | |
109 | // these methods allow more "iterator-like" traversal of the list of | |
110 | // lines, i.e. you may write something like: | |
111 | // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... } | |
112 | ||
f42d2aba | 113 | // NB: const is commented out because not all compilers understand |
be6bf94b VZ |
114 | // 'mutable' keyword yet (m_nCurLine should be mutable) |
115 | wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; } | |
116 | wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; } | |
117 | wxString& GetPrevLine() /* const */ | |
118 | { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; } | |
119 | wxString& GetLastLine() /* const */ | |
120 | { return m_aLines[m_nCurLine = m_aLines.Count() - 1]; } | |
121 | ||
c801d85f | 122 | // get the type of the line (see also GetEOL) |
c2389495 | 123 | wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; } |
c801d85f | 124 | // guess the type of file (m_file is supposed to be opened) |
c2389495 | 125 | wxTextFileType GuessType() const; |
c801d85f | 126 | // get the name of the file |
9d2f3c71 | 127 | const wxChar *GetName() const { return m_strFile.c_str(); } |
c801d85f KB |
128 | |
129 | // add/remove lines | |
130 | // add a line to the end | |
f42d2aba | 131 | void AddLine(const wxString& str, wxTextFileType type = typeDefault) |
c801d85f KB |
132 | { m_aLines.Add(str); m_aTypes.Add(type); } |
133 | // insert a line before the line number n | |
c2389495 VZ |
134 | void InsertLine(const wxString& str, |
135 | size_t n, | |
f42d2aba | 136 | wxTextFileType type = typeDefault) |
c801d85f KB |
137 | { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); } |
138 | // delete one line | |
c86f1403 | 139 | void RemoveLine(size_t n) { m_aLines.Remove(n); m_aTypes.Remove(n); } |
c801d85f KB |
140 | |
141 | // change the file on disk (default argument means "don't change type") | |
142 | // possibly in another format | |
c2389495 | 143 | bool Write(wxTextFileType typeNew = wxTextFileType_None); |
c801d85f | 144 | |
c801d85f KB |
145 | // dtor |
146 | ~wxTextFile(); | |
147 | ||
148 | private: | |
149 | // copy ctor/assignment operator not implemented | |
150 | wxTextFile(const wxTextFile&); | |
151 | wxTextFile& operator=(const wxTextFile&); | |
152 | ||
153 | // read the file in memory (m_file is supposed to be just opened) | |
154 | bool Read(); | |
155 | ||
c801d85f | 156 | wxFile m_file; // current file |
be6bf94b | 157 | |
c801d85f KB |
158 | ArrayFileType m_aTypes; // type of each line |
159 | wxArrayString m_aLines; // lines of file | |
be6bf94b VZ |
160 | |
161 | size_t m_nCurLine; // number of current line in the file | |
162 | ||
7cc98b3e VZ |
163 | bool m_isOpened; // was the file successfully opened the last time? |
164 | ||
c801d85f KB |
165 | wxString m_strFile; // name of the file |
166 | }; | |
167 | ||
a1b82138 | 168 | #else // !wxUSE_TEXTFILE |
ce4169a4 | 169 | |
a1b82138 VZ |
170 | // these static wxTextFile methods are used internally by wxWindows, so should |
171 | // be defined even if we're compiling without wxTextFile at all. | |
172 | ||
173 | class WXDLLEXPORT wxTextFile | |
174 | { | |
175 | public: | |
176 | // default type for current platform (determined at compile time) | |
177 | static const wxTextFileType typeDefault; | |
178 | ||
179 | // this function returns a string which is identical to "text" passed in | |
180 | // except that the line terminator characters are changed to correspond the | |
181 | // given type. Called with the default argument, the function translates | |
182 | // the string to the native format (Unix for Unix, DOS for Windows, ...). | |
183 | static wxString Translate(const wxString& text, | |
184 | wxTextFileType type = typeDefault); | |
185 | ||
186 | // get the file termination string | |
187 | static const wxChar *GetEOL(wxTextFileType type = typeDefault); | |
188 | ||
189 | private: | |
190 | // copy ctor/assignment operator not implemented | |
191 | wxTextFile(const wxTextFile&); | |
192 | wxTextFile& operator=(const wxTextFile&); | |
193 | }; | |
194 | ||
195 | #endif // wxUSE_TEXTFILE | |
196 | ||
197 | #endif // _WX_TEXTFILE_H | |
9f04ccb1 | 198 |