34#ifndef INVALID_FILE_ATTRIBUTES
35#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
40 class DirectoryIteratorImp ;
43class G::DirectoryIteratorImp
46 explicit DirectoryIteratorImp(
const Directory & dir ) ;
47 ~DirectoryIteratorImp() ;
51 std::string sizeString()
const ;
52 Path filePath()
const ;
53 std::string fileName()
const ;
56 DirectoryIteratorImp(
const DirectoryIteratorImp & ) = delete ;
57 DirectoryIteratorImp( DirectoryIteratorImp && ) = delete ;
58 DirectoryIteratorImp & operator=(
const DirectoryIteratorImp & ) = delete ;
59 DirectoryIteratorImp & operator=( DirectoryIteratorImp && ) = delete ;
62 static bool oneDot(
const wchar_t * p )
noexcept {
return p[0] == L
'.' && p[1] == 0 ; }
63 static bool twoDots(
const wchar_t * p )
noexcept {
return p[0] == L
'.' && p[1] == L
'.' && p[2] == 0 ; }
64 static bool oneDot(
const char * p )
noexcept {
return p[0] ==
'.' && p[1] == 0 ; }
65 static bool twoDots(
const char * p )
noexcept {
return p[0] ==
'.' && p[1] ==
'.' && p[2] == 0 ; }
68 nowide::FIND_DATA_type m_context ;
79 DWORD attributes = nowide::getFileAttributes( m_path ) ;
80 if( attributes == INVALID_FILE_ATTRIBUTES )
82 DWORD e = GetLastError() ;
83 if( e == ERROR_ACCESS_DENIED || e == ERROR_NETWORK_ACCESS_DENIED )
87 return ( attributes & FILE_ATTRIBUTE_DIRECTORY ) ? 0 : ENOTDIR ;
92 Path path( m_path , filename.empty() ? tmp() : filename ) ;
99 m_imp(
std::make_unique<DirectoryIteratorImp>(dir))
105 return m_imp->error() ;
110 return m_imp->more() ;
115 return m_imp->filePath() ;
120 return m_imp->fileName() ;
125 return m_imp->isDir() ;
135 return m_imp->sizeString() ;
143G::DirectoryIteratorImp::DirectoryIteratorImp(
const Directory & dir ) :
148 m_handle = nowide::findFirstFile( dir.path()/
"*" , &m_context ) ;
149 if( m_handle == INVALID_HANDLE_VALUE )
151 DWORD err = GetLastError() ;
152 if( err != ERROR_FILE_NOT_FOUND )
157bool G::DirectoryIteratorImp::error()
const
162bool G::DirectoryIteratorImp::more()
164 if( m_handle == INVALID_HANDLE_VALUE )
170 if( !oneDot(m_context.cFileName) && !twoDots(m_context.cFileName) )
176 bool rc = nowide::findNextFile( m_handle , &m_context ) != 0 ;
179 DWORD err = GetLastError() ;
180 if( err != ERROR_NO_MORE_FILES )
183 FindClose( m_handle ) ;
184 m_handle = INVALID_HANDLE_VALUE ;
189 if( !oneDot(m_context.cFileName) && !twoDots(m_context.cFileName) )
196G::Path G::DirectoryIteratorImp::filePath()
const
198 G_ASSERT( m_handle != INVALID_HANDLE_VALUE ) ;
199 return m_dir.path() / fileName() ;
202std::string G::DirectoryIteratorImp::fileName()
const
204 G_ASSERT( m_handle != INVALID_HANDLE_VALUE ) ;
205 return nowide::cFileName( m_context ) ;
208bool G::DirectoryIteratorImp::isDir()
const
210 return !! ( m_context.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ;
213G::DirectoryIteratorImp::~DirectoryIteratorImp()
215 if( m_handle != INVALID_HANDLE_VALUE )
216 FindClose( m_handle ) ;
219std::string G::DirectoryIteratorImp::sizeString()
const
221 const DWORD & hi = m_context.nFileSizeHigh ;
222 const DWORD & lo = m_context.nFileSizeLow ;
232 int i =
static_cast<int>( n % 10 ) ;
233 char c =
static_cast<char>(
'0' + i ) ;
234 s.insert( 0U , 1U , c ) ;
DirectoryIterator(const Directory &dir)
Constructor taking a directory reference.
std::string fileName() const
Returns the name of the current item.
bool error() const
Returns true on error. The caller should stop the iteration.
std::string sizeString() const
Returns the file size as a decimal string.
bool isLink() const
Returns true if the current item is a symlink.
~DirectoryIterator()
Destructor.
bool isDir() const
Returns true if the current item is a directory or a symlink to a directory.
bool more()
Returns true if more and advances by one.
Path filePath() const
Returns the path of the current item.
int usable(bool for_creating_files=false) const
Returns zero if the object represents a valid directory with permissions that dont disallow reading o...
bool writeable(const std::string &probe_filename=tmp()) const
Tries to create and then delete an empty test file in the directory.
static bool probe(const Path &) noexcept
Creates and deletes a temporary probe file.
A Path object represents a file system path.
Contains inline functions that convert to and from UTF-8 strings in order to call wide-character "W()...