diff --git a/cmd/cmd.cpp b/cmd/cmd.cpp index 0694a19..450c444 100644 --- a/cmd/cmd.cpp +++ b/cmd/cmd.cpp @@ -1,19 +1,42 @@ // stub #include -#include -#include -#include +#include // std::fprintf +#include // std::exit +#include // std::strlen -void error_log(const char* msg) { std::fprintf(stderr,"Error: %s\n",msg); } -void debug_print(const char* msg) { std::fprintf(stdout,"Debug: %s\n",msg); } -void fatal_log(const char* msg) { std::fprintf(stderr,"Fatal: %s\n",msg); std::exit(-1); } +// platform.cpp +void platform_setup(int argc, char** argv); +void platform_shutdown(); +int platform_argc(); +const char* platform_argv(int index); -int main() +void error_log(const char* msg) { + std::fprintf(stderr,"Error: %s\n",msg); +} + +void debug_print(const char* msg) +{ + std::fprintf(stdout,"Debug: %s\n",msg); +} + +void fatal_log(const char* msg) +{ + std::fprintf(stderr,"Fatal: %s\n",msg); + platform_shutdown(); + std::exit(-1); +} + +int main(int argc, char** argv) +{ + platform_setup(argc,argv); nsfplay_set_error_log(error_log); nsfplay_set_debug_print(debug_print); nsfplay_set_fatal(fatal_log); + + for (int i=0; i + diff --git a/cmd/cmd.vcxproj.filters b/cmd/cmd.vcxproj.filters index afb30f8..b3b0d89 100644 --- a/cmd/cmd.vcxproj.filters +++ b/cmd/cmd.vcxproj.filters @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/cmd/platform.cpp b/cmd/platform.cpp new file mode 100644 index 0000000..2dce6c5 --- /dev/null +++ b/cmd/platform.cpp @@ -0,0 +1,84 @@ +// platform.cpp +// sets up the console for the chosen platform + +#if defined(_WIN32) || defined(_WIN64) + +#define VC_EXTRALEAN +#define WIN32_LEAN_AND_MEAN +#include // GetConsoleOutputCP, SetConsoleOutputCP +#include // GetCommandLineW, CommandLineToArgW +#include // std::malloc, std::free +#include // HACK + +static UINT startup_cp = 0; +static LPWSTR* store_argv; +static int store_argc; +static char* store_convert = NULL; +static int store_convert_size = 0; + +void platform_setup(int argc, char** argv) +{ + // Ignore argc/argv from main and get the wide command line from Windows + (void)argc; + (void)argv; + store_argv = CommandLineToArgvW(GetCommandLineW(), &store_argc); + + // Windows needs its console set to a UTF8 code page + startup_cp = GetConsoleOutputCP(); + SetConsoleOutputCP(CP_UTF8); +} + +void platform_shutdown() +{ + std::free(store_convert); + SetConsoleOutputCP(startup_cp); + LocalFree(store_argv); +} + +int platform_argc() +{ + return store_argc; +} + +const char* platform_argv(int index) +{ + // convert wchar arguments to UTF-8 + int new_size = WideCharToMultiByte(CP_UTF8,0,store_argv[index],-1,NULL,0,NULL,NULL); + if (new_size > store_convert_size) + { + free(store_convert); + store_convert_size = new_size; + store_convert = (char*)malloc(store_convert_size); + } + WideCharToMultiByte(CP_UTF8,0,store_argv[index],-1,store_convert,store_convert_size,NULL,NULL); + return store_convert; +} + +#else + +// other platforms assume a UTF-8 by default + +static int store_argc; +static const char* const* store_argv; + +void platform_setup(int argc, char** argv) +{ + store_argc = argc; + store_argv = argv; +} + +void platform_shutdown() +{ +} + +int platform_argc() +{ + return store_argc; +} + +const char* platform_argv(int index) +{ + return store_argv[index]; +} + +#endif