diff options
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 93 |
1 files changed, 26 insertions, 67 deletions
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 @@ | |||
| 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> | ||
| 6 | #include "common/assert.h" | 5 | #include "common/assert.h" |
| 7 | #include "common/common_funcs.h" | 6 | #include "common/common_funcs.h" |
| 8 | #include "common/common_paths.h" | 7 | #include "common/common_paths.h" |
| @@ -387,7 +386,7 @@ u64 GetSize(FILE* f) { | |||
| 387 | bool CreateEmptyFile(const std::string& filename) { | 386 | bool CreateEmptyFile(const std::string& filename) { |
| 388 | LOG_TRACE(Common_Filesystem, "{}", filename); | 387 | LOG_TRACE(Common_Filesystem, "{}", filename); |
| 389 | 388 | ||
| 390 | if (!FileUtil::IOFile(filename, "wb").IsOpen()) { | 389 | if (!FileUtil::IOFile(filename, "wb")) { |
| 391 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); | 390 | LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); |
| 392 | return false; | 391 | return false; |
| 393 | } | 392 | } |
| @@ -751,7 +750,7 @@ size_t WriteStringToFile(bool text_file, const std::string& str, const char* fil | |||
| 751 | size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { | 750 | size_t ReadFileToString(bool text_file, const char* filename, std::string& str) { |
| 752 | IOFile file(filename, text_file ? "r" : "rb"); | 751 | IOFile file(filename, text_file ? "r" : "rb"); |
| 753 | 752 | ||
| 754 | if (!file.IsOpen()) | 753 | if (!file) |
| 755 | return false; | 754 | return false; |
| 756 | 755 | ||
| 757 | str.resize(static_cast<u32>(file.GetSize())); | 756 | str.resize(static_cast<u32>(file.GetSize())); |
| @@ -800,57 +799,6 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 800 | } | 799 | } |
| 801 | } | 800 | } |
| 802 | 801 | ||
| 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 | |||
| 854 | IOFile::IOFile() {} | 802 | IOFile::IOFile() {} |
| 855 | 803 | ||
| 856 | IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { | 804 | IOFile::IOFile(const std::string& filename, const char openmode[], int flags) { |
| @@ -872,6 +820,7 @@ IOFile& IOFile::operator=(IOFile&& other) noexcept { | |||
| 872 | 820 | ||
| 873 | void IOFile::Swap(IOFile& other) noexcept { | 821 | void IOFile::Swap(IOFile& other) noexcept { |
| 874 | std::swap(m_file, other.m_file); | 822 | std::swap(m_file, other.m_file); |
| 823 | std::swap(m_good, other.m_good); | ||
| 875 | } | 824 | } |
| 876 | 825 | ||
| 877 | bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { | 826 | 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) | |||
| 888 | m_file = fopen(filename.c_str(), openmode); | 837 | m_file = fopen(filename.c_str(), openmode); |
| 889 | #endif | 838 | #endif |
| 890 | 839 | ||
| 891 | return IsOpen(); | 840 | m_good = IsOpen(); |
| 841 | return m_good; | ||
| 892 | } | 842 | } |
| 893 | 843 | ||
| 894 | bool IOFile::Close() { | 844 | bool IOFile::Close() { |
| 895 | if (!IsOpen() || 0 != std::fclose(m_file)) | 845 | if (!IsOpen() || 0 != std::fclose(m_file)) |
| 896 | return false; | 846 | m_good = false; |
| 897 | 847 | ||
| 898 | m_file = nullptr; | 848 | m_file = nullptr; |
| 899 | return true; | 849 | return m_good; |
| 900 | } | 850 | } |
| 901 | 851 | ||
| 902 | u64 IOFile::GetSize() const { | 852 | u64 IOFile::GetSize() const { |
| @@ -906,8 +856,11 @@ u64 IOFile::GetSize() const { | |||
| 906 | return 0; | 856 | return 0; |
| 907 | } | 857 | } |
| 908 | 858 | ||
| 909 | bool IOFile::Seek(s64 off, int origin) const { | 859 | bool IOFile::Seek(s64 off, int origin) { |
| 910 | return IsOpen() && 0 == fseeko(m_file, off, origin); | 860 | if (!IsOpen() || 0 != fseeko(m_file, off, origin)) |
| 861 | m_good = false; | ||
| 862 | |||
| 863 | return m_good; | ||
| 911 | } | 864 | } |
| 912 | 865 | ||
| 913 | u64 IOFile::Tell() const { | 866 | u64 IOFile::Tell() const { |
| @@ -918,20 +871,26 @@ u64 IOFile::Tell() const { | |||
| 918 | } | 871 | } |
| 919 | 872 | ||
| 920 | bool IOFile::Flush() { | 873 | bool IOFile::Flush() { |
| 921 | return IsOpen() && 0 == std::fflush(m_file); | 874 | if (!IsOpen() || 0 != std::fflush(m_file)) |
| 875 | m_good = false; | ||
| 876 | |||
| 877 | return m_good; | ||
| 922 | } | 878 | } |
| 923 | 879 | ||
| 924 | bool IOFile::Resize(u64 size) { | 880 | bool IOFile::Resize(u64 size) { |
| 925 | return IsOpen() && 0 == | 881 | if (!IsOpen() || 0 != |
| 926 | #ifdef _WIN32 | 882 | #ifdef _WIN32 |
| 927 | // ector: _chsize sucks, not 64-bit safe | 883 | // ector: _chsize sucks, not 64-bit safe |
| 928 | // F|RES: changed to _chsize_s. i think it is 64-bit safe | 884 | // F|RES: changed to _chsize_s. i think it is 64-bit safe |
| 929 | _chsize_s(_fileno(m_file), size) | 885 | _chsize_s(_fileno(m_file), size) |
| 930 | #else | 886 | #else |
| 931 | // TODO: handle 64bit and growing | 887 | // TODO: handle 64bit and growing |
| 932 | ftruncate(fileno(m_file), size) | 888 | ftruncate(fileno(m_file), size) |
| 933 | #endif | 889 | #endif |
| 934 | ; | 890 | ) |
| 891 | m_good = false; | ||
| 892 | |||
| 893 | return m_good; | ||
| 935 | } | 894 | } |
| 936 | 895 | ||
| 937 | } // namespace FileUtil | 896 | } // namespace FileUtil |