game/mario/filesystem/windows/filesystem.cpp

156 lines
4.1 KiB
C++
Raw Normal View History

2022-06-21 12:50:39 +00:00
#include "../../filesystem.hpp"
#include <Shlwapi.h>
#include <cstring>
#include <stdexcept>
#include <windows.h>
char FSLib::dir_divisor = '\\';
2022-07-21 18:17:24 +00:00
FSLib::Directory::Directory(const string &path_) : dir_path(path_) {
2022-06-21 12:50:39 +00:00
// need to append \\* for windows to search files in directory
2022-07-21 18:17:24 +00:00
dir_path.append(L"\\*");
2022-06-21 12:50:39 +00:00
}
2022-07-21 18:17:24 +00:00
FSLib::Directory::Iterator::Iterator(const Directory &d_) {
if (!exists(d_.validPath()) || !isDirectory(d_.validPath())) {
2022-06-21 12:50:39 +00:00
throw std::runtime_error(
2022-07-21 18:17:24 +00:00
"Directory either doesn't exist or isn't a directory");
2022-06-21 12:50:39 +00:00
}
2022-07-21 18:17:24 +00:00
hFind = FindFirstFileW(d_.path(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
if (!wcscmp(data.cFileName, L".") || !wcscmp(data.cFileName, L"..")) {
++(*this);
2022-06-21 12:50:39 +00:00
}
} else {
ended = true;
}
}
FSLib::Directory::Iterator::~Iterator() {
2022-07-21 18:17:24 +00:00
if (hFind)
FindClose(hFind);
2022-06-21 12:50:39 +00:00
}
// 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
2022-07-21 18:17:24 +00:00
FSLib::Directory::Iterator::Iterator(bool ended_) : ended(ended_) {}
2022-06-21 12:50:39 +00:00
2022-07-21 18:17:24 +00:00
bool FSLib::exists(const string &path) {
2022-06-21 12:50:39 +00:00
struct _stat path_stat;
2022-07-21 18:17:24 +00:00
return _wstat(path.c_str(), &path_stat) == 0;
2022-06-21 12:50:39 +00:00
}
2022-07-21 18:17:24 +00:00
string FSLib::canonical(const string &path) {
2022-06-21 12:50:39 +00:00
char_t *full_path = new char_t[MAX_PATH];
char_t *canonical_path = new char_t[MAX_PATH];
2022-07-21 18:17:24 +00:00
auto failed = !GetFullPathName(path.c_str(), MAX_PATH, full_path, NULL);
2022-06-21 12:50:39 +00:00
2022-07-21 18:17:24 +00:00
if (failed) {
2022-06-21 12:50:39 +00:00
delete[] canonical_path;
delete[] full_path;
return string();
}
2022-07-21 18:17:24 +00:00
failed = !PathCanonicalizeW(canonical_path, full_path);
2022-06-21 12:50:39 +00:00
delete[] full_path;
2022-07-21 18:17:24 +00:00
if (failed) {
2022-06-21 12:50:39 +00:00
delete[] canonical_path;
return string();
}
string canonical_string{ canonical_path };
delete[] canonical_path;
return canonical_string;
}
2022-07-21 18:17:24 +00:00
bool FSLib::isDirectory(const string &path) {
2022-06-21 12:50:39 +00:00
struct _stat path_stat;
2022-07-21 18:17:24 +00:00
if (_wstat(path.c_str(), &path_stat) != 0)
2022-06-21 12:50:39 +00:00
return false;
return path_stat.st_mode & _S_IFDIR;
}
2022-07-21 18:17:24 +00:00
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);
2022-06-21 12:50:39 +00:00
}
2022-07-21 18:17:24 +00:00
bool FSLib::createDirectoryFull(const string &path) {
uint64_t pos = path.find_first_of(L":", 0) + 2;
if (pos == string::npos)
2022-06-21 12:50:39 +00:00
pos = 0;
// go through all directories leading to the last one
// and create them if they don't exist
do {
// get partial directory path
2022-07-21 18:17:24 +00:00
pos = path.find_first_of(L"\\", pos);
auto dirname = path.substr(0, pos);
2022-06-21 12:50:39 +00:00
// create it if it doesn't exist
2022-07-21 18:17:24 +00:00
if (!FSLib::exists(dirname)) {
if (!CreateDirectoryW(dirname.c_str(), NULL))
2022-06-21 12:50:39 +00:00
return false;
}
pos++;
2022-07-21 18:17:24 +00:00
} while (pos < path.length() && pos != 0);
2022-06-21 12:50:39 +00:00
return true;
}
FSLib::Directory::iterator FSLib::Directory::end() {
2022-07-21 18:17:24 +00:00
return Iterator(true);
2022-06-21 12:50:39 +00:00
}
FSLib::Directory::const_iterator FSLib::Directory::end() const {
2022-07-21 18:17:24 +00:00
return Iterator(true);
2022-06-21 12:50:39 +00:00
}
char_t const *FSLib::Directory::Iterator::operator*() const {
return data.cFileName;
}
FSLib::Directory::Iterator &FSLib::Directory::Iterator::operator++() {
2022-07-21 18:17:24 +00:00
if (ended == true)
2022-06-21 12:50:39 +00:00
return *this;
// skip . and ..
2022-07-21 18:17:24 +00:00
if (FindNextFileW(hFind, &data) == 0) {
2022-06-21 12:50:39 +00:00
ended = true;
2022-07-21 18:17:24 +00:00
} else if (!wcscmp(data.cFileName, L".") ||
!wcscmp(data.cFileName, L"..")) {
2022-06-21 12:50:39 +00:00
return operator++();
}
return *this;
}
2022-07-21 18:17:24 +00:00
bool FSLib::Directory::Iterator::operator==(const Iterator &i_other) const {
2022-06-21 12:50:39 +00:00
return i_other.ended == ended;
}
2022-07-21 18:17:24 +00:00
string FSLib::getContainingDirectory(const string &path) {
2022-06-21 12:50:39 +00:00
auto pos = path.find_last_of('\\');
2022-07-21 18:17:24 +00:00
if (pos == string::npos) {
2022-06-21 12:50:39 +00:00
return ".";
}
return path.substr(0, pos);
}
2022-07-21 18:17:24 +00:00
string FSLib::getFileName(const string &path) {
2022-06-21 12:50:39 +00:00
auto pos = path.find_last_of('\\');
2022-07-21 18:17:24 +00:00
if (pos == string::npos) {
2022-06-21 12:50:39 +00:00
return path;
}
2022-07-21 18:17:24 +00:00
return path.substr(pos + 1);
2022-06-21 12:50:39 +00:00
}
2022-07-21 18:17:24 +00:00
string FSLib::getFileExtension(const string &path) {
2022-06-21 12:50:39 +00:00
auto pos = path.find_last_of('.');
2022-07-21 18:17:24 +00:00
if (pos == string::npos) {
2022-06-21 12:50:39 +00:00
return "";
}
return path.substr(pos + 1);
}