From 8ab97a959ce2265f5fb326e6f9d963bf0e58021d Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 20 Jan 2022 05:04:03 -0500 Subject: [PATCH] add ps_fopen to properly handle fopen on Windows fixes #22 --- CMakeLists.txt | 1 + src/engine/engine.cpp | 5 +++-- src/engine/instrument.cpp | 5 +++-- src/engine/wavetable.cpp | 3 ++- src/fileutils.cpp | 12 ++++++++++++ src/fileutils.h | 7 +++++++ src/gui/gui.cpp | 9 +++++---- src/main.cpp | 3 ++- 8 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 src/fileutils.cpp create mode 100644 src/fileutils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f56e6129..dce7cf072 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ endif() set(ENGINE_SOURCES src/log.cpp +src/fileutils.cpp extern/Nuked-OPN2/ym3438.c extern/opm/opm.c diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 23d24b6bd..588e79710 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -3,6 +3,7 @@ #include "instrument.h" #include "safeReader.h" #include "../ta-log.h" +#include "../fileutils.h" #include "../audio/sdl.h" #include #include @@ -2260,7 +2261,7 @@ void DivEngine::notifyWaveChange(int wave) { bool DivEngine::saveConf() { configFile=configPath+String(CONFIG_FILE); - FILE* f=fopen(configFile.c_str(),"wb"); + FILE* f=ps_fopen(configFile.c_str(),"wb"); if (f==NULL) { logW("could not write config file! %s\n",strerror(errno)); return false; @@ -2280,7 +2281,7 @@ bool DivEngine::saveConf() { bool DivEngine::loadConf() { char line[4096]; configFile=configPath+String(CONFIG_FILE); - FILE* f=fopen(configFile.c_str(),"rb"); + FILE* f=ps_fopen(configFile.c_str(),"rb"); if (f==NULL) { logI("creating default config.\n"); return saveConf(); diff --git a/src/engine/instrument.cpp b/src/engine/instrument.cpp index d81216c5c..fa1a3e01e 100644 --- a/src/engine/instrument.cpp +++ b/src/engine/instrument.cpp @@ -1,6 +1,7 @@ #include "engine.h" #include "instrument.h" #include "../ta-log.h" +#include "../fileutils.h" void DivInstrument::putInsData(SafeWriter* w) { w->write("INST",4); @@ -159,7 +160,7 @@ bool DivInstrument::save(const char* path) { putInsData(w); - FILE* outFile=fopen(path,"wb"); + FILE* outFile=ps_fopen(path,"wb"); if (outFile==NULL) { logE("could not save instrument: %s!\n",strerror(errno)); w->finish(); @@ -171,4 +172,4 @@ bool DivInstrument::save(const char* path) { fclose(outFile); w->finish(); return true; -} \ No newline at end of file +} diff --git a/src/engine/wavetable.cpp b/src/engine/wavetable.cpp index 4a4910cd3..8e3c76135 100644 --- a/src/engine/wavetable.cpp +++ b/src/engine/wavetable.cpp @@ -1,6 +1,7 @@ #include "engine.h" #include "wavetable.h" #include "../ta-log.h" +#include "../fileutils.h" void DivWavetable::putWaveData(SafeWriter* w) { w->write("WAVE",4); @@ -30,7 +31,7 @@ bool DivWavetable::save(const char* path) { putWaveData(w); - FILE* outFile=fopen(path,"wb"); + FILE* outFile=ps_fopen(path,"wb"); if (outFile==NULL) { logE("could not save wavetable: %s!\n",strerror(errno)); w->finish(); diff --git a/src/fileutils.cpp b/src/fileutils.cpp new file mode 100644 index 000000000..f4dda9b38 --- /dev/null +++ b/src/fileutils.cpp @@ -0,0 +1,12 @@ +#include "fileutils.h" +#ifdef _WIN32 +#include "utfutils.h" +#endif + +FILE* ps_fopen(const char* path, const char* mode) { +#ifdef _WIN32 + return _wfopen(utf8To16(path).c_str(),utf8To16(mode).c_str()); +#else + return fopen(path,mode); +#endif +} diff --git a/src/fileutils.h b/src/fileutils.h new file mode 100644 index 000000000..9b5b431df --- /dev/null +++ b/src/fileutils.h @@ -0,0 +1,7 @@ +#ifndef _FILEUTILS_H +#define _FILEUTILS_H +#include + +FILE* ps_fopen(const char* path, const char* mode); + +#endif diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index acc514f13..b96f5549c 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -8,6 +8,7 @@ #include "fonts.h" #include "icon.h" #include "../ta-log.h" +#include "../fileutils.h" #include "imgui.h" #include "imgui_impl_sdl.h" #include "imgui_impl_sdlrenderer.h" @@ -251,7 +252,7 @@ DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,24 Size=1280,800 Split=Y void FurnaceGUI::prepareLayout() { FILE* check; - check=fopen(finalLayoutPath,"r"); + check=ps_fopen(finalLayoutPath,"r"); if (check!=NULL) { fclose(check); return; @@ -259,7 +260,7 @@ void FurnaceGUI::prepareLayout() { // copy initial layout logI("loading default layout.\n"); - check=fopen(finalLayoutPath,"w"); + check=ps_fopen(finalLayoutPath,"w"); if (check==NULL) { logW("could not write default layout!\n"); return; @@ -3133,7 +3134,7 @@ int FurnaceGUI::save(String path) { lastError=e->getLastError(); return 3; } - FILE* outFile=fopen(path.c_str(),"wb"); + FILE* outFile=ps_fopen(path.c_str(),"wb"); if (outFile==NULL) { lastError=strerror(errno); w->finish(); @@ -3217,7 +3218,7 @@ int FurnaceGUI::save(String path) { int FurnaceGUI::load(String path) { if (!path.empty()) { logI("loading module...\n"); - FILE* f=fopen(path.c_str(),"rb"); + FILE* f=ps_fopen(path.c_str(),"rb"); if (f==NULL) { perror("error"); lastError=strerror(errno); diff --git a/src/main.cpp b/src/main.cpp index 58a039fd2..fb9193e58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include "ta-log.h" +#include "fileutils.h" #include "engine/engine.h" #ifdef _WIN32 @@ -249,7 +250,7 @@ int main(int argc, char** argv) { logI("Furnace version " DIV_VERSION ".\n"); if (!fileName.empty()) { logI("loading module...\n"); - FILE* f=fopen(fileName.c_str(),"rb"); + FILE* f=ps_fopen(fileName.c_str(),"rb"); if (f==NULL) { perror("error"); return 1;