My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
DirList.h
Go to the documentation of this file.
1 // $Id: DirList.h,v 1.1.1.1 2009/07/06 23:57:25 eml Exp $
2 
3 #ifndef GCP_UTIL_DIRLIST_H
4 #define GCP_UTIL_DIRLIST_H
5 
16 #include <iostream>
17 #include <sstream>
18 #include <list>
19 
20 namespace gcp {
21  namespace util {
22 
23  class DirList {
24  public:
25 
26  enum EntryType {
27  TYPE_FILE = 1,
28  TYPE_DIR = 2,
29  TYPE_PIPE = 4,
30  TYPE_LINK = 8,
31  TYPE_ANY = TYPE_FILE | TYPE_DIR | TYPE_PIPE,
32  };
33 
34  // Enumerate an orthogonal set of file permissions that can be
35  // tested for by testPathname()
36 
37  enum EntryRights {
38  ENTRY_NONE = 0,
39  ENTRY_READ = 1, /* Test if the file is readable */
40  ENTRY_WRITE = 2, /* Test if the file is writable */
41  ENTRY_EXE = 4, /* Test if the file is executable */
42  ENTRY_OK = 8 /* Test if the file exists, regardless of permissions */
43  };
44 
45  struct DirEnt {
46  EntryType type_; // Type of this entry
47  EntryRights rights_; // Access of this entry
48  std::string name_; // Name of this entry
49  std::string path_; // Path to this entry, relative to the top directory
50 
51  // If this entry is a directory, this list will contain all
52  // nodes below it
53 
54  std::list<DirEnt> entries_;
55 
56  DirEnt(std::string name, std::string path, EntryType type, EntryRights rights) {
57  name_ = name;
58  path_ = path;
59  type_ = type;
60  rights_ = rights;
61  }
62 
63  bool isDir() {
64  return (unsigned)(type_) & TYPE_DIR;
65  }
66 
67  bool isLink() {
68  return (unsigned)(type_) & TYPE_LINK;
69  }
70 
71  bool isFile() {
72  return (unsigned)(type_) & TYPE_FILE;
73  }
74 
75  bool isReadable() {
76  return (unsigned)(rights_) & ENTRY_READ;
77  }
78 
79  std::string pathName() {
80  std::ostringstream os;
81  os << path_ << "/";
82  return os.str();
83  }
84 
85  std::string fullName() {
86  std::ostringstream os;
87  os << path_ << "/" << name_;
88  return os.str();
89  }
90  };
91 
92  friend std::ostream& operator<<(std::ostream& os, DirEnt& entry);
93 
97  DirList(std::string path, bool descend);
98 
102  DirList(const DirList& objToBeCopied);
103 
107  DirList(DirList& objToBeCopied);
108 
112  void operator=(const DirList& objToBeAssigned);
113 
117  void operator=(DirList& objToBeAssigned);
118 
122  friend std::ostream& operator<<(std::ostream& os, DirList& obj);
123 
127  virtual ~DirList();
128 
129  // Get a listing of the specified directory, and optionally
130  // descend into subdirectories
131 
132  void listEntries();
133 
134  std::list<DirEnt> getFiles(bool includeSymlinks=false);
135  std::list<DirEnt> getDirs(bool includeSymlinks=false);
136 
137  void listEntries(std::list<DirEnt>& entries);
138 
139  private:
140 
141  // The list of entries in this directory
142 
143  std::list<DirEnt> entries_;
144 
145  void findEntries(std::list<DirEnt>& entries_, std::string path,
146  bool descend);
147 
148  // Check whether a pathname refers to a file with selected
149  // attributes.
150 
151  bool testPathname(std::string path, EntryType type, unsigned rights);
152 
153  // Check whether a pathname refers to a file with selected
154  // attributes.
155 
156  EntryType getType(std::string path, bool link=false);
157 
158  // Get the access rights to this entry
159 
160  DirList::EntryRights getRights(std::string path);
161 
162  void getFiles(std::list<DirEnt>& files,
163  std::list<DirEnt>& entries, bool includeSymlinks);
164 
165  void getDirs(std::list<DirEnt>& dirs,
166  std::list<DirEnt>& entries, bool includeSymlinks);
167 
168  }; // End class DirList
169 
170  } // End namespace util
171 } // End namespace gcp
172 
173 
174 
175 #endif // End #ifndef GCP_UTIL_DIRLIST_H
Definition: DirList.h:45
virtual ~DirList()
Definition: DirList.cc:68
void operator=(const DirList &objToBeAssigned)
Definition: DirList.cc:43
void listEntries()
Definition: DirList.cc:263
Definition: DirList.h:23
DirList(std::string path, bool descend)
Definition: DirList.cc:19