Commit a8ccdbb1 authored by Chen Bill's avatar Chen Bill Committed by GitHub

add wrapper function myfopen (#2582)

* add wrapper function myfopen

* open file with myfopen
parent 0d282241
...@@ -58,6 +58,20 @@ inline int myswprintf(wchar_t(&buf)[N], const wchar_t* fmt, TR... args) { ...@@ -58,6 +58,20 @@ inline int myswprintf(wchar_t(&buf)[N], const wchar_t* fmt, TR... args) {
#include "../ocgcore/ocgapi.h" #include "../ocgcore/ocgapi.h"
#include "../ocgcore/common.h" #include "../ocgcore/common.h"
inline FILE* myfopen(const char* filename, const char* mode) {
FILE* fp{};
#ifdef _WIN32
wchar_t wname[256]{};
wchar_t wmode[20]{};
BufferIO::DecodeUTF8(filename, wname);
BufferIO::CopyWStr(mode, wmode, sizeof wmode / sizeof wmode[0]);
fp = _wfopen(wname, wmode);
#else
fp = fopen(filename, mode);
#endif
return fp;
}
#include <irrlicht.h> #include <irrlicht.h>
using namespace irr; using namespace irr;
using namespace core; using namespace core;
......
...@@ -263,15 +263,9 @@ void DeckManager::GetDeckFile(wchar_t* ret, irr::gui::IGUIComboBox* cbCategory, ...@@ -263,15 +263,9 @@ void DeckManager::GetDeckFile(wchar_t* ret, irr::gui::IGUIComboBox* cbCategory,
} }
} }
FILE* DeckManager::OpenDeckFile(const wchar_t* file, const char* mode) { FILE* DeckManager::OpenDeckFile(const wchar_t* file, const char* mode) {
#ifdef WIN32 char fullname[256]{};
wchar_t wmode[20]{}; BufferIO::EncodeUTF8(file, fullname);
BufferIO::CopyWStr(mode, wmode, sizeof wmode / sizeof wmode[0]); FILE* fp = myfopen(fullname, mode);
FILE* fp = _wfopen(file, wmode);
#else
char file2[256];
BufferIO::EncodeUTF8(file, file2);
FILE* fp = fopen(file2, mode);
#endif
return fp; return fp;
} }
IReadFile* DeckManager::OpenDeckReader(const wchar_t* file) { IReadFile* DeckManager::OpenDeckReader(const wchar_t* file) {
......
...@@ -570,14 +570,9 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) { ...@@ -570,14 +570,9 @@ bool MenuHandler::OnEvent(const irr::SEvent& event) {
const wchar_t* name = mainGame->lstSinglePlayList->getListItem(sel); const wchar_t* name = mainGame->lstSinglePlayList->getListItem(sel);
wchar_t fname[256]; wchar_t fname[256];
myswprintf(fname, L"./single/%ls", name); myswprintf(fname, L"./single/%ls", name);
FILE *fp; char fullname[256]{};
#ifdef _WIN32 BufferIO::EncodeUTF8(fname, fullname);
fp = _wfopen(fname, L"rb"); FILE* fp = myfopen(fullname, "rb");
#else
char filename[256];
BufferIO::EncodeUTF8(fname, filename);
fp = fopen(filename, "rb");
#endif
if(!fp) { if(!fp) {
mainGame->stSinglePlayInfo->setText(L""); mainGame->stSinglePlayInfo->setText(L"");
break; break;
......
...@@ -102,39 +102,26 @@ void Replay::SaveReplay(const wchar_t* name) { ...@@ -102,39 +102,26 @@ void Replay::SaveReplay(const wchar_t* name) {
return; return;
wchar_t fname[256]; wchar_t fname[256];
myswprintf(fname, L"./replay/%ls.yrp", name); myswprintf(fname, L"./replay/%ls.yrp", name);
#ifdef WIN32 char fullname[256]{};
fp = _wfopen(fname, L"wb"); BufferIO::EncodeUTF8(fname, fullname);
#else FILE* rfp = myfopen(fullname, "wb");
char fname2[256]; if(!rfp)
BufferIO::EncodeUTF8(fname, fname2);
fp = fopen(fname2, "wb");
#endif
if(!fp)
return; return;
fwrite(&pheader, sizeof(pheader), 1, fp); fwrite(&pheader, sizeof pheader, 1, rfp);
fwrite(comp_data, comp_size, 1, fp); fwrite(comp_data, comp_size, 1, rfp);
fclose(fp); fclose(rfp);
} }
bool Replay::OpenReplay(const wchar_t* name) { bool Replay::OpenReplay(const wchar_t* name) {
#ifdef WIN32 char fullname[256]{};
fp = _wfopen(name, L"rb"); BufferIO::EncodeUTF8(name, fullname);
#else FILE* rfp = myfopen(fullname, "rb");
char name2[256]; if(!rfp) {
BufferIO::EncodeUTF8(name, name2);
fp = fopen(name2, "rb");
#endif
if(!fp) {
wchar_t fname[256]; wchar_t fname[256];
myswprintf(fname, L"./replay/%ls", name); myswprintf(fname, L"./replay/%ls", name);
#ifdef WIN32 BufferIO::EncodeUTF8(fname, fullname);
fp = _wfopen(fname, L"rb"); rfp = myfopen(fullname, "rb");
#else
char fname2[256];
BufferIO::EncodeUTF8(fname, fname2);
fp = fopen(fname2, "rb");
#endif
} }
if(!fp) if(!rfp)
return false; return false;
pdata = replay_data; pdata = replay_data;
...@@ -142,13 +129,13 @@ bool Replay::OpenReplay(const wchar_t* name) { ...@@ -142,13 +129,13 @@ bool Replay::OpenReplay(const wchar_t* name) {
is_replaying = false; is_replaying = false;
replay_size = 0; replay_size = 0;
comp_size = 0; comp_size = 0;
if(fread(&pheader, sizeof(pheader), 1, fp) < 1) { if(fread(&pheader, sizeof pheader, 1, rfp) < 1) {
fclose(fp); fclose(rfp);
return false; return false;
} }
if(pheader.flag & REPLAY_COMPRESSED) { if(pheader.flag & REPLAY_COMPRESSED) {
comp_size = fread(comp_data, 1, MAX_COMP_SIZE, fp); comp_size = fread(comp_data, 1, MAX_COMP_SIZE, rfp);
fclose(fp); fclose(rfp);
if ((int)pheader.datasize < 0 && (int)pheader.datasize > MAX_REPLAY_SIZE) if ((int)pheader.datasize < 0 && (int)pheader.datasize > MAX_REPLAY_SIZE)
return false; return false;
replay_size = pheader.datasize; replay_size = pheader.datasize;
...@@ -159,8 +146,8 @@ bool Replay::OpenReplay(const wchar_t* name) { ...@@ -159,8 +146,8 @@ bool Replay::OpenReplay(const wchar_t* name) {
return false; return false;
} }
} else { } else {
replay_size = fread(replay_data, 1, MAX_REPLAY_SIZE, fp); replay_size = fread(replay_data, 1, MAX_REPLAY_SIZE, rfp);
fclose(fp); fclose(rfp);
comp_size = 0; comp_size = 0;
} }
is_replaying = true; is_replaying = true;
...@@ -169,24 +156,20 @@ bool Replay::OpenReplay(const wchar_t* name) { ...@@ -169,24 +156,20 @@ bool Replay::OpenReplay(const wchar_t* name) {
bool Replay::CheckReplay(const wchar_t* name) { bool Replay::CheckReplay(const wchar_t* name) {
wchar_t fname[256]; wchar_t fname[256];
myswprintf(fname, L"./replay/%ls", name); myswprintf(fname, L"./replay/%ls", name);
#ifdef WIN32 char fullname[256]{};
FILE* rfp = _wfopen(fname, L"rb"); BufferIO::EncodeUTF8(fname, fullname);
#else FILE* rfp = myfopen(fullname, "rb");
char fname2[256];
BufferIO::EncodeUTF8(fname, fname2);
FILE* rfp = fopen(fname2, "rb");
#endif
if(!rfp) if(!rfp)
return false; return false;
ReplayHeader rheader; ReplayHeader rheader;
size_t count = fread(&rheader, sizeof(ReplayHeader), 1, rfp); size_t count = fread(&rheader, sizeof rheader, 1, rfp);
fclose(rfp); fclose(rfp);
return count == 1 && rheader.id == 0x31707279 && rheader.version >= 0x12d0u && (rheader.version < 0x1353u || (rheader.flag & REPLAY_UNIFORM)); return count == 1 && rheader.id == 0x31707279 && rheader.version >= 0x12d0u && (rheader.version < 0x1353u || (rheader.flag & REPLAY_UNIFORM));
} }
bool Replay::DeleteReplay(const wchar_t* name) { bool Replay::DeleteReplay(const wchar_t* name) {
wchar_t fname[256]; wchar_t fname[256];
myswprintf(fname, L"./replay/%ls", name); myswprintf(fname, L"./replay/%ls", name);
#ifdef WIN32 #ifdef _WIN32
BOOL result = DeleteFileW(fname); BOOL result = DeleteFileW(fname);
return !!result; return !!result;
#else #else
...@@ -201,7 +184,7 @@ bool Replay::RenameReplay(const wchar_t* oldname, const wchar_t* newname) { ...@@ -201,7 +184,7 @@ bool Replay::RenameReplay(const wchar_t* oldname, const wchar_t* newname) {
wchar_t newfname[256]; wchar_t newfname[256];
myswprintf(oldfname, L"./replay/%ls", oldname); myswprintf(oldfname, L"./replay/%ls", oldname);
myswprintf(newfname, L"./replay/%ls", newname); myswprintf(newfname, L"./replay/%ls", newname);
#ifdef WIN32 #ifdef _WIN32
BOOL result = MoveFileW(oldfname, newfname); BOOL result = MoveFileW(oldfname, newfname);
return !!result; return !!result;
#else #else
......
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