VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gdirectory.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 
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 
53 {
54 }
55 
57  m_path(other.m_path)
58 {
59 }
60 
62 {
63  m_path = rhs.m_path ;
64  return *this ;
65 }
66 
68 {
69  return m_path ;
70 }
71 
72 // ==
73 
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 
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 
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 
134 {
135  return m_list.at(m_index).m_is_dir ;
136 }
137 
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 
154 {
155  std::sort( m_list.begin() , m_list.end() , std::ptr_fun(compare) ) ;
156 }
157 
158 /// \file gdirectory.cpp
A directory-entry item for G::DirectoryList.
Definition: gdirectory.h:148
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:117
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the given string has the given ending.
Definition: gstr.cpp:1066
Path filePath() const
Returns the path of the current item.
Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:138
void readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:88
void sort()
Sorts the files lexicographically.
Definition: gdirectory.cpp:153
Path path() const
Returns the directory's path.
Definition: gdirectory.cpp:67
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:32
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
An encapsulation of a file system directory that allows for iterating through the set of contained fi...
Definition: gdirectory.h:45
A Directory iterator with the same kind of interface as G::DirectoryIterator, but doing all file i/o ...
Definition: gdirectory.h:145
A variant class holding a string, an item map keyed by name, or an ordered list of items...
Definition: gitem.h:41
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:102
~Directory()
Destructor.
Definition: gdirectory.cpp:52
std::string fileName() const
Returns the name of the current item.
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:133
Directory & operator=(const Directory &)
Assignment operator.
Definition: gdirectory.cpp:61
bool error() const
Returns true on error. The caller should stop the iteration.
std::string fileName() const
Returns the current filename.
Definition: gdirectory.cpp:143
A Path object represents a file system path.
Definition: gpath.h:72
DirectoryList()
Default constructor for an empty list.
Definition: gdirectory.cpp:74