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