diff options
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 58 |
1 files changed, 23 insertions, 35 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 9ada09f8a..6e2867658 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -69,9 +69,10 @@ static void StripTailDirSlashes(std::string &fname) | |||
| 69 | { | 69 | { |
| 70 | if (fname.length() > 1) | 70 | if (fname.length() > 1) |
| 71 | { | 71 | { |
| 72 | size_t i = fname.length() - 1; | 72 | size_t i = fname.length(); |
| 73 | while (fname[i] == DIR_SEP_CHR) | 73 | while (i > 0 && fname[i - 1] == DIR_SEP_CHR) |
| 74 | fname[i--] = '\0'; | 74 | --i; |
| 75 | fname.resize(i); | ||
| 75 | } | 76 | } |
| 76 | return; | 77 | return; |
| 77 | } | 78 | } |
| @@ -85,6 +86,10 @@ bool Exists(const std::string &filename) | |||
| 85 | StripTailDirSlashes(copy); | 86 | StripTailDirSlashes(copy); |
| 86 | 87 | ||
| 87 | #ifdef _WIN32 | 88 | #ifdef _WIN32 |
| 89 | // Windows needs a slash to identify a driver root | ||
| 90 | if (copy.size() != 0 && copy.back() == ':') | ||
| 91 | copy += DIR_SEP_CHR; | ||
| 92 | |||
| 88 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | 93 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); |
| 89 | #else | 94 | #else |
| 90 | int result = stat64(copy.c_str(), &file_info); | 95 | int result = stat64(copy.c_str(), &file_info); |
| @@ -102,6 +107,10 @@ bool IsDirectory(const std::string &filename) | |||
| 102 | StripTailDirSlashes(copy); | 107 | StripTailDirSlashes(copy); |
| 103 | 108 | ||
| 104 | #ifdef _WIN32 | 109 | #ifdef _WIN32 |
| 110 | // Windows needs a slash to identify a driver root | ||
| 111 | if (copy.size() != 0 && copy.back() == ':') | ||
| 112 | copy += DIR_SEP_CHR; | ||
| 113 | |||
| 105 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | 114 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); |
| 106 | #else | 115 | #else |
| 107 | int result = stat64(copy.c_str(), &file_info); | 116 | int result = stat64(copy.c_str(), &file_info); |
| @@ -824,13 +833,12 @@ size_t WriteStringToFile(bool text_file, const std::string &str, const char *fil | |||
| 824 | 833 | ||
| 825 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) | 834 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) |
| 826 | { | 835 | { |
| 827 | FileUtil::IOFile file(filename, text_file ? "r" : "rb"); | 836 | IOFile file(filename, text_file ? "r" : "rb"); |
| 828 | auto const f = file.GetHandle(); | ||
| 829 | 837 | ||
| 830 | if (!f) | 838 | if (!file) |
| 831 | return false; | 839 | return false; |
| 832 | 840 | ||
| 833 | str.resize(static_cast<u32>(GetSize(f))); | 841 | str.resize(static_cast<u32>(file.GetSize())); |
| 834 | return file.ReadArray(&str[0], str.size()); | 842 | return file.ReadArray(&str[0], str.size()); |
| 835 | } | 843 | } |
| 836 | 844 | ||
| @@ -877,15 +885,10 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 877 | } | 885 | } |
| 878 | 886 | ||
| 879 | IOFile::IOFile() | 887 | IOFile::IOFile() |
| 880 | : m_file(nullptr), m_good(true) | 888 | { |
| 881 | {} | 889 | } |
| 882 | |||
| 883 | IOFile::IOFile(std::FILE* file) | ||
| 884 | : m_file(file), m_good(true) | ||
| 885 | {} | ||
| 886 | 890 | ||
| 887 | IOFile::IOFile(const std::string& filename, const char openmode[]) | 891 | IOFile::IOFile(const std::string& filename, const char openmode[]) |
| 888 | : m_file(nullptr), m_good(true) | ||
| 889 | { | 892 | { |
| 890 | Open(filename, openmode); | 893 | Open(filename, openmode); |
| 891 | } | 894 | } |
| @@ -896,7 +899,6 @@ IOFile::~IOFile() | |||
| 896 | } | 899 | } |
| 897 | 900 | ||
| 898 | IOFile::IOFile(IOFile&& other) | 901 | IOFile::IOFile(IOFile&& other) |
| 899 | : m_file(nullptr), m_good(true) | ||
| 900 | { | 902 | { |
| 901 | Swap(other); | 903 | Swap(other); |
| 902 | } | 904 | } |
| @@ -935,26 +937,12 @@ bool IOFile::Close() | |||
| 935 | return m_good; | 937 | return m_good; |
| 936 | } | 938 | } |
| 937 | 939 | ||
| 938 | std::FILE* IOFile::ReleaseHandle() | 940 | u64 IOFile::GetSize() const |
| 939 | { | ||
| 940 | std::FILE* const ret = m_file; | ||
| 941 | m_file = nullptr; | ||
| 942 | return ret; | ||
| 943 | } | ||
| 944 | |||
| 945 | void IOFile::SetHandle(std::FILE* file) | ||
| 946 | { | ||
| 947 | Close(); | ||
| 948 | Clear(); | ||
| 949 | m_file = file; | ||
| 950 | } | ||
| 951 | |||
| 952 | u64 IOFile::GetSize() | ||
| 953 | { | 941 | { |
| 954 | if (IsOpen()) | 942 | if (IsOpen()) |
| 955 | return FileUtil::GetSize(m_file); | 943 | return FileUtil::GetSize(m_file); |
| 956 | else | 944 | |
| 957 | return 0; | 945 | return 0; |
| 958 | } | 946 | } |
| 959 | 947 | ||
| 960 | bool IOFile::Seek(s64 off, int origin) | 948 | bool IOFile::Seek(s64 off, int origin) |
| @@ -965,12 +953,12 @@ bool IOFile::Seek(s64 off, int origin) | |||
| 965 | return m_good; | 953 | return m_good; |
| 966 | } | 954 | } |
| 967 | 955 | ||
| 968 | u64 IOFile::Tell() | 956 | u64 IOFile::Tell() const |
| 969 | { | 957 | { |
| 970 | if (IsOpen()) | 958 | if (IsOpen()) |
| 971 | return ftello(m_file); | 959 | return ftello(m_file); |
| 972 | else | 960 | |
| 973 | return -1; | 961 | return -1; |
| 974 | } | 962 | } |
| 975 | 963 | ||
| 976 | bool IOFile::Flush() | 964 | bool IOFile::Flush() |