E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gdirectory.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 "gfile.h"
24 #include "gpath.h"
25 #include "gstr.h"
26 #include "gassert.h"
27 #include "glog.h"
28 #include <algorithm> // std::sort
29 #include <functional> // std::ptr_fun
30 #include <iterator> // std::distance
31
32 G::Directory::Directory() :
33 m_path(".")
34 {
35 }
36
37 G::Directory::Directory( const char * path ) :
38 m_path(path)
39 {
40 }
41
42 G::Directory::Directory( const std::string & path ) :
43 m_path(path)
44 {
45 }
46
47 G::Directory::Directory( const Path & path ) :
48 m_path(path)
49 {
50 }
51
52 G::Directory::~Directory()
53 {
54 }
55
56 G::Directory::Directory( const Directory &other ) :
57 m_path(other.m_path)
58 {
59 }
60
61 G::Directory &G::Directory::operator=( const Directory &rhs )
62 {
63 m_path = rhs.m_path ;
64 return *this ;
65 }
66
67 G::Path G::Directory::path() const
68 {
69 return m_path ;
70 }
71
72 // ==
73
74 G::DirectoryList::DirectoryList() :
75 m_first(true) ,
76 m_index(0U)
77 {
78 }
79
80 void G::DirectoryList::readAll( const G::Path & dir , std::vector<G::DirectoryList::Item> & out , bool sorted )
81 {
82 DirectoryList list ;
83 list.readAll( dir ) ;
84 if( sorted ) list.sort() ;
85 list.m_list.swap( out ) ;
86 }
87
88 void G::DirectoryList::readAll( const G::Path & dir )
89 {
90 readType( dir , std::string() ) ;
91 }
92
93 void G::DirectoryList::readType( const G::Path & dir , const std::string & suffix , unsigned int limit )
94 {
95 // we do our own filename matching here so as to
96 // reduce our dependency on glob()
97 Directory directory( dir ) ;
98 DirectoryIterator iter( directory ) ;
99 for( unsigned int i = 0U ; iter.more() && !iter.error() ; ++i )
100 {
101 if( suffix.empty() || Str::tailMatch(iter.fileName(),suffix) )
102 {
103 if( limit == 0U || m_list.size() < limit )
104 {
105 Item item ;
106 item.m_is_dir = iter.isDir() ;
107 item.m_path = iter.filePath() ;
108 item.m_name = iter.fileName() ;
109 m_list.push_back( item ) ;
110 }
111 if( m_list.size() == limit )
112 break ;
113 }
114 }
115 }
116
117 bool G::DirectoryList::more()
118 {
119 bool more = false ;
120 if( m_first )
121 {
122 m_first = false ;
123 more = ! m_list.empty() ;
124 }
125 else
126 {
127 m_index++ ;
128 more = m_index < m_list.size() ;
129 }
130 return more ;
131 }
132
133 bool G::DirectoryList::isDir() const
134 {
135 return m_list.at(m_index).m_is_dir ;
136 }
137
138 G::Path G::DirectoryList::filePath() const
139 {
140 return m_list.at(m_index).m_path ;
141 }
142
143 std::string G::DirectoryList::fileName() const
144 {
145 return m_list.at(m_index).m_name ;
146 }
147
148 bool G::DirectoryList::compare( const Item & a , const Item & b )
149 {
150 return a.m_name.compare( b.m_name ) < 0 ;
151 }
152
153 void G::DirectoryList::sort()
154 {
155 std::sort( m_list.begin() , m_list.end() , std::ptr_fun(compare) ) ;
156 }
157
158 /// \file gdirectory.cpp
gdirectory.h
glog.h
G::DirectoryList::Item
A directory-entry item for G::DirectoryList.
Definition: gdirectory.h:148
G::DirectoryList::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:117
G::Str::tailMatch
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the string has the given ending (or ending is empty).
Definition: gstr.cpp:1117
G::DirectoryIterator::filePath
Path filePath() const
Returns the path of the current item.
Definition: gdirectory_unix.cpp:139
G::DirectoryList::filePath
Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:138
G::DirectoryList::readAll
void readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:88
G::DirectoryList::sort
void sort()
Sorts the files lexicographically.
Definition: gdirectory.cpp:153
G::Directory::path
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:67
G::Directory::Directory
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:32
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:93
G::Directory
An encapsulation of a file system directory that allows for iterating through the set of contained fi...
Definition: gdirectory.h:45
gstr.h
gassert.h
gfile.h
G::DirectoryList
A Directory iterator with the same kind of interface as G::DirectoryIterator, but doing all file i/o ...
Definition: gdirectory.h:145
G::DirectoryIterator::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory_unix.cpp:134
G::DirectoryIterator::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory_unix.cpp:149
gpath.h
G::DirectoryIterator
A Directory iterator.
Definition: gdirectory.h:102
G::Directory::~Directory
~Directory()
Destructor.
Definition: gdirectory.cpp:52
G::DirectoryIterator::fileName
std::string fileName() const
Returns the name of the current item.
Definition: gdirectory_unix.cpp:144
G::DirectoryList::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:133
G::Directory::operator=
Directory & operator=(const Directory &)
Assignment operator.
Definition: gdirectory.cpp:61
G::DirectoryIterator::error
bool error() const
Returns true on error. The caller should stop the iteration.
Definition: gdirectory_unix.cpp:129
G::DirectoryList::fileName
std::string fileName() const
Returns the current filename.
Definition: gdirectory.cpp:143
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::DirectoryList::DirectoryList
DirectoryList()
Default constructor for an empty list.
Definition: gdirectory.cpp:74