gdirectory.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.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gdirectory.h"
23 #include "gfs.h"
24 #include "gstr.h"
25 #include "glog.h"
26 
28  m_path(".")
29 {
30 }
31 
33 {
34  std::string s ;
35  s.append( 1U , G::FileSystem::slash() ) ;
36  return Directory( s ) ;
37 }
38 
39 G::Directory::Directory( const char * path ) :
40  m_path(path)
41 {
42 }
43 
44 G::Directory::Directory( const std::string & path ) :
45  m_path(path)
46 {
47 }
48 
49 G::Directory::Directory( const Path & path ) :
50  m_path(path)
51 {
52 }
53 
55 {
56 }
57 
59  m_path(other.m_path)
60 {
61 }
62 
64 {
65  m_path = rhs.m_path ;
66  return *this ;
67 }
68 
70 {
71  return m_path ;
72 }
73 
74 // ==
75 
77  m_first(true) ,
78  m_index(0U)
79 {
80 }
81 
83 {
84  readType( dir , std::string() ) ;
85 }
86 
87 void G::DirectoryList::readType( const G::Path & dir , const std::string & suffix , unsigned int limit )
88 {
89  // we do our own filename matching here so as to reduce
90  // our dependency on the glob()bing DirectoryIterator
91 
92  Directory directory( dir ) ;
93  DirectoryIterator iter( directory ) ;
94  for( unsigned int i = 0U ; iter.more() && !iter.error() ; ++i )
95  {
96  if( suffix.empty() || Str::tailMatch(iter.fileName().str(),suffix) )
97  {
98  if( limit == 0U || m_path.size() < limit )
99  {
100  m_is_dir.push_back( iter.isDir() ) ;
101  m_path.push_back( iter.filePath() ) ;
102  m_name.push_back( iter.fileName() ) ;
103  }
104  if( m_path.size() == limit )
105  break ;
106  }
107  }
108 }
109 
111 {
112  bool more = false ;
113  if( m_first )
114  {
115  m_first = false ;
116  more = ! m_is_dir.empty() ;
117  }
118  else
119  {
120  m_index++ ;
121  more = m_index < m_is_dir.size() ;
122  }
123  return more ;
124 }
125 
127 {
128  return !! m_is_dir[m_index] ;
129 }
130 
132 {
133  return m_path[m_index] ;
134 }
135 
137 {
138  return m_name[m_index] ;
139 }
140 
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:110
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the given string has the given ending.
Definition: gstr.cpp:850
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
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:69
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:27
static char slash()
Definition: gfs_unix.cpp:29
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.
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
~Directory()
Destructor.
Definition: gdirectory.cpp:54
G::Path fileName() const
Returns the current filename.
Definition: gdirectory.cpp:136
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