diff options
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 94 |
1 files changed, 45 insertions, 49 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index c3061479a..53700c865 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -85,7 +85,7 @@ bool Exists(const std::string &filename) | |||
| 85 | StripTailDirSlashes(copy); | 85 | StripTailDirSlashes(copy); |
| 86 | 86 | ||
| 87 | #ifdef _WIN32 | 87 | #ifdef _WIN32 |
| 88 | int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); | 88 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); |
| 89 | #else | 89 | #else |
| 90 | int result = stat64(copy.c_str(), &file_info); | 90 | int result = stat64(copy.c_str(), &file_info); |
| 91 | #endif | 91 | #endif |
| @@ -102,7 +102,7 @@ bool IsDirectory(const std::string &filename) | |||
| 102 | StripTailDirSlashes(copy); | 102 | StripTailDirSlashes(copy); |
| 103 | 103 | ||
| 104 | #ifdef _WIN32 | 104 | #ifdef _WIN32 |
| 105 | int result = _tstat64(Common::UTF8ToTStr(copy).c_str(), &file_info); | 105 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); |
| 106 | #else | 106 | #else |
| 107 | int result = stat64(copy.c_str(), &file_info); | 107 | int result = stat64(copy.c_str(), &file_info); |
| 108 | #endif | 108 | #endif |
| @@ -138,7 +138,7 @@ bool Delete(const std::string &filename) | |||
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | #ifdef _WIN32 | 140 | #ifdef _WIN32 |
| 141 | if (!DeleteFile(Common::UTF8ToTStr(filename).c_str())) | 141 | if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) |
| 142 | { | 142 | { |
| 143 | LOG_ERROR(Common_Filesystem, "DeleteFile failed on %s: %s", | 143 | LOG_ERROR(Common_Filesystem, "DeleteFile failed on %s: %s", |
| 144 | filename.c_str(), GetLastErrorMsg()); | 144 | filename.c_str(), GetLastErrorMsg()); |
| @@ -160,7 +160,7 @@ bool CreateDir(const std::string &path) | |||
| 160 | { | 160 | { |
| 161 | LOG_TRACE(Common_Filesystem, "directory %s", path.c_str()); | 161 | LOG_TRACE(Common_Filesystem, "directory %s", path.c_str()); |
| 162 | #ifdef _WIN32 | 162 | #ifdef _WIN32 |
| 163 | if (::CreateDirectory(Common::UTF8ToTStr(path).c_str(), nullptr)) | 163 | if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr)) |
| 164 | return true; | 164 | return true; |
| 165 | DWORD error = GetLastError(); | 165 | DWORD error = GetLastError(); |
| 166 | if (error == ERROR_ALREADY_EXISTS) | 166 | if (error == ERROR_ALREADY_EXISTS) |
| @@ -241,7 +241,7 @@ bool DeleteDir(const std::string &filename) | |||
| 241 | } | 241 | } |
| 242 | 242 | ||
| 243 | #ifdef _WIN32 | 243 | #ifdef _WIN32 |
| 244 | if (::RemoveDirectory(Common::UTF8ToTStr(filename).c_str())) | 244 | if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) |
| 245 | return true; | 245 | return true; |
| 246 | #else | 246 | #else |
| 247 | if (rmdir(filename.c_str()) == 0) | 247 | if (rmdir(filename.c_str()) == 0) |
| @@ -257,8 +257,13 @@ bool Rename(const std::string &srcFilename, const std::string &destFilename) | |||
| 257 | { | 257 | { |
| 258 | LOG_TRACE(Common_Filesystem, "%s --> %s", | 258 | LOG_TRACE(Common_Filesystem, "%s --> %s", |
| 259 | srcFilename.c_str(), destFilename.c_str()); | 259 | srcFilename.c_str(), destFilename.c_str()); |
| 260 | #ifdef _WIN32 | ||
| 261 | if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), Common::UTF8ToUTF16W(destFilename).c_str()) == 0) | ||
| 262 | return true; | ||
| 263 | #else | ||
| 260 | if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) | 264 | if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) |
| 261 | return true; | 265 | return true; |
| 266 | #endif | ||
| 262 | LOG_ERROR(Common_Filesystem, "failed %s --> %s: %s", | 267 | LOG_ERROR(Common_Filesystem, "failed %s --> %s: %s", |
| 263 | srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 268 | srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); |
| 264 | return false; | 269 | return false; |
| @@ -270,7 +275,7 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename) | |||
| 270 | LOG_TRACE(Common_Filesystem, "%s --> %s", | 275 | LOG_TRACE(Common_Filesystem, "%s --> %s", |
| 271 | srcFilename.c_str(), destFilename.c_str()); | 276 | srcFilename.c_str(), destFilename.c_str()); |
| 272 | #ifdef _WIN32 | 277 | #ifdef _WIN32 |
| 273 | if (CopyFile(Common::UTF8ToTStr(srcFilename).c_str(), Common::UTF8ToTStr(destFilename).c_str(), FALSE)) | 278 | if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(), Common::UTF8ToUTF16W(destFilename).c_str(), FALSE)) |
| 274 | return true; | 279 | return true; |
| 275 | 280 | ||
| 276 | LOG_ERROR(Common_Filesystem, "failed %s --> %s: %s", | 281 | LOG_ERROR(Common_Filesystem, "failed %s --> %s: %s", |
| @@ -358,7 +363,7 @@ u64 GetSize(const std::string &filename) | |||
| 358 | 363 | ||
| 359 | struct stat64 buf; | 364 | struct stat64 buf; |
| 360 | #ifdef _WIN32 | 365 | #ifdef _WIN32 |
| 361 | if (_tstat64(Common::UTF8ToTStr(filename).c_str(), &buf) == 0) | 366 | if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) |
| 362 | #else | 367 | #else |
| 363 | if (stat64(filename.c_str(), &buf) == 0) | 368 | if (stat64(filename.c_str(), &buf) == 0) |
| 364 | #endif | 369 | #endif |
| @@ -432,16 +437,16 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo | |||
| 432 | 437 | ||
| 433 | #ifdef _WIN32 | 438 | #ifdef _WIN32 |
| 434 | // Find the first file in the directory. | 439 | // Find the first file in the directory. |
| 435 | WIN32_FIND_DATA ffd; | 440 | WIN32_FIND_DATAW ffd; |
| 436 | 441 | ||
| 437 | HANDLE handle_find = FindFirstFile(Common::UTF8ToTStr(directory + "\\*").c_str(), &ffd); | 442 | HANDLE handle_find = FindFirstFileW(Common::UTF8ToUTF16W(directory + "\\*").c_str(), &ffd); |
| 438 | if (handle_find == INVALID_HANDLE_VALUE) { | 443 | if (handle_find == INVALID_HANDLE_VALUE) { |
| 439 | FindClose(handle_find); | 444 | FindClose(handle_find); |
| 440 | return false; | 445 | return false; |
| 441 | } | 446 | } |
| 442 | // windows loop | 447 | // windows loop |
| 443 | do { | 448 | do { |
| 444 | const std::string virtual_name(Common::TStrToUTF8(ffd.cFileName)); | 449 | const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName)); |
| 445 | #else | 450 | #else |
| 446 | struct dirent dirent, *result = nullptr; | 451 | struct dirent dirent, *result = nullptr; |
| 447 | 452 | ||
| @@ -465,7 +470,7 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo | |||
| 465 | found_entries += ret_entries; | 470 | found_entries += ret_entries; |
| 466 | 471 | ||
| 467 | #ifdef _WIN32 | 472 | #ifdef _WIN32 |
| 468 | } while (FindNextFile(handle_find, &ffd) != 0); | 473 | } while (FindNextFileW(handle_find, &ffd) != 0); |
| 469 | FindClose(handle_find); | 474 | FindClose(handle_find); |
| 470 | #else | 475 | #else |
| 471 | } | 476 | } |
| @@ -572,15 +577,23 @@ void CopyDir(const std::string &source_path, const std::string &dest_path) | |||
| 572 | // Returns the current directory | 577 | // Returns the current directory |
| 573 | std::string GetCurrentDir() | 578 | std::string GetCurrentDir() |
| 574 | { | 579 | { |
| 575 | char *dir; | ||
| 576 | // Get the current working directory (getcwd uses malloc) | 580 | // Get the current working directory (getcwd uses malloc) |
| 581 | #ifdef _WIN32 | ||
| 582 | wchar_t *dir; | ||
| 583 | if (!(dir = _wgetcwd(nullptr, 0))) { | ||
| 584 | #else | ||
| 585 | char *dir; | ||
| 577 | if (!(dir = getcwd(nullptr, 0))) { | 586 | if (!(dir = getcwd(nullptr, 0))) { |
| 578 | 587 | #endif | |
| 579 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: %s", | 588 | LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: %s", |
| 580 | GetLastErrorMsg()); | 589 | GetLastErrorMsg()); |
| 581 | return nullptr; | 590 | return nullptr; |
| 582 | } | 591 | } |
| 592 | #ifdef _WIN32 | ||
| 593 | std::string strDir = Common::UTF16ToUTF8(dir); | ||
| 594 | #else | ||
| 583 | std::string strDir = dir; | 595 | std::string strDir = dir; |
| 596 | #endif | ||
| 584 | free(dir); | 597 | free(dir); |
| 585 | return strDir; | 598 | return strDir; |
| 586 | } | 599 | } |
| @@ -588,7 +601,11 @@ std::string GetCurrentDir() | |||
| 588 | // Sets the current directory to the given directory | 601 | // Sets the current directory to the given directory |
| 589 | bool SetCurrentDir(const std::string &directory) | 602 | bool SetCurrentDir(const std::string &directory) |
| 590 | { | 603 | { |
| 604 | #ifdef _WIN32 | ||
| 605 | return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; | ||
| 606 | #else | ||
| 591 | return chdir(directory.c_str()) == 0; | 607 | return chdir(directory.c_str()) == 0; |
| 608 | #endif | ||
| 592 | } | 609 | } |
| 593 | 610 | ||
| 594 | #if defined(__APPLE__) | 611 | #if defined(__APPLE__) |
| @@ -613,9 +630,9 @@ std::string& GetExeDirectory() | |||
| 613 | static std::string exe_path; | 630 | static std::string exe_path; |
| 614 | if (exe_path.empty()) | 631 | if (exe_path.empty()) |
| 615 | { | 632 | { |
| 616 | TCHAR tchar_exe_path[2048]; | 633 | wchar_t wchar_exe_path[2048]; |
| 617 | GetModuleFileName(nullptr, tchar_exe_path, 2048); | 634 | GetModuleFileNameW(nullptr, wchar_exe_path, 2048); |
| 618 | exe_path = Common::TStrToUTF8(tchar_exe_path); | 635 | exe_path = Common::UTF16ToUTF8(wchar_exe_path); |
| 619 | exe_path = exe_path.substr(0, exe_path.find_last_of('\\')); | 636 | exe_path = exe_path.substr(0, exe_path.find_last_of('\\')); |
| 620 | } | 637 | } |
| 621 | return exe_path; | 638 | return exe_path; |
| @@ -807,13 +824,12 @@ size_t WriteStringToFile(bool text_file, const std::string &str, const char *fil | |||
| 807 | 824 | ||
| 808 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) | 825 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) |
| 809 | { | 826 | { |
| 810 | FileUtil::IOFile file(filename, text_file ? "r" : "rb"); | 827 | IOFile file(filename, text_file ? "r" : "rb"); |
| 811 | auto const f = file.GetHandle(); | ||
| 812 | 828 | ||
| 813 | if (!f) | 829 | if (!file) |
| 814 | return false; | 830 | return false; |
| 815 | 831 | ||
| 816 | str.resize(static_cast<u32>(GetSize(f))); | 832 | str.resize(static_cast<u32>(file.GetSize())); |
| 817 | return file.ReadArray(&str[0], str.size()); | 833 | return file.ReadArray(&str[0], str.size()); |
| 818 | } | 834 | } |
| 819 | 835 | ||
| @@ -860,15 +876,10 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 860 | } | 876 | } |
| 861 | 877 | ||
| 862 | IOFile::IOFile() | 878 | IOFile::IOFile() |
| 863 | : m_file(nullptr), m_good(true) | 879 | { |
| 864 | {} | 880 | } |
| 865 | |||
| 866 | IOFile::IOFile(std::FILE* file) | ||
| 867 | : m_file(file), m_good(true) | ||
| 868 | {} | ||
| 869 | 881 | ||
| 870 | IOFile::IOFile(const std::string& filename, const char openmode[]) | 882 | IOFile::IOFile(const std::string& filename, const char openmode[]) |
| 871 | : m_file(nullptr), m_good(true) | ||
| 872 | { | 883 | { |
| 873 | Open(filename, openmode); | 884 | Open(filename, openmode); |
| 874 | } | 885 | } |
| @@ -879,7 +890,6 @@ IOFile::~IOFile() | |||
| 879 | } | 890 | } |
| 880 | 891 | ||
| 881 | IOFile::IOFile(IOFile&& other) | 892 | IOFile::IOFile(IOFile&& other) |
| 882 | : m_file(nullptr), m_good(true) | ||
| 883 | { | 893 | { |
| 884 | Swap(other); | 894 | Swap(other); |
| 885 | } | 895 | } |
| @@ -900,7 +910,7 @@ bool IOFile::Open(const std::string& filename, const char openmode[]) | |||
| 900 | { | 910 | { |
| 901 | Close(); | 911 | Close(); |
| 902 | #ifdef _WIN32 | 912 | #ifdef _WIN32 |
| 903 | _tfopen_s(&m_file, Common::UTF8ToTStr(filename).c_str(), Common::UTF8ToTStr(openmode).c_str()); | 913 | _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), Common::UTF8ToUTF16W(openmode).c_str()); |
| 904 | #else | 914 | #else |
| 905 | m_file = fopen(filename.c_str(), openmode); | 915 | m_file = fopen(filename.c_str(), openmode); |
| 906 | #endif | 916 | #endif |
| @@ -918,26 +928,12 @@ bool IOFile::Close() | |||
| 918 | return m_good; | 928 | return m_good; |
| 919 | } | 929 | } |
| 920 | 930 | ||
| 921 | std::FILE* IOFile::ReleaseHandle() | 931 | u64 IOFile::GetSize() const |
| 922 | { | ||
| 923 | std::FILE* const ret = m_file; | ||
| 924 | m_file = nullptr; | ||
| 925 | return ret; | ||
| 926 | } | ||
| 927 | |||
| 928 | void IOFile::SetHandle(std::FILE* file) | ||
| 929 | { | ||
| 930 | Close(); | ||
| 931 | Clear(); | ||
| 932 | m_file = file; | ||
| 933 | } | ||
| 934 | |||
| 935 | u64 IOFile::GetSize() | ||
| 936 | { | 932 | { |
| 937 | if (IsOpen()) | 933 | if (IsOpen()) |
| 938 | return FileUtil::GetSize(m_file); | 934 | return FileUtil::GetSize(m_file); |
| 939 | else | 935 | |
| 940 | return 0; | 936 | return 0; |
| 941 | } | 937 | } |
| 942 | 938 | ||
| 943 | bool IOFile::Seek(s64 off, int origin) | 939 | bool IOFile::Seek(s64 off, int origin) |
| @@ -948,12 +944,12 @@ bool IOFile::Seek(s64 off, int origin) | |||
| 948 | return m_good; | 944 | return m_good; |
| 949 | } | 945 | } |
| 950 | 946 | ||
| 951 | u64 IOFile::Tell() | 947 | u64 IOFile::Tell() const |
| 952 | { | 948 | { |
| 953 | if (IsOpen()) | 949 | if (IsOpen()) |
| 954 | return ftello(m_file); | 950 | return ftello(m_file); |
| 955 | else | 951 | |
| 956 | return -1; | 952 | return -1; |
| 957 | } | 953 | } |
| 958 | 954 | ||
| 959 | bool IOFile::Flush() | 955 | bool IOFile::Flush() |