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