Skip to content

Commit

Permalink
Rewrite to_wchar, rename to as_wchar
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Oct 4, 2019
1 parent 28db875 commit bee74c2
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ using namespace std::literals::string_literals;
#include <cwchar>
#include <Windows.h>

static std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
static const wchar_t* as_wchar(const std::string& source)
{
// String size + null terminator
const std::size_t buf_size = source.size() + 1;

// Safe size
const int size = narrow<int>(buf_size, "to_wchar" HERE);
const int size = narrow<int>(buf_size, "as_wchar" HERE);

// Buffer for max possible output length
std::unique_ptr<wchar_t[]> buffer(new wchar_t[buf_size]);
static thread_local std::wstring buffer;
buffer.reserve(buf_size);

verify("to_wchar" HERE), MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get(), size);
verify("as_wchar" HERE), MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.data(), size);

return buffer;
return buffer.c_str();
}

static void to_utf8(std::string& out, const wchar_t* source)
Expand Down Expand Up @@ -315,7 +316,7 @@ bool fs::stat(const std::string& path, stat_t& info)

#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
if (!GetFileAttributesExW(as_wchar(path), GetFileExInfoStandard, &attrs))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -358,7 +359,7 @@ bool fs::exists(const std::string& path)
}

#ifdef _WIN32
if (GetFileAttributesW(to_wchar(path).get()) == INVALID_FILE_ATTRIBUTES)
if (GetFileAttributesW(as_wchar(path)) == INVALID_FILE_ATTRIBUTES)
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -397,7 +398,7 @@ bool fs::is_file(const std::string& path)
}

#ifdef _WIN32
const DWORD attrs = GetFileAttributesW(to_wchar(path).get());
const DWORD attrs = GetFileAttributesW(as_wchar(path));
if (attrs == INVALID_FILE_ATTRIBUTES)
{
g_tls_error = to_error(GetLastError());
Expand Down Expand Up @@ -446,7 +447,7 @@ bool fs::is_dir(const std::string& path)
}

#ifdef _WIN32
const DWORD attrs = GetFileAttributesW(to_wchar(path).get());
const DWORD attrs = GetFileAttributesW(as_wchar(path));
if (attrs == INVALID_FILE_ATTRIBUTES)
{
g_tls_error = to_error(GetLastError());
Expand Down Expand Up @@ -486,7 +487,7 @@ bool fs::statfs(const std::string& path, fs::device_stat& info)
ULARGE_INTEGER total_size;
ULARGE_INTEGER total_free;

if (!GetDiskFreeSpaceExW(to_wchar(path).get(), &avail_free, &total_size, &total_free))
if (!GetDiskFreeSpaceExW(as_wchar(path), &avail_free, &total_size, &total_free))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -521,7 +522,7 @@ bool fs::create_dir(const std::string& path)
}

#ifdef _WIN32
if (!CreateDirectoryW(to_wchar(path).get(), NULL))
if (!CreateDirectoryW(as_wchar(path), NULL))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -571,7 +572,7 @@ bool fs::remove_dir(const std::string& path)
}

#ifdef _WIN32
if (!RemoveDirectoryW(to_wchar(path).get()))
if (!RemoveDirectoryW(as_wchar(path)))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -611,24 +612,24 @@ bool fs::rename(const std::string& from, const std::string& to, bool overwrite)
}

#ifdef _WIN32
const auto ws1 = to_wchar(from);
const auto ws2 = to_wchar(to);
const auto ws1 = as_wchar(from);
const auto ws2 = as_wchar(to);

if (!MoveFileExW(ws1.get(), ws2.get(), overwrite ? MOVEFILE_REPLACE_EXISTING : 0))
if (!MoveFileExW(ws1, ws2, overwrite ? MOVEFILE_REPLACE_EXISTING : 0))
{
DWORD error1 = GetLastError();

if (overwrite && error1 == ERROR_ACCESS_DENIED && is_dir(from) && is_dir(to))
{
if (RemoveDirectoryW(ws2.get()))
if (RemoveDirectoryW(ws2))
{
if (MoveFileW(ws1.get(), ws2.get()))
if (MoveFileW(ws1, ws2))
{
return true;
}

error1 = GetLastError();
CreateDirectoryW(ws2.get(), NULL); // TODO
CreateDirectoryW(ws2, NULL); // TODO
}
else
{
Expand Down Expand Up @@ -683,7 +684,7 @@ bool fs::copy_file(const std::string& from, const std::string& to, bool overwrit
}

#ifdef _WIN32
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
if (!CopyFileW(as_wchar(from), as_wchar(to), !overwrite))
{
g_tls_error = to_error(GetLastError());
return false;
Expand Down Expand Up @@ -770,7 +771,7 @@ bool fs::remove_file(const std::string& path)
}

#ifdef _WIN32
if (!DeleteFileW(to_wchar(path).get()))
if (!DeleteFileW(as_wchar(path)))
{
g_tls_error = to_error(GetLastError());
return false;
Expand All @@ -797,7 +798,7 @@ bool fs::truncate_file(const std::string& path, u64 length)

#ifdef _WIN32
// Open the file
const auto handle = CreateFileW(to_wchar(path).get(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
const auto handle = CreateFileW(as_wchar(path), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
g_tls_error = to_error(GetLastError());
Expand Down Expand Up @@ -836,7 +837,7 @@ bool fs::utime(const std::string& path, s64 atime, s64 mtime)

#ifdef _WIN32
// Open the file
const auto handle = CreateFileW(to_wchar(path).get(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
const auto handle = CreateFileW(as_wchar(path), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
g_tls_error = to_error(GetLastError());
Expand Down Expand Up @@ -933,7 +934,7 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
share |= FILE_SHARE_WRITE | FILE_SHARE_DELETE;
}

const HANDLE handle = CreateFileW(to_wchar(path).get(), access, share, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
const HANDLE handle = CreateFileW(as_wchar(path), access, share, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);

if (handle == INVALID_HANDLE_VALUE)
{
Expand Down Expand Up @@ -1324,7 +1325,7 @@ bool fs::dir::open(const std::string& path)

#ifdef _WIN32
WIN32_FIND_DATAW found;
const auto handle = FindFirstFileExW(to_wchar(path + "/*").get(), FindExInfoBasic, &found, FindExSearchNameMatch, NULL, FIND_FIRST_EX_CASE_SENSITIVE | FIND_FIRST_EX_LARGE_FETCH);
const auto handle = FindFirstFileExW(as_wchar(path + "/*"), FindExInfoBasic, &found, FindExSearchNameMatch, NULL, FIND_FIRST_EX_CASE_SENSITIVE | FIND_FIRST_EX_LARGE_FETCH);

if (handle == INVALID_HANDLE_VALUE)
{
Expand Down

0 comments on commit bee74c2

Please sign in to comment.