]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/textfile.tex
Added periods
[wxWidgets.git] / docs / latex / wx / textfile.tex
CommitLineData
7f561143
VZ
1\section{\class{wxTextFile}}\label{wxtextfile}
2
3The wxTextFile is a simple class which allows to work with text files on line by
4line basis. It also understands the differences in line termination characters
5under different platforms and will not do anything bad to files with "non
6native" line termination sequences - in fact, it can be also used to modify the
631f1bfe 7text files and change the line termination characters from one type (say DOS) to
7f561143
VZ
8another (say Unix).
9
43e8916f
MW
10One word of warning: the class is not at all optimized for big files and thus
11it will load the file entirely into memory when opened. Of course, you should not
7f561143
VZ
12work in this way with large files (as an estimation, anything over 1 Megabyte is
13surely too big for this class). On the other hand, it is not a serious
43e8916f 14limitation for small files like configuration files or program sources
7f561143
VZ
15which are well handled by wxTextFile.
16
17The typical things you may do with wxTextFile in order are:
dface61c 18
7f561143 19\begin{itemize}\itemsep=0pt
f6bcfd97
BP
20\item Create and open it: this is done with either
21\helpref{Create}{wxtextfilecreate} or \helpref{Open}{wxtextfileopen}
22function which opens the file (name may be specified either as the argument to
23these functions or in the constructor), reads its contents in memory (in the
24case of {\tt Open()}) and closes it.
7f561143 25\item Work with the lines in the file: this may be done either with "direct
631f1bfe 26access" functions like \helpref{GetLineCount}{wxtextfilegetlinecount} and
7f561143
VZ
27\helpref{GetLine}{wxtextfilegetline} ({\it operator[]} does exactly the same
28but looks more like array addressing) or with "sequential access" functions
29which include \helpref{GetFirstLine}{wxtextfilegetfirstline}/
30\helpref{GetNextLine}{wxtextfilegetnextline} and also
dface61c 31\helpref{GetLastLine}{wxtextfilegetlastline}/\helpref{GetPrevLine}{wxtextfilegetprevline}.
7f561143
VZ
32For the sequential access functions the current line number is maintained: it is
33returned by \helpref{GetCurrentLine}{wxtextfilegetcurrentline} and may be
34changed with \helpref{GoToLine}{wxtextfilegotoline}.
35\item Add/remove lines to the file: \helpref{AddLine}{wxtextfileaddline} and
36\helpref{InsertLine}{wxtextfileinsertline} add new lines while
37\helpref{RemoveLine}{wxtextfileremoveline} deletes the existing ones.
0bcd7416 38\helpref{Clear}{wxtextfileclear} resets the file to empty.
631f1bfe
JS
39\item Save your changes: notice that the changes you make to the file will {\bf
40not} be saved automatically; calling \helpref{Close}{wxtextfileclose} or doing
41nothing discards them! To save the changes you must explicitly call
7f561143
VZ
42\helpref{Write}{wxtextfilewrite} - here, you may also change the line
43termination type if you wish.
44\end{itemize}
45
7f561143
VZ
46\wxheading{Derived from}
47
48No base class
49
954b8ae6
JS
50\wxheading{Include files}
51
52<wx/textfile.h>
53
a7af285d
VZ
54\wxheading{Library}
55
f87da781 56\helpref{wxBase}{librarieslist}
a7af285d 57
7f561143
VZ
58\wxheading{Data structures}
59
60The following constants identify the line termination type:
b2cf617c 61
7f561143
VZ
62\begin{verbatim}
63enum wxTextFileType
64{
65 wxTextFileType_None, // incomplete (the last line of the file only)
66 wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
67 wxTextFileType_Dos, // 'CR' 'LF'
68 wxTextFileType_Mac // 'CR' = 0xD = 13 = '\r'
b82827dd 69};
7f561143
VZ
70\end{verbatim}
71
b82827dd
JS
72\wxheading{See also}
73
74\helpref{wxFile}{wxfile}
75
7f561143
VZ
76\latexignore{\rtfignore{\wxheading{Members}}}
77
78\membersection{wxTextFile::wxTextFile}\label{wxtextfilectordef}
b82827dd 79
7f561143
VZ
80\constfunc{}{wxTextFile}{\void}
81
f6bcfd97
BP
82Default constructor, use \helpref{Create}{wxtextfilecreate} or
83\helpref{Open}{wxtextfileopen} with a file name parameter to initialize the object.
7f561143
VZ
84
85\membersection{wxTextFile::wxTextFile}\label{wxtextfilector}
dface61c 86
7f561143
VZ
87\constfunc{}{wxTextFile}{\param{const wxString\& }{strFile}}
88
89Constructor does not load the file into memory, use Open() to do it.
90
f6bcfd97 91\membersection{wxTextFile::\destruct{wxTextFile}}\label{wxtextfiledtor}
dface61c 92
f6bcfd97 93\constfunc{}{\destruct{wxTextFile}}{\void}
7f561143 94
f6bcfd97 95Destructor does nothing.
7f561143 96
f6bcfd97 97\membersection{wxTextFile::AddLine}\label{wxtextfileaddline}
dface61c 98
f6bcfd97 99\constfunc{void}{AddLine}{\param{const wxString\& }{str}, \param{wxTextFileType }{type = typeDefault}}
7f561143 100
f6bcfd97 101Adds a line to the end of file.
7f561143
VZ
102
103\membersection{wxTextFile::Close}\label{wxtextfileclose}
dface61c 104
7f561143
VZ
105\constfunc{bool}{Close}{\void}
106
107Closes the file and frees memory, {\bf losing all changes}. Use \helpref{Write()}{wxtextfilewrite}
108if you want to save them.
109
f6bcfd97
BP
110\membersection{wxTextFile::Create}\label{wxtextfilecreate}
111
112\constfunc{bool}{Create}{\void}
113
114\constfunc{bool}{Create}{\param{const wxString\& }{strFile}}
115
116Creates the file with the given name or the name which was given in the
117\helpref{constructor}{wxtextfilector}. The array of file lines is initially
118empty.
119
120It will fail if the file already exists, \helpref{Open}{wxtextfileopen} should
121be used in this case.
122
123\membersection{wxTextFile::Exists}\label{wxtextfileexists}
124
125\constfunc{bool}{Exists}{\void}
126
cc81d32f 127Return true if file exists - the name of the file should have been specified
f6bcfd97
BP
128in the constructor before calling Exists().
129
7f561143 130\membersection{wxTextFile::IsOpened}\label{wxtextfileisopened}
dface61c 131
7f561143
VZ
132\constfunc{bool}{IsOpened}{\void}
133
cc81d32f 134Returns true if the file is currently opened.
7f561143
VZ
135
136\membersection{wxTextFile::GetLineCount}\label{wxtextfilegetlinecount}
dface61c 137
7f561143
VZ
138\constfunc{size\_t}{GetLineCount}{\void}
139
140Get the number of lines in the file.
141
142\membersection{wxTextFile::GetLine}\label{wxtextfilegetline}
dface61c 143
7f561143
VZ
144\constfunc{wxString\&}{GetLine}{\param{size\_t }{n}}
145
146Retrieves the line number {\it n} from the file. The returned line may be
147modified but you shouldn't add line terminator at the end - this will be done
148by wxTextFile.
149
dface61c
JS
150\membersection{wxTextFile::operator[]}\label{wxtextfileoperatorarray}
151
7f561143
VZ
152\constfunc{wxString\&}{operator[]}{\param{size\_t }{n}}
153
154The same as \helpref{GetLine}{wxtextfilegetline}.
155
156\membersection{wxTextFile::GetCurrentLine}\label{wxtextfilegetcurrentline}
dface61c 157
7f561143
VZ
158\constfunc{size\_t}{GetCurrentLine}{\void}
159
160Returns the current line: it has meaning only when you're using
161GetFirstLine()/GetNextLine() functions, it doesn't get updated when
162you're using "direct access" functions like GetLine(). GetFirstLine() and
163GetLastLine() also change the value of the current line, as well as
164GoToLine().
165
166\membersection{wxTextFile::GoToLine}\label{wxtextfilegotoline}
dface61c 167
7f561143
VZ
168\constfunc{void}{GoToLine}{\param{size\_t }{n}}
169
170Changes the value returned by \helpref{GetCurrentLine}{wxtextfilegetcurrentline}
631f1bfe 171and used by \helpref{GetFirstLine()}{wxtextfilegetfirstline}/\helpref{GetNextLine()}{wxtextfilegetnextline}.
7f561143
VZ
172
173\membersection{wxTextFile::Eof}\label{wxtextfileeof}
dface61c 174
7f561143
VZ
175\constfunc{bool}{Eof}{\void}
176
cc81d32f 177Returns true if the current line is the last one.
7f561143 178
f6bcfd97
BP
179\membersection{wxTextFile::GetEOL}\label{wxtextfilegeteol}
180
181\constfunc{static const char*}{GetEOL}{\param{wxTextFileType }{type = typeDefault}}
182
183Get the line termination string corresponding to given constant. {\it typeDefault} is
bb89dec3
GD
184the value defined during the compilation and corresponds to the native format
185of the platform, i.e. it will be wxTextFileType\_Dos under Windows,
186wxTextFileType\_Unix under Unix (including Mac OS X when compiling with the
187Apple Developer Tools) and wxTextFileType\_Mac under Mac OS (including
188Mac OS X when compiling with CodeWarrior).
f6bcfd97 189
7f561143 190\membersection{wxTextFile::GetFirstLine}\label{wxtextfilegetfirstline}
dface61c 191
7f561143
VZ
192\constfunc{wxString\&}{GetFirstLine}{\void}
193
194This method together with \helpref{GetNextLine()}{wxtextfilegetnextline}
195allows more "iterator-like" traversal of the list of lines, i.e. you may
196write something like:
197
198\begin{verbatim}
3ca6a5f0
BP
199wxTextFile file;
200...
201for ( str = file.GetFirstLine(); !file.Eof(); str = file.GetNextLine() )
7f561143
VZ
202{
203 // do something with the current line in str
204}
3ca6a5f0 205// do something with the last line in str
7f561143
VZ
206\end{verbatim}
207
208\membersection{wxTextFile::GetNextLine}\label{wxtextfilegetnextline}
dface61c 209
7f561143
VZ
210\func{wxString\&}{GetNextLine}{\void}
211
212Gets the next line (see \helpref{GetFirstLine}{wxtextfilegetfirstline} for
213the example).
214
215\membersection{wxTextFile::GetPrevLine}\label{wxtextfilegetprevline}
dface61c 216
7f561143
VZ
217\func{wxString\&}{GetPrevLine}{\void}
218
219Gets the previous line in the file.
220
221\membersection{wxTextFile::GetLastLine}\label{wxtextfilegetlastline}
dface61c 222
7f561143
VZ
223\func{wxString\&}{GetLastLine}{\void}
224
3ca6a5f0
BP
225Gets the last line of the file. Together with
226\helpref{GetPrevLine}{wxtextfilegetprevline} it allows to enumerate the lines
227in the file from the end to the beginning like this:
228
229\begin{verbatim}
230wxTextFile file;
231...
232for ( str = file.GetLastLine();
233 file.GetCurrentLine() > 0;
234 str = file.GetPrevLine() )
235{
236 // do something with the current line in str
237}
238// do something with the first line in str
239\end{verbatim}
7f561143
VZ
240
241\membersection{wxTextFile::GetLineType}\label{wxtextfilegetlinetype}
dface61c 242
7f561143
VZ
243\constfunc{wxTextFileType}{GetLineType}{\param{size\_t }{n}}
244
245Get the type of the line (see also \helpref{GetEOL}{wxtextfilegeteol})
246
247\membersection{wxTextFile::GuessType}\label{wxtextfileguesstype}
dface61c 248
7f561143
VZ
249\constfunc{wxTextFileType}{GuessType}{\void}
250
251Guess the type of file (which is supposed to be opened). If sufficiently
252many lines of the file are in DOS/Unix/Mac format, the corresponding value will
253be returned. If the detection mechanism fails wxTextFileType\_None is returned.
254
255\membersection{wxTextFile::GetName}\label{wxtextfilegetname}
dface61c 256
7f561143
VZ
257\constfunc{const char*}{GetName}{\void}
258
259Get the name of the file.
260
7f561143 261\membersection{wxTextFile::InsertLine}\label{wxtextfileinsertline}
dface61c 262
7f561143
VZ
263\constfunc{void}{InsertLine}{\param{const wxString\& }{str}, \param{size\_t }{n}, \param{wxTextFileType }{type = typeDefault}}
264
265Insert a line before the line number {\it n}.
266
f6bcfd97
BP
267\membersection{wxTextFile::Open}\label{wxtextfileopen}
268
5487ff0f 269\constfunc{bool}{Open}{\param{const wxMBConv\&}{ conv = wxConvAuto()}}
f6bcfd97 270
5487ff0f 271\constfunc{bool}{Open}{\param{const wxString\& }{strFile}, \param{const wxMBConv\&}{ conv = wxConvAuto()}}
f6bcfd97
BP
272
273Open() opens the file with the given name or the name which was given in the
274\helpref{constructor}{wxtextfilector} and also loads file in memory on
275success. It will fail if the file does not exist,
276\helpref{Create}{wxtextfilecreate} should be used in this case.
277
fc2171bd 278The {\it conv} argument is only meaningful in Unicode build of wxWidgets when
455df0f4
VS
279it is used to convert the file to wide character representation.
280
7f561143 281\membersection{wxTextFile::RemoveLine}\label{wxtextfileremoveline}
dface61c 282
7f561143
VZ
283\constfunc{void}{RemoveLine}{\param{size\_t }{n}}
284
631f1bfe 285Delete line number {\it n} from the file.
7f561143 286
0bcd7416
VZ
287\membersection{wxTextFile::Clear}\label{wxtextfileclear}
288
289\constfunc{void}{Clear}{\void}
290
291Delete all lines from the file, set current line number to 0.
292
7f561143 293\membersection{wxTextFile::Write}\label{wxtextfilewrite}
dface61c 294
5487ff0f 295\constfunc{bool}{Write}{\param{wxTextFileType }{typeNew = wxTextFileType\_None}, \param{const wxMBConv\&}{ conv = wxConvAuto()}}
7f561143 296
631f1bfe 297Change the file on disk. The {\it typeNew} parameter allows you to change the
7f561143
VZ
298file format (default argument means "don't change type") and may be used to
299convert, for example, DOS files to Unix.
300
fc2171bd 301The {\it conv} argument is only meaningful in Unicode build of wxWidgets when
455df0f4
VS
302it is used to convert all lines to multibyte representation before writing them
303them to physical file.
304
cc81d32f 305Returns true if operation succeeded, false if it failed.
189a89ae 306