]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: file.h | |
e54c96f1 | 3 | // Purpose: interface of wxTempFile |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxTempFile | |
7c913512 | 11 | |
23324ae1 FM |
12 | wxTempFile provides a relatively safe way to replace the contents of the |
13 | existing file. The name is explained by the fact that it may be also used as | |
14 | just a temporary file if you don't replace the old file contents. | |
7c913512 | 15 | |
23324ae1 FM |
16 | Usually, when a program replaces the contents of some file it first opens it for |
17 | writing, thus losing all of the old data and then starts recreating it. This | |
18 | approach is not very safe because during the regeneration of the file bad things | |
19 | may happen: the program may find that there is an internal error preventing it | |
20 | from completing file generation, the user may interrupt it (especially if file | |
21 | generation takes long time) and, finally, any other external interrupts (power | |
22 | supply failure or a disk error) will leave you without either the original file | |
23 | or the new one. | |
7c913512 | 24 | |
23324ae1 FM |
25 | wxTempFile addresses this problem by creating a temporary file which is meant to |
26 | replace the original file - but only after it is fully written. So, if the user | |
27 | interrupts the program during the file generation, the old file won't be lost. | |
28 | Also, if the program discovers itself that it doesn't want to replace the old | |
29 | file there is no problem - in fact, wxTempFile will @b not replace the old | |
7c913512 | 30 | file by default, you should explicitly call wxTempFile::Commit |
23324ae1 FM |
31 | to do it. Calling wxTempFile::Discard explicitly discards any |
32 | modifications: it closes and deletes the temporary file and leaves the original | |
33 | file unchanged. If you don't call neither of Commit() and Discard(), the | |
34 | destructor will call Discard() automatically. | |
7c913512 | 35 | |
23324ae1 FM |
36 | To summarize: if you want to replace another file, create an instance of |
37 | wxTempFile passing the name of the file to be replaced to the constructor (you | |
7c913512 FM |
38 | may also use default constructor and pass the file name to |
39 | wxTempFile::Open). Then you can wxTempFile::write | |
23324ae1 FM |
40 | to wxTempFile using wxFile-like functions and later call |
41 | Commit() to replace the old file (and close this one) or call Discard() to | |
42 | cancel | |
43 | the modifications. | |
7c913512 | 44 | |
23324ae1 FM |
45 | @library{wxbase} |
46 | @category{file} | |
47 | */ | |
7c913512 | 48 | class wxTempFile |
23324ae1 FM |
49 | { |
50 | public: | |
51 | /** | |
7c913512 | 52 | Associates wxTempFile with the file to be replaced and opens it. You should use |
23324ae1 FM |
53 | IsOpened() to verify if the constructor succeeded. |
54 | */ | |
55 | wxTempFile(const wxString& strName); | |
56 | ||
57 | /** | |
58 | Destructor calls Discard() if temporary file | |
59 | is still opened. | |
60 | */ | |
61 | ~wxTempFile(); | |
62 | ||
63 | /** | |
64 | Validate changes: deletes the old file of name m_strName and renames the new | |
65 | file to the old name. Returns @true if both actions succeeded. If @false is | |
66 | returned it may unfortunately mean two quite different things: either that | |
67 | either the old file couldn't be deleted or that the new file couldn't be renamed | |
68 | to the old name. | |
69 | */ | |
70 | bool Commit(); | |
71 | ||
72 | /** | |
73 | Discard changes: the old file contents is not changed, temporary file is | |
74 | deleted. | |
75 | */ | |
76 | void Discard(); | |
77 | ||
78 | /** | |
79 | Returns @true if the file was successfully opened. | |
80 | */ | |
328f5751 | 81 | bool IsOpened() const; |
23324ae1 FM |
82 | |
83 | /** | |
84 | Returns the length of the file. | |
41f6f17d VZ |
85 | |
86 | This method may return wxInvalidOffset if the length couldn't be | |
87 | determined or also 0 even for non-empty files if the file is not | |
88 | seekable. In general, the only way to determine if the file for which | |
89 | this function returns 0 is really empty or not is to try reading from | |
90 | it. | |
23324ae1 | 91 | */ |
328f5751 | 92 | wxFileOffset Length() const; |
23324ae1 FM |
93 | |
94 | /** | |
95 | Open the temporary file, returns @true on success, @false if an error | |
96 | occurred. | |
4cc4bfaf FM |
97 | @a strName is the name of file to be replaced. The temporary file is always |
98 | created in the directory where @a strName is. In particular, if | |
99 | @a strName doesn't include the path, it is created in the current directory | |
23324ae1 FM |
100 | and the program should have write access to it for the function to succeed. |
101 | */ | |
102 | bool Open(const wxString& strName); | |
103 | ||
104 | /** | |
105 | Seeks to the specified position. | |
106 | */ | |
107 | wxFileOffset Seek(wxFileOffset ofs, | |
108 | wxSeekMode mode = wxFromStart); | |
109 | ||
110 | /** | |
111 | Returns the current position or wxInvalidOffset if file is not opened or if | |
112 | another | |
113 | error occurred. | |
114 | */ | |
328f5751 | 115 | wxFileOffset Tell() const; |
23324ae1 FM |
116 | |
117 | /** | |
118 | Write to the file, return @true on success, @false on failure. | |
23324ae1 | 119 | The second argument is only meaningful in Unicode build of wxWidgets when |
4cc4bfaf | 120 | @a conv is used to convert @a str to multibyte representation. |
23324ae1 FM |
121 | */ |
122 | bool Write(const wxString& str, | |
123 | const wxMBConv& conv = wxConvUTF8); | |
124 | }; | |
125 | ||
126 | ||
e54c96f1 | 127 | |
23324ae1 FM |
128 | /** |
129 | @class wxFile | |
7c913512 | 130 | |
23324ae1 FM |
131 | A wxFile performs raw file I/O. This is a very small class designed to |
132 | minimize the overhead of using it - in fact, there is hardly any overhead at | |
133 | all, but using it brings you automatic error checking and hides differences | |
134 | between platforms and compilers. wxFile also automatically closes the file in | |
135 | its destructor making it unnecessary to worry about forgetting to do it. | |
7c913512 | 136 | wxFile is a wrapper around @c file descriptor. - see also |
23324ae1 | 137 | wxFFile for a wrapper around @c FILE structure. |
7c913512 FM |
138 | |
139 | @c wxFileOffset is used by the wxFile functions which require offsets as | |
23324ae1 FM |
140 | parameter or return them. If the platform supports it, wxFileOffset is a typedef |
141 | for a native 64 bit integer, otherwise a 32 bit integer is used for | |
142 | wxFileOffset. | |
7c913512 | 143 | |
23324ae1 FM |
144 | @library{wxbase} |
145 | @category{file} | |
146 | */ | |
7c913512 | 147 | class wxFile |
23324ae1 FM |
148 | { |
149 | public: | |
23324ae1 | 150 | /** |
c1d97593 RR |
151 | Opening mode |
152 | */ | |
153 | enum OpenMode { read, write, read_write, write_append, write_excl }; | |
154 | ||
155 | /** | |
156 | Standard file descriptors | |
157 | */ | |
158 | enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; | |
159 | ||
160 | /** | |
161 | Default constructor. | |
162 | */ | |
163 | wxFile(); | |
164 | ||
165 | /** | |
166 | Opens a file with a filename. | |
3c4f71cc | 167 | |
7c913512 | 168 | @param filename |
4cc4bfaf | 169 | The filename. |
7c913512 | 170 | @param mode |
c1d97593 RR |
171 | The mode in which to open the file. May be one of @c wxFile::read, |
172 | @c wxFile::write and @c wxFile::read_write. | |
23324ae1 | 173 | */ |
7c913512 FM |
174 | wxFile(const wxString& filename, |
175 | wxFile::OpenMode mode = wxFile::read); | |
c1d97593 RR |
176 | /** |
177 | Associates the file with the given file descriptor, which has already been | |
178 | opened. See Attach() for the list of predefined descriptors. | |
179 | ||
180 | @param fd | |
181 | An existing file descriptor. | |
182 | */ | |
7c913512 | 183 | wxFile(int fd); |
23324ae1 FM |
184 | |
185 | /** | |
186 | Destructor will close the file. | |
1f1d2182 | 187 | @note it is not virtual so you should not use wxFile polymorphically. |
23324ae1 FM |
188 | */ |
189 | ~wxFile(); | |
190 | ||
191 | /** | |
c1d97593 RR |
192 | This function verifies if we may access the given file in specified mode. |
193 | Only values of @c wxFile::read or @c wxFile::write really make sense here. | |
23324ae1 FM |
194 | */ |
195 | static bool Access(const wxString& name, OpenMode mode); | |
196 | ||
197 | /** | |
c1d97593 RR |
198 | Attaches an existing file descriptor to the wxFile object. Example of |
199 | predefined file descriptors are 0, 1 and 2 which correspond to stdin, | |
200 | stdout and stderr (and have symbolic names of @c wxFile::fd_stdin, | |
201 | @c wxFile::fd_stdout and @c wxFile::fd_stderr). | |
202 | ||
23324ae1 FM |
203 | The descriptor should be already opened and it will be closed by wxFile |
204 | object. | |
205 | */ | |
206 | void Attach(int fd); | |
207 | ||
208 | /** | |
209 | Closes the file. | |
210 | */ | |
211 | void Close(); | |
212 | ||
213 | /** | |
214 | Creates a file for writing. If the file already exists, setting @b overwrite to | |
215 | @true | |
216 | will ensure it is overwritten. | |
217 | */ | |
4cc4bfaf | 218 | bool Create(const wxString& filename, bool overwrite = false, |
23324ae1 FM |
219 | int access = wxS_DEFAULT); |
220 | ||
221 | /** | |
222 | Get back a file descriptor from wxFile object - the caller is responsible for | |
223 | closing the file if this | |
224 | descriptor is opened. IsOpened() will return @false after call to Detach(). | |
225 | */ | |
226 | void Detach(); | |
227 | ||
228 | /** | |
229 | Returns @true if the end of the file has been reached. | |
7c913512 | 230 | Note that the behaviour of the file pointer based class |
c1d97593 RR |
231 | wxFFile is different as wxFFile::Eof will return @true here only if an |
232 | attempt has been made to read @b past the last byte of the file, while | |
233 | wxFile::Eof() will return @true even before such attempt is made if the | |
234 | file pointer is at the last position in the file. | |
235 | ||
23324ae1 FM |
236 | Note also that this function doesn't work on unseekable file descriptors |
237 | (examples include pipes, terminals and sockets under Unix) and an attempt to | |
238 | use it will result in an error message in such case. So, to read the entire | |
7c913512 | 239 | file into memory, you should write a loop which uses |
23324ae1 FM |
240 | Read() repeatedly and tests its return condition instead |
241 | of using Eof() as this will not work for special files under Unix. | |
242 | */ | |
328f5751 | 243 | bool Eof() const; |
23324ae1 FM |
244 | |
245 | /** | |
246 | Returns @true if the given name specifies an existing regular file (not a | |
247 | directory or a link) | |
248 | */ | |
249 | static bool Exists(const wxString& filename); | |
250 | ||
251 | /** | |
252 | Flushes the file descriptor. | |
23324ae1 FM |
253 | Note that Flush() is not implemented on some Windows compilers |
254 | due to a missing fsync function, which reduces the usefulness of this function | |
255 | (it can still be called but it will do nothing on unsupported compilers). | |
256 | */ | |
257 | bool Flush(); | |
258 | ||
259 | /** | |
260 | Returns the type of the file. Possible return values are: | |
261 | */ | |
328f5751 | 262 | wxFileKind GetKind() const; |
23324ae1 FM |
263 | |
264 | /** | |
265 | Returns @true if the file has been opened. | |
266 | */ | |
328f5751 | 267 | bool IsOpened() const; |
23324ae1 FM |
268 | |
269 | /** | |
270 | Returns the length of the file. | |
271 | */ | |
328f5751 | 272 | wxFileOffset Length() const; |
23324ae1 FM |
273 | |
274 | /** | |
275 | Opens the file, returning @true if successful. | |
3c4f71cc | 276 | |
7c913512 | 277 | @param filename |
4cc4bfaf | 278 | The filename. |
7c913512 | 279 | @param mode |
4cc4bfaf | 280 | The mode in which to open the file. May be one of read(), write() and |
23324ae1 FM |
281 | wxFile::read_write. |
282 | */ | |
283 | bool Open(const wxString& filename, | |
284 | wxFile::OpenMode mode = wxFile::read); | |
285 | ||
23324ae1 | 286 | /** |
c1d97593 RR |
287 | Reads from the file into a memory buffer. |
288 | ||
289 | @param buffer | |
290 | Buffer to write in | |
291 | @param count | |
292 | Bytes to read | |
293 | ||
294 | @return The number of bytes read, or the symbol wxInvalidOffset | |
23324ae1 FM |
295 | */ |
296 | size_t Read(void* buffer, size_t count); | |
23324ae1 FM |
297 | |
298 | /** | |
299 | Seeks to the specified position. | |
3c4f71cc | 300 | |
7c913512 | 301 | @param ofs |
4cc4bfaf | 302 | Offset to seek to. |
7c913512 | 303 | @param mode |
4cc4bfaf | 304 | One of wxFromStart, wxFromEnd, wxFromCurrent. |
3c4f71cc | 305 | |
d29a9a8a | 306 | @return The actual offset position achieved, or wxInvalidOffset on |
4cc4bfaf | 307 | failure. |
23324ae1 FM |
308 | */ |
309 | wxFileOffset Seek(wxFileOffset ofs, | |
310 | wxSeekMode mode = wxFromStart); | |
311 | ||
312 | /** | |
313 | Moves the file pointer to the specified number of bytes relative to the end of | |
314 | the file. For example, @c SeekEnd(-5) would position the pointer 5 | |
315 | bytes before the end. | |
3c4f71cc | 316 | |
7c913512 | 317 | @param ofs |
4cc4bfaf | 318 | Number of bytes before the end of the file. |
3c4f71cc | 319 | |
d29a9a8a | 320 | @return The actual offset position achieved, or wxInvalidOffset on |
4cc4bfaf | 321 | failure. |
23324ae1 FM |
322 | */ |
323 | wxFileOffset SeekEnd(wxFileOffset ofs = 0); | |
324 | ||
325 | /** | |
c1d97593 RR |
326 | Returns the current position or wxInvalidOffset if file is not opened or |
327 | if another error occurred. | |
23324ae1 | 328 | */ |
328f5751 | 329 | wxFileOffset Tell() const; |
23324ae1 | 330 | |
c1d97593 RR |
331 | /** |
332 | Write data to the file (descriptor). | |
333 | ||
4050e98d | 334 | @param buffer |
c1d97593 RR |
335 | Buffer from which to read data |
336 | @param count | |
337 | Number of bytes to write | |
338 | ||
339 | @return The number of bytes written. | |
340 | */ | |
341 | size_t Write(const void *buffer, size_t count); | |
342 | ||
23324ae1 FM |
343 | /** |
344 | Writes the contents of the string to the file, returns @true on success. | |
23324ae1 | 345 | The second argument is only meaningful in Unicode build of wxWidgets when |
c1d97593 | 346 | @a conv is used to convert @a s to a multibyte representation. |
23324ae1 | 347 | Note that this method only works with @c NUL-terminated strings, if you want |
7c913512 | 348 | to write data with embedded @c NULs to the file you should use the other |
c1d97593 | 349 | Write() overload. |
23324ae1 FM |
350 | */ |
351 | bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8); | |
352 | ||
353 | /** | |
354 | Returns the file descriptor associated with the file. | |
355 | */ | |
328f5751 | 356 | int fd() const; |
23324ae1 | 357 | }; |
e54c96f1 | 358 |