]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/file.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxTempFile
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 These constants define the file access rights and are used with wxFile::Create and wxFile::Open.
14 #define wxS_IRUSR 00400
15 #define wxS_IWUSR 00200
16 #define wxS_IXUSR 00100
18 #define wxS_IRGRP 00040
19 #define wxS_IWGRP 00020
20 #define wxS_IXGRP 00010
22 #define wxS_IROTH 00004
23 #define wxS_IWOTH 00002
24 #define wxS_IXOTH 00001
26 /** Default mode for the new files: corresponds to umask 022 */
27 #define wxS_DEFAULT (wxS_IRUSR | wxS_IWUSR | wxS_IRGRP | wxS_IWGRP | wxS_IROTH | wxS_IWOTH)
35 wxTempFile provides a relatively safe way to replace the contents of the
36 existing file. The name is explained by the fact that it may be also used as
37 just a temporary file if you don't replace the old file contents.
39 Usually, when a program replaces the contents of some file it first opens it for
40 writing, thus losing all of the old data and then starts recreating it.
41 This approach is not very safe because during the regeneration of the file bad
42 things may happen: the program may find that there is an internal error preventing
43 it from completing file generation, the user may interrupt it (especially if file
44 generation takes long time) and, finally, any other external interrupts (power
45 supply failure or a disk error) will leave you without either the original file
48 wxTempFile addresses this problem by creating a temporary file which is meant to
49 replace the original file - but only after it is fully written. So, if the user
50 interrupts the program during the file generation, the old file won't be lost.
51 Also, if the program discovers itself that it doesn't want to replace the old
52 file there is no problem - in fact, wxTempFile will @b not replace the old
53 file by default, you should explicitly call wxTempFile::Commit() to do it.
54 Calling wxTempFile::Discard() explicitly discards any modifications: it
55 closes and deletes the temporary file and leaves the original file unchanged.
56 If you don't call neither of Commit() and Discard(), the destructor will
57 call Discard() automatically.
59 To summarize: if you want to replace another file, create an instance of
60 wxTempFile passing the name of the file to be replaced to the constructor
61 (you may also use default constructor and pass the file name to wxTempFile::Open).
62 Then you can write to wxTempFile using wxFile-like functions and later call
63 wxTempFile::Commit() to replace the old file (and close this one) or call
64 wxTempFile::Discard() to cancel the modifications.
73 Associates wxTempFile with the file to be replaced and opens it.
74 You should use IsOpened() to verify if the constructor succeeded.
76 wxTempFile(const wxString
& strName
);
79 Destructor calls Discard() if temporary file is still opened.
84 Validate changes: deletes the old file of name m_strName and renames the new
85 file to the old name. Returns @true if both actions succeeded.
87 If @false is returned it may unfortunately mean two quite different things:
88 either that either the old file couldn't be deleted or that the new file
89 couldn't be renamed to the old name.
94 Discard changes: the old file contents is not changed, temporary file is
100 Returns @true if the file was successfully opened.
102 bool IsOpened() const;
105 Returns the length of the file.
107 This method may return wxInvalidOffset if the length couldn't be
108 determined or also 0 even for non-empty files if the file is not
111 In general, the only way to determine if the file for which this function
112 returns 0 is really empty or not is to try reading from it.
114 wxFileOffset
Length() const;
117 Open the temporary file, returns @true on success, @false if an error
119 @a strName is the name of file to be replaced. The temporary file is always
120 created in the directory where @a strName is. In particular, if @a strName
121 doesn't include the path, it is created in the current directory and the
122 program should have write access to it for the function to succeed.
124 bool Open(const wxString
& strName
);
127 Seeks to the specified position.
129 wxFileOffset
Seek(wxFileOffset ofs
,
130 wxSeekMode mode
= wxFromStart
);
133 Returns the current position or wxInvalidOffset if file is not opened or
134 if another error occurred.
136 wxFileOffset
Tell() const;
139 Write to the file, return @true on success, @false on failure.
140 The second argument is only meaningful in Unicode build of wxWidgets when
141 @a conv is used to convert @a str to multibyte representation.
143 bool Write(const wxString
& str
,
144 const wxMBConv
& conv
= wxConvUTF8
);
152 A wxFile performs raw file I/O.
154 This is a very small class designed to minimize the overhead of using it - in fact,
155 there is hardly any overhead at all, but using it brings you automatic error
156 checking and hides differences between platforms and compilers.
158 wxFile also automatically closes the file in its destructor making it unnecessary
159 to worry about forgetting to do it.
161 A wxFile performs raw file I/O. This is a very small class designed to
162 minimize the overhead of using it - in fact, there is hardly any overhead at
163 all, but using it brings you automatic error checking and hides differences
164 between platforms and compilers. wxFile also automatically closes the file in
165 its destructor making it unnecessary to worry about forgetting to do it.
166 wxFile is a wrapper around @c file descriptor. - see also wxFFile for a
167 wrapper around @c FILE structure.
169 ::wxFileOffset is used by the wxFile functions which require offsets as
170 parameter or return them. If the platform supports it, wxFileOffset is a
171 typedef for a native 64 bit integer, otherwise a 32 bit integer is used for
182 The OpenMode enumeration defines the different modes for opening a file with wxFile.
183 It is also used with wxFile::Access function.
187 /** Open file for reading or test if it can be opened for reading with Access() */
190 /** Open file for writing deleting the contents of the file if it already exists
191 or test if it can be opened for writing with Access(). */
194 /** Open file for reading and writing; can not be used with Access() */
197 /** Open file for appending: the file is opened for writing, but the old contents
198 of the file is not erased and the file pointer is initially placed at the end
199 of the file; can not be used with Access().
201 This is the same as OpenMode::write if the file doesn't exist.
206 Open the file securely for writing (Uses O_EXCL | O_CREAT).
207 Will fail if the file already exists, else create and open it atomically.
208 Useful for opening temporary files without being vulnerable to race exploits.
214 Standard file descriptors
216 enum { fd_invalid
= -1, fd_stdin
, fd_stdout
, fd_stderr
};
224 Opens a file with a filename.
229 The mode in which to open the file.
231 wxFile(const wxString
& filename
,
232 wxFile::OpenMode mode
= wxFile::read
);
235 Associates the file with the given file descriptor, which has already been
236 opened. See Attach() for the list of predefined descriptors.
239 An existing file descriptor.
244 Destructor will close the file.
245 @note it is not virtual so you should not use wxFile polymorphically.
250 This function verifies if we may access the given file in specified mode.
251 Only values of @c wxFile::read or @c wxFile::write really make sense here.
253 static bool Access(const wxString
& name
, wxFile::OpenMode mode
);
256 Attaches an existing file descriptor to the wxFile object.
257 Example of predefined file descriptors are 0, 1 and 2 which correspond to
258 stdin, stdout and stderr (and have symbolic names of @c wxFile::fd_stdin,
259 @c wxFile::fd_stdout and @c wxFile::fd_stderr).
261 The descriptor should be already opened and it will be closed by wxFile
272 Creates a file for writing.
274 If the file already exists, setting @b overwrite to @true will ensure
277 @a access may be an OR combination of the file access values
278 like ::wxS_IRUSR, ::wxS_IWUSR, etc, etc.
280 bool Create(const wxString
& filename
,
281 bool overwrite
= false,
282 int access
= wxS_DEFAULT
);
285 Get back a file descriptor from wxFile object - the caller is responsible for
286 closing the file if this descriptor is opened.
287 IsOpened() will return @false after call to Detach().
292 Returns @true if the end of the file has been reached.
293 Note that the behaviour of the file pointer based class wxFFile is
294 different as wxFFile::Eof() will return @true here only if an
295 attempt has been made to read @b past the last byte of the file, while
296 wxFile::Eof() will return @true even before such attempt is made if the
297 file pointer is at the last position in the file.
299 Note also that this function doesn't work on unseekable file descriptors
300 (examples include pipes, terminals and sockets under Unix) and an attempt to
301 use it will result in an error message in such case.
303 So, to read the entire file into memory, you should write a loop which uses
304 Read() repeatedly and tests its return condition instead of using Eof()
305 as this will not work for special files under Unix.
310 Returns @true if the given name specifies an existing regular file
311 (not a directory or a link)
313 static bool Exists(const wxString
& filename
);
316 Flushes the file descriptor.
318 Note that Flush() is not implemented on some Windows compilers due to a
319 missing fsync function, which reduces the usefulness of this function
320 (it can still be called but it will do nothing on unsupported compilers).
325 Returns the type of the file.
327 wxFileKind
GetKind() const;
330 Returns @true if the file has been opened.
332 bool IsOpened() const;
335 Returns the length of the file.
337 wxFileOffset
Length() const;
340 Opens the file, returning @true if successful.
345 The mode in which to open the file.
347 bool Open(const wxString
& filename
,
348 wxFile::OpenMode mode
= wxFile::read
);
351 Reads from the file into a memory buffer.
358 @return The number of bytes read, or the symbol wxInvalidOffset.
360 size_t Read(void* buffer
, size_t count
);
363 Seeks to the specified position.
368 One of wxFromStart, wxFromEnd, wxFromCurrent.
370 @return The actual offset position achieved, or wxInvalidOffset on
373 wxFileOffset
Seek(wxFileOffset ofs
,
374 wxSeekMode mode
= wxFromStart
);
377 Moves the file pointer to the specified number of bytes relative to the
378 end of the file. For example, @c SeekEnd(-5) would position the pointer 5
379 bytes before the end.
382 Number of bytes before the end of the file.
384 @return The actual offset position achieved, or wxInvalidOffset on
387 wxFileOffset
SeekEnd(wxFileOffset ofs
= 0);
390 Returns the current position or wxInvalidOffset if file is not opened or
391 if another error occurred.
393 wxFileOffset
Tell() const;
396 Write data to the file (descriptor).
399 Buffer from which to read data
401 Number of bytes to write
403 @return The number of bytes written.
405 size_t Write(const void *buffer
, size_t count
);
408 Writes the contents of the string to the file, returns @true on success.
409 The second argument is only meaningful in Unicode build of wxWidgets when
410 @a conv is used to convert @a s to a multibyte representation.
412 Note that this method only works with @c NUL-terminated strings, if you want
413 to write data with embedded @c NULs to the file you should use the other
416 bool Write(const wxString
& s
, const wxMBConv
& conv
= wxConvUTF8
);
419 Returns the file descriptor associated with the file.