]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/textfile.h
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / interface / wx / textfile.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: textfile.h
e54c96f1 3// Purpose: interface of wxTextFile
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
23324ae1
FM
7/////////////////////////////////////////////////////////////////////////////
8
b46eeeae 9// TODO: document wxTextBuffer
c6cf894a 10
59b9603a 11/**
b46eeeae 12 The line termination type.
59b9603a 13*/
c6cf894a
FM
14enum wxTextFileType
15{
16 wxTextFileType_None, //!< incomplete (the last line of the file only)
4876436a 17 wxTextFileType_Unix, //!< line is terminated with 'LF' = 0xA = 10 = '\\n'
c6cf894a 18 wxTextFileType_Dos, //!< line is terminated with 'CR' 'LF'
4876436a 19 wxTextFileType_Mac, //!< line is terminated with 'CR' = 0xD = 13 = '\\r'
c6cf894a
FM
20 wxTextFileType_Os2 //!< line is terminated with 'CR' 'LF'
21};
22
23324ae1
FM
23/**
24 @class wxTextFile
7c913512 25
23324ae1
FM
26 The wxTextFile is a simple class which allows to work with text files on line by
27 line basis. It also understands the differences in line termination characters
28 under different platforms and will not do anything bad to files with "non
29 native" line termination sequences - in fact, it can be also used to modify the
30 text files and change the line termination characters from one type (say DOS) to
31 another (say Unix).
7c913512 32
23324ae1
FM
33 One word of warning: the class is not at all optimized for big files and thus
34 it will load the file entirely into memory when opened. Of course, you should
c6cf894a
FM
35 not work in this way with large files (as an estimation, anything over 1 Megabyte
36 is surely too big for this class). On the other hand, it is not a serious
23324ae1
FM
37 limitation for small files like configuration files or program sources
38 which are well handled by wxTextFile.
7c913512 39
23324ae1 40 The typical things you may do with wxTextFile in order are:
7c913512 41
c6cf894a
FM
42 - Create and open it: this is done with either wxTextFile::Create or wxTextFile::Open
43 function which opens the file (name may be specified either as the argument to
44 these functions or in the constructor), reads its contents in memory (in the
59b9603a 45 case of wxTextFile::Open()) and closes it.
c6cf894a
FM
46 - Work with the lines in the file: this may be done either with "direct
47 access" functions like wxTextFile::GetLineCount and wxTextFile::GetLine
48 (@e operator[] does exactly the same but looks more like array addressing)
49 or with "sequential access" functions which include wxTextFile::GetFirstLine,
50 wxTextFile::GetNextLine and also wxTextFile::GetLastLine, wxTextFile::GetPrevLine.
51 For the sequential access functions the current line number is maintained: it is
52 returned by wxTextFile::GetCurrentLine and may be changed with wxTextFile::GoToLine.
53 - Add/remove lines to the file: wxTextFile::AddLine and wxTextFile::InsertLine
54 add new lines while wxTextFile::RemoveLine deletes the existing ones.
55 wxTextFile::Clear resets the file to empty.
56 - Save your changes: notice that the changes you make to the file will @b not be
57 saved automatically; calling wxTextFile::Close or doing nothing discards them!
482dce2a 58 To save the changes you must explicitly call wxTextFile::Write - here, you may
c6cf894a 59 also change the line termination type if you wish.
7c913512 60
23324ae1
FM
61 @library{wxbase}
62 @category{file}
7c913512 63
e54c96f1 64 @see wxFile
23324ae1 65*/
7c913512 66class wxTextFile
23324ae1
FM
67{
68public:
b46eeeae
VZ
69 /**
70 Default type for current platform determined at compile time.
71 */
72 static const wxTextFileType typeDefault;
73
c6cf894a
FM
74 /**
75 Default constructor, use Create() or Open() with a file name parameter to
76 initialize the object.
77 */
78 wxTextFile();
79
23324ae1
FM
80 /**
81 Constructor does not load the file into memory, use Open() to do it.
82 */
adaaa686 83 wxTextFile(const wxString& strFile);
23324ae1
FM
84
85 /**
86 Destructor does nothing.
87 */
adaaa686 88 virtual ~wxTextFile();
23324ae1
FM
89
90 /**
91 Adds a line to the end of file.
92 */
b46eeeae 93 void AddLine(const wxString& str, wxTextFileType type = typeDefault);
23324ae1
FM
94
95 /**
96 Delete all lines from the file, set current line number to 0.
97 */
0004982c 98 void Clear();
23324ae1
FM
99
100 /**
c6cf894a
FM
101 Closes the file and frees memory, @b "losing all changes".
102 Use Write() if you want to save them.
23324ae1 103 */
0004982c 104 bool Close();
23324ae1 105
23324ae1 106 /**
e2c4ccaf
FM
107 Creates the file with the name which was given in the
108 wxTextFile(const wxString&) constructor.
109 The array of file lines is initially empty.
c6cf894a
FM
110
111 It will fail if the file already exists, Open() should be used in this case.
23324ae1 112 */
ccf39540 113 bool Create();
e2c4ccaf
FM
114
115 /**
116 Creates the file with the given name.
117 The array of file lines is initially empty.
118
119 It will fail if the file already exists, Open() should be used in this case.
120 */
ccf39540 121 bool Create(const wxString& strFile);
23324ae1
FM
122
123 /**
124 Returns @true if the current line is the last one.
125 */
328f5751 126 bool Eof() const;
23324ae1
FM
127
128 /**
129 Return @true if file exists - the name of the file should have been specified
130 in the constructor before calling Exists().
131 */
328f5751 132 bool Exists() const;
23324ae1
FM
133
134 /**
135 Returns the current line: it has meaning only when you're using
136 GetFirstLine()/GetNextLine() functions, it doesn't get updated when
c6cf894a
FM
137 you're using "direct access" functions like GetLine().
138 GetFirstLine() and GetLastLine() also change the value of the current line,
139 as well as GoToLine().
23324ae1 140 */
328f5751 141 size_t GetCurrentLine() const;
23324ae1
FM
142
143 /**
c6cf894a
FM
144 Get the line termination string corresponding to given constant.
145
146 @e typeDefault is the value defined during the compilation and corresponds
147 to the native format of the platform, i.e. it will be @c wxTextFileType_Dos
2415cf67
VZ
148 under Windows and @c wxTextFileType_Unix under Unix (including Mac OS
149 X, the value @c wxTextFileType_Mac was only used for classic Mac OS
150 versions).
23324ae1 151 */
b46eeeae 152 static const wxChar* GetEOL(wxTextFileType type = typeDefault);
23324ae1
FM
153
154 /**
c6cf894a
FM
155 This method together with GetNextLine() allows more "iterator-like"
156 traversal of the list of lines, i.e. you may write something like:
157
158 @code
159 wxTextFile file;
160 ...
161 for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
162 {
163 // do something with the current line in str
164 }
165 // do something with the last line in str
166 @endcode
23324ae1 167 */
43c48e1e 168 wxString& GetFirstLine();
23324ae1
FM
169
170 /**
c6cf894a
FM
171 Gets the last line of the file.
172
173 Together with GetPrevLine() it allows to enumerate the lines
23324ae1 174 in the file from the end to the beginning like this:
c6cf894a
FM
175
176 @code
177 wxTextFile file;
178 ...
179 for ( str = file.GetLastLine();
180 file.GetCurrentLine() > 0;
181 str = file.GetPrevLine() )
182 {
183 // do something with the current line in str
184 }
185 // do something with the first line in str
186 @endcode
23324ae1 187 */
43c48e1e 188 wxString& GetLastLine();
23324ae1
FM
189
190 /**
c6cf894a
FM
191 Retrieves the line number @a n from the file.
192
214ffcf5
VZ
193 The returned line may be modified when non-const method is used but you
194 shouldn't add line terminator at the end -- this will be done by
195 wxTextFile itself.
196 */
197 //@{
198 wxString& GetLine(size_t n);
199 const wxString& GetLine(size_t n) const;
200 //@}
23324ae1
FM
201
202 /**
203 Get the number of lines in the file.
204 */
328f5751 205 size_t GetLineCount() const;
23324ae1
FM
206
207 /**
c6cf894a 208 Get the type of the line (see also wxTextFile::GetEOL).
23324ae1 209 */
328f5751 210 wxTextFileType GetLineType(size_t n) const;
23324ae1
FM
211
212 /**
213 Get the name of the file.
214 */
43c48e1e 215 const wxString& GetName() const;
23324ae1
FM
216
217 /**
c6cf894a 218 Gets the next line (see GetFirstLine() for the example).
23324ae1 219 */
43c48e1e 220 wxString& GetNextLine();
23324ae1
FM
221
222 /**
223 Gets the previous line in the file.
224 */
43c48e1e 225 wxString& GetPrevLine();
23324ae1
FM
226
227 /**
c6cf894a
FM
228 Changes the value returned by GetCurrentLine() and used by GetFirstLine()
229 and GetNextLine().
23324ae1 230 */
0004982c 231 void GoToLine(size_t n);
23324ae1
FM
232
233 /**
c6cf894a
FM
234 Guess the type of file (which is supposed to be opened).
235
236 If sufficiently many lines of the file are in DOS/Unix/Mac format,
237 the corresponding value will be returned.
238 If the detection mechanism fails @c wxTextFileType_None is returned.
23324ae1 239 */
328f5751 240 wxTextFileType GuessType() const;
23324ae1
FM
241
242 /**
c6cf894a 243 Insert a line before the line number @a n.
23324ae1
FM
244 */
245 void InsertLine(const wxString& str, size_t n,
b46eeeae 246 wxTextFileType type = typeDefault);
23324ae1
FM
247
248 /**
249 Returns @true if the file is currently opened.
250 */
328f5751 251 bool IsOpened() const;
23324ae1 252
23324ae1 253 /**
482dce2a
FM
254 Opens the file with the name which was given in the wxTextFile(const wxString&)
255 constructor and also loads file in memory on success.
c6cf894a
FM
256
257 It will fail if the file does not exist, Create() should be used in this case.
258
259 The @a conv argument is only meaningful in Unicode build of wxWidgets when
23324ae1
FM
260 it is used to convert the file to wide character representation.
261 */
8362ae0a 262 bool Open(const wxMBConv& conv = wxConvAuto());
482dce2a
FM
263
264 /**
265 Opens the file with the given name and also loads file in memory on success.
266
267 It will fail if the file does not exist, Create() should be used in this case.
268
269 The @a conv argument is only meaningful in Unicode build of wxWidgets when
270 it is used to convert the file to wide character representation.
271 */
8362ae0a 272 bool Open(const wxString& strFile, const wxMBConv& conv = wxConvAuto());
23324ae1
FM
273
274 /**
4cc4bfaf 275 Delete line number @a n from the file.
23324ae1 276 */
0004982c 277 void RemoveLine(size_t n);
23324ae1
FM
278
279 /**
c6cf894a
FM
280 Change the file on disk.
281
282 The @a typeNew parameter allows you to change the file format
283 (default argument means "don't change type") and may be used to convert,
284 for example, DOS files to Unix.
285
286 The @a conv argument is only meaningful in Unicode build of wxWidgets when
4876436a 287 it is used to convert all lines to multibyte representation before writing
23324ae1 288 them to physical file.
c6cf894a
FM
289
290 @return
291 @true if operation succeeded, @false if it failed.
23324ae1 292 */
c6cf894a 293 bool Write(wxTextFileType typeNew = wxTextFileType_None,
fadc2df6 294 const wxMBConv& conv = wxConvAuto());
23324ae1
FM
295
296 /**
297 The same as GetLine().
298 */
43c48e1e 299 wxString& operator[](size_t n) const;
23324ae1 300};
e54c96f1 301