]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/file.h
fix parsing of IP literals in URIs, added test for it
[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 //@{
11 /**
12 These constants define the file access rights and are used with wxFile::Create and wxFile::Open.
13 */
14 #define wxS_IRUSR 00400
15 #define wxS_IWUSR 00200
16 #define wxS_IXUSR 00100
17
18 #define wxS_IRGRP 00040
19 #define wxS_IWGRP 00020
20 #define wxS_IXGRP 00010
21
22 #define wxS_IROTH 00004
23 #define wxS_IWOTH 00002
24 #define wxS_IXOTH 00001
25
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)
28 //@}
29
30
31
32 /**
33 @class wxTempFile
34
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.
38
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
46 or the new one.
47
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.
58
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.
65
66 @library{wxbase}
67 @category{file}
68 */
69 class wxTempFile
70 {
71 public:
72 /**
73 Associates wxTempFile with the file to be replaced and opens it.
74 You should use IsOpened() to verify if the constructor succeeded.
75 */
76 wxTempFile(const wxString& strName);
77
78 /**
79 Destructor calls Discard() if temporary file is still opened.
80 */
81 ~wxTempFile();
82
83 /**
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.
86
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.
90 */
91 bool Commit();
92
93 /**
94 Discard changes: the old file contents is not changed, temporary file is
95 deleted.
96 */
97 void Discard();
98
99 /**
100 Returns @true if the file was successfully opened.
101 */
102 bool IsOpened() const;
103
104 /**
105 Returns the length of the file.
106
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
109 seekable.
110
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.
113 */
114 wxFileOffset Length() const;
115
116 /**
117 Open the temporary file, returns @true on success, @false if an error
118 occurred.
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.
123 */
124 bool Open(const wxString& strName);
125
126 /**
127 Seeks to the specified position.
128 */
129 wxFileOffset Seek(wxFileOffset ofs,
130 wxSeekMode mode = wxFromStart);
131
132 /**
133 Returns the current position or wxInvalidOffset if file is not opened or
134 if another error occurred.
135 */
136 wxFileOffset Tell() const;
137
138 /**
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.
142 */
143 bool Write(const wxString& str,
144 const wxMBConv& conv = wxConvUTF8);
145 };
146
147
148
149 /**
150 @class wxFile
151
152 A wxFile performs raw file I/O.
153
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.
157
158 wxFile also automatically closes the file in its destructor making it unnecessary
159 to worry about forgetting to do it.
160
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.
168
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
172 ::wxFileOffset.
173
174 @library{wxbase}
175 @category{file}
176 */
177 class wxFile
178 {
179 public:
180
181 /**
182 The OpenMode enumeration defines the different modes for opening a file with wxFile.
183 It is also used with wxFile::Access function.
184 */
185 enum OpenMode {
186
187 /** Open file for reading or test if it can be opened for reading with Access() */
188 read,
189
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(). */
192 write,
193
194 /** Open file for reading and writing; can not be used with Access() */
195 read_write,
196
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().
200
201 This is the same as OpenMode::write if the file doesn't exist.
202 */
203 write_append,
204
205 /**
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.
209 */
210 write_excl
211 };
212
213 /**
214 Standard file descriptors
215 */
216 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
217
218 /**
219 Default constructor.
220 */
221 wxFile();
222
223 /**
224 Opens a file with a filename.
225
226 @param filename
227 The filename.
228 @param mode
229 The mode in which to open the file.
230 */
231 wxFile(const wxString& filename,
232 wxFile::OpenMode mode = wxFile::read);
233
234 /**
235 Associates the file with the given file descriptor, which has already been
236 opened. See Attach() for the list of predefined descriptors.
237
238 @param fd
239 An existing file descriptor.
240 */
241 wxFile(int fd);
242
243 /**
244 Destructor will close the file.
245 @note it is not virtual so you should not use wxFile polymorphically.
246 */
247 ~wxFile();
248
249 /**
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.
252 */
253 static bool Access(const wxString& name, wxFile::OpenMode mode);
254
255 /**
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).
260
261 The descriptor should be already opened and it will be closed by wxFile
262 object.
263 */
264 void Attach(int fd);
265
266 /**
267 Closes the file.
268 */
269 void Close();
270
271 /**
272 Creates a file for writing.
273
274 If the file already exists, setting @b overwrite to @true will ensure
275 it is overwritten.
276
277 @a access may be an OR combination of the file access values
278 like ::wxS_IRUSR, ::wxS_IWUSR, etc, etc.
279 */
280 bool Create(const wxString& filename,
281 bool overwrite = false,
282 int access = wxS_DEFAULT);
283
284 /**
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().
288 */
289 void Detach();
290
291 /**
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.
298
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.
302
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.
306 */
307 bool Eof() const;
308
309 /**
310 Returns @true if the given name specifies an existing regular file
311 (not a directory or a link)
312 */
313 static bool Exists(const wxString& filename);
314
315 /**
316 Flushes the file descriptor.
317
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).
321 */
322 bool Flush();
323
324 /**
325 Returns the type of the file.
326 */
327 wxFileKind GetKind() const;
328
329 /**
330 Returns @true if the file has been opened.
331 */
332 bool IsOpened() const;
333
334 /**
335 Returns the length of the file.
336 */
337 wxFileOffset Length() const;
338
339 /**
340 Opens the file, returning @true if successful.
341
342 @param filename
343 The filename.
344 @param mode
345 The mode in which to open the file.
346 */
347 bool Open(const wxString& filename,
348 wxFile::OpenMode mode = wxFile::read);
349
350 /**
351 Reads from the file into a memory buffer.
352
353 @param buffer
354 Buffer to write in
355 @param count
356 Bytes to read
357
358 @return The number of bytes read, or the symbol wxInvalidOffset.
359 */
360 size_t Read(void* buffer, size_t count);
361
362 /**
363 Seeks to the specified position.
364
365 @param ofs
366 Offset to seek to.
367 @param mode
368 One of wxFromStart, wxFromEnd, wxFromCurrent.
369
370 @return The actual offset position achieved, or wxInvalidOffset on
371 failure.
372 */
373 wxFileOffset Seek(wxFileOffset ofs,
374 wxSeekMode mode = wxFromStart);
375
376 /**
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.
380
381 @param ofs
382 Number of bytes before the end of the file.
383
384 @return The actual offset position achieved, or wxInvalidOffset on
385 failure.
386 */
387 wxFileOffset SeekEnd(wxFileOffset ofs = 0);
388
389 /**
390 Returns the current position or wxInvalidOffset if file is not opened or
391 if another error occurred.
392 */
393 wxFileOffset Tell() const;
394
395 /**
396 Write data to the file (descriptor).
397
398 @param buffer
399 Buffer from which to read data
400 @param count
401 Number of bytes to write
402
403 @return The number of bytes written.
404 */
405 size_t Write(const void *buffer, size_t count);
406
407 /**
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.
411
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
414 Write() overload.
415 */
416 bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8);
417
418 /**
419 Returns the file descriptor associated with the file.
420 */
421 int fd() const;
422 };
423