]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/file.h
Fixed and clarified editor control event handling
[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
FM
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
23324ae1
FM
32/**
33 @class wxTempFile
7c913512 34
23324ae1
FM
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.
7c913512 38
23324ae1 39 Usually, when a program replaces the contents of some file it first opens it for
23b7f0cb
FM
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
23324ae1
FM
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.
7c913512 47
23324ae1
FM
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
23b7f0cb
FM
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.
7c913512 58
23324ae1 59 To summarize: if you want to replace another file, create an instance of
23b7f0cb
FM
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.
7c913512 65
23324ae1
FM
66 @library{wxbase}
67 @category{file}
68*/
7c913512 69class wxTempFile
23324ae1
FM
70{
71public:
72 /**
23b7f0cb
FM
73 Associates wxTempFile with the file to be replaced and opens it.
74 You should use IsOpened() to verify if the constructor succeeded.
23324ae1
FM
75 */
76 wxTempFile(const wxString& strName);
77
78 /**
23b7f0cb 79 Destructor calls Discard() if temporary file is still opened.
23324ae1
FM
80 */
81 ~wxTempFile();
82
83 /**
84 Validate changes: deletes the old file of name m_strName and renames the new
23b7f0cb
FM
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.
23324ae1
FM
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 */
328f5751 102 bool IsOpened() const;
23324ae1
FM
103
104 /**
105 Returns the length of the file.
41f6f17d
VZ
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
23b7f0cb
FM
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.
23324ae1 113 */
328f5751 114 wxFileOffset Length() const;
23324ae1
FM
115
116 /**
117 Open the temporary file, returns @true on success, @false if an error
118 occurred.
4cc4bfaf 119 @a strName is the name of file to be replaced. The temporary file is always
23b7f0cb
FM
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.
23324ae1
FM
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 /**
23b7f0cb
FM
133 Returns the current position or wxInvalidOffset if file is not opened or
134 if another error occurred.
23324ae1 135 */
328f5751 136 wxFileOffset Tell() const;
23324ae1
FM
137
138 /**
139 Write to the file, return @true on success, @false on failure.
23324ae1 140 The second argument is only meaningful in Unicode build of wxWidgets when
4cc4bfaf 141 @a conv is used to convert @a str to multibyte representation.
23324ae1
FM
142 */
143 bool Write(const wxString& str,
144 const wxMBConv& conv = wxConvUTF8);
145};
146
147
e54c96f1 148
23324ae1
FM
149/**
150 @class wxFile
7c913512 151
23b7f0cb
FM
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
23324ae1
FM
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.
23b7f0cb
FM
166 wxFile is a wrapper around @c file descriptor. - see also wxFFile for a
167 wrapper around @c FILE structure.
7c913512 168
23b7f0cb
FM
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.
7c913512 173
23324ae1
FM
174 @library{wxbase}
175 @category{file}
176*/
7c913512 177class wxFile
23324ae1
FM
178{
179public:
23b7f0cb 180
23324ae1 181 /**
23b7f0cb
FM
182 The OpenMode enumeration defines the different modes for opening a file with wxFile.
183 It is also used with wxFile::Access function.
c1d97593 184 */
23b7f0cb
FM
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 /**
c1d97593
RR
214 Standard file descriptors
215 */
216 enum { fd_invalid = -1, fd_stdin, fd_stdout, fd_stderr };
217
23b7f0cb 218 /**
c1d97593
RR
219 Default constructor.
220 */
221 wxFile();
23b7f0cb 222
c1d97593
RR
223 /**
224 Opens a file with a filename.
3c4f71cc 225
7c913512 226 @param filename
4cc4bfaf 227 The filename.
7c913512 228 @param mode
23b7f0cb 229 The mode in which to open the file.
23324ae1 230 */
7c913512
FM
231 wxFile(const wxString& filename,
232 wxFile::OpenMode mode = wxFile::read);
23b7f0cb 233
c1d97593
RR
234 /**
235 Associates the file with the given file descriptor, which has already been
236 opened. See Attach() for the list of predefined descriptors.
23b7f0cb 237
c1d97593 238 @param fd
23b7f0cb 239 An existing file descriptor.
c1d97593 240 */
7c913512 241 wxFile(int fd);
23324ae1
FM
242
243 /**
244 Destructor will close the file.
1f1d2182 245 @note it is not virtual so you should not use wxFile polymorphically.
23324ae1
FM
246 */
247 ~wxFile();
248
249 /**
c1d97593
RR
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.
23324ae1 252 */
23b7f0cb 253 static bool Access(const wxString& name, wxFile::OpenMode mode);
23324ae1
FM
254
255 /**
23b7f0cb
FM
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,
c1d97593 259 @c wxFile::fd_stdout and @c wxFile::fd_stderr).
23b7f0cb 260
23324ae1
FM
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 /**
23b7f0cb
FM
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.
23324ae1 279 */
23b7f0cb
FM
280 bool Create(const wxString& filename,
281 bool overwrite = false,
23324ae1
FM
282 int access = wxS_DEFAULT);
283
284 /**
285 Get back a file descriptor from wxFile object - the caller is responsible for
23b7f0cb
FM
286 closing the file if this descriptor is opened.
287 IsOpened() will return @false after call to Detach().
23324ae1
FM
288 */
289 void Detach();
290
291 /**
292 Returns @true if the end of the file has been reached.
23b7f0cb
FM
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
c1d97593
RR
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.
23b7f0cb 298
23324ae1
FM
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
23b7f0cb
FM
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.
23324ae1 306 */
328f5751 307 bool Eof() const;
23324ae1
FM
308
309 /**
23b7f0cb
FM
310 Returns @true if the given name specifies an existing regular file
311 (not a directory or a link)
23324ae1
FM
312 */
313 static bool Exists(const wxString& filename);
314
315 /**
316 Flushes the file descriptor.
23b7f0cb
FM
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
23324ae1
FM
320 (it can still be called but it will do nothing on unsupported compilers).
321 */
322 bool Flush();
323
324 /**
23b7f0cb 325 Returns the type of the file.
23324ae1 326 */
328f5751 327 wxFileKind GetKind() const;
23324ae1
FM
328
329 /**
330 Returns @true if the file has been opened.
331 */
328f5751 332 bool IsOpened() const;
23324ae1
FM
333
334 /**
335 Returns the length of the file.
336 */
328f5751 337 wxFileOffset Length() const;
23324ae1
FM
338
339 /**
340 Opens the file, returning @true if successful.
3c4f71cc 341
7c913512 342 @param filename
4cc4bfaf 343 The filename.
7c913512 344 @param mode
23b7f0cb 345 The mode in which to open the file.
23324ae1
FM
346 */
347 bool Open(const wxString& filename,
348 wxFile::OpenMode mode = wxFile::read);
349
23324ae1 350 /**
23b7f0cb
FM
351 Reads from the file into a memory buffer.
352
c1d97593
RR
353 @param buffer
354 Buffer to write in
355 @param count
23b7f0cb
FM
356 Bytes to read
357
358 @return The number of bytes read, or the symbol wxInvalidOffset.
23324ae1
FM
359 */
360 size_t Read(void* buffer, size_t count);
23324ae1
FM
361
362 /**
363 Seeks to the specified position.
3c4f71cc 364
7c913512 365 @param ofs
4cc4bfaf 366 Offset to seek to.
7c913512 367 @param mode
4cc4bfaf 368 One of wxFromStart, wxFromEnd, wxFromCurrent.
3c4f71cc 369
d29a9a8a 370 @return The actual offset position achieved, or wxInvalidOffset on
23b7f0cb 371 failure.
23324ae1
FM
372 */
373 wxFileOffset Seek(wxFileOffset ofs,
374 wxSeekMode mode = wxFromStart);
375
376 /**
23b7f0cb
FM
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
23324ae1 379 bytes before the end.
3c4f71cc 380
7c913512 381 @param ofs
4cc4bfaf 382 Number of bytes before the end of the file.
3c4f71cc 383
d29a9a8a 384 @return The actual offset position achieved, or wxInvalidOffset on
23b7f0cb 385 failure.
23324ae1
FM
386 */
387 wxFileOffset SeekEnd(wxFileOffset ofs = 0);
388
389 /**
c1d97593 390 Returns the current position or wxInvalidOffset if file is not opened or
23b7f0cb 391 if another error occurred.
23324ae1 392 */
328f5751 393 wxFileOffset Tell() const;
23324ae1 394
c1d97593
RR
395 /**
396 Write data to the file (descriptor).
23b7f0cb 397
4050e98d 398 @param buffer
c1d97593
RR
399 Buffer from which to read data
400 @param count
401 Number of bytes to write
23b7f0cb 402
c1d97593
RR
403 @return The number of bytes written.
404 */
405 size_t Write(const void *buffer, size_t count);
406
23324ae1
FM
407 /**
408 Writes the contents of the string to the file, returns @true on success.
23324ae1 409 The second argument is only meaningful in Unicode build of wxWidgets when
c1d97593 410 @a conv is used to convert @a s to a multibyte representation.
23b7f0cb 411
23324ae1 412 Note that this method only works with @c NUL-terminated strings, if you want
7c913512 413 to write data with embedded @c NULs to the file you should use the other
c1d97593 414 Write() overload.
23324ae1
FM
415 */
416 bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8);
417
418 /**
419 Returns the file descriptor associated with the file.
420 */
328f5751 421 int fd() const;
23324ae1 422};
e54c96f1 423