Skip to content

Commit

Permalink
windows also needs a UTF8 fopen abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbradsmith committed Apr 22, 2024
1 parent 88cdea0 commit 9ffb666
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cmd/cmd.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// stub
// cmd.cpp
// Main entry point for nsfplac.

#include <nsfplaycore.h>
#include <cstdio> // std::fprintf
#include <cstdlib> // std::exit
#include <cstring> // std::strlen

// platform.cpp
// platform specific abstractions (platform.cpp)
void platform_setup(int argc, char** argv);
void platform_shutdown();
int platform_argc();
const char* platform_argv(int index);
FILE* platform_fopen(const char* path, const char* mode);

// logging functions

void error_log(const char* msg)
{
Expand All @@ -28,6 +32,8 @@ void fatal_log(const char* msg)
std::exit(-1);
}

// main

int main(int argc, char** argv)
{
platform_setup(argc,argv);
Expand Down
40 changes: 37 additions & 3 deletions cmd/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // GetConsoleOutputCP, SetConsoleOutputCP
#include <windows.h> // GetConsoleOutputCP, SetConsoleOutputCP, WireCharToMultiByte, MultiByteToWideChar
#include <shellapi.h> // GetCommandLineW, CommandLineToArgW
#include <cstdlib> // std::malloc, std::free
#include <stdio.h> // HACK
#include <cstdio> // std::_wfopen

#if defined(_MSC_VER)
#pragma warning(disable:6387) // MSVC thinks store_wconvert could be NULL and warns about _wfopen_s
#endif

static UINT startup_cp = 0;
static LPWSTR* store_argv;
static int store_argc;
static char* store_convert = NULL;
static int store_convert_size = 0;
static wchar_t* store_wconvert = NULL;
static int store_wconvert_size = 0;


void platform_setup(int argc, char** argv)
{
Expand All @@ -30,6 +37,7 @@ void platform_setup(int argc, char** argv)

void platform_shutdown()
{
std::free(store_wconvert);
std::free(store_convert);
SetConsoleOutputCP(startup_cp);
LocalFree(store_argv);
Expand All @@ -48,14 +56,35 @@ const char* platform_argv(int index)
{
free(store_convert);
store_convert_size = new_size;
store_convert = (char*)malloc(store_convert_size);
store_convert = (char*)std::malloc(store_convert_size);
}
WideCharToMultiByte(CP_UTF8,0,store_argv[index],-1,store_convert,store_convert_size,NULL,NULL);
return store_convert;
}

FILE* platform_fopen(const char* path, const char* mode)
{
static wchar_t wmode[16];
MultiByteToWideChar(CP_UTF8,0,mode,-1,wmode,sizeof(wmode)/sizeof(wmode[0]));

int new_size = MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
if (new_size > store_wconvert_size)
{
free(store_wconvert);
store_wconvert_size = new_size;
store_wconvert = (wchar_t*)std::malloc(store_wconvert_size*sizeof(wchar_t));
}
MultiByteToWideChar(CP_UTF8,0,path,-1,store_wconvert,store_wconvert_size);

FILE* f;
if (0 == _wfopen_s(&f,store_wconvert,wmode)) return f;
return NULL;
}

#else

#include <cstdio> // std::fopen

// other platforms assume a UTF-8 by default

static int store_argc;
Expand All @@ -81,4 +110,9 @@ const char* platform_argv(int index)
return store_argv[index];
}

FILE* platform_fopen(const char* path, const char* mode)
{
return std::fopen(path,mode);
}

#endif

0 comments on commit 9ffb666

Please sign in to comment.