From 29aff8d5ab46c8d0199aa4bfa7eeff5d4fa2d7ef Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Wed, 18 Jul 2018 21:07:11 -0400 Subject: Virtual Filesystem 2: Electric Boogaloo (#676) * Virtual Filesystem * Fix delete bug and documentate * Review fixes + other stuff * Fix puyo regression --- src/common/file_util.cpp | 107 +++++++++++++++++++++++++++++++++++------------ src/common/file_util.h | 66 +++++++++++++++-------------- 2 files changed, 116 insertions(+), 57 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7213abe18..bf955386c 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/assert.h" #include "common/common_funcs.h" #include "common/common_paths.h" @@ -386,7 +387,7 @@ u64 GetSize(FILE* f) { bool CreateEmptyFile(const std::string& filename) { LOG_TRACE(Common_Filesystem, "{}", filename); - if (!FileUtil::IOFile(filename, "wb")) { + if (!FileUtil::IOFile(filename, "wb").IsOpen()) { LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); return false; } @@ -750,7 +751,7 @@ size_t WriteStringToFile(bool text_file, const std::string& str, const char* fil size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { IOFile file(filename, text_file ? "r" : "rb"); - if (!file) + if (!file.IsOpen()) return false; str.resize(static_cast(file.GetSize())); @@ -799,6 +800,71 @@ void SplitFilename83(const std::string& filename, std::array& short_nam } } +std::vector SplitPathComponents(const std::string& filename) { + auto copy(filename); + std::replace(copy.begin(), copy.end(), '\\', '/'); + std::vector out; + + std::stringstream stream(filename); + std::string item; + while (std::getline(stream, item, '/')) + out.push_back(std::move(item)); + + return out; +} + +std::string GetParentPath(const std::string& path) { + auto out = path; + const auto name_bck_index = out.find_last_of('\\'); + const auto name_fwd_index = out.find_last_of('/'); + size_t name_index; + if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos) + name_index = std::min(name_bck_index, name_fwd_index); + else + name_index = std::max(name_bck_index, name_fwd_index); + + return out.erase(name_index); +} + +std::string GetPathWithoutTop(std::string path) { + if (path.empty()) + return ""; + while (path[0] == '\\' || path[0] == '/') { + path = path.substr(1); + if (path.empty()) + return ""; + } + const auto name_bck_index = path.find_first_of('\\'); + const auto name_fwd_index = path.find_first_of('/'); + return path.substr(std::min(name_bck_index, name_fwd_index) + 1); + return path.substr(std::min(name_bck_index, name_fwd_index) + 1); +} + +std::string GetFilename(std::string path) { + std::replace(path.begin(), path.end(), '\\', '/'); + auto name_index = path.find_last_of('/'); + if (name_index == std::string::npos) + return ""; + return path.substr(name_index + 1); +} + +std::string GetExtensionFromFilename(const std::string& name) { + size_t index = name.find_last_of('.'); + if (index == std::string::npos) + return ""; + + return name.substr(index + 1); +} + +std::string RemoveTrailingSlash(const std::string& path) { + if (path.empty()) + return path; + if (path.back() == '\\' || path.back() == '/') + return path.substr(0, path.size() - 1); + + return path; +} + IOFile::IOFile() {} IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { @@ -820,7 +886,6 @@ IOFile& IOFile::operator=(IOFile&& other) noexcept { void IOFile::Swap(IOFile& other) noexcept { std::swap(m_file, other.m_file); - std::swap(m_good, other.m_good); } bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { @@ -837,16 +902,15 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags) m_file = fopen(filename.c_str(), openmode); #endif - m_good = IsOpen(); - return m_good; + return IsOpen(); } bool IOFile::Close() { if (!IsOpen() || 0 != std::fclose(m_file)) - m_good = false; + return false; m_file = nullptr; - return m_good; + return true; } u64 IOFile::GetSize() const { @@ -856,11 +920,8 @@ u64 IOFile::GetSize() const { return 0; } -bool IOFile::Seek(s64 off, int origin) { - if (!IsOpen() || 0 != fseeko(m_file, off, origin)) - m_good = false; - - return m_good; +bool IOFile::Seek(s64 off, int origin) const { + return IsOpen() && 0 == fseeko(m_file, off, origin); } u64 IOFile::Tell() const { @@ -871,26 +932,20 @@ u64 IOFile::Tell() const { } bool IOFile::Flush() { - if (!IsOpen() || 0 != std::fflush(m_file)) - m_good = false; - - return m_good; + return IsOpen() && 0 == std::fflush(m_file); } bool IOFile::Resize(u64 size) { - if (!IsOpen() || 0 != + return IsOpen() && 0 == #ifdef _WIN32 - // ector: _chsize sucks, not 64-bit safe - // F|RES: changed to _chsize_s. i think it is 64-bit safe - _chsize_s(_fileno(m_file), size) + // ector: _chsize sucks, not 64-bit safe + // F|RES: changed to _chsize_s. i think it is 64-bit safe + _chsize_s(_fileno(m_file), size) #else - // TODO: handle 64bit and growing - ftruncate(fileno(m_file), size) + // TODO: handle 64bit and growing + ftruncate(fileno(m_file), size) #endif - ) - m_good = false; - - return m_good; + ; } } // namespace FileUtil diff --git a/src/common/file_util.h b/src/common/file_util.h index 5bc7fbf7c..026c84d94 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -150,6 +150,34 @@ size_t ReadFileToString(bool text_file, const char* filename, std::string& str); void SplitFilename83(const std::string& filename, std::array& short_name, std::array& extension); +// Splits the path on '/' or '\' and put the components into a vector +// i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } +std::vector SplitPathComponents(const std::string& filename); + +// Gets all of the text up to the last '/' or '\' in the path. +std::string GetParentPath(const std::string& path); + +// Gets all of the text after the first '/' or '\' in the path. +std::string GetPathWithoutTop(std::string path); + +// Gets the filename of the path +std::string GetFilename(std::string path); + +// Gets the extension of the filename +std::string GetExtensionFromFilename(const std::string& name); + +// Removes the final '/' or '\' if one exists +std::string RemoveTrailingSlash(const std::string& path); + +// Creates a new vector containing indices [first, last) from the original. +template +std::vector SliceVector(const std::vector& vector, size_t first, size_t last) { + if (first >= last) + return {}; + last = std::min(last, vector.size()); + return std::vector(vector.begin() + first, vector.begin() + first + last); +} + // simple wrapper for cstdlib file functions to // hopefully will make error checking easier // and make forgetting an fclose() harder @@ -172,41 +200,27 @@ public: bool Close(); template - size_t ReadArray(T* data, size_t length) { + size_t ReadArray(T* data, size_t length) const { static_assert(std::is_trivially_copyable(), "Given array does not consist of trivially copyable objects"); - if (!IsOpen()) { - m_good = false; + if (!IsOpen()) return -1; - } - - size_t items_read = std::fread(data, sizeof(T), length, m_file); - if (items_read != length) - m_good = false; - return items_read; + return std::fread(data, sizeof(T), length, m_file); } template size_t WriteArray(const T* data, size_t length) { static_assert(std::is_trivially_copyable(), "Given array does not consist of trivially copyable objects"); - - if (!IsOpen()) { - m_good = false; + if (!IsOpen()) return -1; - } - - size_t items_written = std::fwrite(data, sizeof(T), length, m_file); - if (items_written != length) - m_good = false; - - return items_written; + return std::fwrite(data, sizeof(T), length, m_file); } template - size_t ReadBytes(T* data, size_t length) { + size_t ReadBytes(T* data, size_t length) const { static_assert(std::is_trivially_copyable(), "T must be trivially copyable"); return ReadArray(reinterpret_cast(data), length); } @@ -231,15 +245,7 @@ public: return nullptr != m_file; } - // m_good is set to false when a read, write or other function fails - bool IsGood() const { - return m_good; - } - explicit operator bool() const { - return IsGood(); - } - - bool Seek(s64 off, int origin); + bool Seek(s64 off, int origin) const; u64 Tell() const; u64 GetSize() const; bool Resize(u64 size); @@ -247,13 +253,11 @@ public: // clear error state void Clear() { - m_good = true; std::clearerr(m_file); } private: std::FILE* m_file = nullptr; - bool m_good = true; }; } // namespace FileUtil -- cgit v1.2.3