Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a bug where tuw can not open paths contain utf-8 characters on windows #81

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ constexpr char CMD_TOKEN_PERCENT[] = "";
constexpr char CMD_TOKEN_CURRENT_DIR[] = "__CWD__";
constexpr char CMD_TOKEN_HOME_DIR[] = "__HOME__";

#ifdef _WIN32
FILE* FileOpen(const char* path, const char* mode) noexcept;
#else
#define FileOpen(path, mode) fopen(path, mode)
#endif

namespace json_utils {

struct JsonResult {
Expand Down
6 changes: 3 additions & 3 deletions src/exe_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ json_utils::JsonResult ExeContainer::Read(const noex::string& exe_path) noexcept
}

m_exe_path = exe_path;
FILE* file_io = fopen(exe_path.c_str(), "rb");
FILE* file_io = FileOpen(exe_path.c_str(), "rb");
if (!file_io)
return { false, "Failed to open " + exe_path };

Expand Down Expand Up @@ -160,11 +160,11 @@ json_utils::JsonResult ExeContainer::Write(const noex::string& exe_path) noexcep
if (JSON_SIZE_MAX <= json_size)
return { false, noex::string("Unexpected json size. (") + json_size + ")" };

FILE* old_io = fopen(m_exe_path.c_str(), "rb");
FILE* old_io = FileOpen(m_exe_path.c_str(), "rb");
if (!old_io)
return { false, "Failed to open a file. (" + m_exe_path + ")" };

FILE* new_io = fopen(exe_path.c_str(), "wb");
FILE* new_io = FileOpen(exe_path.c_str(), "wb");
if (!new_io) {
fclose(old_io);
return { false, "Failed to open a file. (" + exe_path + ")" };
Expand Down
15 changes: 13 additions & 2 deletions src/json_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
#include "string_utils.h"
#include "noex/vector.hpp"

#ifdef _WIN32
FILE* FileOpen(const char* path, const char* mode) noexcept {
// Use wfopen as fopen might not use utf-8.
noex::wstring wpath = UTF8toUTF16(path);
noex::wstring wmode = UTF8toUTF16(mode);
if (wpath.empty() || wmode.empty())
return nullptr;
return _wfopen(wpath.c_str(), wmode.c_str());
}
#endif

namespace json_utils {

enum ComponentType: int {
Expand All @@ -35,7 +46,7 @@ namespace json_utils {
rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag;

JsonResult LoadJson(const noex::string& file, rapidjson::Document& json) noexcept {
FILE* fp = fopen(file.c_str(), "rb");
FILE* fp = FileOpen(file.c_str(), "rb");
if (!fp)
return { false, "Failed to open " + file };

Expand All @@ -58,7 +69,7 @@ namespace json_utils {
}

JsonResult SaveJson(rapidjson::Document& json, const noex::string& file) noexcept {
FILE* fp = fopen(file.c_str(), "wb");
FILE* fp = FileOpen(file.c_str(), "wb");
if (!fp)
return { false, "Failed to open " + file + "." };

Expand Down