]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: file.h | |
0b70c946 | 3 | // Purpose: interface of wxTempFile, wxFile |
23324ae1 | 4 | // Author: wxWidgets team |
526954c5 | 5 | // Licence: wxWindows licence |
23324ae1 FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
23b7f0cb | 8 | |
23324ae1 FM |
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 | 16 | Usually, when a program replaces the contents of some file it first opens it for |
23b7f0cb FM |
17 | writing, thus losing all of the old data and then starts recreating it. |
18 | This approach is not very safe because during the regeneration of the file bad | |
19 | things may happen: the program may find that there is an internal error preventing | |
20 | it from completing file generation, the user may interrupt it (especially if file | |
23324ae1 FM |
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 | |
23b7f0cb FM |
30 | file by default, you should explicitly call wxTempFile::Commit() to do it. |
31 | Calling wxTempFile::Discard() explicitly discards any modifications: it | |
32 | closes and deletes the temporary file and leaves the original file unchanged. | |
9cfebe8c | 33 | If you call neither Commit() nor Discard(), the destructor will |
23b7f0cb | 34 | call Discard() automatically. |
7c913512 | 35 | |
23324ae1 | 36 | To summarize: if you want to replace another file, create an instance of |
9cfebe8c FM |
37 | wxTempFile passing the name of the file to be replaced to the constructor. |
38 | (You may also use default constructor and pass the file name to wxTempFile::Open.) | |
23b7f0cb FM |
39 | Then you can write to wxTempFile using wxFile-like functions and later call |
40 | wxTempFile::Commit() to replace the old file (and close this one) or call | |
41 | wxTempFile::Discard() to cancel the modifications. | |
7c913512 | 42 | |
23324ae1 FM |
43 | @library{wxbase} |
44 | @category{file} | |
45 | */ | |
7c913512 | 46 | class wxTempFile |
23324ae1 FM |
47 | { |
48 | public: | |
49 | /** | |
23b7f0cb | 50 | Associates wxTempFile with the file to be replaced and opens it. |
57bf907d FM |
51 | |
52 | @warning | |
9cfebe8c | 53 | You should use IsOpened() to verify that the constructor succeeded. |
23324ae1 FM |
54 | */ |
55 | wxTempFile(const wxString& strName); | |
56 | ||
57 | /** | |
9cfebe8c | 58 | Destructor calls Discard() if temporary file is still open. |
23324ae1 FM |
59 | */ |
60 | ~wxTempFile(); | |
61 | ||
62 | /** | |
63 | Validate changes: deletes the old file of name m_strName and renames the new | |
23b7f0cb FM |
64 | file to the old name. Returns @true if both actions succeeded. |
65 | ||
66 | If @false is returned it may unfortunately mean two quite different things: | |
9cfebe8c | 67 | either that the old file couldn't be deleted or that the new file |
23b7f0cb | 68 | couldn't be renamed to the old name. |
23324ae1 FM |
69 | */ |
70 | bool Commit(); | |
71 | ||
72 | /** | |
9cfebe8c FM |
73 | Discard changes: the old file contents are not changed, the temporary |
74 | file is deleted. | |
23324ae1 FM |
75 | */ |
76 | void Discard(); | |
77 | ||
f1a73c6a VZ |
78 | /** |
79 | Flush the data written to the file to disk. | |
80 | ||
81 | This simply calls wxFile::Flush() for the underlying file and may be | |
82 | necessary with file systems such as XFS and Ext4 under Linux. Calling | |
83 | this function may however have serious performance implications and | |
84 | also is not necessary with many other file systems so it is not done by | |
85 | default -- but you can call it before calling Commit() to absolutely | |
86 | ensure that the data was indeed written to the disk correctly. | |
87 | */ | |
88 | bool Flush(); | |
89 | ||
23324ae1 FM |
90 | /** |
91 | Returns @true if the file was successfully opened. | |
92 | */ | |
328f5751 | 93 | bool IsOpened() const; |
23324ae1 FM |
94 | |
95 | /** | |
96 | Returns the length of the file. | |
41f6f17d | 97 | |
acdad9db | 98 | This method may return ::wxInvalidOffset if the length couldn't be |
9cfebe8c | 99 | determined or 0 even for non-empty files if the file is not seekable. |
23b7f0cb FM |
100 | |
101 | In general, the only way to determine if the file for which this function | |
102 | returns 0 is really empty or not is to try reading from it. | |
23324ae1 | 103 | */ |
328f5751 | 104 | wxFileOffset Length() const; |
23324ae1 FM |
105 | |
106 | /** | |
107 | Open the temporary file, returns @true on success, @false if an error | |
108 | occurred. | |
4cc4bfaf | 109 | @a strName is the name of file to be replaced. The temporary file is always |
23b7f0cb FM |
110 | created in the directory where @a strName is. In particular, if @a strName |
111 | doesn't include the path, it is created in the current directory and the | |
112 | program should have write access to it for the function to succeed. | |
23324ae1 FM |
113 | */ |
114 | bool Open(const wxString& strName); | |
115 | ||
116 | /** | |
117 | Seeks to the specified position. | |
118 | */ | |
119 | wxFileOffset Seek(wxFileOffset ofs, | |
120 | wxSeekMode mode = wxFromStart); | |
121 | ||
122 | /** | |
acdad9db | 123 | Returns the current position or ::wxInvalidOffset if file is not opened or |
23b7f0cb | 124 | if another error occurred. |
23324ae1 | 125 | */ |
328f5751 | 126 | wxFileOffset Tell() const; |
23324ae1 FM |
127 | |
128 | /** | |
129 | Write to the file, return @true on success, @false on failure. | |
23324ae1 | 130 | The second argument is only meaningful in Unicode build of wxWidgets when |
4cc4bfaf | 131 | @a conv is used to convert @a str to multibyte representation. |
23324ae1 FM |
132 | */ |
133 | bool Write(const wxString& str, | |
134 | const wxMBConv& conv = wxConvUTF8); | |
135 | }; | |
136 | ||
137 | ||
e54c96f1 | 138 | |
23324ae1 FM |
139 | /** |
140 | @class wxFile | |
7c913512 | 141 | |
23324ae1 FM |
142 | A wxFile performs raw file I/O. This is a very small class designed to |
143 | minimize the overhead of using it - in fact, there is hardly any overhead at | |
144 | all, but using it brings you automatic error checking and hides differences | |
145 | between platforms and compilers. wxFile also automatically closes the file in | |
9cfebe8c | 146 | its destructor so you won't forget to do so. |
23b7f0cb FM |
147 | wxFile is a wrapper around @c file descriptor. - see also wxFFile for a |
148 | wrapper around @c FILE structure. | |
7c913512 | 149 | |
23b7f0cb FM |
150 | ::wxFileOffset is used by the wxFile functions which require offsets as |
151 | parameter or return them. If the platform supports it, wxFileOffset is a | |
152 | typedef for a native 64 bit integer, otherwise a 32 bit integer is used for | |
153 | ::wxFileOffset. | |
7c913512 | 154 | |
23324ae1 FM |
155 | @library{wxbase} |
156 | @category{file} | |
157 | */ | |
7c913512 | 158 | class wxFile |
23324ae1 FM |
159 | { |
160 | public: | |
23b7f0cb | 161 | |
23324ae1 | 162 | /** |
23b7f0cb FM |
163 | The OpenMode enumeration defines the different modes for opening a file with wxFile. |
164 | It is also used with wxFile::Access function. | |
c1d97593 | 165 | */ |
23b7f0cb FM |
166 | enum OpenMode { |
167 | ||
168 | /** Open file for reading or test if it can be opened for reading with Access() */ | |
169 | read, | |
170 | ||
171 | /** Open file for writing deleting the contents of the file if it already exists | |
172 | or test if it can be opened for writing with Access(). */ | |
173 | write, | |
174 | ||
4c51a665 | 175 | /** Open file for reading and writing; cannot be used with Access() */ |
23b7f0cb FM |
176 | read_write, |
177 | ||
178 | /** Open file for appending: the file is opened for writing, but the old contents | |
9cfebe8c | 179 | of the file are not erased and the file pointer is initially placed at the end |
4c51a665 | 180 | of the file; cannot be used with Access(). |
23b7f0cb FM |
181 | |
182 | This is the same as OpenMode::write if the file doesn't exist. | |
183 | */ | |
184 | write_append, | |
185 | ||
186 | /** | |
187 | Open the file securely for writing (Uses O_EXCL | O_CREAT). | |
188 | Will fail if the file already exists, else create and open it atomically. | |
189 | Useful for opening temporary files without being vulnerable to race exploits. | |
190 | */ | |
191 | write_excl | |
192 | }; | |
193 | ||
194 | /** | |
c1d97593 RR |
195 | Standard file descriptors |
196 | */ | |
197 | enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr }; | |
198 | ||
23b7f0cb | 199 | /** |
c1d97593 RR |
200 | Default constructor. |
201 | */ | |
202 | wxFile(); | |
23b7f0cb | 203 | |
c1d97593 RR |
204 | /** |
205 | Opens a file with a filename. | |
3c4f71cc | 206 | |
7c913512 | 207 | @param filename |
4cc4bfaf | 208 | The filename. |
7c913512 | 209 | @param mode |
23b7f0cb | 210 | The mode in which to open the file. |
57bf907d FM |
211 | |
212 | @warning | |
213 | You should use IsOpened() to verify that the constructor succeeded. | |
23324ae1 | 214 | */ |
7c913512 FM |
215 | wxFile(const wxString& filename, |
216 | wxFile::OpenMode mode = wxFile::read); | |
23b7f0cb | 217 | |
c1d97593 RR |
218 | /** |
219 | Associates the file with the given file descriptor, which has already been | |
220 | opened. See Attach() for the list of predefined descriptors. | |
23b7f0cb | 221 | |
c1d97593 | 222 | @param fd |
23b7f0cb | 223 | An existing file descriptor. |
c1d97593 | 224 | */ |
7c913512 | 225 | wxFile(int fd); |
23324ae1 FM |
226 | |
227 | /** | |
228 | Destructor will close the file. | |
9cfebe8c | 229 | @note This destructor is not virtual so you should not use wxFile polymorphically. |
23324ae1 FM |
230 | */ |
231 | ~wxFile(); | |
232 | ||
65fe93d8 VZ |
233 | /** |
234 | Returns the error code for the last unsuccessful operation. | |
235 | ||
236 | The error code is system-dependent and corresponds to the value of the | |
237 | standard @c errno variable when the last error occurred. | |
238 | ||
239 | Notice that only simple accessors such as IsOpened() and Eof() (and | |
240 | this method itself) don't modify the last error value, all other | |
241 | methods can potentially change it if an error occurs, including the | |
242 | const ones such as Tell() or Length(). | |
243 | ||
244 | @since 2.9.2 | |
245 | ||
246 | @see ClearLastError() | |
247 | */ | |
248 | int GetLastError() const; | |
249 | ||
250 | /** | |
251 | Resets the error code. | |
252 | ||
253 | GetLastError() will return 0 until the next error occurs. | |
254 | ||
255 | @since 2.9.2 | |
256 | */ | |
257 | void ClearLastError(); | |
258 | ||
23324ae1 | 259 | /** |
c1d97593 RR |
260 | This function verifies if we may access the given file in specified mode. |
261 | Only values of @c wxFile::read or @c wxFile::write really make sense here. | |
23324ae1 | 262 | */ |
23b7f0cb | 263 | static bool Access(const wxString& name, wxFile::OpenMode mode); |
23324ae1 FM |
264 | |
265 | /** | |
23b7f0cb | 266 | Attaches an existing file descriptor to the wxFile object. |
9cfebe8c | 267 | Examples of predefined file descriptors are 0, 1 and 2 which correspond to |
23b7f0cb | 268 | stdin, stdout and stderr (and have symbolic names of @c wxFile::fd_stdin, |
c1d97593 | 269 | @c wxFile::fd_stdout and @c wxFile::fd_stderr). |
23b7f0cb | 270 | |
23324ae1 FM |
271 | The descriptor should be already opened and it will be closed by wxFile |
272 | object. | |
273 | */ | |
274 | void Attach(int fd); | |
275 | ||
276 | /** | |
277 | Closes the file. | |
278 | */ | |
5267aefd | 279 | bool Close(); |
23324ae1 FM |
280 | |
281 | /** | |
23b7f0cb FM |
282 | Creates a file for writing. |
283 | ||
284 | If the file already exists, setting @b overwrite to @true will ensure | |
285 | it is overwritten. | |
286 | ||
f41d6c8c FM |
287 | @a access may be an OR combination of the ::wxPosixPermissions enumeration |
288 | values. | |
23324ae1 | 289 | */ |
23b7f0cb FM |
290 | bool Create(const wxString& filename, |
291 | bool overwrite = false, | |
23324ae1 FM |
292 | int access = wxS_DEFAULT); |
293 | ||
294 | /** | |
295 | Get back a file descriptor from wxFile object - the caller is responsible for | |
23b7f0cb FM |
296 | closing the file if this descriptor is opened. |
297 | IsOpened() will return @false after call to Detach(). | |
23324ae1 FM |
298 | */ |
299 | void Detach(); | |
300 | ||
301 | /** | |
302 | Returns @true if the end of the file has been reached. | |
9cfebe8c | 303 | Note that the behaviour of the file pointer-based class wxFFile is |
23b7f0cb | 304 | different as wxFFile::Eof() will return @true here only if an |
c1d97593 RR |
305 | attempt has been made to read @b past the last byte of the file, while |
306 | wxFile::Eof() will return @true even before such attempt is made if the | |
307 | file pointer is at the last position in the file. | |
23b7f0cb | 308 | |
23324ae1 FM |
309 | Note also that this function doesn't work on unseekable file descriptors |
310 | (examples include pipes, terminals and sockets under Unix) and an attempt to | |
9cfebe8c | 311 | use it will result in an error message. |
23b7f0cb FM |
312 | |
313 | So, to read the entire file into memory, you should write a loop which uses | |
314 | Read() repeatedly and tests its return condition instead of using Eof() | |
315 | as this will not work for special files under Unix. | |
23324ae1 | 316 | */ |
328f5751 | 317 | bool Eof() const; |
23324ae1 FM |
318 | |
319 | /** | |
23b7f0cb | 320 | Returns @true if the given name specifies an existing regular file |
9cfebe8c | 321 | (not a directory or a link). |
23324ae1 FM |
322 | */ |
323 | static bool Exists(const wxString& filename); | |
324 | ||
325 | /** | |
326 | Flushes the file descriptor. | |
23b7f0cb FM |
327 | |
328 | Note that Flush() is not implemented on some Windows compilers due to a | |
329 | missing fsync function, which reduces the usefulness of this function | |
23324ae1 FM |
330 | (it can still be called but it will do nothing on unsupported compilers). |
331 | */ | |
332 | bool Flush(); | |
333 | ||
334 | /** | |
23b7f0cb | 335 | Returns the type of the file. |
23324ae1 | 336 | */ |
328f5751 | 337 | wxFileKind GetKind() const; |
23324ae1 FM |
338 | |
339 | /** | |
340 | Returns @true if the file has been opened. | |
341 | */ | |
328f5751 | 342 | bool IsOpened() const; |
23324ae1 FM |
343 | |
344 | /** | |
345 | Returns the length of the file. | |
346 | */ | |
328f5751 | 347 | wxFileOffset Length() const; |
23324ae1 FM |
348 | |
349 | /** | |
350 | Opens the file, returning @true if successful. | |
3c4f71cc | 351 | |
7c913512 | 352 | @param filename |
4cc4bfaf | 353 | The filename. |
7c913512 | 354 | @param mode |
23b7f0cb | 355 | The mode in which to open the file. |
f41d6c8c | 356 | @param access |
992ff331 | 357 | An OR-combination of ::wxPosixPermissions enumeration values. |
23324ae1 | 358 | */ |
5267aefd | 359 | bool Open(const wxString& filename, wxFile::OpenMode mode = wxFile::read, |
f41d6c8c | 360 | int access = wxS_DEFAULT); |
23324ae1 | 361 | |
23324ae1 | 362 | /** |
23b7f0cb FM |
363 | Reads from the file into a memory buffer. |
364 | ||
c1d97593 RR |
365 | @param buffer |
366 | Buffer to write in | |
367 | @param count | |
23b7f0cb FM |
368 | Bytes to read |
369 | ||
acdad9db | 370 | @return The number of bytes read, or the symbol ::wxInvalidOffset. |
23324ae1 | 371 | */ |
5267aefd | 372 | ssize_t Read(void* buffer, size_t count); |
23324ae1 | 373 | |
614108e2 VZ |
374 | /** |
375 | Reads the entire contents of the file into a string. | |
376 | ||
377 | @param str | |
378 | Non-@NULL pointer to a string to read data into. | |
379 | @param conv | |
380 | Conversion object to use in Unicode build; by default supposes | |
381 | that file contents is encoded in UTF-8 but falls back to the | |
382 | current locale encoding (or Latin-1 if it is UTF-8 too) if it is | |
383 | not. | |
384 | ||
385 | @return @true if file was read successfully, @false otherwise. | |
386 | ||
387 | @since 2.9.5 | |
388 | */ | |
389 | bool ReadAll(wxString* str, const wxMBConv& conv = wxConvAuto()); | |
390 | ||
23324ae1 FM |
391 | /** |
392 | Seeks to the specified position. | |
3c4f71cc | 393 | |
7c913512 | 394 | @param ofs |
4cc4bfaf | 395 | Offset to seek to. |
7c913512 | 396 | @param mode |
4cc4bfaf | 397 | One of wxFromStart, wxFromEnd, wxFromCurrent. |
3c4f71cc | 398 | |
acdad9db | 399 | @return The actual offset position achieved, or ::wxInvalidOffset on |
23b7f0cb | 400 | failure. |
23324ae1 FM |
401 | */ |
402 | wxFileOffset Seek(wxFileOffset ofs, | |
403 | wxSeekMode mode = wxFromStart); | |
404 | ||
405 | /** | |
23b7f0cb FM |
406 | Moves the file pointer to the specified number of bytes relative to the |
407 | end of the file. For example, @c SeekEnd(-5) would position the pointer 5 | |
23324ae1 | 408 | bytes before the end. |
3c4f71cc | 409 | |
7c913512 | 410 | @param ofs |
4cc4bfaf | 411 | Number of bytes before the end of the file. |
3c4f71cc | 412 | |
acdad9db | 413 | @return The actual offset position achieved, or ::wxInvalidOffset on |
23b7f0cb | 414 | failure. |
23324ae1 FM |
415 | */ |
416 | wxFileOffset SeekEnd(wxFileOffset ofs = 0); | |
417 | ||
418 | /** | |
acdad9db | 419 | Returns the current position or ::wxInvalidOffset if file is not opened or |
23b7f0cb | 420 | if another error occurred. |
23324ae1 | 421 | */ |
328f5751 | 422 | wxFileOffset Tell() const; |
23324ae1 | 423 | |
c1d97593 RR |
424 | /** |
425 | Write data to the file (descriptor). | |
23b7f0cb | 426 | |
4050e98d | 427 | @param buffer |
c1d97593 RR |
428 | Buffer from which to read data |
429 | @param count | |
430 | Number of bytes to write | |
23b7f0cb | 431 | |
c1d97593 RR |
432 | @return The number of bytes written. |
433 | */ | |
434 | size_t Write(const void *buffer, size_t count); | |
435 | ||
23324ae1 FM |
436 | /** |
437 | Writes the contents of the string to the file, returns @true on success. | |
23324ae1 | 438 | The second argument is only meaningful in Unicode build of wxWidgets when |
c1d97593 | 439 | @a conv is used to convert @a s to a multibyte representation. |
23b7f0cb | 440 | |
23324ae1 | 441 | Note that this method only works with @c NUL-terminated strings, if you want |
7c913512 | 442 | to write data with embedded @c NULs to the file you should use the other |
c1d97593 | 443 | Write() overload. |
23324ae1 FM |
444 | */ |
445 | bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8); | |
446 | ||
447 | /** | |
448 | Returns the file descriptor associated with the file. | |
449 | */ | |
328f5751 | 450 | int fd() const; |
23324ae1 | 451 | }; |
e54c96f1 | 452 |