101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
#include <Shlwapi.h>
|
|
#include <windows.h>
|
|
#include "../filesystem.hpp"
|
|
|
|
FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) {
|
|
// need to append \\* for windows to search files in directory
|
|
dir_path.append( L"\\*" );
|
|
}
|
|
|
|
FSLib::Directory::Iterator::Iterator( const Directory &d_ ) {
|
|
hFind = FindFirstFileW( d_.path(), &data );
|
|
if ( hFind != INVALID_HANDLE_VALUE ) {
|
|
if ( !wcscmp( data.cFileName, L"." ) ||
|
|
!wcscmp( data.cFileName, L".." ) ) {
|
|
++( *this );
|
|
}
|
|
} else {
|
|
ended = true;
|
|
}
|
|
}
|
|
|
|
FSLib::Directory::Iterator::~Iterator() {
|
|
if ( hFind )
|
|
FindClose( hFind );
|
|
}
|
|
|
|
// this is definitely not a good way to create the "end" iterator
|
|
// but it was the only way I thought of with my limited knowledge of
|
|
// windows.h
|
|
FSLib::Directory::Iterator::Iterator( bool ended_ ) : ended( ended_ ) {}
|
|
|
|
#ifndef GUI // these functions aren't needed in GUI
|
|
|
|
bool FSLib::exists( const string &path ) {
|
|
struct _stat path_stat;
|
|
return _wstat( path.c_str(), &path_stat ) == 0;
|
|
}
|
|
|
|
string FSLib::canonical( const string &path ) {
|
|
char_t *canonical_path = new char_t[MAX_PATH];
|
|
|
|
auto failed = !PathCanonicalizeW( canonical_path, path.c_str() );
|
|
|
|
if ( failed ) {
|
|
delete[] canonical_path;
|
|
return string();
|
|
}
|
|
|
|
string canonical_string{ canonical_path };
|
|
delete[] canonical_path;
|
|
return canonical_string;
|
|
}
|
|
|
|
#endif // ndef GUI
|
|
|
|
bool FSLib::isDirectory( const string &path ) {
|
|
struct _stat path_stat;
|
|
|
|
_wstat( path.c_str(), &path_stat );
|
|
|
|
return path_stat.st_mode & _S_IFDIR;
|
|
}
|
|
|
|
bool FSLib::rename( const string &file_a, const string &file_b ) {
|
|
return MoveFileExW( file_a.c_str(), file_b.c_str(),
|
|
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING );
|
|
}
|
|
|
|
FSLib::Directory::iterator FSLib::Directory::end() {
|
|
return Iterator( true );
|
|
}
|
|
|
|
FSLib::Directory::const_iterator FSLib::Directory::end() const {
|
|
return Iterator( true );
|
|
}
|
|
|
|
const char_t *FSLib::Directory::path() const {
|
|
return dir_path.c_str();
|
|
}
|
|
|
|
char_t const *FSLib::Directory::Iterator::operator*() const {
|
|
return data.cFileName;
|
|
}
|
|
|
|
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
|
|
if ( ended == true )
|
|
return *this;
|
|
// skip . and ..
|
|
if ( FindNextFileW( hFind, &data ) == 0 ) {
|
|
ended = true;
|
|
} else if ( !wcscmp( data.cFileName, L"." ) ||
|
|
!wcscmp( data.cFileName, L".." ) ) {
|
|
return operator++();
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
bool FSLib::Directory::Iterator::operator==( const Iterator &i_other ) const {
|
|
return i_other.ended == ended;
|
|
}
|