]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/textfile.tex
Doc tweaks
[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
10One word of warning: the class is not at all optimized for big files and so it
11will load the file entirely into memory when opened. Of course, you should not
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
14limitation for the small files like configuration files or programs sources
15which are well handled by wxTextFile.
16
17The typical things you may do with wxTextFile in order are:
dface61c 18
7f561143
VZ
19\begin{itemize}\itemsep=0pt
20\item Create and open it: this is done with \helpref{Open}{wxtextfileopen}
21function which opens the file (name may be specified either as Open argument or
22in the constructor), reads its contents in memory and closes it. If all of these
23operations are successful, Open() will return TRUE and FALSE on error.
24\item Work with the lines in the file: this may be done either with "direct
631f1bfe 25access" functions like \helpref{GetLineCount}{wxtextfilegetlinecount} and
7f561143
VZ
26\helpref{GetLine}{wxtextfilegetline} ({\it operator[]} does exactly the same
27but looks more like array addressing) or with "sequential access" functions
28which include \helpref{GetFirstLine}{wxtextfilegetfirstline}/
29\helpref{GetNextLine}{wxtextfilegetnextline} and also
dface61c 30\helpref{GetLastLine}{wxtextfilegetlastline}/\helpref{GetPrevLine}{wxtextfilegetprevline}.
7f561143
VZ
31For the sequential access functions the current line number is maintained: it is
32returned by \helpref{GetCurrentLine}{wxtextfilegetcurrentline} and may be
33changed with \helpref{GoToLine}{wxtextfilegotoline}.
34\item Add/remove lines to the file: \helpref{AddLine}{wxtextfileaddline} and
35\helpref{InsertLine}{wxtextfileinsertline} add new lines while
36\helpref{RemoveLine}{wxtextfileremoveline} deletes the existing ones.
631f1bfe
JS
37\item Save your changes: notice that the changes you make to the file will {\bf
38not} be saved automatically; calling \helpref{Close}{wxtextfileclose} or doing
39nothing discards them! To save the changes you must explicitly call
7f561143
VZ
40\helpref{Write}{wxtextfilewrite} - here, you may also change the line
41termination type if you wish.
42\end{itemize}
43
7f561143
VZ
44\wxheading{Derived from}
45
46No base class
47
954b8ae6
JS
48\wxheading{Include files}
49
50<wx/textfile.h>
51
7f561143
VZ
52\wxheading{Data structures}
53
54The following constants identify the line termination type:
b2cf617c 55
7f561143
VZ
56\begin{verbatim}
57enum wxTextFileType
58{
59 wxTextFileType_None, // incomplete (the last line of the file only)
60 wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
61 wxTextFileType_Dos, // 'CR' 'LF'
62 wxTextFileType_Mac // 'CR' = 0xD = 13 = '\r'
b82827dd 63};
7f561143
VZ
64\end{verbatim}
65
b82827dd
JS
66\wxheading{See also}
67
68\helpref{wxFile}{wxfile}
69
7f561143
VZ
70\latexignore{\rtfignore{\wxheading{Members}}}
71
72\membersection{wxTextFile::wxTextFile}\label{wxtextfilectordef}
b82827dd 73
7f561143
VZ
74\constfunc{}{wxTextFile}{\void}
75
76Default constructor, use Open(string) to initialize the object.
77
78\membersection{wxTextFile::wxTextFile}\label{wxtextfilector}
dface61c 79
7f561143
VZ
80\constfunc{}{wxTextFile}{\param{const wxString\& }{strFile}}
81
82Constructor does not load the file into memory, use Open() to do it.
83
84\membersection{wxTextFile::Exists}\label{wxtextfileexists}
dface61c 85
7f561143
VZ
86\constfunc{bool}{Exists}{\void}
87
88Return TRUE if file exists - the name of the file should have been specified
89in the constructor before calling Exists().
90
91\membersection{wxTextFile::Open}\label{wxtextfileopen}
dface61c 92
7f561143
VZ
93\constfunc{bool}{Open}{\void}
94
95Open() opens the file with the name which was given in the \helpref{constructor}{wxtextfilector}
96and also loads file in memory on success.
97
98\membersection{wxTextFile::Open}\label{wxtextfileopenname}
dface61c 99
7f561143
VZ
100\constfunc{bool}{Open}{\param{const wxString\& }{strFile}}
101
102Same as \helpref{Open()}{wxtextfileopen} but allows to specify the file name
103(must be used if the default constructor was used to create the object).
104
105\membersection{wxTextFile::Close}\label{wxtextfileclose}
dface61c 106
7f561143
VZ
107\constfunc{bool}{Close}{\void}
108
109Closes the file and frees memory, {\bf losing all changes}. Use \helpref{Write()}{wxtextfilewrite}
110if you want to save them.
111
112\membersection{wxTextFile::IsOpened}\label{wxtextfileisopened}
dface61c 113
7f561143
VZ
114\constfunc{bool}{IsOpened}{\void}
115
116Returns TRUE if the file is currently opened.
117
118\membersection{wxTextFile::GetLineCount}\label{wxtextfilegetlinecount}
dface61c 119
7f561143
VZ
120\constfunc{size\_t}{GetLineCount}{\void}
121
122Get the number of lines in the file.
123
124\membersection{wxTextFile::GetLine}\label{wxtextfilegetline}
dface61c 125
7f561143
VZ
126\constfunc{wxString\&}{GetLine}{\param{size\_t }{n}}
127
128Retrieves the line number {\it n} from the file. The returned line may be
129modified but you shouldn't add line terminator at the end - this will be done
130by wxTextFile.
131
dface61c
JS
132\membersection{wxTextFile::operator[]}\label{wxtextfileoperatorarray}
133
7f561143
VZ
134\constfunc{wxString\&}{operator[]}{\param{size\_t }{n}}
135
136The same as \helpref{GetLine}{wxtextfilegetline}.
137
138\membersection{wxTextFile::GetCurrentLine}\label{wxtextfilegetcurrentline}
dface61c 139
7f561143
VZ
140\constfunc{size\_t}{GetCurrentLine}{\void}
141
142Returns the current line: it has meaning only when you're using
143GetFirstLine()/GetNextLine() functions, it doesn't get updated when
144you're using "direct access" functions like GetLine(). GetFirstLine() and
145GetLastLine() also change the value of the current line, as well as
146GoToLine().
147
148\membersection{wxTextFile::GoToLine}\label{wxtextfilegotoline}
dface61c 149
7f561143
VZ
150\constfunc{void}{GoToLine}{\param{size\_t }{n}}
151
152Changes the value returned by \helpref{GetCurrentLine}{wxtextfilegetcurrentline}
631f1bfe 153and used by \helpref{GetFirstLine()}{wxtextfilegetfirstline}/\helpref{GetNextLine()}{wxtextfilegetnextline}.
7f561143
VZ
154
155\membersection{wxTextFile::Eof}\label{wxtextfileeof}
dface61c 156
7f561143
VZ
157\constfunc{bool}{Eof}{\void}
158
159Returns TRUE if the current line is the last one.
160
161\membersection{wxTextFile::GetFirstLine}\label{wxtextfilegetfirstline}
dface61c 162
7f561143
VZ
163\constfunc{wxString\&}{GetFirstLine}{\void}
164
165This method together with \helpref{GetNextLine()}{wxtextfilegetnextline}
166allows more "iterator-like" traversal of the list of lines, i.e. you may
167write something like:
168
169\begin{verbatim}
170for ( str = GetFirstLine(); !Eof(); str = GetNextLine() )
171{
172 // do something with the current line in str
173}
174\end{verbatim}
175
176\membersection{wxTextFile::GetNextLine}\label{wxtextfilegetnextline}
dface61c 177
7f561143
VZ
178\func{wxString\&}{GetNextLine}{\void}
179
180Gets the next line (see \helpref{GetFirstLine}{wxtextfilegetfirstline} for
181the example).
182
183\membersection{wxTextFile::GetPrevLine}\label{wxtextfilegetprevline}
dface61c 184
7f561143
VZ
185\func{wxString\&}{GetPrevLine}{\void}
186
187Gets the previous line in the file.
188
189\membersection{wxTextFile::GetLastLine}\label{wxtextfilegetlastline}
dface61c 190
7f561143
VZ
191\func{wxString\&}{GetLastLine}{\void}
192
193Gets the last line of the file.
194
195\membersection{wxTextFile::GetLineType}\label{wxtextfilegetlinetype}
dface61c 196
7f561143
VZ
197\constfunc{wxTextFileType}{GetLineType}{\param{size\_t }{n}}
198
199Get the type of the line (see also \helpref{GetEOL}{wxtextfilegeteol})
200
201\membersection{wxTextFile::GuessType}\label{wxtextfileguesstype}
dface61c 202
7f561143
VZ
203\constfunc{wxTextFileType}{GuessType}{\void}
204
205Guess the type of file (which is supposed to be opened). If sufficiently
206many lines of the file are in DOS/Unix/Mac format, the corresponding value will
207be returned. If the detection mechanism fails wxTextFileType\_None is returned.
208
209\membersection{wxTextFile::GetName}\label{wxtextfilegetname}
dface61c 210
7f561143
VZ
211\constfunc{const char*}{GetName}{\void}
212
213Get the name of the file.
214
215\membersection{wxTextFile::AddLine}\label{wxtextfileaddline}
dface61c 216
7f561143
VZ
217\constfunc{void}{AddLine}{\param{const wxString\& }{str}, \param{wxTextFileType }{type = typeDefault}}
218
219Adds a line to the end of file.
220
221\membersection{wxTextFile::InsertLine}\label{wxtextfileinsertline}
dface61c 222
7f561143
VZ
223\constfunc{void}{InsertLine}{\param{const wxString\& }{str}, \param{size\_t }{n}, \param{wxTextFileType }{type = typeDefault}}
224
225Insert a line before the line number {\it n}.
226
227\membersection{wxTextFile::RemoveLine}\label{wxtextfileremoveline}
dface61c 228
7f561143
VZ
229\constfunc{void}{RemoveLine}{\param{size\_t }{n}}
230
631f1bfe 231Delete line number {\it n} from the file.
7f561143
VZ
232
233\membersection{wxTextFile::Write}\label{wxtextfilewrite}
dface61c 234
7f561143
VZ
235\constfunc{bool}{Write}{\param{wxTextFileType }{typeNew = wxTextFileType\_None}}
236
631f1bfe 237Change the file on disk. The {\it typeNew} parameter allows you to change the
7f561143
VZ
238file format (default argument means "don't change type") and may be used to
239convert, for example, DOS files to Unix.
240
189a89ae
VZ
241Returns TRUE if operation succeeded, FALSE if it failed.
242
7f561143 243\membersection{wxTextFile::GetEOL}\label{wxtextfilegeteol}
dface61c 244
7f561143
VZ
245\constfunc{static const char*}{GetEOL}{\param{wxTextFileType }{type = typeDefault}}
246
247Get the line termination string corresponding to given constant. {\it typeDefault} is
248the value defined during the compilation and corresponds to the native format of the
249platform, i.e. it will be wxTextFileType\_Dos under Windows, wxTextFileType\_Unix under
250Unix and wxTextFileType\_Mac under Mac.
251
252\membersection{wxTextFile::\destruct{wxTextFile}}\label{wxtextfiledtor}
dface61c 253
7f561143
VZ
254\constfunc{}{\destruct{wxTextFile}}{\void}
255
256Destructor does nothing.
dface61c 257