gdirectory.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_DIRECTORY_H
22 #define G_DIRECTORY_H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gexception.h"
27 #include <string>
28 #include <vector>
29 #include <sys/types.h>
30 
32 namespace G
33 {
34  class DirectoryIteratorImp ;
35  class Directory ;
36  class DirectoryIterator ;
37  class DirectoryList ;
38 }
39 
47 {
48 public:
49  Directory() ;
51 
52  explicit Directory( const char * path ) ;
54 
55  explicit Directory( const Path & path ) ;
57 
58  explicit Directory( const std::string & path ) ;
60 
61  ~Directory() ;
63 
64  bool valid( bool for_creating_files = false ) const ;
73 
74  bool writeable( std::string probe_filename = tmp() ) const ;
78 
79  Path path() const ;
81 
82  Directory( const Directory & other ) ;
84 
85  Directory & operator=( const Directory & ) ;
87 
88  static Directory root() ;
91 
92  static std::string tmp() ;
97 
98 private:
99  Path m_path ;
100 } ;
101 
109 {
110 public:
111  DirectoryIterator( const Directory & dir , const std::string & wc ) ;
119 
120  explicit DirectoryIterator( const Directory & dir ) ;
123 
126 
127  bool error() const ;
129 
130  bool more() ;
132 
133  bool isDir() const ;
135 
136  std::string modificationTimeString() const ;
139 
140  std::string sizeString() const ;
143 
144  Path filePath() const ;
146 
147  Path fileName() const ;
149 
150 private:
151  DirectoryIterator( const DirectoryIterator & ) ; // not implemented
152  void operator=( const DirectoryIterator & ) ; // not implemented
153 
154 private:
155  DirectoryIteratorImp * m_imp ;
156 } ;
157 
165 {
166 public:
167  DirectoryList() ;
170 
171  void readAll( const Path & dir ) ;
175 
176  void readType( const Path & dir , const std::string & suffix , unsigned int limit = 0U ) ;
180 
181  bool more() ;
183 
184  bool isDir() const ;
186 
187  G::Path filePath() const ;
189 
190  G::Path fileName() const ;
192 
193 private:
194  DirectoryList( const DirectoryList & ) ;
195  void operator=( const DirectoryList & ) ;
196 
197 private:
198  bool m_first ;
199  unsigned int m_index ;
200  std::vector<int> m_is_dir ;
201  std::vector<G::Path> m_path ;
202  std::vector<G::Path> m_name ;
203 } ;
204 
205 #endif
std::string sizeString() const
Returns the file size as a decimal string.
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:110
Path filePath() const
Returns the path of the current item.
G::Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:131
void readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:82
DirectoryIterator(const Directory &dir, const std::string &wc)
Constructor taking a directory reference and a wildcard specification.
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:69
bool valid(bool for_creating_files=false) const
Returns true if the object represents a valid directory.
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:27
bool writeable(std::string probe_filename=tmp()) const
Tries to create and then delete an empty test file in the directory.
void readType(const Path &dir, const std::string &suffix, unsigned int limit=0U)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:87
An encapsulation of a file system directory which allows for iterating through the set of contained f...
Definition: gdirectory.h:46
Path fileName() const
Returns the name of the current item.
Low-level classes.
A Directory iterator that does all file i/o in one go.
Definition: gdirectory.h:164
static std::string tmp()
A convenience function for constructing a filename for writeable().
bool more()
Returns true if more and advances by one.
bool isDir() const
Returns true if the current item is a directory.
A Directory iterator.
Definition: gdirectory.h:108
std::string modificationTimeString() const
Returns the last-modified time for the file in an undefined format – used for comparison.
~Directory()
Destructor.
Definition: gdirectory.cpp:54
G::Path fileName() const
Returns the current filename.
Definition: gdirectory.cpp:136
A pimple-pattern implementation class for DirectoryIterator.
static Directory root()
Returns a root directory object.
Definition: gdirectory.cpp:32
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:126
Directory & operator=(const Directory &)
Assignment operator.
Definition: gdirectory.cpp:63
bool error() const
Returns true on error. The caller should stop the iteration.
A Path object represents a file system path.
Definition: gpath.h:44
DirectoryList()
Default constructor for an empty list.
Definition: gdirectory.cpp:76