diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/file_util.cpp | 93 | ||||
| -rw-r--r-- | src/common/file_util.h | 63 |
2 files changed, 99 insertions, 57 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7213abe18..493a81e01 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <sstream> | ||
| 5 | #include "common/assert.h" | 6 | #include "common/assert.h" |
| 6 | #include "common/common_funcs.h" | 7 | #include "common/common_funcs.h" |
| 7 | #include "common/common_paths.h" | 8 | #include "common/common_paths.h" |
| @@ -386,7 +387,7 @@ u64 GetSize(FILE* f) { | |||
| 386 | bool CreateEmptyFile(const std::string& filename) { | 387 | bool CreateEmptyFile(const std::string& filename) { |
| 387 | LOG_TRACE(Common_Filesystem, "{}", filename); | 388 | LOG_TRACE(Common_Filesystem, "{}", filename); |
| 388 | 389 | ||
| 389 | if (!FileUtil::IOFile(filename, "wb")) { | 390 | if (!FileUtil::IOFile(filename, "wb").IsOpen()) { |
| 390 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); | 391 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); |
| 391 | return false; | 392 | return false; |
| 392 | } | 393 | } |
| @@ -750,7 +751,7 @@ size_t WriteStringToFile(bool text_file, const std::string& str, const char* fil | |||
| 750 | size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { | 751 | size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { |
| 751 | IOFile file(filename, text_file ? "r" : "rb"); | 752 | IOFile file(filename, text_file ? "r" : "rb"); |
| 752 | 753 | ||
| 753 | if (!file) | 754 | if (!file.IsOpen()) |
| 754 | return false; | 755 | return false; |
| 755 | 756 | ||
| 756 | str.resize(static_cast<u32>(file.GetSize())); | 757 | str.resize(static_cast<u32>(file.GetSize())); |
| @@ -799,6 +800,57 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 799 | } | 800 | } |
| 800 | } | 801 | } |
| 801 | 802 | ||
| 803 | std::vector<std::string> SplitPathComponents(const std::string& filename) { | ||
| 804 | auto copy(filename); | ||
| 805 | std::replace(copy.begin(), copy.end(), '\\', '/'); | ||
| 806 | std::vector<std::string> out; | ||
| 807 | |||
| 808 | std::stringstream stream(filename); | ||
| 809 | std::string item; | ||
| 810 | while (std::getline(stream, item, '/')) | ||
| 811 | out.push_back(std::move(item)); | ||
| 812 | |||
| 813 | return out; | ||
| 814 | } | ||
| 815 | |||
| 816 | std::string GetParentPath(const std::string& path) { | ||
| 817 | auto out = path; | ||
| 818 | const auto name_bck_index = out.find_last_of('\\'); | ||
| 819 | const auto name_fwd_index = out.find_last_of('/'); | ||
| 820 | size_t name_index; | ||
| 821 | if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos) | ||
| 822 | name_index = std::min<size_t>(name_bck_index, name_fwd_index); | ||
| 823 | else | ||
| 824 | name_index = std::max<size_t>(name_bck_index, name_fwd_index); | ||
| 825 | |||
| 826 | return out.erase(name_index); | ||
| 827 | } | ||
| 828 | |||
| 829 | std::string GetFilename(std::string path) { | ||
| 830 | std::replace(path.begin(), path.end(), '\\', '/'); | ||
| 831 | auto name_index = path.find_last_of('/'); | ||
| 832 | if (name_index == std::string::npos) | ||
| 833 | return ""; | ||
| 834 | return path.substr(name_index + 1); | ||
| 835 | } | ||
| 836 | |||
| 837 | std::string GetExtensionFromFilename(const std::string& name) { | ||
| 838 | size_t index = name.find_last_of('.'); | ||
| 839 | if (index == std::string::npos) | ||
| 840 | return ""; | ||
| 841 | |||
| 842 | return name.substr(index + 1); | ||
| 843 | } | ||
| 844 | |||
| 845 | std::string RemoveTrailingSlash(const std::string& path) { | ||
| 846 | if (path.empty()) | ||
| 847 | return path; | ||
| 848 | if (path.back() == '\\' || path.back() == '/') | ||
| 849 | return path.substr(0, path.size() - 1); | ||
| 850 | |||
| 851 | return path; | ||
| 852 | } | ||
| 853 | |||
| 802 | IOFile::IOFile() {} | 854 | IOFile::IOFile() {} |
| 803 | 855 | ||
| 804 | IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { | 856 | IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { |
| @@ -820,7 +872,6 @@ IOFile& IOFile::operator=(IOFile&& other) noexcept { | |||
| 820 | 872 | ||
| 821 | void IOFile::Swap(IOFile& other) noexcept { | 873 | void IOFile::Swap(IOFile& other) noexcept { |
| 822 | std::swap(m_file, other.m_file); | 874 | std::swap(m_file, other.m_file); |
| 823 | std::swap(m_good, other.m_good); | ||
| 824 | } | 875 | } |
| 825 | 876 | ||
| 826 | bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { | 877 | bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { |
| @@ -837,16 +888,15 @@ bool IOFile::Open(const std::string& filename, const char openmode[], int flags) | |||
| 837 | m_file = fopen(filename.c_str(), openmode); | 888 | m_file = fopen(filename.c_str(), openmode); |
| 838 | #endif | 889 | #endif |
| 839 | 890 | ||
| 840 | m_good = IsOpen(); | 891 | return IsOpen(); |
| 841 | return m_good; | ||
| 842 | } | 892 | } |
| 843 | 893 | ||
| 844 | bool IOFile::Close() { | 894 | bool IOFile::Close() { |
| 845 | if (!IsOpen() || 0 != std::fclose(m_file)) | 895 | if (!IsOpen() || 0 != std::fclose(m_file)) |
| 846 | m_good = false; | 896 | return false; |
| 847 | 897 | ||
| 848 | m_file = nullptr; | 898 | m_file = nullptr; |
| 849 | return m_good; | 899 | return true; |
| 850 | } | 900 | } |
| 851 | 901 | ||
| 852 | u64 IOFile::GetSize() const { | 902 | u64 IOFile::GetSize() const { |
| @@ -856,11 +906,8 @@ u64 IOFile::GetSize() const { | |||
| 856 | return 0; | 906 | return 0; |
| 857 | } | 907 | } |
| 858 | 908 | ||
| 859 | bool IOFile::Seek(s64 off, int origin) { | 909 | bool IOFile::Seek(s64 off, int origin) const { |
| 860 | if (!IsOpen() || 0 != fseeko(m_file, off, origin)) | 910 | return IsOpen() && 0 == fseeko(m_file, off, origin); |
| 861 | m_good = false; | ||
| 862 | |||
| 863 | return m_good; | ||
| 864 | } | 911 | } |
| 865 | 912 | ||
| 866 | u64 IOFile::Tell() const { | 913 | u64 IOFile::Tell() const { |
| @@ -871,26 +918,20 @@ u64 IOFile::Tell() const { | |||
| 871 | } | 918 | } |
| 872 | 919 | ||
| 873 | bool IOFile::Flush() { | 920 | bool IOFile::Flush() { |
| 874 | if (!IsOpen() || 0 != std::fflush(m_file)) | 921 | return IsOpen() && 0 == std::fflush(m_file); |
| 875 | m_good = false; | ||
| 876 | |||
| 877 | return m_good; | ||
| 878 | } | 922 | } |
| 879 | 923 | ||
| 880 | bool IOFile::Resize(u64 size) { | 924 | bool IOFile::Resize(u64 size) { |
| 881 | if (!IsOpen() || 0 != | 925 | return IsOpen() && 0 == |
| 882 | #ifdef _WIN32 | 926 | #ifdef _WIN32 |
| 883 | // ector: _chsize sucks, not 64-bit safe | 927 | // ector: _chsize sucks, not 64-bit safe |
| 884 | // F|RES: changed to _chsize_s. i think it is 64-bit safe | 928 | // F|RES: changed to _chsize_s. i think it is 64-bit safe |
| 885 | _chsize_s(_fileno(m_file), size) | 929 | _chsize_s(_fileno(m_file), size) |
| 886 | #else | 930 | #else |
| 887 | // TODO: handle 64bit and growing | 931 | // TODO: handle 64bit and growing |
| 888 | ftruncate(fileno(m_file), size) | 932 | ftruncate(fileno(m_file), size) |
| 889 | #endif | 933 | #endif |
| 890 | ) | 934 | ; |
| 891 | m_good = false; | ||
| 892 | |||
| 893 | return m_good; | ||
| 894 | } | 935 | } |
| 895 | 936 | ||
| 896 | } // namespace FileUtil | 937 | } // namespace FileUtil |
diff --git a/src/common/file_util.h b/src/common/file_util.h index 5bc7fbf7c..9bb3c4109 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -150,6 +150,31 @@ size_t ReadFileToString(bool text_file, const char* filename, std::string& str); | |||
| 150 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, | 150 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, |
| 151 | std::array<char, 4>& extension); | 151 | std::array<char, 4>& extension); |
| 152 | 152 | ||
| 153 | // Splits the path on '/' or '\' and put the components into a vector | ||
| 154 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } | ||
| 155 | std::vector<std::string> SplitPathComponents(const std::string& filename); | ||
| 156 | |||
| 157 | // Gets all of the text prior to the last '/' or '\' in the path. | ||
| 158 | std::string GetParentPath(const std::string& path); | ||
| 159 | |||
| 160 | // Gets the filename of the path | ||
| 161 | std::string GetFilename(std::string path); | ||
| 162 | |||
| 163 | // Gets the extension of the filename | ||
| 164 | std::string GetExtensionFromFilename(const std::string& name); | ||
| 165 | |||
| 166 | // Removes the final '/' or '\' if one exists | ||
| 167 | std::string RemoveTrailingSlash(const std::string& path); | ||
| 168 | |||
| 169 | // Creates a new vector containing indices [first, last) from the original. | ||
| 170 | template <typename T> | ||
| 171 | std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t last) { | ||
| 172 | if (first >= last) | ||
| 173 | return {}; | ||
| 174 | last = std::min<size_t>(last, vector.size()); | ||
| 175 | return std::vector<T>(vector.begin() + first, vector.begin() + first + last); | ||
| 176 | } | ||
| 177 | |||
| 153 | // simple wrapper for cstdlib file functions to | 178 | // simple wrapper for cstdlib file functions to |
| 154 | // hopefully will make error checking easier | 179 | // hopefully will make error checking easier |
| 155 | // and make forgetting an fclose() harder | 180 | // and make forgetting an fclose() harder |
| @@ -172,41 +197,27 @@ public: | |||
| 172 | bool Close(); | 197 | bool Close(); |
| 173 | 198 | ||
| 174 | template <typename T> | 199 | template <typename T> |
| 175 | size_t ReadArray(T* data, size_t length) { | 200 | size_t ReadArray(T* data, size_t length) const { |
| 176 | static_assert(std::is_trivially_copyable<T>(), | 201 | static_assert(std::is_trivially_copyable<T>(), |
| 177 | "Given array does not consist of trivially copyable objects"); | 202 | "Given array does not consist of trivially copyable objects"); |
| 178 | 203 | ||
| 179 | if (!IsOpen()) { | 204 | if (!IsOpen()) |
| 180 | m_good = false; | ||
| 181 | return -1; | 205 | return -1; |
| 182 | } | ||
| 183 | 206 | ||
| 184 | size_t items_read = std::fread(data, sizeof(T), length, m_file); | 207 | return std::fread(data, sizeof(T), length, m_file); |
| 185 | if (items_read != length) | ||
| 186 | m_good = false; | ||
| 187 | |||
| 188 | return items_read; | ||
| 189 | } | 208 | } |
| 190 | 209 | ||
| 191 | template <typename T> | 210 | template <typename T> |
| 192 | size_t WriteArray(const T* data, size_t length) { | 211 | size_t WriteArray(const T* data, size_t length) { |
| 193 | static_assert(std::is_trivially_copyable<T>(), | 212 | static_assert(std::is_trivially_copyable<T>(), |
| 194 | "Given array does not consist of trivially copyable objects"); | 213 | "Given array does not consist of trivially copyable objects"); |
| 195 | 214 | if (!IsOpen()) | |
| 196 | if (!IsOpen()) { | ||
| 197 | m_good = false; | ||
| 198 | return -1; | 215 | return -1; |
| 199 | } | 216 | return std::fwrite(data, sizeof(T), length, m_file); |
| 200 | |||
| 201 | size_t items_written = std::fwrite(data, sizeof(T), length, m_file); | ||
| 202 | if (items_written != length) | ||
| 203 | m_good = false; | ||
| 204 | |||
| 205 | return items_written; | ||
| 206 | } | 217 | } |
| 207 | 218 | ||
| 208 | template <typename T> | 219 | template <typename T> |
| 209 | size_t ReadBytes(T* data, size_t length) { | 220 | size_t ReadBytes(T* data, size_t length) const { |
| 210 | static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); | 221 | static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); |
| 211 | return ReadArray(reinterpret_cast<char*>(data), length); | 222 | return ReadArray(reinterpret_cast<char*>(data), length); |
| 212 | } | 223 | } |
| @@ -231,15 +242,7 @@ public: | |||
| 231 | return nullptr != m_file; | 242 | return nullptr != m_file; |
| 232 | } | 243 | } |
| 233 | 244 | ||
| 234 | // m_good is set to false when a read, write or other function fails | 245 | bool Seek(s64 off, int origin) const; |
| 235 | bool IsGood() const { | ||
| 236 | return m_good; | ||
| 237 | } | ||
| 238 | explicit operator bool() const { | ||
| 239 | return IsGood(); | ||
| 240 | } | ||
| 241 | |||
| 242 | bool Seek(s64 off, int origin); | ||
| 243 | u64 Tell() const; | 246 | u64 Tell() const; |
| 244 | u64 GetSize() const; | 247 | u64 GetSize() const; |
| 245 | bool Resize(u64 size); | 248 | bool Resize(u64 size); |
| @@ -247,13 +250,11 @@ public: | |||
| 247 | 250 | ||
| 248 | // clear error state | 251 | // clear error state |
| 249 | void Clear() { | 252 | void Clear() { |
| 250 | m_good = true; | ||
| 251 | std::clearerr(m_file); | 253 | std::clearerr(m_file); |
| 252 | } | 254 | } |
| 253 | 255 | ||
| 254 | private: | 256 | private: |
| 255 | std::FILE* m_file = nullptr; | 257 | std::FILE* m_file = nullptr; |
| 256 | bool m_good = true; | ||
| 257 | }; | 258 | }; |
| 258 | 259 | ||
| 259 | } // namespace FileUtil | 260 | } // namespace FileUtil |