From d4c17e5f8d0fc700c682f14e0f7113d95e50a47a Mon Sep 17 00:00:00 2001 From: zeromus Date: Tue, 5 May 2020 01:49:14 -0400 Subject: [PATCH] winport - fix some unicode file opening crap --- desmume/src/emufile.cpp | 17 +++++++++++++++++ desmume/src/emufile.h | 12 +----------- desmume/src/frontend/windows/main.cpp | 5 ++++- desmume/src/mc.cpp | 6 +++++- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/desmume/src/emufile.cpp b/desmume/src/emufile.cpp index 19b9b6f4c..0448fa365 100644 --- a/desmume/src/emufile.cpp +++ b/desmume/src/emufile.cpp @@ -23,6 +23,7 @@ THE SOFTWARE. */ #include "emufile.h" +#include "utils/xstring.h" #include @@ -509,3 +510,19 @@ size_t EMUFILE::read_MemoryStream(EMUFILE_MEMORY &ms) return 1; } + +void EMUFILE_FILE::open(const char* fname, const char* mode) +{ + mPositionCacheEnabled = false; + mCondition = eCondition_Clean; + mFilePosition = 0; + #ifdef HOST_WINDOWS + fp = _wfopen(mbstowcs((std::string)fname).c_str(),mbstowcs(mode).c_str()); + #else + fp = fopen(fname,mode); + #endif + if(!fp) + failbit = true; + this->fname = fname; + strcpy(this->mode,mode); +} diff --git a/desmume/src/emufile.h b/desmume/src/emufile.h index b78801344..d9bdbf775 100644 --- a/desmume/src/emufile.h +++ b/desmume/src/emufile.h @@ -294,17 +294,7 @@ protected: } mCondition; private: - void open(const char* fname, const char* mode) - { - mPositionCacheEnabled = false; - mCondition = eCondition_Clean; - mFilePosition = 0; - fp = fopen(fname,mode); - if(!fp) - failbit = true; - this->fname = fname; - strcpy(this->mode,mode); - } + void open(const char* fname, const char* mode); public: diff --git a/desmume/src/frontend/windows/main.cpp b/desmume/src/frontend/windows/main.cpp index 370d775ea..664ceed50 100755 --- a/desmume/src/frontend/windows/main.cpp +++ b/desmume/src/frontend/windows/main.cpp @@ -3088,7 +3088,10 @@ static BOOL OpenCore(const char* filename) memset(batteryPath, 0, MAX_PATH); path.getpathnoext(path.BATTERY, batteryPath); std::string batteryPathString = std::string(batteryPath) + ".dsv"; - FILE *testFs = fopen(batteryPathString.c_str(), "rb+"); + std::wstring batteryPathStringW = mbstowcs(batteryPathString); + FILE *testFs = _wfopen(batteryPathStringW.c_str(), L"rb"); + if (testFs == NULL) + testFs = _wfopen(batteryPathStringW.c_str(), L"wb"); if (testFs == NULL) { msgbox->warn("\ diff --git a/desmume/src/mc.cpp b/desmume/src/mc.cpp index daa1897a5..2823efdad 100644 --- a/desmume/src/mc.cpp +++ b/desmume/src/mc.cpp @@ -234,7 +234,11 @@ BackupDevice::BackupDevice() MCLOG("MC: %s\n", _fileName.c_str()); - bool fexists = (access(_fileName.c_str(), 0) == 0)?true:false; + bool fexists = false; + { + EMUFILE_FILE testfile(_fileName, "rb"); + fexists = testfile.is_open(); + } if (fexists && CommonSettings.backupSave) {