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