From b461f63ae4769c1042d7ee1362f102bc3bfc2456 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Thu, 2 Jul 2015 10:37:07 -1000 Subject: [PATCH] wchar_t support for windows --- NODLib.pro | 3 +++ driver/driver.pro | 3 +++ driver/main.cpp | 16 ++++++++++++---- include/NOD/Util.hpp | 12 +++++++++++- lib/DiscBase.cpp | 27 ++++++++++++++++----------- lib/DiscIOISO.cpp | 4 ++-- lib/DiscIOWBFS.cpp | 4 ++-- lib/FileIOFILE.cpp | 7 +++---- lib/NOD.cpp | 4 ++++ lib/WideStringConvert.cpp | 2 +- 10 files changed, 57 insertions(+), 25 deletions(-) diff --git a/NODLib.pro b/NODLib.pro index 083cf79..536a556 100644 --- a/NODLib.pro +++ b/NODLib.pro @@ -1,3 +1,6 @@ +win32-g++ { +CROSS_COMPILE = x86_64-w64-mingw32- +} TEMPLATE = subdirs CONFIG -= app_bundle CONFIG -= qt diff --git a/driver/driver.pro b/driver/driver.pro index c8fd4c6..5c22a88 100644 --- a/driver/driver.pro +++ b/driver/driver.pro @@ -1,3 +1,6 @@ +win32-g++ { +QMAKE_LFLAGS += -municode +} TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle diff --git a/driver/main.cpp b/driver/main.cpp index 34c2fbf..bccbcc9 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -9,7 +9,15 @@ static void printHelp() " nodlib make []\n"); } +#if NOD_UCS2 +#ifdef strcasecmp +#undef strcasecmp +#endif +#define strcasecmp _wcsicmp +int wmain(int argc, wchar_t* argv[]) +#else int main(int argc, char* argv[]) +#endif { if (argc < 3) { @@ -17,8 +25,8 @@ int main(int argc, char* argv[]) return -1; } - const char* inDir = nullptr; - const char* outDir = "."; + const NOD::SystemChar* inDir = nullptr; + const NOD::SystemChar* outDir = _S("."); bool force = false; for (int a=2 ; a disc = NOD::OpenDiscFromImage(inDir); if (!disc) @@ -42,7 +50,7 @@ int main(int argc, char* argv[]) dataPart->extractToDirectory(outDir, force); } - else if (!strcasecmp(argv[1], "make")) + else if (!strcasecmp(argv[1], _S("make"))) { } else diff --git a/include/NOD/Util.hpp b/include/NOD/Util.hpp index d10a362..128e352 100644 --- a/include/NOD/Util.hpp +++ b/include/NOD/Util.hpp @@ -7,11 +7,21 @@ namespace NOD { -/* System char type */ +/* filesystem char type */ #if _WIN32 && UNICODE #include +#include +#include #define NOD_UCS2 1 +typedef struct _stat Sstat; +static inline int Mkdir(const wchar_t* path, int) {return _wmkdir(path);} +static inline int Stat(const wchar_t* path, Sstat* statout) {return _wstat(path, statout);} +#else #include +#include +typedef struct stat Sstat; +static inline int Mkdir(const char* path, mode_t mode) {return mkdir(path, mode);} +static inline int Stat(const char* path, Sstat* statout) {return stat(path, statout);} #endif /* String Converters */ diff --git a/lib/DiscBase.cpp b/lib/DiscBase.cpp index 8c193d0..c32f81d 100644 --- a/lib/DiscBase.cpp +++ b/lib/DiscBase.cpp @@ -1,4 +1,3 @@ -#include #include "NOD/DiscBase.hpp" #include "NOD/IFileIO.hpp" @@ -55,15 +54,18 @@ void DiscBase::IPartition::Node::extractToDirectory(const SystemString& basePath SystemString path = basePath + _S("/") + nameView.sys_str(); if (m_kind == NODE_DIRECTORY) { - if (mkdir(path.c_str(), 0755) && errno != EEXIST) - throw std::runtime_error("unable to mkdir '" + path + "'"); + if (Mkdir(path.c_str(), 0755) && errno != EEXIST) + { + SystemUTF8View pathView(path); + throw std::runtime_error("unable to mkdir '" + pathView.utf8_str() + "'"); + } for (Node& subnode : *this) subnode.extractToDirectory(path, force); } else if (m_kind == NODE_FILE) { - struct stat theStat; - if (force || stat(path.c_str(), &theStat)) + Sstat theStat; + if (force || Stat(path.c_str(), &theStat)) { m_hddFile = NewFileIO(path); std::unique_ptr rs = beginReadStream(); @@ -75,18 +77,21 @@ void DiscBase::IPartition::Node::extractToDirectory(const SystemString& basePath void DiscBase::IPartition::extractToDirectory(const SystemString& path, bool force) { - struct stat theStat; - if (mkdir(path.c_str(), 0755) && errno != EEXIST) - throw std::runtime_error("unable to mkdir '" + path + "'"); + Sstat theStat; + if (Mkdir(path.c_str(), 0755) && errno != EEXIST) + { + SystemUTF8View pathView(path); + throw std::runtime_error("unable to mkdir '" + pathView.utf8_str() + "'"); + } /* Extract Apploader */ - std::string apploaderPath = path + "/apploader.bin"; - if (force || stat(apploaderPath.c_str(), &theStat)) + SystemString apploaderPath = path + _S("/apploader.bin"); + if (force || Stat(apploaderPath.c_str(), &theStat)) { std::unique_ptr buf(new uint8_t[m_apploaderSz]); std::unique_ptr rs = beginReadStream(0x2440); rs->read(buf.get(), m_apploaderSz); - std::unique_ptr ws = NewFileIO(path + "/apploader.bin")->beginWriteStream(); + std::unique_ptr ws = NewFileIO(path + _S("/apploader.bin"))->beginWriteStream(); ws->write(buf.get(), m_apploaderSz); } diff --git a/lib/DiscIOISO.cpp b/lib/DiscIOISO.cpp index 14819cc..107bde5 100644 --- a/lib/DiscIOISO.cpp +++ b/lib/DiscIOISO.cpp @@ -29,7 +29,7 @@ public: std::unique_ptr beginReadStream(uint64_t offset) const { #if NOD_UCS2 - FILE* fp = wfopen(filepath.c_str(), L"rb"); + FILE* fp = _wfopen(filepath.c_str(), L"rb"); #else FILE* fp = fopen(filepath.c_str(), "rb"); #endif @@ -60,7 +60,7 @@ public: std::unique_ptr beginWriteStream(uint64_t offset) const { #if NOD_UCS2 - FILE* fp = wfopen(filepath.c_str(), L"wb"); + FILE* fp = _wfopen(filepath.c_str(), L"wb"); #else FILE* fp = fopen(filepath.c_str(), "wb"); #endif diff --git a/lib/DiscIOWBFS.cpp b/lib/DiscIOWBFS.cpp index 0f78ad5..f44691e 100644 --- a/lib/DiscIOWBFS.cpp +++ b/lib/DiscIOWBFS.cpp @@ -29,7 +29,7 @@ public: std::unique_ptr beginReadStream(uint64_t offset) const { #if NOD_UCS2 - FILE* fp = wfopen(filepath.c_str(), L"rb"); + FILE* fp = _wfopen(filepath.c_str(), L"rb"); #else FILE* fp = fopen(filepath.c_str(), "rb"); #endif @@ -60,7 +60,7 @@ public: std::unique_ptr beginWriteStream(uint64_t offset) const { #if NOD_UCS2 - FILE* fp = wfopen(filepath.c_str(), L"wb"); + FILE* fp = _wfopen(filepath.c_str(), L"wb"); #else FILE* fp = fopen(filepath.c_str(), "wb"); #endif diff --git a/lib/FileIOFILE.cpp b/lib/FileIOFILE.cpp index bfaafef..94f9f90 100644 --- a/lib/FileIOFILE.cpp +++ b/lib/FileIOFILE.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -22,7 +21,7 @@ public: uint64_t size() { #if NOD_UCS2 - FILE* fp = wfopen(m_path.c_str(), L"rb"); + FILE* fp = _wfopen(m_path.c_str(), L"rb"); #else FILE* fp = fopen(m_path.c_str(), "rb"); #endif @@ -41,7 +40,7 @@ public: WriteStream(const SystemString& path) { #if NOD_UCS2 - fp = wfopen(path.c_str(), L"wb"); + fp = _wfopen(path.c_str(), L"wb"); #else fp = fopen(path.c_str(), "wb"); #endif @@ -84,7 +83,7 @@ public: ReadStream(const SystemString& path) { #if NOD_UCS2 - fp = wfopen(path.c_str(), L"rb"); + fp = _wfopen(path.c_str(), L"rb"); #else fp = fopen(path.c_str(), "rb"); #endif diff --git a/lib/NOD.cpp b/lib/NOD.cpp index da0679f..7b3bd47 100644 --- a/lib/NOD.cpp +++ b/lib/NOD.cpp @@ -10,7 +10,11 @@ std::unique_ptr NewDiscIOWBFS(const SystemChar* path); std::unique_ptr OpenDiscFromImage(const SystemChar* path, bool& isWii) { /* Temporary file handle to determine image type */ +#if NOD_UCS2 + FILE* fp = _wfopen(path, L"rb"); +#else FILE* fp = fopen(path, "rb"); +#endif if (!fp) { #if NOD_UCS2 diff --git a/lib/WideStringConvert.cpp b/lib/WideStringConvert.cpp index 6fb1a97..6976f2f 100644 --- a/lib/WideStringConvert.cpp +++ b/lib/WideStringConvert.cpp @@ -1,6 +1,6 @@ -#include "NOD/NOD.hpp" #include #include +#include "NOD/NOD.hpp" namespace NOD {