PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pdirect.h
Go to the documentation of this file.
1 /*
2  * pdirect.h
3  *
4  * File system directory class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
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/
14  *
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.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 
31 #ifndef PTLIB_DIRECTORY_H
32 #define PTLIB_DIRECTORY_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
39 
40 
41 #ifdef Fifo
42 #undef Fifo
43 #endif
44 
45 #ifdef _WIN32
46 #define PDIR_SEPARATOR '\\'
47 #define PPATH_SEPARATOR ';' // As used in PATH environment variable
48 const PINDEX P_MAX_PATH = _MAX_PATH;
50 #else
51 #define PDIR_SEPARATOR '/'
52 #define PPATH_SEPARATOR ':' // As used in PATH environment variable
53 #define P_MAX_PATH (_POSIX_PATH_MAX)
55 #endif
56 
57 
59 // File System
60 
65 class PFileInfo : public PObject
66 {
67  PCLASSINFO(PFileInfo, PObject);
68 
69  public:
74  P_DECLARE_BITWISE_ENUM_EX(FileTypes, 10,
75  (
76  NoFileType, // Should not happen!
77 
78  RegularFile,
79  SymbolicLink,
80  SubDirectory,
81  ParentDirectory,
82  CurrentDirectory,
83  CharDevice,
84  BlockDevice,
85  Fifo,
86  SocketDevice,
87  UnknownFileType
88  ),
89  AllFiles = 0x3ff,
90  DefaultSearch = RegularFile | SymbolicLink | SubDirectory
91  );
92 
94  FileTypes type;
95 
100 
103 
108 
112  PUInt64 size;
113 
115  P_DECLARE_BITWISE_ENUM_EX(Permissions,9,
116  (
117  // Note the following bits are as per unixish / C standard lib values, do not change!
118  NoPermissions,
119  WorldExecute,
120  WorldWrite,
121  WorldRead,
122  GroupExecute,
123  GroupWrite,
124  GroupRead,
125  UserExecute,
126  UserWrite,
127  UserRead
128  ),
129  AllPermissions = 0x1ff,
130  DefaultPerms = UserRead|UserWrite|GroupRead|WorldRead,
131  DefaultDirPerms = DefaultPerms|UserExecute|GroupExecute|WorldExecute
132  );
133 
139  Permissions permissions;
140 
145  bool hidden;
146 };
147 
148 
174 {
175  PCONTAINERINFO(PDirectory, PFilePathString);
176 
177  public:
180  PDirectory();
182 
187  PDirectory(
188  const char * cpathname
189  );
190 
195  PDirectory(
196  const PString & pathname
197  );
198 
202  const PString & pathname
203  );
204 
208  const char * cpathname
209  );
211 
221  PDirectory GetParent() const;
222 
233  PFilePathString GetVolume() const;
234 
240  bool IsRoot() const;
241 
247  PDirectory GetRoot() const;
248 
253  PStringArray GetPath() const;
254 
261  PINLINE static bool IsSeparator(
262  char ch
263  );
264 
275  bool GetVolumeSpace(
276  PInt64 & total,
277  PInt64 & free,
278  DWORD & clusterSize
279  ) const;
281 
289  bool Exists() const;
290 
296  static bool Exists(
297  const PString & path
298  );
299 
305  bool Change() const;
306 
312  static bool Change(
313  const PString & path
314  );
315 
321  bool Create(
322  int perm = PFileInfo::DefaultDirPerms, // Permission on new directory.
323  bool recurse = false
324  ) const;
330  static bool Create(
331  const PString & p,
332  int perm = PFileInfo::DefaultDirPerms, // Permission on new directory.
333  bool recurse = false
334  );
335 
341  bool Remove();
342 
348  static bool Remove(
349  const PString & path
350  );
351 
353  static PDirectory GetTemporary();
355 
373  virtual bool Open(
374  PFileInfo::FileTypes scanMask = PFileInfo::DefaultSearch
375  );
376 
391  virtual bool Restart(
392  PFileInfo::FileTypes scanMask = PFileInfo::DefaultSearch
393  );
394 
406  virtual bool Next();
407 
409  virtual void Close();
410 
425  virtual PFilePathString GetEntryName() const;
426 
436  virtual bool IsSubDir() const;
437 
443  virtual bool GetInfo(
444  PFileInfo & info
445  ) const;
446 
447  struct Entry : PFileInfo {
449  };
450  typedef std::vector<Entry> Entries;
451 
452  P_DECLARE_ENUM(Sorting,
453  Unsorted,
454  SortByType,
455  SortByName,
456  SortBySize,
457  SortByCreated,
458  SortByModified,
459  SortByAccessed,
460  SortByPermission
461  );
462 
465  bool GetEntries(
466  Entries & entries,
467  Sorting sortBy = Unsorted
468  );
469  bool GetEntries(
470  Entries & entries,
471  const PCaselessString & sortBy
472  );
474 
475 
476  protected:
477  // New functions for class
478  void Construct();
479  void Destruct()
481 
482  // Member variables
484  PFileInfo::FileTypes m_scanMask;
485 
486 // Include platform dependent part of class
487 #ifdef _WIN32
488 #include "msos/ptlib/pdirect.h"
489 #else
490 #include "unix/ptlib/pdirect.h"
491 #endif
492 
493 };
494 
495 
496 #endif // PTLIB_DIRECTORY_H
497 
498 
499 // End Of File ///////////////////////////////////////////////////////////////
virtual bool Restart(PFileInfo::FileTypes scanMask=PFileInfo::DefaultSearch)
Restart file list scan from the beginning of directory.
FileTypes type
File type for this file. Only one bit is set at a time here.
Definition: pdirect.h:94
virtual void Close()
Close the directory during or after a file list scan.
void Destruct()
Internal function called from container destructors.
std::vector< Entry > Entries
Definition: pdirect.h:450
void Construct()
PDirectory & operator=(const PString &pathname)
Set the directory to the specified path.
bool GetVolumeSpace(PInt64 &total, PInt64 &free, DWORD &clusterSize) const
Determine the total number of bytes and number of bytes free on the volume that this directory is con...
bool Exists() const
Test for if the directory exists.
This class defines an absolute time and date.
Definition: ptime.h:49
bool Create(int perm=PFileInfo::DefaultDirPerms, bool recurse=false) const
Create a new directory with the specified permissions.
bool IsRoot() const
Determine if the directory is the root directory of a volume.
PTime accessed
Time of last access to the file.
Definition: pdirect.h:107
#define PINLINE
Definition: object.h:194
PString m_name
Definition: pdirect.h:448
virtual PFilePathString GetEntryName() const
Get the name (without the volume or directory path) of the current entry in the directory scan...
void Destruct()
Definition: pdirect.h:479
This class is a variation of a string that ignores case.
Definition: pstring.h:2012
static PINLINE bool IsSeparator(char ch)
Determine if the character ch is a directory path separator.
This is an array collection class of PString objects.
Definition: pstring.h:2365
P_DECLARE_BITWISE_ENUM_EX(FileTypes, 10,(NoFileType, RegularFile, SymbolicLink, SubDirectory, ParentDirectory, CurrentDirectory, CharDevice, BlockDevice, Fifo, SocketDevice, UnknownFileType), AllFiles=0x3ff, DefaultSearch=RegularFile|SymbolicLink|SubDirectory)
All types that a particular file path may be.
PDirectory GetParent() const
Get the directory for the parent to the current directory.
#define P_MAX_PATH
Definition: pdirect.h:53
bool hidden
File is a hidden file.
Definition: pdirect.h:145
bool Change() const
Change the current working directory to the objects location.
Class to represent a directory in the operating system file system.
Definition: pdirect.h:173
P_DECLARE_ENUM(Sorting, Unsorted, SortByType, SortByName, SortBySize, SortByCreated, SortByModified, SortByAccessed, SortByPermission)
PTime modified
Time of last modifiaction of the file.
Definition: pdirect.h:102
#define free(p)
Override of system call for memory check system.
Definition: object.h:1870
PTime created
Time of file creation of the file.
Definition: pdirect.h:99
PString PFilePathString
Definition: pdirect.h:54
virtual bool IsSubDir() const
Determine if the directory entry currently being scanned is itself another directory entry...
PFileInfo::FileTypes m_scanMask
Mask of file types that the directory scan will return.
Definition: pdirect.h:484
The character string class.
Definition: pstring.h:108
PDirectory GetRoot() const
Get the root directory of a volume.
Base string type for a file path.
Definition: filepath.h:44
virtual bool GetInfo(PFileInfo &info) const
Get file information on the current directory entry.
static PDirectory GetTemporary()
Get the OS temporary directory.
Permissions permissions
A bit mask of all the file acces permissions.
Definition: pdirect.h:139
PUInt64 size
Size of the file in bytes.
Definition: pdirect.h:112
virtual bool Next()
Move to the next file in the directory scan.
PStringArray GetPath() const
Get the directory path as an array of strings.
virtual bool Open(PFileInfo::FileTypes scanMask=PFileInfo::DefaultSearch)
Open the directory for scanning its list of files.
Definition: pdirect.h:447
PDirectory()
Create a directory object of the current working directory.
bool Remove()
Delete the directory.
Class containing the system information on a file path.
Definition: pdirect.h:65
PFilePathString GetVolume() const
Get the volume name that the directory is in.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
bool GetEntries(Entries &entries, Sorting sortBy=Unsorted)
Get all the entries in this directory.