Commit 9350f5d6 authored by mercury233's avatar mercury233 Committed by GitHub

add myfopen, fix setlocale (#2713)

parent 7f29eb15
......@@ -72,6 +72,21 @@ inline FILE* mywfopen(const wchar_t* filename, const char* mode) {
return fp;
}
#if !defined(_WIN32)
#define myfopen std::fopen
#elif defined(WDK_NTDDI_VERSION) && (WDK_NTDDI_VERSION >= 0x0A000005) // Redstone 4, Version 1803, Build 17134.
#define FOPEN_WINDOWS_SUPPORT_UTF8
#define myfopen std::fopen
#else
inline FILE* myfopen(const char* filename, const char* mode) {
wchar_t wfilename[256]{};
BufferIO::DecodeUTF8(filename, wfilename);
wchar_t wmode[20]{};
BufferIO::CopyCharArray(mode, wmode);
return _wfopen(wfilename, wmode);
}
#endif
#include <irrlicht.h>
extern const unsigned short PRO_VERSION;
......
......@@ -108,7 +108,7 @@ bool DataManager::LoadDB(const wchar_t* wfile) {
return ret;
}
bool DataManager::LoadStrings(const char* file) {
FILE* fp = std::fopen(file, "r");
FILE* fp = myfopen(file, "r");
if(!fp)
return false;
char linebuf[TEXT_LINE_SIZE]{};
......@@ -431,9 +431,7 @@ unsigned char* DataManager::ReadScriptFromIrrFS(const char* script_name, int* sl
return scriptBuffer;
}
unsigned char* DataManager::ReadScriptFromFile(const char* script_name, int* slen) {
wchar_t fname[256]{};
BufferIO::DecodeUTF8(script_name, fname);
FILE* fp = mywfopen(fname, "rb");
FILE* fp = myfopen(script_name, "rb");
if (!fp)
return nullptr;
size_t len = std::fread(scriptBuffer, 1, sizeof scriptBuffer, fp);
......
......@@ -10,7 +10,7 @@ DeckManager deckManager;
void DeckManager::LoadLFListSingle(const char* path) {
auto cur = _lfList.rend();
FILE* fp = std::fopen(path, "r");
FILE* fp = myfopen(path, "r");
char linebuf[256]{};
wchar_t strBuffer[256]{};
char str1[16]{};
......
......@@ -1271,7 +1271,7 @@ void Game::RefreshBot() {
if(!gameConf.enable_bot_mode)
return;
botInfo.clear();
FILE* fp = std::fopen("bot.conf", "r");
FILE* fp = myfopen("bot.conf", "r");
char linebuf[256]{};
char strbuf[256]{};
if(fp) {
......@@ -1324,7 +1324,7 @@ void Game::RefreshBot() {
}
}
void Game::LoadConfig() {
FILE* fp = std::fopen("system.conf", "r");
FILE* fp = myfopen("system.conf", "r");
if(!fp)
return;
char linebuf[CONFIG_LINE_SIZE]{};
......@@ -1462,7 +1462,7 @@ void Game::LoadConfig() {
std::fclose(fp);
}
void Game::SaveConfig() {
FILE* fp = std::fopen("system.conf", "w");
FILE* fp = myfopen("system.conf", "w");
std::fprintf(fp, "#config file\n#nickname & gamename should be less than 20 characters\n");
char linebuf[CONFIG_LINE_SIZE];
std::fprintf(fp, "use_d3d = %d\n", gameConf.use_d3d ? 1 : 0);
......@@ -1715,7 +1715,7 @@ void Game::AddDebugMsg(const char* msg) {
}
}
void Game::ErrorLog(const char* msg) {
FILE* fp = std::fopen("error.log", "a");
FILE* fp = myfopen("error.log", "a");
if(!fp)
return;
time_t nowtime = std::time(nullptr);
......
......@@ -23,8 +23,12 @@ void ClickButton(irr::gui::IGUIElement* btn) {
}
int main(int argc, char* argv[]) {
#ifndef _WIN32
std::setlocale(LC_CTYPE, "UTF-8");
#if defined(FOPEN_WINDOWS_SUPPORT_UTF8)
std::setlocale(LC_CTYPE, ".UTF-8");
#elif defined(__APPLE__)
std::setlocale(LC_CTYPE, "C.UTF-8");
#elif !defined(_WIN32)
std::setlocale(LC_CTYPE, "");
#endif
#ifdef __APPLE__
CFURLRef bundle_url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
......
......@@ -24,7 +24,7 @@ void Replay::BeginRecord() {
#else
if(is_recording)
std::fclose(fp);
fp = std::fopen("./replay/_LastReplay.yrp", "wb");
fp = myfopen("./replay/_LastReplay.yrp", "wb");
if(!fp)
return;
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment