- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gdirectory_unix_glob.cpp
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 // ===
17 //
18 // gdirectory_unix_glob.cpp
19 //
20
21 #include "gdef.h"
22 #include "gdirectory.h"
23 #include "gfile.h"
24 #include "gdebug.h"
25 #include "glog.h"
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <glob.h>
30 #include <fcntl.h>
31
32 namespace G
33 {
34 class DirectoryIteratorImp ;
35 }
36
37 // ===
38
42 class G::DirectoryIteratorImp
43 {
44 private:
45 glob_t m_glob ;
46 Directory m_dir ;
47 bool m_first ;
48 size_t m_index ; // 'int' on some systems
49 bool m_error ;
50
51 public:
52 DirectoryIteratorImp( const Directory &dir , const std::string & wildcard ) ;
53 ~DirectoryIteratorImp() ;
54 bool isDir() const ;
55 bool more() ;
56 bool error() const ;
57 std::string modificationTimeString() const ;
58 std::string sizeString() const ;
59 Path filePath() const ;
60 Path fileName() const ;
61
62 private:
63 void operator=( const DirectoryIteratorImp & ) ;
64 DirectoryIteratorImp( const DirectoryIteratorImp & ) ;
65 } ;
66
67 // ===
68
69 G::DirectoryIterator::DirectoryIterator( const Directory & dir ) :
70 m_imp( new DirectoryIteratorImp(dir,std::string()) )
71 {
72 }
73
74 G::DirectoryIterator::DirectoryIterator( const Directory & dir , const std::string & wc ) :
75 m_imp( new DirectoryIteratorImp(dir,wc) )
76 {
77 // note that this overload is rarely used -- if glob() is
78 // unavailable and this overload is never called then the
79 // DirectoryIteratorImp class can be rewritten with
80 // just a default constructor that uses readdir()
81 }
82
83 bool G::DirectoryIterator::error() const
84 {
85 return m_imp->error() ;
86 }
87
88 bool G::DirectoryIterator::more()
89 {
90 return m_imp->more() ;
91 }
92
93 G::Path G::DirectoryIterator::filePath() const
94 {
95 return m_imp->filePath() ;
96 }
97
98 G::Path G::DirectoryIterator::fileName() const
99 {
100 return m_imp->fileName() ;
101 }
102
103 bool G::DirectoryIterator::isDir() const
104 {
105 return m_imp->isDir() ;
106 }
107
108 std::string G::DirectoryIterator::modificationTimeString() const
109 {
110 return m_imp->modificationTimeString() ;
111 }
112
113 std::string G::DirectoryIterator::sizeString() const
114 {
115 return m_imp->sizeString() ;
116 }
117
118 G::DirectoryIterator::~DirectoryIterator()
119 {
120 delete m_imp ;
121 }
122
123 // ===
124
125 extern "C"
126 {
127 int gdirectory_unix_on_error_( const char * , int ) ;
128 int gdirectory_unix_on_error_( const char * , int )
129 {
130 const int abort = 1 ;
131 return abort ;
132 }
133 }
134
135 G::DirectoryIteratorImp::DirectoryIteratorImp( const Directory & dir , const std::string & wildcard ) :
136 m_dir(dir) ,
137 m_first(true) ,
138 m_index(0) ,
139 m_error(false)
140 {
141 m_glob.gl_pathc = 0 ;
142 m_glob.gl_pathv = NULL ;
143
144 Path wild_path( dir.path() , wildcard.empty() ? std::string("*") : wildcard ) ;
145
146 int flags = 0 | GLOB_ERR ;
147 int error = ::glob( wild_path.str().c_str() , flags , gdirectory_unix_on_error_ , &m_glob ) ;
148 if( error || m_glob.gl_pathv == NULL )
149 m_error = true ;
150 }
151
152 bool G::DirectoryIteratorImp::error() const
153 {
154 return m_error ;
155 }
156
157 bool G::DirectoryIteratorImp::more()
158 {
159 if( m_error )
160 return false ;
161
162 if( ! m_first )
163 m_index++ ;
164 m_first = false ;
165
166 if( m_index >= static_cast<size_t>(m_glob.gl_pathc) )
167 return false ;
168
169 return true ;
170 }
171
172 G::Path G::DirectoryIteratorImp::filePath() const
173 {
174 const bool sane =
175 m_index < static_cast<size_t>(m_glob.gl_pathc) &&
176 m_glob.gl_pathv != NULL &&
177 m_glob.gl_pathv[m_index] != NULL ;
178 const char * file_path = sane ? m_glob.gl_pathv[m_index] : "" ;
179 return Path( file_path ) ;
180 }
181
182 G::Path G::DirectoryIteratorImp::fileName() const
183 {
184 return Path( filePath().basename() ) ;
185 }
186
187 bool G::DirectoryIteratorImp::isDir() const
188 {
189 Directory dir( filePath().str() ) ;
190 return dir.valid() ;
191 }
192
193 G::DirectoryIteratorImp::~DirectoryIteratorImp()
194 {
195 if( ! m_error )
196 ::globfree( &m_glob ) ;
197 }
198
199 std::string G::DirectoryIteratorImp::modificationTimeString() const
200 {
201 return std::string() ; // for now
202 }
203
204 std::string G::DirectoryIteratorImp::sizeString() const
205 {
206 std::string s = G::File::sizeString( filePath() ) ;
207 return s.empty() ? std::string("0") : s ;
208 }
209
gdirectory.h
G::DirectoryIteratorImp::error
bool error() const
Definition: gdirectory_unix_glob.cpp:152
G::DirectoryIterator::sizeString
std::string sizeString() const
Returns the file size as a decimal string.
Definition: gdirectory_unix_glob.cpp:113
glog.h
G::DirectoryIteratorImp::modificationTimeString
std::string modificationTimeString() const
Definition: gdirectory_unix_glob.cpp:199
G::DirectoryIteratorImp::sizeString
std::string sizeString() const
Definition: gdirectory_unix_glob.cpp:204
G::DirectoryIterator::filePath
Path filePath() const
Returns the path of the current item.
Definition: gdirectory_unix_glob.cpp:93
G::DirectoryIteratorImp::more
bool more()
Definition: gdirectory_unix_glob.cpp:157
G::DirectoryIterator::DirectoryIterator
DirectoryIterator(const Directory &dir, const std::string &wc)
Constructor taking a directory reference and a wildcard specification.
Definition: gdirectory_unix_glob.cpp:74
G::Directory::path
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:69
G::Directory::valid
bool valid(bool for_creating_files=false) const
Returns true if the object represents a valid directory.
Definition: gdirectory_unix.cpp:39
G::DirectoryIteratorImp::isDir
bool isDir() const
Definition: gdirectory_unix_glob.cpp:187
G::DirectoryIterator::~DirectoryIterator
~DirectoryIterator()
Destructor.
Definition: gdirectory_unix_glob.cpp:118
G::Directory
An encapsulation of a file system directory which allows for iterating through the set of contained f...
Definition: gdirectory.h:46
G::DirectoryIterator::fileName
Path fileName() const
Returns the name of the current item.
Definition: gdirectory_unix_glob.cpp:98
G
Low-level classes.
gfile.h
gdirectory_unix_on_error_
int gdirectory_unix_on_error_(const char *, int)
Definition: gdirectory_unix_glob.cpp:128
G::DirectoryIteratorImp::DirectoryIteratorImp
DirectoryIteratorImp(const Directory &dir, const std::string &wildcard)
Definition: gdirectory_unix_glob.cpp:135
G::DirectoryIteratorImp::fileName
Path fileName() const
Definition: gdirectory_unix_glob.cpp:182
G::DirectoryIterator::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory_unix_glob.cpp:88
G::DirectoryIteratorImp::filePath
Path filePath() const
Definition: gdirectory_unix_glob.cpp:172
G::DirectoryIterator::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory_unix_glob.cpp:103
G::DirectoryIteratorImp::~DirectoryIteratorImp
~DirectoryIteratorImp()
Definition: gdirectory_unix_glob.cpp:193
G::DirectoryIterator::modificationTimeString
std::string modificationTimeString() const
Returns the last-modified time for the file in an undefined format – used for comparison.
Definition: gdirectory_unix_glob.cpp:108
G::File::sizeString
static std::string sizeString(const Path &file)
Returns the file's size in string format.
Definition: gfile_unix.cpp:67
G::DirectoryIteratorImp
A pimple-pattern implementation class for DirectoryIterator.
Definition: gdirectory_unix_glob.cpp:42
G::DirectoryIterator::error
bool error() const
Returns true on error. The caller should stop the iteration.
Definition: gdirectory_unix_glob.cpp:83
G::Path
A Path object represents a file system path.
Definition: gpath.h:44
gdebug.h