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