- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
27 G::Directory::Directory() :
28 m_path(".")
29 {
30 }
31
32 G::Directory G::Directory::root()
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
54 G::Directory::~Directory()
55 {
56 }
57
58 G::Directory::Directory( const Directory &other ) :
59 m_path(other.m_path)
60 {
61 }
62
63 G::Directory &G::Directory::operator=( const Directory &rhs )
64 {
65 m_path = rhs.m_path ;
66 return *this ;
67 }
68
69 G::Path G::Directory::path() const
70 {
71 return m_path ;
72 }
73
74 // ==
75
76 G::DirectoryList::DirectoryList() :
77 m_first(true) ,
78 m_index(0U)
79 {
80 }
81
82 void G::DirectoryList::readAll( const G::Path & dir )
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
110 bool G::DirectoryList::more()
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
126 bool G::DirectoryList::isDir() const
127 {
128 return !! m_is_dir[m_index] ;
129 }
130
131 G::Path G::DirectoryList::filePath() const
132 {
133 return m_path[m_index] ;
134 }
135
136 G::Path G::DirectoryList::fileName() const
137 {
138 return m_name[m_index] ;
139 }
140
G::Path::str
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
gdirectory.h
glog.h
G::DirectoryList::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:110
G::Str::tailMatch
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
G::DirectoryIterator::filePath
Path filePath() const
Returns the path of the current item.
Definition: gdirectory_unix_glob.cpp:93
G::DirectoryList::filePath
G::Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:131
G::DirectoryList::readAll
void readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:82
G::Directory::path
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:69
G::Directory::Directory
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:27
G::FileSystem::slash
static char slash()
Definition: gfs_unix.cpp:29
G::DirectoryList::readType
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
G::Directory
An encapsulation of a file system directory which allows for iterating through the set of contained f...
Definition: gdirectory.h:46
gfs.h
G::DirectoryIterator::fileName
Path fileName() const
Returns the name of the current item.
Definition: gdirectory_unix_glob.cpp:98
gstr.h
G::DirectoryIterator::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory_unix_glob.cpp:88
G::DirectoryIterator::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory_unix_glob.cpp:103
G::DirectoryIterator
A Directory iterator.
Definition: gdirectory.h:108
G::Directory::~Directory
~Directory()
Destructor.
Definition: gdirectory.cpp:54
G::DirectoryList::fileName
G::Path fileName() const
Returns the current filename.
Definition: gdirectory.cpp:136
G::Directory::root
static Directory root()
Returns a root directory object.
Definition: gdirectory.cpp:32
G::DirectoryList::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:126
G::Directory::operator=
Directory & operator=(const Directory &)
Assignment operator.
Definition: gdirectory.cpp:63
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
G::DirectoryList::DirectoryList
DirectoryList()
Default constructor for an empty list.
Definition: gdirectory.cpp:76