]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/file.h
don't call OnData() if OnEnter() rejected the drag (#9634)
[wxWidgets.git] / interface / wx / file.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: file.h
3 // Purpose: interface of wxTempFile
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @class wxTempFile
11
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.
15
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.
24
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
30 file by default, you should explicitly call wxTempFile::Commit
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.
35
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
38 may also use default constructor and pass the file name to
39 wxTempFile::Open). Then you can wxTempFile::write
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.
44
45 @library{wxbase}
46 @category{file}
47 */
48 class wxTempFile
49 {
50 public:
51 /**
52 Associates wxTempFile with the file to be replaced and opens it. You should use
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 */
81 bool IsOpened() const;
82
83 /**
84 Returns the length of the file.
85 */
86 wxFileOffset Length() const;
87
88 /**
89 Open the temporary file, returns @true on success, @false if an error
90 occurred.
91 @a strName is the name of file to be replaced. The temporary file is always
92 created in the directory where @a strName is. In particular, if
93 @a strName doesn't include the path, it is created in the current directory
94 and the program should have write access to it for the function to succeed.
95 */
96 bool Open(const wxString& strName);
97
98 /**
99 Seeks to the specified position.
100 */
101 wxFileOffset Seek(wxFileOffset ofs,
102 wxSeekMode mode = wxFromStart);
103
104 /**
105 Returns the current position or wxInvalidOffset if file is not opened or if
106 another
107 error occurred.
108 */
109 wxFileOffset Tell() const;
110
111 /**
112 Write to the file, return @true on success, @false on failure.
113 The second argument is only meaningful in Unicode build of wxWidgets when
114 @a conv is used to convert @a str to multibyte representation.
115 */
116 bool Write(const wxString& str,
117 const wxMBConv& conv = wxConvUTF8);
118 };
119
120
121
122 /**
123 @class wxFile
124
125 A wxFile performs raw file I/O. This is a very small class designed to
126 minimize the overhead of using it - in fact, there is hardly any overhead at
127 all, but using it brings you automatic error checking and hides differences
128 between platforms and compilers. wxFile also automatically closes the file in
129 its destructor making it unnecessary to worry about forgetting to do it.
130 wxFile is a wrapper around @c file descriptor. - see also
131 wxFFile for a wrapper around @c FILE structure.
132
133 @c wxFileOffset is used by the wxFile functions which require offsets as
134 parameter or return them. If the platform supports it, wxFileOffset is a typedef
135 for a native 64 bit integer, otherwise a 32 bit integer is used for
136 wxFileOffset.
137
138 @library{wxbase}
139 @category{file}
140 */
141 class wxFile
142 {
143 public:
144 //@{
145 /**
146 Associates the file with the given file descriptor, which has already been
147 opened.
148
149 @param filename
150 The filename.
151 @param mode
152 The mode in which to open the file. May be one of read(), write() and
153 wxFile::read_write.
154 @param fd
155 An existing file descriptor (see Attach() for the list of predefined
156 descriptors)
157 */
158 wxFile();
159 wxFile(const wxString& filename,
160 wxFile::OpenMode mode = wxFile::read);
161 wxFile(int fd);
162 //@}
163
164 /**
165 Destructor will close the file.
166 @note it is not virtual so you should not use wxFile polymorphically.
167 */
168 ~wxFile();
169
170 /**
171 This function verifies if we may access the given file in specified mode. Only
172 values of read() or write() really make sense here.
173 */
174 static bool Access(const wxString& name, OpenMode mode);
175
176 /**
177 Attaches an existing file descriptor to the wxFile object. Example of predefined
178 file descriptors are 0, 1 and 2 which correspond to stdin, stdout and stderr
179 (and
180 have symbolic names of @b wxFile::fd_stdin, @b wxFile::fd_stdout and @b
181 wxFile::fd_stderr).
182 The descriptor should be already opened and it will be closed by wxFile
183 object.
184 */
185 void Attach(int fd);
186
187 /**
188 Closes the file.
189 */
190 void Close();
191
192 /**
193 Creates a file for writing. If the file already exists, setting @b overwrite to
194 @true
195 will ensure it is overwritten.
196 */
197 bool Create(const wxString& filename, bool overwrite = false,
198 int access = wxS_DEFAULT);
199
200 /**
201 Get back a file descriptor from wxFile object - the caller is responsible for
202 closing the file if this
203 descriptor is opened. IsOpened() will return @false after call to Detach().
204 */
205 void Detach();
206
207 /**
208 Returns @true if the end of the file has been reached.
209 Note that the behaviour of the file pointer based class
210 wxFFile is different as wxFFile::Eof
211 will return @true here only if an attempt has been made to read
212 @e past the last byte of the file, while wxFile::Eof() will return @true
213 even before such attempt is made if the file pointer is at the last position
214 in the file.
215 Note also that this function doesn't work on unseekable file descriptors
216 (examples include pipes, terminals and sockets under Unix) and an attempt to
217 use it will result in an error message in such case. So, to read the entire
218 file into memory, you should write a loop which uses
219 Read() repeatedly and tests its return condition instead
220 of using Eof() as this will not work for special files under Unix.
221 */
222 bool Eof() const;
223
224 /**
225 Returns @true if the given name specifies an existing regular file (not a
226 directory or a link)
227 */
228 static bool Exists(const wxString& filename);
229
230 /**
231 Flushes the file descriptor.
232 Note that Flush() is not implemented on some Windows compilers
233 due to a missing fsync function, which reduces the usefulness of this function
234 (it can still be called but it will do nothing on unsupported compilers).
235 */
236 bool Flush();
237
238 /**
239 Returns the type of the file. Possible return values are:
240 */
241 wxFileKind GetKind() const;
242
243 /**
244 Returns @true if the file has been opened.
245 */
246 bool IsOpened() const;
247
248 /**
249 Returns the length of the file.
250 */
251 wxFileOffset Length() const;
252
253 /**
254 Opens the file, returning @true if successful.
255
256 @param filename
257 The filename.
258 @param mode
259 The mode in which to open the file. May be one of read(), write() and
260 wxFile::read_write.
261 */
262 bool Open(const wxString& filename,
263 wxFile::OpenMode mode = wxFile::read);
264
265 //@{
266 /**
267 if there was an error.
268 */
269 size_t Read(void* buffer, size_t count);
270 Parameters Return value
271 The number of bytes read, or the symbol wxInvalidOffset();
272 //@}
273
274 /**
275 Seeks to the specified position.
276
277 @param ofs
278 Offset to seek to.
279 @param mode
280 One of wxFromStart, wxFromEnd, wxFromCurrent.
281
282 @return The actual offset position achieved, or wxInvalidOffset on
283 failure.
284 */
285 wxFileOffset Seek(wxFileOffset ofs,
286 wxSeekMode mode = wxFromStart);
287
288 /**
289 Moves the file pointer to the specified number of bytes relative to the end of
290 the file. For example, @c SeekEnd(-5) would position the pointer 5
291 bytes before the end.
292
293 @param ofs
294 Number of bytes before the end of the file.
295
296 @return The actual offset position achieved, or wxInvalidOffset on
297 failure.
298 */
299 wxFileOffset SeekEnd(wxFileOffset ofs = 0);
300
301 /**
302 Returns the current position or wxInvalidOffset if file is not opened or if
303 another
304 error occurred.
305 */
306 wxFileOffset Tell() const;
307
308 /**
309 Writes the contents of the string to the file, returns @true on success.
310 The second argument is only meaningful in Unicode build of wxWidgets when
311 @a conv is used to convert @a s to multibyte representation.
312 Note that this method only works with @c NUL-terminated strings, if you want
313 to write data with embedded @c NULs to the file you should use the other
314 @ref write() "Write() overload".
315 */
316 bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8);
317
318 /**
319 Returns the file descriptor associated with the file.
320 */
321 int fd() const;
322 };
323