fix typo; distribute docs between Open() overloads for better rendering/better functi...
[wxWidgets.git] / interface / wx / textfile.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textfile.h
3 // Purpose: interface of wxTextFile
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 /**
11 The line termination type (kept wxTextFileType name for compability).
12 */
13 enum wxTextFileType
14 {
15 wxTextFileType_None, //!< incomplete (the last line of the file only)
16 wxTextFileType_Unix, //!< line is terminated with 'LF' = 0xA = 10 = '\\n'
17 wxTextFileType_Dos, //!< line is terminated with 'CR' 'LF'
18 wxTextFileType_Mac, //!< line is terminated with 'CR' = 0xD = 13 = '\\r'
19 wxTextFileType_Os2 //!< line is terminated with 'CR' 'LF'
20 };
21
22 /**
23 @class wxTextFile
24
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).
31
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
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
36 limitation for small files like configuration files or program sources
37 which are well handled by wxTextFile.
38
39 The typical things you may do with wxTextFile in order are:
40
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
44 case of wxTextFile::Open()) and closes it.
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!
57 To save the changes you must explicitly call wxTextFile::Write - here, you may
58 also change the line termination type if you wish.
59
60 @library{wxbase}
61 @category{file}
62
63 @see wxFile
64 */
65 class wxTextFile
66 {
67 public:
68 /**
69 Default constructor, use Create() or Open() with a file name parameter to
70 initialize the object.
71 */
72 wxTextFile();
73
74 /**
75 Constructor does not load the file into memory, use Open() to do it.
76 */
77 wxTextFile(const wxString& strFile);
78
79 /**
80 Destructor does nothing.
81 */
82 virtual ~wxTextFile();
83
84 /**
85 Adds a line to the end of file.
86 */
87 void AddLine(const wxString& str,
88 wxTextFileType type = wxTextBuffer::typeDefault);
89
90 /**
91 Delete all lines from the file, set current line number to 0.
92 */
93 void Clear();
94
95 /**
96 Closes the file and frees memory, @b "losing all changes".
97 Use Write() if you want to save them.
98 */
99 bool Close();
100
101 /**
102 Creates the file with the name which was given in the
103 wxTextFile(const wxString&) constructor.
104 The array of file lines is initially empty.
105
106 It will fail if the file already exists, Open() should be used in this case.
107 */
108 bool Create();
109
110 /**
111 Creates the file with the given name.
112 The array of file lines is initially empty.
113
114 It will fail if the file already exists, Open() should be used in this case.
115 */
116 bool Create(const wxString& strFile);
117
118 /**
119 Returns @true if the current line is the last one.
120 */
121 bool Eof() const;
122
123 /**
124 Return @true if file exists - the name of the file should have been specified
125 in the constructor before calling Exists().
126 */
127 bool Exists() const;
128
129 /**
130 Returns the current line: it has meaning only when you're using
131 GetFirstLine()/GetNextLine() functions, it doesn't get updated when
132 you're using "direct access" functions like GetLine().
133 GetFirstLine() and GetLastLine() also change the value of the current line,
134 as well as GoToLine().
135 */
136 size_t GetCurrentLine() const;
137
138 /**
139 Get the line termination string corresponding to given constant.
140
141 @e typeDefault is the value defined during the compilation and corresponds
142 to the native format of the platform, i.e. it will be @c wxTextFileType_Dos
143 under Windows, @c wxTextFileType_Unix under Unix (including Mac OS X when
144 compiling with the Apple Developer Tools) and @c wxTextFileType_Mac under
145 Mac OS (including Mac OS X when compiling with CodeWarrior).
146 */
147 static const wxChar* GetEOL(wxTextFileType type = wxTextBuffer::typeDefault);
148
149 /**
150 This method together with GetNextLine() allows more "iterator-like"
151 traversal of the list of lines, i.e. you may write something like:
152
153 @code
154 wxTextFile file;
155 ...
156 for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
157 {
158 // do something with the current line in str
159 }
160 // do something with the last line in str
161 @endcode
162 */
163 wxString& GetFirstLine();
164
165 /**
166 Gets the last line of the file.
167
168 Together with GetPrevLine() it allows to enumerate the lines
169 in the file from the end to the beginning like this:
170
171 @code
172 wxTextFile file;
173 ...
174 for ( str = file.GetLastLine();
175 file.GetCurrentLine() > 0;
176 str = file.GetPrevLine() )
177 {
178 // do something with the current line in str
179 }
180 // do something with the first line in str
181 @endcode
182 */
183 wxString& GetLastLine();
184
185 /**
186 Retrieves the line number @a n from the file.
187
188 The returned line may be modified but you shouldn't add line terminator
189 at the end - this will be done by wxTextFile.
190 */
191 wxString& GetLine(size_t n) const;
192
193 /**
194 Get the number of lines in the file.
195 */
196 size_t GetLineCount() const;
197
198 /**
199 Get the type of the line (see also wxTextFile::GetEOL).
200 */
201 wxTextFileType GetLineType(size_t n) const;
202
203 /**
204 Get the name of the file.
205 */
206 const wxString& GetName() const;
207
208 /**
209 Gets the next line (see GetFirstLine() for the example).
210 */
211 wxString& GetNextLine();
212
213 /**
214 Gets the previous line in the file.
215 */
216 wxString& GetPrevLine();
217
218 /**
219 Changes the value returned by GetCurrentLine() and used by GetFirstLine()
220 and GetNextLine().
221 */
222 void GoToLine(size_t n);
223
224 /**
225 Guess the type of file (which is supposed to be opened).
226
227 If sufficiently many lines of the file are in DOS/Unix/Mac format,
228 the corresponding value will be returned.
229 If the detection mechanism fails @c wxTextFileType_None is returned.
230 */
231 wxTextFileType GuessType() const;
232
233 /**
234 Insert a line before the line number @a n.
235 */
236 void InsertLine(const wxString& str, size_t n,
237 wxTextFileType type = wxTextBuffer::typeDefault);
238
239 /**
240 Returns @true if the file is currently opened.
241 */
242 bool IsOpened() const;
243
244 /**
245 Opens the file with the name which was given in the wxTextFile(const wxString&)
246 constructor and also loads file in memory on success.
247
248 It will fail if the file does not exist, Create() should be used in this case.
249
250 The @a conv argument is only meaningful in Unicode build of wxWidgets when
251 it is used to convert the file to wide character representation.
252 */
253 bool Open(const wxMBConv& conv = wxConvAuto()) const;
254
255 /**
256 Opens the file with the given name and also loads file in memory on success.
257
258 It will fail if the file does not exist, Create() should be used in this case.
259
260 The @a conv argument is only meaningful in Unicode build of wxWidgets when
261 it is used to convert the file to wide character representation.
262 */
263 bool Open(const wxString& strFile, const wxMBConv& conv = wxConvAuto()) const;
264
265 /**
266 Delete line number @a n from the file.
267 */
268 void RemoveLine(size_t n);
269
270 /**
271 Change the file on disk.
272
273 The @a typeNew parameter allows you to change the file format
274 (default argument means "don't change type") and may be used to convert,
275 for example, DOS files to Unix.
276
277 The @a conv argument is only meaningful in Unicode build of wxWidgets when
278 it is used to convert all lines to multibyte representation before writing
279 them to physical file.
280
281 @return
282 @true if operation succeeded, @false if it failed.
283 */
284 bool Write(wxTextFileType typeNew = wxTextFileType_None,
285 const wxMBConv& conv = wxConvAuto());
286
287 /**
288 The same as GetLine().
289 */
290 wxString& operator[](size_t n) const;
291 };
292