diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/assert.h | 2 | ||||
-rw-r--r-- | src/common/common_funcs.h | 4 | ||||
-rw-r--r-- | src/common/file_util.cpp | 93 | ||||
-rw-r--r-- | src/common/file_util.h | 63 | ||||
-rw-r--r-- | src/common/memory_util.cpp | 8 | ||||
-rw-r--r-- | src/common/swap.h | 2 |
6 files changed, 65 insertions, 107 deletions
diff --git a/src/common/assert.h b/src/common/assert.h index 655446f34..0d4eddc19 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -52,5 +52,5 @@ __declspec(noinline, noreturn) #define DEBUG_ASSERT_MSG(_a_, _desc_, ...) #endif -#define UNIMPLEMENTED() DEBUG_ASSERT_MSG(false, "Unimplemented code!") +#define UNIMPLEMENTED() LOG_CRITICAL(Debug, "Unimplemented code!") #define UNIMPLEMENTED_MSG(...) ASSERT_MSG(false, __VA_ARGS__) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 7cf7b7997..995938d0b 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -4,7 +4,7 @@ #pragma once -#if !defined(ARCHITECTURE_x86_64) && !defined(_M_ARM) +#if !defined(ARCHITECTURE_x86_64) && !defined(ARCHITECTURE_ARM) #include <cstdlib> // for exit #endif #include "common/common_types.h" @@ -30,7 +30,7 @@ #ifdef ARCHITECTURE_x86_64 #define Crash() __asm__ __volatile__("int $3") -#elif defined(_M_ARM) +#elif defined(ARCHITECTURE_ARM) #define Crash() __asm__ __volatile__("trap") #else #define Crash() exit(1) diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 493a81e01..7213abe18 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -2,7 +2,6 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include <sstream> #include "common/assert.h" #include "common/common_funcs.h" #include "common/common_paths.h" @@ -387,7 +386,7 @@ u64 GetSize(FILE* f) { bool CreateEmptyFile(const std::string& filename) { LOG_TRACE(Common_Filesystem, "{}", filename); - if (!FileUtil::IOFile(filename, "wb").IsOpen()) { + if (!FileUtil::IOFile(filename, "wb")) { LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); return false; } @@ -751,7 +750,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.IsOpen()) + if (!file) return false; str.resize(static_cast<u32>(file.GetSize())); @@ -800,57 +799,6 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam } } -std::vector<std::string> SplitPathComponents(const std::string& filename) { - auto copy(filename); - std::replace(copy.begin(), copy.end(), '\\', '/'); - std::vector<std::string> 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<size_t>(name_bck_index, name_fwd_index); - else - name_index = std::max<size_t>(name_bck_index, name_fwd_index); - - return out.erase(name_index); -} - -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) { @@ -872,6 +820,7 @@ 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) { @@ -888,15 +837,16 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags) m_file = fopen(filename.c_str(), openmode); #endif - return IsOpen(); + m_good = IsOpen(); + return m_good; } bool IOFile::Close() { if (!IsOpen() || 0 != std::fclose(m_file)) - return false; + m_good = false; m_file = nullptr; - return true; + return m_good; } u64 IOFile::GetSize() const { @@ -906,8 +856,11 @@ u64 IOFile::GetSize() const { return 0; } -bool IOFile::Seek(s64 off, int origin) const { - return IsOpen() && 0 == fseeko(m_file, off, origin); +bool IOFile::Seek(s64 off, int origin) { + if (!IsOpen() || 0 != fseeko(m_file, off, origin)) + m_good = false; + + return m_good; } u64 IOFile::Tell() const { @@ -918,20 +871,26 @@ u64 IOFile::Tell() const { } bool IOFile::Flush() { - return IsOpen() && 0 == std::fflush(m_file); + if (!IsOpen() || 0 != std::fflush(m_file)) + m_good = false; + + return m_good; } bool IOFile::Resize(u64 size) { - return IsOpen() && 0 == + if (!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 9bb3c4109..5bc7fbf7c 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -150,31 +150,6 @@ size_t ReadFileToString(bool text_file, const char* filename, std::string& str); void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, std::array<char, 4>& 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<std::string> SplitPathComponents(const std::string& filename); - -// Gets all of the text prior to the last '/' or '\' in the path. -std::string GetParentPath(const 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 <typename T> -std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) { - if (first >= last) - return {}; - last = std::min<size_t>(last, vector.size()); - return std::vector<T>(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 @@ -197,27 +172,41 @@ public: bool Close(); template <typename T> - size_t ReadArray(T* data, size_t length) const { + size_t ReadArray(T* data, size_t length) { static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); - if (!IsOpen()) + if (!IsOpen()) { + m_good = false; return -1; + } - return std::fread(data, sizeof(T), length, m_file); + size_t items_read = std::fread(data, sizeof(T), length, m_file); + if (items_read != length) + m_good = false; + + return items_read; } template <typename T> size_t WriteArray(const T* data, size_t length) { static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); - if (!IsOpen()) + + if (!IsOpen()) { + m_good = false; return -1; - return std::fwrite(data, sizeof(T), length, m_file); + } + + size_t items_written = std::fwrite(data, sizeof(T), length, m_file); + if (items_written != length) + m_good = false; + + return items_written; } template <typename T> - size_t ReadBytes(T* data, size_t length) const { + size_t ReadBytes(T* data, size_t length) { static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); return ReadArray(reinterpret_cast<char*>(data), length); } @@ -242,7 +231,15 @@ public: return nullptr != m_file; } - bool Seek(s64 off, int origin) const; + // 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); u64 Tell() const; u64 GetSize() const; bool Resize(u64 size); @@ -250,11 +247,13 @@ 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 diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp index 5d89209ed..09462ccee 100644 --- a/src/common/memory_util.cpp +++ b/src/common/memory_util.cpp @@ -16,7 +16,7 @@ #include <sys/mman.h> #endif -#if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) +#if !defined(_WIN32) && defined(ARCHITECTURE_x86_64) && !defined(MAP_32BIT) #include <unistd.h> #define PAGE_MASK (getpagesize() - 1) #define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) @@ -30,7 +30,7 @@ void* AllocateExecutableMemory(size_t size, bool low) { void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else static char* map_hint = nullptr; -#if defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) +#if defined(ARCHITECTURE_x86_64) && !defined(MAP_32BIT) // This OS has no flag to enforce allocation below the 4 GB boundary, // but if we hint that we want a low address it is very likely we will // get one. @@ -42,7 +42,7 @@ void* AllocateExecutableMemory(size_t size, bool low) { #endif void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE -#if defined(ARCHITECTURE_X64) && defined(MAP_32BIT) +#if defined(ARCHITECTURE_x86_64) && defined(MAP_32BIT) | (low ? MAP_32BIT : 0) #endif , @@ -57,7 +57,7 @@ void* AllocateExecutableMemory(size_t size, bool low) { #endif LOG_ERROR(Common_Memory, "Failed to allocate executable memory"); } -#if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) +#if !defined(_WIN32) && defined(ARCHITECTURE_x86_64) && !defined(MAP_32BIT) else { if (low) { map_hint += size; diff --git a/src/common/swap.h b/src/common/swap.h index 4a4012d1a..f025f7450 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -69,7 +69,7 @@ inline u32 swap32(u32 _data) { inline u64 swap64(u64 _data) { return _byteswap_uint64(_data); } -#elif _M_ARM +#elif ARCHITECTURE_ARM inline u16 swap16(u16 _data) { u32 data = _data; __asm__("rev16 %0, %1\n" : "=l"(data) : "l"(data)); |