E-MailRelay
gdirectory.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 <algorithm>
28#include <functional>
29#include <iterator> // std::distance
30
31#ifndef G_LIB_SMALL
33 m_path(".")
34{
35}
36#endif
37
39 m_path(path)
40{
41}
42
43#ifndef G_LIB_SMALL
44G::Directory::Directory( const std::string & path ) :
45 m_path(path)
46{
47}
48#endif
49
51{
52 return m_path ;
53}
54
55std::string G::Directory::tmp()
56{
57 std::ostringstream ss ;
58 static int sequence = 1 ;
59 ss << "." << SystemTime::now() << "." << sequence++ << "." << Process::Id() << ".tmp" ;
60 return ss.str() ;
61}
62
63bool G::Directory::valid( bool for_creation ) const
64{
65 return 0 == usable( for_creation ) ;
66}
67
68// ==
69
71= default;
72
73#ifndef G_LIB_SMALL
74void G::DirectoryList::readAll( const G::Path & dir , std::vector<Item> & out )
75{
76 DirectoryList list ;
77 list.readAll( dir ) ;
78 list.m_list.swap( out ) ;
79}
80#endif
81
82std::size_t G::DirectoryList::readAll( const G::Path & dir )
83{
84 readType( dir , {} ) ;
85 return m_list.size() ;
86}
87
88std::size_t G::DirectoryList::readDirectories( const G::Path & dir , unsigned int limit )
89{
90 readImp( dir , true , {} , limit ) ;
91 return m_list.size() ;
92}
93
94std::size_t G::DirectoryList::readType( const G::Path & dir , std::string_view suffix , unsigned int limit )
95{
96 readImp( dir , false , suffix , limit ) ;
97 return m_list.size() ;
98}
99
100void G::DirectoryList::readImp( const G::Path & dir , bool sub_dirs , std::string_view suffix , unsigned int limit )
101{
102 Directory directory( dir ) ;
103 DirectoryIterator iter( directory ) ;
104 while( iter.more() && !iter.error() )
105 {
106 if( sub_dirs ? iter.isDir() : ( suffix.empty() || Str::tailMatch(iter.fileName(),suffix) ) )
107 {
108 if( limit == 0U || m_list.size() < limit )
109 {
110 Item item ;
111 item.m_is_dir = iter.isDir() ;
112 item.m_is_link = iter.isLink() ;
113 item.m_path = iter.filePath() ;
114 item.m_name = iter.fileName() ;
115 m_list.insert( std::lower_bound(m_list.begin(),m_list.end(),item) , item ) ;
116 }
117 if( m_list.size() == limit )
118 break ;
119 }
120 }
121}
122
123#ifndef G_LIB_SMALL
124bool G::DirectoryList::empty() const noexcept
125{
126 return m_list.empty() ;
127}
128#endif
129
131{
132 bool more = false ;
133 if( m_first )
134 {
135 m_first = false ;
136 more = ! m_list.empty() ;
137 }
138 else
139 {
140 m_index++ ;
141 more = m_index < m_list.size() ;
142 }
143 return more ;
144}
145
146#ifndef G_LIB_SMALL
148{
149 return m_list.at(m_index).m_is_link ;
150}
151#endif
152
154{
155 return m_list.at(m_index).m_is_dir ;
156}
157
159{
160 return m_list.at(m_index).m_path ;
161}
162
163std::string G::DirectoryList::fileName() const
164{
165 return m_list.at(m_index).m_name ;
166}
167
A iterator that returns unsorted filenames in a directory.
Definition: gdirectory.h:99
A iterator similar to G::DirectoryIterator but doing all file i/o in one go and providing a sorted re...
Definition: gdirectory.h:150
bool isLink() const
Returns true if the current item is a symlink.
Definition: gdirectory.cpp:147
Path filePath() const
Returns the current path.
Definition: gdirectory.cpp:158
std::size_t readAll(const Path &dir)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:82
bool empty() const noexcept
Returns true if empty.
Definition: gdirectory.cpp:124
bool more()
Returns true if more and advances by one.
Definition: gdirectory.cpp:130
std::string fileName() const
Returns the current filename.
Definition: gdirectory.cpp:163
std::size_t readType(const Path &dir, std::string_view suffix, unsigned int limit=0U)
An initialiser that is to be used after default construction.
Definition: gdirectory.cpp:94
std::size_t readDirectories(const Path &dir, unsigned int limit=0U)
An initialiser that reads all sub-directories.
Definition: gdirectory.cpp:88
DirectoryList()
Default constructor for an empty list.
bool isDir() const
Returns true if the current item is a directory.
Definition: gdirectory.cpp:153
An encapsulation of a file system directory that works with G::DirectoryIterator.
Definition: gdirectory.h:48
static std::string tmp()
A convenience function for constructing a filename for writeable().
Definition: gdirectory.cpp:55
Path path() const
Returns the directory's path, as passed in to the ctor.
Definition: gdirectory.cpp:50
bool valid(bool for_creating_files=false) const
Returns true iff usable() is zero.
Definition: gdirectory.cpp:63
Directory()
Default constructor for the current directory.
Definition: gdirectory.cpp:32
A Path object represents a file system path.
Definition: gpath.h:82
Process-id class.
Definition: gprocess.h:167
static bool tailMatch(std::string_view in, std::string_view ending) noexcept
Returns true if the string has the given ending (or the given ending is empty).
Definition: gstr.cpp:1352
static SystemTime now()
Factory function for the current time.
Definition: gdatetime.cpp:328