2019-02-04 16:39:48 +00:00
|
|
|
#include <limits.h>
|
2019-01-03 18:01:43 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-04 16:39:48 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Shlwapi.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "filesystem.hpp"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#define S_ISDIR( a ) a & _S_IFDIR
|
|
|
|
|
|
|
|
using stat_t = struct _stat;
|
|
|
|
|
|
|
|
#else
|
2019-01-03 18:01:43 +00:00
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
using stat_t = struct stat;
|
2019-01-23 19:46:03 +00:00
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
#ifndef GUI // these functions aren't needed in GUI
|
|
|
|
|
|
|
|
bool FSLib::exists( const string &path ) {
|
|
|
|
stat_t path_stat;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _wstat( path.c_str(), &path_stat ) == 0;
|
|
|
|
#else
|
2019-01-04 19:19:08 +00:00
|
|
|
return stat( path.c_str(), &path_stat ) == 0;
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
string FSLib::canonical( const string &path ) {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
auto PATH_MAX = MAX_PATH;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char_t *canonical_path = new char_t[PATH_MAX];
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
auto failed = !PathCanonicalizeW( canonical_path, path.c_str() );
|
|
|
|
#else
|
|
|
|
auto failed = realpath( path.c_str(), canonical_path ) == nullptr;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( failed ) {
|
|
|
|
delete[] canonical_path;
|
|
|
|
return string();
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
2019-02-04 16:39:48 +00:00
|
|
|
|
|
|
|
string canonical_string{ canonical_path };
|
|
|
|
delete[] canonical_path;
|
2019-01-04 19:19:08 +00:00
|
|
|
return canonical_string;
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif // ndef GUI
|
|
|
|
|
|
|
|
bool FSLib::isDirectory( const string &path ) {
|
|
|
|
stat_t path_stat;
|
2019-01-23 19:46:03 +00:00
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
_wstat( path.c_str(), &path_stat );
|
|
|
|
#else
|
2019-01-23 19:46:03 +00:00
|
|
|
stat( path.c_str(), &path_stat );
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return S_ISDIR( path_stat.st_mode );
|
2019-01-23 19:46:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
bool FSLib::rename( const string &file_a, const string &file_b ) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return MoveFileExW( file_a.c_str(), file_b.c_str(),
|
|
|
|
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING );
|
|
|
|
#else
|
2019-01-04 19:19:08 +00:00
|
|
|
return ::rename( file_a.c_str(), file_b.c_str() ) == 0;
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif
|
2019-01-03 18:01:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 19:40:16 +00:00
|
|
|
FSLib::Directory::iterator FSLib::Directory::begin() {
|
2019-02-04 16:39:48 +00:00
|
|
|
return Iterator( *this );
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::begin() const {
|
2019-02-04 16:39:48 +00:00
|
|
|
return Iterator( *this );
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cbegin() const {
|
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
2019-02-04 16:39:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return Iterator( true );
|
|
|
|
#else
|
|
|
|
return Iterator( *this, nullptr );
|
|
|
|
#endif
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
2019-02-04 16:39:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return Iterator( true );
|
|
|
|
#else
|
|
|
|
return Iterator( *this, nullptr );
|
|
|
|
#endif
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::cend() const {
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
const char_t *FSLib::Directory::path() const {
|
2019-01-06 19:40:16 +00:00
|
|
|
return dir_path.c_str();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
char_t const *FSLib::Directory::Iterator::operator*() const {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return data.cFileName;
|
|
|
|
#else
|
2019-01-06 19:40:16 +00:00
|
|
|
return current_entry->d_name;
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2019-01-06 19:40:16 +00:00
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
2019-02-04 16:39:48 +00:00
|
|
|
if ( ended == true )
|
2019-01-06 19:40:16 +00:00
|
|
|
return *this;
|
2019-02-04 16:39:48 +00:00
|
|
|
// skip . and ..
|
|
|
|
if ( FindNextFileW( hFind, &data ) == 0 ) {
|
|
|
|
ended = true;
|
|
|
|
} else if ( !wcscmp( data.cFileName, L"." ) ||
|
|
|
|
!wcscmp( data.cFileName, L".." ) ) {
|
2019-01-07 21:21:02 +00:00
|
|
|
return operator++();
|
2019-02-04 16:39:48 +00:00
|
|
|
}
|
2019-01-06 19:40:16 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FSLib::Directory::Iterator FSLib::Directory::Iterator::operator++( int ) {
|
|
|
|
Iterator ret( *this );
|
2019-01-06 19:40:16 +00:00
|
|
|
operator++();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
bool FSLib::Directory::Iterator::operator==( const Iterator &i_other ) const {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return i_other.ended == ended;
|
|
|
|
#else
|
2019-01-06 19:40:16 +00:00
|
|
|
return i_other.current_entry == current_entry;
|
2019-02-04 16:39:48 +00:00
|
|
|
#endif
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:39:48 +00:00
|
|
|
bool FSLib::Directory::Iterator::operator!=( const Iterator &i_other ) const {
|
|
|
|
return !( i_other == *this );
|
2019-01-06 19:40:16 +00:00
|
|
|
}
|