CARMA C++
DirList.h
Go to the documentation of this file.
1 // $Id: DirList.h,v 1.1 2010/12/13 21:06:30 eml Exp $
2 
3 #ifndef SZA_UTIL_DIRLIST_H
4 #define SZA_UTIL_DIRLIST_H
5 
16 #include <iostream>
17 #include <sstream>
18 #include <list>
19 
20 namespace sza {
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  volatile EntryType type_; // Type of this entry
47  volatile 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,
57  EntryRights rights) {
58  name_ = name;
59  path_ = path;
60  type_ = type;
61  rights_ = rights;
62  }
63 
64  bool isDir() {
65  return (unsigned)(type_) & TYPE_DIR;
66  }
67 
68  bool isLink() {
69  return (unsigned)(type_) & TYPE_LINK;
70  }
71 
72  bool isFile() {
73  return (unsigned)(type_) & TYPE_FILE;
74  }
75 
76  bool isReadable() {
77  return (unsigned)(rights_) & ENTRY_READ;
78  }
79 
80  std::string pathName() {
81  std::ostringstream os;
82  os << path_ << "/";
83  return os.str();
84  }
85 
86  std::string fullName() {
87  std::ostringstream os;
88  os << path_ << "/" << name_;
89  return os.str();
90  }
91  };
92 
93  friend std::ostream& operator<<(std::ostream& os, DirEnt& entry);
94 
98  DirList(std::string path, bool descend);
99 
103  DirList(const DirList& objToBeCopied);
104 
108  DirList(DirList& objToBeCopied);
109 
113  void operator=(const DirList& objToBeAssigned);
114 
118  void operator=(DirList& objToBeAssigned);
119 
123  friend std::ostream& operator<<(std::ostream& os, DirList& obj);
124 
128  virtual ~DirList();
129 
130  // Get a listing of the specified directory, and optionally
131  // descend into subdirectories
132 
133  void listEntries();
134 
135  std::list<DirEnt> getFiles(bool includeSymlinks=false);
136  std::list<DirEnt> getDirs(bool includeSymlinks=false);
137 
138  void listEntries(std::list<DirEnt>& entries);
139 
140  private:
141 
142  // The list of entries in this directory
143 
144  std::list<DirEnt> entries_;
145 
146  void findEntries(std::list<DirEnt>& entries_, std::string path,
147  bool descend);
148 
149  // Check whether a pathname refers to a file with selected
150  // attributes.
151 
152  bool testPathname(std::string path, EntryType type, unsigned rights);
153 
154  // Check whether a pathname refers to a file with selected
155  // attributes.
156 
157  EntryType getType(std::string path, bool link=false);
158 
159  // Get the access rights to this entry
160 
161  DirList::EntryRights getRights(std::string path);
162 
163  void getFiles(std::list<DirEnt>& files,
164  std::list<DirEnt>& entries, bool includeSymlinks);
165 
166  void getDirs(std::list<DirEnt>& dirs,
167  std::list<DirEnt>& entries, bool includeSymlinks);
168 
169  }; // End class DirList
170 
171  } // End namespace util
172 } // End namespace sza
173 
174 
175 
176 #endif // End #ifndef SZA_UTIL_DIRLIST_H