83 lines
2.3 KiB
C++
83 lines
2.3 KiB
C++
#include "../filesystem.hpp"
|
|
#include <sys/stat.h>
|
|
|
|
FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) {}
|
|
|
|
FSLib::Directory::Iterator::Iterator( const Directory &d_ )
|
|
: d( opendir( d_.path() ) ) {
|
|
current_entry = readdir( d );
|
|
|
|
// skip "." and ".."
|
|
if ( current_entry != nullptr &&
|
|
( !strcmp( current_entry->d_name, "." ) ||
|
|
!strcmp( current_entry->d_name, ".." ) ) )
|
|
++( *this );
|
|
}
|
|
|
|
FSLib::Directory::Iterator::Iterator( const Directory &d_,
|
|
const struct dirent *current_entry_ )
|
|
: d( opendir( d_.path() ) ), current_entry( current_entry_ ) {}
|
|
|
|
FSLib::Directory::Iterator::~Iterator() {
|
|
closedir( d );
|
|
}
|
|
|
|
bool FSLib::exists( const string &path ) {
|
|
struct stat path_stat;
|
|
return stat( path.c_str(), &path_stat ) == 0;
|
|
}
|
|
|
|
string FSLib::canonical( const string &path ) {
|
|
char_t *canonical_path = new char_t[PATH_MAX];
|
|
auto failed = realpath( path.c_str(), canonical_path ) == nullptr;
|
|
|
|
if ( failed ) {
|
|
delete[] canonical_path;
|
|
return string();
|
|
}
|
|
|
|
string canonical_string{ canonical_path };
|
|
delete[] canonical_path;
|
|
return canonical_string;
|
|
}
|
|
|
|
bool FSLib::isDirectory( const string &path ) {
|
|
struct stat path_stat;
|
|
|
|
stat( path.c_str(), &path_stat );
|
|
|
|
return S_ISDIR( path_stat.st_mode );
|
|
}
|
|
|
|
bool FSLib::rename( const string &file_a, const string &file_b ) {
|
|
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
|
|
}
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
|
return Iterator( *this, nullptr );
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
|
return Iterator( *this, nullptr );
|
|
}
|
|
|
|
char_t const *FSLib::Directory::Iterator::operator*() const {
|
|
return current_entry->d_name;
|
|
}
|
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
|
if ( current_entry == nullptr )
|
|
return *this;
|
|
current_entry = readdir( d );
|
|
// skip . and ..
|
|
if ( current_entry != nullptr &&
|
|
( !strcmp( current_entry->d_name, "." ) ||
|
|
!strcmp( current_entry->d_name, ".." ) ) )
|
|
return operator++();
|
|
return *this;
|
|
}
|
|
|
|
bool FSLib::Directory::Iterator::operator==( const Iterator &i_other ) const {
|
|
return i_other.current_entry == current_entry;
|
|
}
|