More factory changes to help with DLL
[pwlib.git] / include / ptlib / pdirect.h
blob9896fa4a166f63de03030341d9b96f7a44e6ecde
1 /*
2 * pdirect.h
4 * File system directory 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.41 2003/09/17 05:41:58 csoutheren
31 * Removed recursive includes
33 * Revision 1.40 2003/09/17 01:18:02 csoutheren
34 * Removed recursive include file system and removed all references
35 * to deprecated coooperative threading support
37 * Revision 1.39 2003/06/06 15:04:17 dsandras
39 * Fixed compilation warning with gcc 3.3 by removing the PINLINE
41 * Revision 1.38 2002/11/20 00:13:43 robertj
42 * Fixed some documentation
44 * Revision 1.37 2002/11/19 12:07:02 robertj
45 * Added function to get root directory.
47 * Revision 1.36 2002/11/19 10:34:59 robertj
48 * Added function to extract a path as an array of directories components.
50 * Revision 1.35 2002/09/16 01:08:59 robertj
51 * Added #define so can select if #pragma interface/implementation is used on
52 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
54 * Revision 1.34 2001/05/22 12:49:32 robertj
55 * Did some seriously wierd rewrite of platform headers to eliminate the
56 * stupid GNU compiler warning about braces not matching.
58 * Revision 1.33 2001/02/13 06:55:21 robertj
59 * Fixed problem with operator= in PDirectory class, part of larger change previously made.
61 * Revision 1.32 2000/06/26 11:17:19 robertj
62 * Nucleus++ port (incomplete).
64 * Revision 1.31 2000/04/03 18:41:27 robertj
65 * Fixed BeOS compatibility problem with openlog() function.
67 * Revision 1.30 1999/03/09 02:59:50 robertj
68 * Changed comments to doc++ compatible documentation.
70 * Revision 1.29 1999/02/16 08:11:09 robertj
71 * MSVC 6.0 compatibility changes.
73 * Revision 1.28 1998/09/23 06:21:06 robertj
74 * Added open source copyright license.
76 * Revision 1.27 1998/03/05 12:44:34 robertj
77 * Added cluster size.
79 * Revision 1.26 1997/03/31 11:34:00 robertj
80 * Fixed default permissions for directories \b, different from that for files.
82 * Revision 1.25 1997/01/12 04:22:21 robertj
83 * Added function to get disk size and free space.
85 * Revision 1.24 1995/12/23 03:45:31 robertj
86 * Added constructor for C string literals.
88 * Revision 1.23 1995/11/09 12:17:23 robertj
89 * Added platform independent base type access classes.
91 * Revision 1.22 1995/10/14 15:02:22 robertj
92 * Added function to get parent directory.
94 * Revision 1.21 1995/06/17 11:12:52 robertj
95 * Documentation update.
97 * Revision 1.20 1995/03/14 12:42:00 robertj
98 * Updated documentation to use HTML codes.
100 * Revision 1.19 1995/03/12 04:42:48 robertj
101 * Updated documentation.
102 * Changed return type of functions to the correct case string.
104 * Revision 1.18 1995/02/22 10:50:33 robertj
105 * Changes required for compiling release (optimised) version.
107 * Revision 1.17 1995/01/06 10:42:25 robertj
108 * Moved identifiers into different scope.
109 * Changed file size to 64 bit integer.
110 * Documentation
112 * Revision 1.16 1994/10/24 00:07:03 robertj
113 * Changed PFilePath and PDirectory so descends from either PString or
114 * PCaselessString depending on the platform.
116 * Revision 1.15 1994/10/23 04:49:25 robertj
117 * Chnaged PDirectory to descend of PString.
118 * Added PDirectory Exists() function.
120 * Revision 1.14 1994/08/23 11:32:52 robertj
121 * Oops
123 * Revision 1.13 1994/08/22 00:46:48 robertj
124 * Added pragma fro GNU C++ compiler.
126 * Revision 1.12 1994/06/25 11:55:15 robertj
127 * Unix version synchronisation.
129 * Revision 1.11 1994/04/20 12:17:44 robertj
130 * Split name into PFilePath
132 * Revision 1.10 1994/04/11 14:16:27 robertj
133 * Added function for determining if character is a valid directory separator.
135 * Revision 1.9 1994/04/01 14:14:57 robertj
136 * Put platform independent file permissions and type codes back.
138 * Revision 1.7 1994/01/13 03:17:55 robertj
139 * Added functions to get the name of the volume the directory is contained in
140 * and a function to determine if the directory is a root directory of the
141 * volume (ie is does not have a parent directory).
143 * Revision 1.6 1994/01/03 04:42:23 robertj
144 * Mass changes to common container classes and interactors etc etc etc.
146 * Revision 1.5 1993/12/31 06:45:38 robertj
147 * Made inlines optional for debugging purposes.
149 * Revision 1.4 1993/08/21 01:50:33 robertj
150 * Made Clone() function optional, default will assert if called.
152 * Revision 1.3 1993/07/14 12:49:16 robertj
153 * Fixed RCS keywords.
158 #ifndef _PDIRECTORY
159 #define _PDIRECTORY
161 #ifdef P_USE_PRAGMA
162 #pragma interface
163 #endif
165 #ifdef Fifo
166 #undef Fifo
167 #endif
169 #ifdef _WIN32
170 #define PDIR_SEPARATOR '\\'
171 const PINDEX P_MAX_PATH = _MAX_PATH;
172 typedef PCaselessString PFilePathString;
173 #else
174 #define PDIR_SEPARATOR '/'
175 #define P_MAX_PATH (_POSIX_PATH_MAX)
176 typedef PString PFilePathString;
177 #endif
179 ///////////////////////////////////////////////////////////////////////////////
180 // File System
182 /**Class containing the system information on a file path. Information can be
183 obtained on any directory entry event if it is not a "file" in the strictest
184 sense. Sub-directories, devices etc may also have information retrieved.
186 class PFileInfo : public PObject
188 PCLASSINFO(PFileInfo, PObject);
190 public:
191 /**All types that a particular file path may be. Not all platforms support
192 all of the file types. For example under DOS no file may be of the
193 type #SymbolicLink#.
195 enum FileTypes {
196 /// Ordinary disk file.
197 RegularFile = 1,
198 /// File path is a symbolic link.
199 SymbolicLink = 2,
200 /// File path is a sub-directory
201 SubDirectory = 4,
202 /// File path is a character device name.
203 CharDevice = 8,
204 /// File path is a block device name.
205 BlockDevice = 16,
206 /// File path is a fifo (pipe) device.
207 Fifo = 32,
208 /// File path is a socket device.
209 SocketDevice = 64,
210 /// File path is of an unknown type.
211 UnknownFileType = 256,
212 /// Mask for all file types.
213 AllFiles = 0x1ff
216 /// File type for this file. Only one bit is set at a time here.
217 FileTypes type;
219 /**Time of file creation of the file. Not all platforms support a separate
220 creation time in which case the last modified time is returned.
222 PTime created;
224 /// Time of last modifiaction of the file.
225 PTime modified;
227 /**Time of last access to the file. Not all platforms support a separate
228 access time in which case the last modified time is returned.
230 PTime accessed;
232 /**Size of the file in bytes. This is a quadword or 8 byte value to allow
233 for files greater than 4 gigabytes.
235 PUInt64 size;
237 /// File access permissions for the file.
238 enum Permissions {
239 /// File has world execute permission
240 WorldExecute = 1,
241 /// File has world write permission
242 WorldWrite = 2,
243 /// File has world read permission
244 WorldRead = 4,
245 /// File has group execute permission
246 GroupExecute = 8,
247 /// File has group write permission
248 GroupWrite = 16,
249 /// File has group read permission
250 GroupRead = 32,
251 /// File has owner execute permission
252 UserExecute = 64,
253 /// File has owner write permission
254 UserWrite = 128,
255 /// File has owner read permission
256 UserRead = 256,
257 /// All possible permissions.
258 AllPermissions = 0x1ff,
259 /// Owner read & write plus group and world read permissions.
260 DefaultPerms = UserRead|UserWrite|GroupRead|WorldRead,
261 /// Owner read & write & execute plus group and world read & exectute permissions.
262 DefaultDirPerms = DefaultPerms|UserExecute|GroupExecute|WorldExecute
266 /**A bit mask of all the file acces permissions. See the
267 #Permissions enum# for the possible bit values.
269 Not all platforms support all permissions.
271 int permissions;
273 /**File is a hidden file. What constitutes a hidden file is platform
274 dependent, for example under unix it is a file beginning with a '.'
275 character while under MS-DOS there is a file system attribute for it.
277 BOOL hidden;
281 /**Class to represent a directory in the operating system file system. A
282 directory is a special file that contains a list of file paths.
284 The directory paths are highly platform dependent and a minimum number of
285 assumptions should be made.
287 The PDirectory object is a string consisting of a possible volume name, and
288 a series directory names in the path from the volumes root to the directory
289 that the object represents. Each directory is separated by the platform
290 dependent separator character which is defined by the PDIR_SEPARATOR macro.
291 The path always has a trailing separator.
293 Some platforms allow more than one character to act as a directory separator
294 so when doing any processing the #IsSeparator()# function should be
295 used to determine if a character is a possible separator.
297 The directory may be opened to gain access to the list of files that it
298 contains. Note that the directory does {\bf not} contain the "." and ".."
299 entries that some platforms support.
301 The ancestor class is dependent on the platform. For file systems that are
302 case sensitive, eg Unix, the ancestor is #PString#. For other
303 platforms, the ancestor class is #PCaselessString#.
305 class PDirectory : public PFilePathString
307 PCONTAINERINFO(PDirectory, PFilePathString);
309 public:
310 /**@name Construction */
311 //@{
312 /// Create a directory object of the current working directory
313 PDirectory();
315 /**Create a directory object of the specified directory. The
316 #pathname# parameter may be a relative directory which is
317 made absolute by the creation of the #PDirectory# object.
319 PDirectory(
320 const char * cpathname /// Directory path name for new object.
323 /**Create a directory object of the specified directory. The
324 #pathname# parameter may be a relative directory which is
325 made absolute by the creation of the #PDirectory# object.
327 PDirectory(
328 const PString & pathname /// Directory path name for new object.
331 /**Set the directory to the specified path.
333 PDirectory & operator=(
334 const PString & pathname /// Directory path name for new object.
337 /**Set the directory to the specified path.
339 PDirectory & operator=(
340 const char * cpathname /// Directory path name for new object.
342 //@}
344 /**@name Access functions */
345 //@{
346 /**Get the directory for the parent to the current directory. If the
347 directory is already the root directory it returns the root directory
348 again.
350 @return
351 parent directory.
353 PDirectory GetParent() const;
355 /**Get the volume name that the directory is in.
357 This is platform dependent, for example for MS-DOS it is the 11
358 character volume name for the drive, eg "DOS_DISK", and for Macintosh it
359 is the disks volume name eg "Untitled". For a unix platform it is the
360 device name for the file system eg "/dev/sda1".
362 @return
363 string for the directory volume.
365 PFilePathString GetVolume() const;
367 /**Determine if the directory is the root directory of a volume.
369 @return
370 TRUE if the object is a root directory.
372 BOOL IsRoot() const;
374 /**Get the root directory of a volume.
376 @return
377 root directory.
379 PDirectory GetRoot() const;
381 /**Get the directory path as an array of strings.
382 The first element in the array is the volume string, eg under Win32 it
383 is "c:" or "\\machine", while under unix it is an empty string.
385 PStringArray GetPath() const;
387 /**Determine if the character #ch# is a directory path
388 separator.
390 @return
391 TRUE if may be used to separate directories in a path.
393 PINLINE static BOOL IsSeparator(
394 char ch /// Character to check as being a separator.
397 /**Determine the total number of bytes and number of bytes free on the
398 volume that this directory is contained on.
400 Note that the free space will be the physical limit and if user quotas
401 are in force by the operating system, the use may not actually be able
402 to use all of these bytes.
404 @return
405 TRUE if the information could be determined.
407 BOOL GetVolumeSpace(
408 PInt64 & total, /// Total number of bytes available on volume
409 PInt64 & free, /// Number of bytes unused on the volume
410 DWORD & clusterSize /// "Quantisation factor" in bytes for files on volume
411 ) const;
412 //@}
414 /**@name File system functions */
415 //@{
416 /**Test for if the directory exists.
418 @return
419 TRUE if directory exists.
421 BOOL Exists() const;
423 /**Test for if the specified directory exists.
425 @return
426 TRUE if directory exists.
428 static BOOL Exists(
429 const PString & path /// Directory file path.
432 /**Change the current working directory to the objects location.
434 @return
435 TRUE if current working directory was changed.
437 BOOL Change() const;
439 /**Change the current working directory to that specified..
441 @return
442 TRUE if current working directory was changed.
444 static BOOL Change(
445 const PString & path /// Directory file path.
448 /**Create a new directory with the specified permissions.
450 @return
451 TRUE if directory created.
453 BOOL Create(
454 int perm = PFileInfo::DefaultDirPerms // Permission on new directory.
455 ) const;
456 /**Create a new directory as specified with the specified permissions.
458 @return
459 TRUE if directory created.
461 static BOOL Create(
462 const PString & p, /// Directory file path.
463 int perm = PFileInfo::DefaultDirPerms /// Permission on new directory.
466 /**Delete the directory.
468 @return
469 TRUE if directory was deleted.
471 BOOL Remove();
473 /**Delete the specified directory.
475 @return
476 TRUE if directory was deleted.
478 static BOOL Remove(
479 const PString & path /// Directory file path.
481 //@}
483 /**@name Directory listing functions */
484 //@{
485 /**Open the directory for scanning its list of files. Once opened the
486 #GetEntryName()# function may be used to get the current directory
487 entry and the #Next()# function used to move to the next directory
488 entry.
490 Only files that are of a type that is specified in the mask will be
491 returned.
493 Note that the directory scan will {\bf not} return the "." and ".."
494 entries that some platforms support.
496 @return
497 TRUE if directory was successfully opened, and there was at least one
498 file in it of the specified types.
500 BOOL Open(
501 int scanMask = PFileInfo::AllFiles /// Mask of files to provide.
504 /**Restart file list scan from the beginning of directory. This is similar
505 to the #Open()# command but does not require that the directory be
506 closed (using #Close()#) first.
508 Only files that are of a type that is specified in the mask will be
509 returned.
511 Note that the directory scan will {\bf not} return the "." and ".."
512 entries that some platforms support.
514 @return
515 TRUE if directory was successfully opened, and there was at least one
516 file in it of the specified types.
518 BOOL Restart(
519 int scanMask = PFileInfo::AllFiles /// Mask of files to provide.
522 /**Move to the next file in the directory scan.
524 Only files that are of a type that is specified in the mask passed to
525 the #Open()# or #Restart()# functions will be returned.
527 Note that the directory scan will {\bf not} return the "." and ".."
528 entries that some platforms support.
530 @return
531 TRUE if there is another valid file in the directory.
533 BOOL Next();
535 /// Close the directory during or after a file list scan.
536 void Close();
538 /**Get the name (without the volume or directory path) of the current
539 entry in the directory scan. This may be the name of a file or a
540 subdirectory or even a link or device for operating systems that support
541 them.
543 To get a full path name concatenate the PDirectory object itself with
544 the entry name.
546 Note that the directory scan will {\bf not} return the "." and ".."
547 entries that some platforms support.
549 @return
550 string for directory entry.
552 PFilePathString GetEntryName() const;
554 /**Determine if the directory entry currently being scanned is itself
555 another directory entry.
557 Note that the directory scan will {\bf not} return the "." and ".."
558 entries that some platforms support.
560 @return
561 TRUE if entry is a subdirectory.
563 BOOL IsSubDir() const;
565 /**Get file information on the current directory entry.
567 @return
568 TRUE if file information was successfully retrieved.
570 BOOL GetInfo(
571 PFileInfo & info /// Object to receive the file information.
572 ) const;
573 //@}
576 protected:
577 // New functions for class
578 void Construct();
579 // Common constructor code
582 // Member variables
583 /// Mask of file types that the directory scan will return.
584 int scanMask;
586 // Include platform dependent part of class
587 #ifdef _WIN32
588 #include "msos/ptlib/pdirect.h"
589 #else
590 #include "unix/ptlib/pdirect.h"
591 #endif
595 #endif
597 // End Of File ///////////////////////////////////////////////////////////////