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
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::Path G::Directory::path() const
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
65 bool G::Directory::valid( bool for_creation ) const
66{
67 return 0 == usable( for_creation ) ;
68}
69
70// ==
71
72 G::DirectoryList::DirectoryList()
73= default;
74
75 void 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
83 void G::DirectoryList::readAll( const G::Path & dir )
84{
85 readType( dir , std::string() ) ;
86}
87
88 void 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
112 bool G::DirectoryList::more()
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
128 bool G::DirectoryList::isDir() const
129{
130 return m_list.at(m_index).m_is_dir ;
131}
132
133 G::Path G::DirectoryList::filePath() const
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
143 bool G::DirectoryList::compare( const Item & a , const Item & b )
144{
145 return a.m_name.compare( b.m_name ) < 0 ;
146}
147
148 void G::DirectoryList::sort()
149{
150 std::sort( m_list.begin() , m_list.end() , compare ) ;
151}
152
G::DirectoryIterator
A iterator that returns filenames in a directory.
Definition: gdirectory.h:100
G::DirectoryIterator::fileName
std::string fileName() const
Returns the name of the current item.
Definition: gdirectory_unix.cpp:126
G::DirectoryIterator::error
bool error() const
Returns true on error. The caller should stop the iteration.
Definition: gdirectory_unix.cpp:111
G::DirectoryIterator::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory_unix.cpp:131
G::DirectoryIterator::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory_unix.cpp:116
G::DirectoryIterator::filePath
Path filePath() const
Returns the path of the current item.
Definition: gdirectory_unix.cpp:121
G::DirectoryList
A iterator similar to G::DirectoryIterator but doing all file i/o in one go.
Definition: gdirectory.h:146
G::DirectoryList::sort
void sort()
Sorts the files lexicographically.
Definition: gdirectory.cpp:148
G::DirectoryList::filePath
Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:133
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:88
G::DirectoryList::more
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:112
G::DirectoryList::fileName
std::string fileName() const
Returns the current filename.
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:83
G::DirectoryList::DirectoryList
DirectoryList()
Default constructor for an empty list.
G::DirectoryList::isDir
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:128
G::Directory
An encapsulation of a file system directory that works with G::DirectoryIterator.
Definition: gdirectory.h:46
G::Directory::tmp
static std::string tmp()
A convenience function for constructing a filename for writeable().
Definition: gdirectory.cpp:57
G::Directory::path
Path path() const
Returns the directory's path, as passed in to the ctor.
Definition: gdirectory.cpp:52
G::Directory::valid
bool valid(bool for_creating_files=false) const
Returns true iff usable() is zero.
Definition: gdirectory.cpp:65
G::Directory::Directory
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:32
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
G::Process::Id
Process-id class.
Definition: gprocess.h:140
G::Str::tailMatch
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
G::SystemTime::now
static SystemTime now()
Factory function for the current time.
Definition: gdatetime.cpp:260
gdirectory.h
gfile.h
glog.h
gpath.h
gprocess.h
gstr.h
G::DirectoryList::Item
A directory-entry item for G::DirectoryList.
Definition: gdirectory.h:149