diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2020-07-27 13:43:20 +0300 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2020-09-03 19:56:24 +0300 |
commit | 80b8eff6aa41ba79175a5152ba5b2b9b16f6de3f (patch) | |
tree | 39ed96f7b9062e2f4ae1e20560fdb1f2f04c4d67 /drivers/windows | |
parent | 0864f12f0de50ffecbc9964cdf4edbae75e27be5 (diff) |
[Complex Test Layouts] Change `String` to use UTF-32 encoding on all platforms.
Diffstat (limited to 'drivers/windows')
-rw-r--r-- | drivers/windows/dir_access_windows.cpp | 132 | ||||
-rw-r--r-- | drivers/windows/file_access_windows.cpp | 57 |
2 files changed, 96 insertions, 93 deletions
diff --git a/drivers/windows/dir_access_windows.cpp b/drivers/windows/dir_access_windows.cpp index b8153907a9..03e4e30797 100644 --- a/drivers/windows/dir_access_windows.cpp +++ b/drivers/windows/dir_access_windows.cpp @@ -65,7 +65,7 @@ Error DirAccessWindows::list_dir_begin() { _cishidden = false; list_dir_end(); - p->h = FindFirstFileExW((current_dir + "\\*").c_str(), FindExInfoStandard, &p->fu, FindExSearchNameMatch, nullptr, 0); + p->h = FindFirstFileExW((LPCWSTR)(String(current_dir + "\\*").utf16().get_data()), FindExInfoStandard, &p->fu, FindExSearchNameMatch, nullptr, 0); if (p->h == INVALID_HANDLE_VALUE) { return ERR_CANT_OPEN; @@ -75,13 +75,14 @@ Error DirAccessWindows::list_dir_begin() { } String DirAccessWindows::get_next() { - if (p->h == INVALID_HANDLE_VALUE) + if (p->h == INVALID_HANDLE_VALUE) { return ""; + } _cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); _cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN); - String name = p->fu.cFileName; + String name = String::utf16((const char16_t *)(p->fu.cFileName)); if (FindNextFileW(p->h, &p->fu) == 0) { FindClose(p->h); @@ -111,8 +112,9 @@ int DirAccessWindows::get_drive_count() { } String DirAccessWindows::get_drive(int p_drive) { - if (p_drive < 0 || p_drive >= drive_count) + if (p_drive < 0 || p_drive >= drive_count) { return ""; + } return String::chr(drives[p_drive]) + ":"; } @@ -122,18 +124,17 @@ Error DirAccessWindows::change_dir(String p_dir) { p_dir = fix_path(p_dir); - wchar_t real_current_dir_name[2048]; + WCHAR real_current_dir_name[2048]; GetCurrentDirectoryW(2048, real_current_dir_name); - String prev_dir = real_current_dir_name; + String prev_dir = String::utf16((const char16_t *)real_current_dir_name); - SetCurrentDirectoryW(current_dir.c_str()); - bool worked = (SetCurrentDirectoryW(p_dir.c_str()) != 0); + SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data())); + bool worked = (SetCurrentDirectoryW((LPCWSTR)(p_dir.utf16().get_data())) != 0); String base = _get_root_path(); if (base != "") { GetCurrentDirectoryW(2048, real_current_dir_name); - String new_dir; - new_dir = String(real_current_dir_name).replace("\\", "/"); + String new_dir = String::utf16((const char16_t *)real_current_dir_name).replace("\\", "/"); if (!new_dir.begins_with(base)) { worked = false; } @@ -141,13 +142,11 @@ Error DirAccessWindows::change_dir(String p_dir) { if (worked) { GetCurrentDirectoryW(2048, real_current_dir_name); - current_dir = real_current_dir_name; // TODO, utf8 parser + current_dir = String::utf16((const char16_t *)real_current_dir_name); current_dir = current_dir.replace("\\", "/"); + } - } //else { - - SetCurrentDirectoryW(prev_dir.c_str()); - //} + SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data())); return worked ? OK : ERR_INVALID_PARAMETER; } @@ -156,8 +155,9 @@ Error DirAccessWindows::make_dir(String p_dir) { GLOBAL_LOCK_FUNCTION p_dir = fix_path(p_dir); - if (p_dir.is_rel_path()) + if (p_dir.is_rel_path()) { p_dir = current_dir.plus_file(p_dir); + } p_dir = p_dir.replace("/", "\\"); @@ -167,16 +167,16 @@ Error DirAccessWindows::make_dir(String p_dir) { p_dir = "\\\\?\\" + p_dir; //done according to // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx - success = CreateDirectoryW(p_dir.c_str(), nullptr); + success = CreateDirectoryW((LPCWSTR)(p_dir.utf16().get_data()), nullptr); err = GetLastError(); if (success) { return OK; - }; + } if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) { return ERR_ALREADY_EXISTS; - }; + } return ERR_CANT_CREATE; } @@ -185,12 +185,11 @@ String DirAccessWindows::get_current_dir(bool p_include_drive) { String base = _get_root_path(); if (base != "") { String bd = current_dir.replace("\\", "/").replace_first(base, ""); - if (bd.begins_with("/")) + if (bd.begins_with("/")) { return _get_root_string() + bd.substr(1, bd.length()); - else + } else { return _get_root_string() + bd; - - } else { + } } if (p_include_drive) { @@ -209,20 +208,18 @@ String DirAccessWindows::get_current_dir(bool p_include_drive) { bool DirAccessWindows::file_exists(String p_file) { GLOBAL_LOCK_FUNCTION - if (!p_file.is_abs_path()) + if (!p_file.is_abs_path()) { p_file = get_current_dir().plus_file(p_file); + } p_file = fix_path(p_file); - //p_file.replace("/","\\"); - - //WIN32_FILE_ATTRIBUTE_DATA fileInfo; - DWORD fileAttr; - fileAttr = GetFileAttributesW(p_file.c_str()); - if (INVALID_FILE_ATTRIBUTES == fileAttr) + fileAttr = GetFileAttributesW((LPCWSTR)(p_file.utf16().get_data())); + if (INVALID_FILE_ATTRIBUTES == fileAttr) { return false; + } return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY); } @@ -230,31 +227,30 @@ bool DirAccessWindows::file_exists(String p_file) { bool DirAccessWindows::dir_exists(String p_dir) { GLOBAL_LOCK_FUNCTION - if (p_dir.is_rel_path()) + if (p_dir.is_rel_path()) { p_dir = get_current_dir().plus_file(p_dir); + } p_dir = fix_path(p_dir); - //p_dir.replace("/","\\"); - - //WIN32_FILE_ATTRIBUTE_DATA fileInfo; - DWORD fileAttr; - - fileAttr = GetFileAttributesW(p_dir.c_str()); - if (INVALID_FILE_ATTRIBUTES == fileAttr) + fileAttr = GetFileAttributesW((LPCWSTR)(p_dir.utf16().get_data())); + if (INVALID_FILE_ATTRIBUTES == fileAttr) { return false; + } return (fileAttr & FILE_ATTRIBUTE_DIRECTORY); } Error DirAccessWindows::rename(String p_path, String p_new_path) { - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { p_path = get_current_dir().plus_file(p_path); + } p_path = fix_path(p_path); - if (p_new_path.is_rel_path()) + if (p_new_path.is_rel_path()) { p_new_path = get_current_dir().plus_file(p_new_path); + } p_new_path = fix_path(p_new_path); @@ -262,16 +258,16 @@ Error DirAccessWindows::rename(String p_path, String p_new_path) { if (p_path.to_lower() == p_new_path.to_lower()) { WCHAR tmpfile[MAX_PATH]; - if (!GetTempFileNameW(fix_path(get_current_dir()).c_str(), nullptr, 0, tmpfile)) { + if (!GetTempFileNameW((LPCWSTR)(fix_path(get_current_dir()).utf16().get_data()), nullptr, 0, tmpfile)) { return FAILED; } - if (!::ReplaceFileW(tmpfile, p_path.c_str(), nullptr, 0, nullptr, nullptr)) { + if (!::ReplaceFileW(tmpfile, (LPCWSTR)(p_path.utf16().get_data()), nullptr, 0, nullptr, nullptr)) { DeleteFileW(tmpfile); return FAILED; } - return ::_wrename(tmpfile, p_new_path.c_str()) == 0 ? OK : FAILED; + return ::_wrename(tmpfile, (LPCWSTR)(p_new_path.utf16().get_data())) == 0 ? OK : FAILED; } else { if (file_exists(p_new_path)) { @@ -280,60 +276,60 @@ Error DirAccessWindows::rename(String p_path, String p_new_path) { } } - return ::_wrename(p_path.c_str(), p_new_path.c_str()) == 0 ? OK : FAILED; + return ::_wrename((LPCWSTR)(p_path.utf16().get_data()), (LPCWSTR)(p_new_path.utf16().get_data())) == 0 ? OK : FAILED; } } Error DirAccessWindows::remove(String p_path) { - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { p_path = get_current_dir().plus_file(p_path); + } p_path = fix_path(p_path); DWORD fileAttr; - fileAttr = GetFileAttributesW(p_path.c_str()); - if (INVALID_FILE_ATTRIBUTES == fileAttr) + fileAttr = GetFileAttributesW((LPCWSTR)(p_path.utf16().get_data())); + if (INVALID_FILE_ATTRIBUTES == fileAttr) { return FAILED; - if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) - return ::_wrmdir(p_path.c_str()) == 0 ? OK : FAILED; - else - return ::_wunlink(p_path.c_str()) == 0 ? OK : FAILED; + } + if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) { + return ::_wrmdir((LPCWSTR)(p_path.utf16().get_data())) == 0 ? OK : FAILED; + } else { + return ::_wunlink((LPCWSTR)(p_path.utf16().get_data())) == 0 ? OK : FAILED; + } } /* FileType DirAccessWindows::get_file_type(const String& p_file) const { + WCHAR real_current_dir_name[2048]; + GetCurrentDirectoryW(2048, real_current_dir_name); + String prev_dir = Strong::utf16((const char16_t *)real_current_dir_name); - - wchar_t real_current_dir_name[2048]; - GetCurrentDirectoryW(2048,real_current_dir_name); - String prev_dir=real_current_dir_name; - - bool worked SetCurrentDirectoryW(current_dir.c_str()); + bool worked = SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data())); DWORD attr; if (worked) { - - WIN32_FILE_ATTRIBUTE_DATA fileInfo; - attr = GetFileAttributesExW(p_file.c_str(), GetFileExInfoStandard, &fileInfo); - + WIN32_FILE_ATTRIBUTE_DATA fileInfo; + attr = GetFileAttributesExW((LPCWSTR)(p_file.utf16().get_data()), GetFileExInfoStandard, &fileInfo); } - SetCurrentDirectoryW(prev_dir.c_str()); + SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data())); - if (!worked) + if (!worked) { return FILE_TYPE_NONE; + } - - return (attr&FILE_ATTRIBUTE_DIRECTORY)?FILE_TYPE_ + return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FILE_TYPE_ } */ size_t DirAccessWindows::get_space_left() { uint64_t bytes = 0; - if (!GetDiskFreeSpaceEx(nullptr, (PULARGE_INTEGER)&bytes, nullptr, nullptr)) + if (!GetDiskFreeSpaceEx(nullptr, (PULARGE_INTEGER)&bytes, nullptr, nullptr)) { return 0; + } //this is either 0 or a value in bytes. return (size_t)bytes; @@ -352,7 +348,7 @@ String DirAccessWindows::get_filesystem_type() const { DWORD dwMaxFileNameLength = 0; DWORD dwFileSystemFlags = 0; - if (::GetVolumeInformationW(unit.c_str(), + if (::GetVolumeInformationW((LPCWSTR)(unit.utf16().get_data()), szVolumeName, sizeof(szVolumeName), &dwSerialNumber, @@ -360,7 +356,7 @@ String DirAccessWindows::get_filesystem_type() const { &dwFileSystemFlags, szFileSystemName, sizeof(szFileSystemName)) == TRUE) { - return String(szFileSystemName); + return String::utf16((const char16_t *)szFileSystemName); } ERR_FAIL_V(""); diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp index 50366d0b2d..dd86061ea7 100644 --- a/drivers/windows/file_access_windows.cpp +++ b/drivers/windows/file_access_windows.cpp @@ -59,29 +59,32 @@ void FileAccessWindows::check_errors() const { Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { path_src = p_path; path = fix_path(p_path); - if (f) + if (f) { close(); + } - const wchar_t *mode_string; + const WCHAR *mode_string; - if (p_mode_flags == READ) + if (p_mode_flags == READ) { mode_string = L"rb"; - else if (p_mode_flags == WRITE) + } else if (p_mode_flags == WRITE) { mode_string = L"wb"; - else if (p_mode_flags == READ_WRITE) + } else if (p_mode_flags == READ_WRITE) { mode_string = L"rb+"; - else if (p_mode_flags == WRITE_READ) + } else if (p_mode_flags == WRITE_READ) { mode_string = L"wb+"; - else + } else { return ERR_INVALID_PARAMETER; + } /* pretty much every implementation that uses fopen as primary backend supports utf8 encoding */ struct _stat st; - if (_wstat(path.c_str(), &st) == 0) { - if (!S_ISREG(st.st_mode)) + if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) { + if (!S_ISREG(st.st_mode)) { return ERR_FILE_CANT_OPEN; + } }; #ifdef TOOLS_ENABLED @@ -91,9 +94,9 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { // platforms). if (p_mode_flags == READ) { WIN32_FIND_DATAW d; - HANDLE f = FindFirstFileW(path.c_str(), &d); + HANDLE f = FindFirstFileW((LPCWSTR)(path.utf16().get_data()), &d); if (f != INVALID_HANDLE_VALUE) { - String fname = d.cFileName; + String fname = String::utf16((const char16_t *)(d.cFileName)); if (fname != String()) { String base_file = path.get_file(); if (base_file != fname && base_file.findn(fname) == 0) { @@ -110,7 +113,7 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { path = path + ".tmp"; } - errno_t errcode = _wfopen_s(&f, path.c_str(), mode_string); + errno_t errcode = _wfopen_s(&f, (LPCWSTR)(path.utf16().get_data()), mode_string); if (f == nullptr) { switch (errcode) { @@ -130,8 +133,9 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { } void FileAccessWindows::close() { - if (!f) + if (!f) { return; + } fclose(f); f = nullptr; @@ -148,16 +152,16 @@ void FileAccessWindows::close() { // UWP has no PathFileExists, so we check attributes instead DWORD fileAttr; - fileAttr = GetFileAttributesW(save_path.c_str()); + fileAttr = GetFileAttributesW((LPCWSTR)(save_path.utf16().get_data())); if (INVALID_FILE_ATTRIBUTES == fileAttr) { #else - if (!PathFileExistsW(save_path.c_str())) { + if (!PathFileExistsW((LPCWSTR)(save_path.utf16().get_data()))) { #endif //creating new file - rename_error = _wrename((save_path + ".tmp").c_str(), save_path.c_str()) != 0; + rename_error = _wrename((LPCWSTR)((save_path + ".tmp").utf16().get_data()), (LPCWSTR)(save_path.utf16().get_data())) != 0; } else { //atomic replace for existing file - rename_error = !ReplaceFileW(save_path.c_str(), (save_path + ".tmp").c_str(), nullptr, 2 | 4, nullptr, nullptr); + rename_error = !ReplaceFileW((LPCWSTR)(save_path.utf16().get_data()), (LPCWSTR)((save_path + ".tmp").utf16().get_data()), nullptr, 2 | 4, nullptr, nullptr); } if (rename_error) { attempts--; @@ -192,15 +196,17 @@ bool FileAccessWindows::is_open() const { void FileAccessWindows::seek(size_t p_position) { ERR_FAIL_COND(!f); last_error = OK; - if (fseek(f, p_position, SEEK_SET)) + if (fseek(f, p_position, SEEK_SET)) { check_errors(); + } prev_op = 0; } void FileAccessWindows::seek_end(int64_t p_position) { ERR_FAIL_COND(!f); - if (fseek(f, p_position, SEEK_END)) + if (fseek(f, p_position, SEEK_END)) { check_errors(); + } prev_op = 0; } @@ -209,7 +215,7 @@ size_t FileAccessWindows::get_position() const { aux_position = ftell(f); if (!aux_position) { check_errors(); - }; + } return aux_position; } @@ -241,7 +247,7 @@ uint8_t FileAccessWindows::get_8() const { if (fread(&b, 1, 1, f) == 0) { check_errors(); b = '\0'; - }; + } return b; } @@ -266,8 +272,9 @@ Error FileAccessWindows::get_error() const { void FileAccessWindows::flush() { ERR_FAIL_COND(!f); fflush(f); - if (prev_op == WRITE) + if (prev_op == WRITE) { prev_op = 0; + } } void FileAccessWindows::store_8(uint8_t p_dest) { @@ -298,9 +305,9 @@ void FileAccessWindows::store_buffer(const uint8_t *p_src, int p_length) { bool FileAccessWindows::file_exists(const String &p_name) { FILE *g; - //printf("opening file %s\n", p_fname.c_str()); + //printf("opening file %s\n", p_fname.utf8().get_data()); String filename = fix_path(p_name); - _wfopen_s(&g, filename.c_str(), L"rb"); + _wfopen_s(&g, (LPCWSTR)(filename.utf16().get_data()), L"rb"); if (g == nullptr) { return false; } else { @@ -315,7 +322,7 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) { file = file.substr(0, file.length() - 1); struct _stat st; - int rv = _wstat(file.c_str(), &st); + int rv = _wstat((LPCWSTR)(file.utf16().get_data()), &st); if (rv == 0) { return st.st_mtime; |