123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #if defined(_WIN32)
- # include <stdio.h>
- # include <stdlib.h>
- # include <wchar.h>
- # include <io.h>
- # include <fcntl.h>
- # define WIN32_LEAN_AND_MEAN
- # define WIN32_EXTRA_LEAN
- # include <windows.h>
- # include "win32utf8.h"
- static char *utf16_to_utf8(const wchar_t *_src){
- char *dst;
- size_t len;
- size_t si;
- size_t di;
- len=wcslen(_src);
- dst=(char *)malloc(sizeof(*dst)*(3*len+1));
- if(dst==NULL)return dst;
- for(di=si=0;si<len;si++){
- unsigned c0;
- c0=_src[si];
- if(c0<0x80){
-
- dst[di++]=(char)c0;
- continue;
- }
- else if(c0<0x800){
-
- dst[di++]=(char)(0xC0|c0>>6);
- dst[di++]=(char)(0x80|c0&0x3F);
- continue;
- }
- else if(c0>=0xD800&&c0<0xDC00){
- unsigned c1;
-
- c1=_src[si+1];
- if(c1>=0xDC00&&c1<0xE000){
- unsigned w;
-
- w=((c0&0x3FF)<<10|c1&0x3FF)+0x10000;
-
- dst[di++]=(char)(0xF0|w>>18);
- dst[di++]=(char)(0x80|w>>12&0x3F);
- dst[di++]=(char)(0x80|w>>6&0x3F);
- dst[di++]=(char)(0x80|w&0x3F);
- si++;
- continue;
- }
- }
-
- dst[di++]=(char)(0xE0|c0>>12);
- dst[di++]=(char)(0x80|c0>>6&0x3F);
- dst[di++]=(char)(0x80|c0&0x3F);
- }
- dst[di++]='\0';
- return dst;
- }
- typedef LPWSTR *(APIENTRY *command_line_to_argv_w_func)(LPCWSTR cmd_line,
- int *num_args);
- void win32_utf8_setup(int *_argc,const char ***_argv){
- HMODULE hlib;
-
- _setmode(_fileno(stdin),_O_BINARY);
- _setmode(_fileno(stdout),_O_BINARY);
- hlib=LoadLibraryA("shell32.dll");
- if(hlib!=NULL){
- command_line_to_argv_w_func command_line_to_argv_w;
-
- command_line_to_argv_w=(command_line_to_argv_w_func)GetProcAddress(hlib,
- "CommandLineToArgvW");
- if(command_line_to_argv_w!=NULL){
- wchar_t **argvw;
- int argc;
- argvw=(*command_line_to_argv_w)(GetCommandLineW(),&argc);
- if(argvw!=NULL){
- int ai;
-
- if(argc>*_argc)argc=*_argc;
- for(ai=0;ai<argc;ai++){
- char *argv;
- argv=utf16_to_utf8(argvw[ai]);
- if(argv!=NULL)(*_argv)[ai]=argv;
- }
- *_argc=argc;
- LocalFree(argvw);
- }
- }
- FreeLibrary(hlib);
- }
- # if defined(CP_UTF8)
-
-
- # endif
- }
- #endif
|