E-MailRelay
gdirectory.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file gdirectory.cpp
19///
20
21#include "gdef.h"
22#include "gdirectory.h"
23#include "gfile.h"
24#include "gpath.h"
25#include "gprocess.h"
26#include "gstr.h"
27#include "glog.h"
28#include <algorithm> // std::sort
29#include <functional>
30#include <iterator> // std::distance
31
33 m_path(".")
34{
35}
36
37G::Directory::Directory( const char * path ) :
38 m_path(path)
39{
40}
41
42G::Directory::Directory( const std::string & path ) :
43 m_path(path)
44{
45}
46
48 m_path(path)
49{
50}
51
53{
54 return m_path ;
55}
56
57std::string G::Directory::tmp()
58{
59 std::ostringstream ss ;
60 static int sequence = 1 ;
61 ss << "." << SystemTime::now() << "." << sequence++ << "." << Process::Id() << ".tmp" ;
62 return ss.str() ;
63}
64
65bool G::Directory::valid( bool for_creation ) const
66{
67 return 0 == usable( for_creation ) ;
68}
69
70// ==
71
73= default;
74
75void G::DirectoryList::readAll( const G::Path & dir , std::vector<G::DirectoryList::Item> & out , bool sorted )
76{
77 DirectoryList list ;
78 list.readAll( dir ) ;
79 if( sorted ) list.sort() ;
80 list.m_list.swap( out ) ;
81}
82
84{
85 readType( dir , std::string() ) ;
86}
87
88void G::DirectoryList::readType( const G::Path & dir , const std::string & suffix , unsigned int limit )
89{
90 // we do our own filename matching here so as to
91 // reduce our dependency on glob()
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(),suffix) )
97 {
98 if( limit == 0U || m_list.size() < limit )
99 {
100 Item item ;
101 item.m_is_dir = iter.isDir() ;
102 item.m_path = iter.filePath() ;
103 item.m_name = iter.fileName() ;
104 m_list.push_back( item ) ;
105 }
106 if( m_list.size() == limit )
107 break ;
108 }
109 }
110}
111
113{
114 bool more = false ;
115 if( m_first )
116 {
117 m_first = false ;
118 more = ! m_list.empty() ;
119 }
120 else
121 {
122 m_index++ ;
123 more = m_index < m_list.size() ;
124 }
125 return more ;
126}
127
129{
130 return m_list.at(m_index).m_is_dir ;
131}
132
134{
135 return m_list.at(m_index).m_path ;
136}
137
138std::string G::DirectoryList::fileName() const
139{
140 return m_list.at(m_index).m_name ;
141}
142
143bool G::DirectoryList::compare( const Item & a , const Item & b )
144{
145 return a.m_name.compare( b.m_name ) < 0 ;
146}
147
149{
150 std::sort( m_list.begin() , m_list.end() , compare ) ;
151}
152
A iterator that returns filenames in a directory.
Definition: gdirectory.h:100
std::string fileName() const
Returns the name of the current item.
bool error() const
Returns true on error. The caller should stop the iteration.
bool isDir() const
Returns true if the current item is a directory.
bool more()
Returns true if more and advances by one.
Path filePath() const
Returns the path of the current item.
A iterator similar to G::DirectoryIterator but doing all file i/o in one go.
Definition: gdirectory.h:146
void sort()
Sorts the files lexicographically.
Definition: gdirectory.cpp:148
Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:133
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:88
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:112
std::string fileName() const
Returns the current filename.
Definition: gdirectory.cpp:138
void readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:83
DirectoryList()
Default constructor for an empty list.
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:128
An encapsulation of a file system directory that works with G::DirectoryIterator.
Definition: gdirectory.h:46
static std::string tmp()
A convenience function for constructing a filename for writeable().
Definition: gdirectory.cpp:57
Path path() const
Returns the directory's path, as passed in to the ctor.
Definition: gdirectory.cpp:52
bool valid(bool for_creating_files=false) const
Returns true iff usable() is zero.
Definition: gdirectory.cpp:65
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:32
A Path object represents a file system path.
Definition: gpath.h:72
Process-id class.
Definition: gprocess.h:140
static bool tailMatch(const std::string &in, const std::string &ending)
Returns true if the string has the given ending (or the given ending is empty).
Definition: gstr.cpp:1302
static SystemTime now()
Factory function for the current time.
Definition: gdatetime.cpp:260
A directory-entry item for G::DirectoryList.
Definition: gdirectory.h:149