Made some subtle changes to the way the static variables are instantiated in
[pwlib.git] / include / ptlib / file.h
blob191fb32751eb713b7550cfb437a67ef51e3fd8f0
1 /*
2 * file.h
4 * Operating System file I/O channel class.
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.42 2003/09/26 09:58:50 rogerhardiman
31 * Move #include <sys/stat.h> from the unix file.h to the main file.h
32 * FreeBSD's sys/stat.h includes extern "C" for some prototypes and you
33 * cannot have an extern "C" in the middle of a C++ class
35 * Revision 1.41 2003/09/17 05:41:58 csoutheren
36 * Removed recursive includes
38 * Revision 1.40 2003/09/17 01:18:02 csoutheren
39 * Removed recursive include file system and removed all references
40 * to deprecated coooperative threading support
42 * Revision 1.39 2002/09/16 01:08:59 robertj
43 * Added #define so can select if #pragma interface/implementation is used on
44 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
46 * Revision 1.38 2002/07/02 08:00:55 craigs
47 * Also made GetPosition, SetPosition and SetLength virtual as well
49 * Revision 1.37 2002/07/02 07:59:42 craigs
50 * Added virtual to GetLength call
52 * Revision 1.36 2002/01/13 20:54:55 rogerh
53 * Make the other Open() virtual so it can be overridden (eg in PWAVFile)
55 * Revision 1.35 2001/05/22 12:49:32 robertj
56 * Did some seriously wierd rewrite of platform headers to eliminate the
57 * stupid GNU compiler warning about braces not matching.
59 * Revision 1.34 2000/09/27 05:58:07 craigs
60 * Added virtual to PFile::Open to allow overriding in descandant classes
62 * Revision 1.33 2000/07/09 14:05:46 robertj
63 * Added file share options.
65 * Revision 1.32 1999/06/13 13:54:07 robertj
66 * Added PConsoleChannel class for access to stdin/stdout/stderr.
68 * Revision 1.31 1999/06/09 02:05:20 robertj
69 * Added ability to open file as standard input, output and error streams.
71 * Revision 1.30 1999/03/09 02:59:49 robertj
72 * Changed comments to doc++ compatible documentation.
74 * Revision 1.29 1999/02/16 08:07:11 robertj
75 * MSVC 6.0 compatibility changes.
77 * Revision 1.28 1998/09/23 06:20:35 robertj
78 * Added open source copyright license.
80 * Revision 1.27 1995/07/31 12:15:43 robertj
81 * Removed PContainer from PChannel ancestor.
83 * Revision 1.26 1995/06/17 11:12:33 robertj
84 * Documentation update.
86 * Revision 1.25 1995/04/22 00:43:57 robertj
87 * Added Move() function and changed semantics of Rename().
88 * Changed all file name strings to PFilePath objects.
90 * Revision 1.24 1995/03/14 12:41:23 robertj
91 * Updated documentation to use HTML codes.
93 * Revision 1.23 1995/03/12 04:37:13 robertj
94 * Moved GetHandle() function from PFile to PChannel.
96 * Revision 1.22 1995/01/14 06:22:11 robertj
97 * Documentation
99 * Revision 1.21 1994/12/21 11:52:54 robertj
100 * Documentation and variable normalisation.
102 * Revision 1.20 1994/08/23 11:32:52 robertj
103 * Oops
105 * Revision 1.19 1994/08/22 00:46:48 robertj
106 * Added pragma fro GNU C++ compiler.
108 * Revision 1.18 1994/08/21 23:43:02 robertj
109 * Added "remove on close" feature for temporary files.
110 * Added "force" option to Remove/Rename etc to override write protection.
111 * Added function to set file permissions.
113 * Revision 1.17 1994/07/17 10:46:06 robertj
114 * Moved data to platform dependent files.
116 * Revision 1.16 1994/06/25 11:55:15 robertj
117 * Unix version synchronisation.
119 * Revision 1.15 1994/04/20 12:17:44 robertj
120 * Split name into PFilePath
122 * Revision 1.14 1994/04/01 14:11:03 robertj
123 * Added const to functions.
124 * Added SetName function.
126 * Revision 1.13 1994/03/07 07:38:19 robertj
127 * Major enhancementsacross the board.
129 * Revision 1.12 1994/01/13 03:40:22 robertj
130 * Added hidden flag to file info.
132 * Revision 1.12 1994/01/13 03:36:48 robertj
133 * Created intermediate class PInteractorLayout for dialog-ish windows.
135 * Revision 1.11 1994/01/03 04:42:23 robertj
136 * Mass changes to common container classes and interactors etc etc etc.
138 * Revision 1.10 1993/12/31 06:45:38 robertj
139 * Made inlines optional for debugging purposes.
141 * Revision 1.9 1993/09/27 16:35:25 robertj
142 * Changed GetName() to GetTitle(), better naming convention.
143 * Moved internal functions to private section.
145 * Revision 1.8 1993/08/31 03:38:02 robertj
146 * Changed PFile::Status to PFile::Info due to X-Windows compatibility.
148 * Revision 1.7 1993/08/27 18:17:47 robertj
149 * Moved code from MS-DOS platform to common files.
151 * Revision 1.6 1993/08/21 04:40:19 robertj
152 * Added Copy() function.
154 * Revision 1.5 1993/08/21 01:50:33 robertj
155 * Made Clone() function optional, default will assert if called.
157 * Revision 1.4 1993/08/01 14:05:27 robertj
158 * Added GetFileName() function required for proper portability.
159 * Improved some comments.
161 * Revision 1.3 1993/07/14 12:49:16 robertj
162 * Fixed RCS keywords.
167 #ifndef _PFILE
168 #define _PFILE
170 #ifdef P_USE_PRAGMA
171 #pragma interface
172 #endif
174 #ifndef _WIN32
175 #include <sys/stat.h>
176 #endif
180 ///////////////////////////////////////////////////////////////////////////////
181 // Binary Files
183 /**This class represents a disk file. This is a particular type of I/O channel
184 that has certain attributes. All platforms have a disk file, though exact
185 details of naming convertions etc may be different.
187 The basic model for files is that they are a named sequence of bytes that
188 persists within a directory structure. The transfer of data to and from
189 the file is made at a current position in the file. This may be set to
190 random locations within the file.
192 class PFile : public PChannel
194 PCLASSINFO(PFile, PChannel);
196 public:
197 /**@name Construction */
198 //@{
199 /**Create a file object but do not open it. It does not initially have a
200 valid file name. However, an attempt to open the file using the
201 #Open()# function will generate a unique temporary file.
203 PFile();
205 /**When a file is opened, it may restrict the access available to
206 operations on the object instance. A value from this enum is passed to
207 the #Open()# function to set the mode.
209 enum OpenMode {
210 /// File can be read but not written.
211 ReadOnly,
212 /// File can be written but not read.
213 WriteOnly,
214 /// File can be both read and written.
215 ReadWrite
218 /**When a file is opened, a number of options may be associated with the
219 open file. These describe what action to take on opening the file and
220 what to do on closure. A value from this enum is passed to the
221 #Open()# function to set the options.
223 The #ModeDefault# option will use the following values:
224 \begin{tabular}{rr}
225 Mode & Options \\
226 \hline
227 #ReadOnly# & #MustExist# \\
228 #WriteOnly# & #Create | Truncate# \\
229 #ReadWrite# & #Create# \\
230 \hline
231 \end{tabular}
233 enum OpenOptions {
234 /// File options depend on the OpenMode parameter.
235 ModeDefault = -1,
236 /// File open fails if file does not exist.
237 MustExist = 0,
238 /// File is created if it does not exist.
239 Create = 1,
240 /// File is set to zero length if it already exists.
241 Truncate = 2,
242 /// File open fails if file already exists.
243 Exclusive = 4,
244 /// File is temporary and is to be deleted when closed.
245 Temporary = 8,
246 /// File may not be read by another process.
247 DenySharedRead = 16,
248 /// File may not be written by another process.
249 DenySharedWrite = 32
252 /**Create a unique temporary file name, and open the file in the specified
253 mode and using the specified options. Note that opening a new, unique,
254 temporary file name in ReadOnly mode will always fail. This would only
255 be usefull in a mode and options that will create the file.
257 The #PChannel::IsOpen()# function may be used after object
258 construction to determine if the file was successfully opened.
260 PFile(
261 OpenMode mode, /// Mode in which to open the file.
262 int opts = ModeDefault /// #OpenOptions enum# for open operation.
265 /**Create a file object with the specified name and open it in the
266 specified mode and with the specified options.
268 The #PChannel::IsOpen()# function may be used after object
269 construction to determine if the file was successfully opened.
271 PFile(
272 const PFilePath & name, /// Name of file to open.
273 OpenMode mode = ReadWrite, /// Mode in which to open the file.
274 int opts = ModeDefault /// #OpenOptions enum# for open operation.
277 /// Close the file on destruction.
278 ~PFile();
279 //@}
282 /**@name Overrides from class PObject */
283 //@{
284 /**Determine the relative rank of the two objects. This is essentially the
285 string comparison of the #PFilePath# names of the files.
287 @return
288 relative rank of the file paths.
290 Comparison Compare(
291 const PObject & obj /// Other file to compare against.
292 ) const;
293 //@}
296 /**@name Overrides from class PChannel */
297 //@{
298 /**Get the platform and I/O channel type name of the channel. For example,
299 it would return the filename in #PFile# type channels.
301 @return
302 the name of the channel.
304 virtual PString GetName() const;
306 /**Low level read from the file channel. The read timeout is ignored for
307 file I/O. The GetLastReadCount() function returns the actual number
308 of bytes read.
310 The GetErrorCode() function should be consulted after Read() returns
311 FALSE to determine what caused the failure.
313 @return
314 TRUE indicates that at least one character was read from the channel.
315 FALSE means no bytes were read due to timeout or some other I/O error.
317 virtual BOOL Read(
318 void * buf, /// Pointer to a block of memory to receive the read bytes.
319 PINDEX len /// Maximum number of bytes to read into the buffer.
322 /**Low level write to the file channel. The write timeout is ignored for
323 file I/O. The GetLastWriteCount() function returns the actual number
324 of bytes written.
326 The GetErrorCode() function should be consulted after Write() returns
327 FALSE to determine what caused the failure.
329 @return TRUE if at least len bytes were written to the channel.
331 virtual BOOL Write(
332 const void * buf, /// Pointer to a block of memory to write.
333 PINDEX len /// Number of bytes to write.
336 /** Close the file channel.
337 @return TRUE if close was OK.
339 virtual BOOL Close();
340 //@}
343 /**@name File manipulation functions */
344 //@{
345 /**Check for file existance.
346 Determine if the file specified actually exists within the platforms
347 file system.
349 @return
350 TRUE if the file exists.
352 static BOOL Exists(
353 const PFilePath & name /// Name of file to see if exists.
356 /**Check for file existance.
357 Determine if the file path specification associated with the instance
358 of the object actually exists within the platforms file system.
360 @return
361 TRUE if the file exists.
363 BOOL Exists() const;
365 /**Check for file access modes.
366 Determine if the file specified may be opened in the specified mode. This would
367 check the current access rights to the file for the mode. For example,
368 for a file that is read only, using mode == ReadWrite would return
369 FALSE but mode == ReadOnly would return TRUE.
371 @return
372 TRUE if a file open would succeed.
374 static BOOL Access(
375 const PFilePath & name, /// Name of file to have its access checked.
376 OpenMode mode /// Mode in which the file open would be done.
379 /**Check for file access modes.
380 Determine if the file path specification associated with the
381 instance of the object may be opened in the specified mode. This would
382 check the current access rights to the file for the mode. For example,
383 for a file that is read only, using mode == ReadWrite would return
384 FALSE but mode == ReadOnly would return TRUE.
386 @return
387 TRUE if a file open would succeed.
389 BOOL Access(
390 OpenMode mode /// Mode in which the file open would be done.
393 /**Delete the specified file. If #force# is FALSE and the file
394 is protected against being deleted then the function fails. If
395 #force# is TRUE then the protection is ignored. What
396 constitutes file deletion protection is platform dependent, eg on DOS
397 is the Read Only attribute and on a Novell network it is a Delete
398 trustee right. Some protection may not be able to overridden with the
399 #force# parameter at all, eg on a Unix system and you are
400 not the owner of the file.
402 @return
403 TRUE if the file was deleted.
405 static BOOL Remove(
406 const PFilePath & name, // Name of file to delete.
407 BOOL force = FALSE // Force deletion even if file is protected.
410 /**Delete the current file. If #force# is FALSE and the file
411 is protected against being deleted then the function fails. If
412 #force# is TRUE then the protection is ignored. What
413 constitutes file deletion protection is platform dependent, eg on DOS
414 is the Read Only attribute and on a Novell network it is a Delete
415 trustee right. Some protection may not be able to overridden with the
416 #force# parameter at all, eg on a Unix system and you are
417 not the owner of the file.
419 @return
420 TRUE if the file was deleted.
422 BOOL Remove(
423 BOOL force = FALSE // Force deletion even if file is protected.
426 /**Change the specified files name. This does not move the file in the
427 directory hierarchy, it only changes the name of the directory entry.
429 The #newname# parameter must consist only of the file name
430 part, as returned by the #PFilePath::GetFileName()# function. Any
431 other file path parts will cause an error.
433 The first form uses the file path specification associated with the
434 instance of the object. The name within the instance is changed to the
435 new name if the function succeeds. The second static function uses an
436 arbitrary file specified by name.
438 @return
439 TRUE if the file was renamed.
441 static BOOL Rename(
442 const PFilePath & oldname, /// Old name of the file.
443 const PString & newname, /// New name for the file.
444 BOOL force = FALSE
445 /// Delete file if a destination exists with the same name.
448 /**Change the current files name.
449 This does not move the file in the
450 directory hierarchy, it only changes the name of the directory entry.
452 The #newname# parameter must consist only of the file name
453 part, as returned by the #PFilePath::GetFileName()# function. Any
454 other file path parts will cause an error.
456 The first form uses the file path specification associated with the
457 instance of the object. The name within the instance is changed to the
458 new name if the function succeeds. The second static function uses an
459 arbitrary file specified by name.
461 @return
462 TRUE if the file was renamed.
464 BOOL Rename(
465 const PString & newname, /// New name for the file.
466 BOOL force = FALSE
467 /// Delete file if a destination exists with the same name.
470 /**Make a copy of the specified file.
472 @return
473 TRUE if the file was renamed.
475 static BOOL Copy(
476 const PFilePath & oldname, /// Old name of the file.
477 const PFilePath & newname, /// New name for the file.
478 BOOL force = FALSE
479 /// Delete file if a destination exists with the same name.
482 /**Make a copy of the current file.
484 @return
485 TRUE if the file was renamed.
487 BOOL Copy(
488 const PFilePath & newname, /// New name for the file.
489 BOOL force = FALSE
490 /// Delete file if a destination exists with the same name.
493 /**Move the specified file. This will move the file from one position in
494 the directory hierarchy to another position. The actual operation is
495 platform dependent but the reslt is the same. For instance, for Unix,
496 if the move is within a file system then a simple rename is done, if
497 it is across file systems then a copy and a delete is performed.
499 @return
500 TRUE if the file was moved.
502 static BOOL Move(
503 const PFilePath & oldname, /// Old path and name of the file.
504 const PFilePath & newname, /// New path and name for the file.
505 BOOL force = FALSE
506 /// Delete file if a destination exists with the same name.
509 /**Move the current file. This will move the file from one position in
510 the directory hierarchy to another position. The actual operation is
511 platform dependent but the reslt is the same. For instance, for Unix,
512 if the move is within a file system then a simple rename is done, if
513 it is across file systems then a copy and a delete is performed.
515 @return
516 TRUE if the file was moved.
518 BOOL Move(
519 const PFilePath & newname, /// New path and name for the file.
520 BOOL force = FALSE
521 /// Delete file if a destination exists with the same name.
523 //@}
525 /**@name File channel functions */
526 //@{
527 /**Get the full path name of the file. The #PFilePath# object
528 describes the full file name specification for the particular platform.
530 @return
531 the name of the file.
533 const PFilePath & GetFilePath() const;
535 /**Set the full path name of the file. The #PFilePath# object
536 describes the full file name specification for the particular platform.
538 void SetFilePath(
539 const PString & path /// New file path.
543 /**Open the current file in the specified mode and with
544 the specified options. If the file object already has an open file then
545 it is closed.
547 If there has not been a filename attached to the file object (via
548 #SetFilePath()#, the #name# parameter or a previous
549 open) then a new unique temporary filename is generated.
551 @return
552 TRUE if the file was successfully opened.
554 virtual BOOL Open(
555 OpenMode mode = ReadWrite, // Mode in which to open the file.
556 int opts = ModeDefault // Options for open operation.
559 /**Open the specified file name in the specified mode and with
560 the specified options. If the file object already has an open file then
561 it is closed.
563 Note: if #mode# is StandardInput, StandardOutput or StandardError,
564 then the #name# parameter is ignored.
566 @return
567 TRUE if the file was successfully opened.
569 virtual BOOL Open(
570 const PFilePath & name, // Name of file to open.
571 OpenMode mode = ReadWrite, // Mode in which to open the file.
572 int opts = ModeDefault // #OpenOptions enum# for open operation.
575 /**Get the current size of the file.
577 @return
578 length of file in bytes.
580 virtual off_t GetLength() const;
582 /**Set the size of the file, padding with 0 bytes if it would require
583 expanding the file, or truncating it if being made shorter.
585 @return
586 TRUE if the file size was changed to the length specified.
588 virtual BOOL SetLength(
589 off_t len // New length of file.
592 /// Options for the origin in setting the file position.
593 enum FilePositionOrigin {
594 /// Set position relative to start of file.
595 Start = SEEK_SET,
596 /// Set position relative to current file position.
597 Current = SEEK_CUR,
598 /// Set position relative to end of file.
599 End = SEEK_END
602 /**Set the current active position in the file for the next read or write
603 operation. The #pos# variable is a signed number which is
604 added to the specified origin. For #origin == PFile::Start#
605 only positive values for #pos# are meaningful. For
606 #origin == PFile::End# only negative values for
607 #pos# are meaningful.
609 @return
610 TRUE if the new file position was set.
612 virtual BOOL SetPosition(
613 off_t pos, /// New position to set.
614 FilePositionOrigin origin = Start /// Origin for position change.
617 /**Get the current active position in the file for the next read or write
618 operation.
620 @return
621 current file position relative to start of file.
623 virtual off_t GetPosition() const;
625 /**Determine if the current file position is at the end of the file. If
626 this is TRUE then any read operation will fail.
628 @return
629 TRUE if at end of file.
631 BOOL IsEndOfFile() const;
633 /**Get information (eg protection, timestamps) on the specified file.
635 @return
636 TRUE if the file info was retrieved.
638 static BOOL GetInfo(
639 const PFilePath & name, // Name of file to get the information on.
640 PFileInfo & info
641 // #PFileInfo# structure to receive the information.
644 /**Get information (eg protection, timestamps) on the current file.
646 @return
647 TRUE if the file info was retrieved.
649 BOOL GetInfo(
650 PFileInfo & info
651 // #PFileInfo# structure to receive the information.
654 /**Set permissions on the specified file.
656 @return
657 TRUE if the file was renamed.
659 static BOOL SetPermissions(
660 const PFilePath & name, // Name of file to change the permission of.
661 int permissions // New permissions mask for the file.
663 /**Set permissions on the current file.
665 @return
666 TRUE if the file was renamed.
668 BOOL SetPermissions(
669 int permissions // New permissions mask for the file.
671 //@}
673 protected:
674 // Member variables
675 /// The fully qualified path name for the file.
676 PFilePath path;
678 /// File is to be removed when closed.
679 BOOL removeOnClose;
682 // Include platform dependent part of class
683 #ifdef _WIN32
684 #include "msos/ptlib/file.h"
685 #else
686 #include "unix/ptlib/file.h"
687 #endif
690 #endif
692 // End Of File ///////////////////////////////////////////////////////////////