diff options
Diffstat (limited to 'src/portable.cpp')
-rw-r--r-- | src/portable.cpp | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/src/portable.cpp b/src/portable.cpp index a1f1e96..11fcb82 100644 --- a/src/portable.cpp +++ b/src/portable.cpp @@ -16,6 +16,7 @@ extern char **environ; #endif +#include <assert.h> #include <ctype.h> #include <map> #include <string> @@ -146,8 +147,10 @@ int Portable::system(const char *command,const char *args,bool commandHasConsole // For that case COM is initialized as follows CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); - QString commandw = QString::fromUtf8( commandCorrectedPath ); - QString argsw = QString::fromUtf8( args ); + uint16_t *commandw = NULL; + recodeUtf8StringToW( commandCorrectedPath, &commandw ); + uint16_t *argsw = NULL; + recodeUtf8StringToW( args, &argsw ); // gswin32 is a GUI api which will pop up a window and run // asynchronously. To prevent both, we use ShellExecuteEx and @@ -161,8 +164,8 @@ int Portable::system(const char *command,const char *args,bool commandHasConsole */ NULL, /* window handle */ NULL, /* action to perform: open */ - (LPCWSTR)commandw.ucs2(), /* file to execute */ - (LPCWSTR)argsw.ucs2(), /* argument list */ + (LPCWSTR)commandw, /* file to execute */ + (LPCWSTR)argsw, /* argument list */ NULL, /* use current working dir */ SW_HIDE, /* minimize on start-up */ 0, /* application instance handle */ @@ -176,6 +179,8 @@ int Portable::system(const char *command,const char *args,bool commandHasConsole if (!ShellExecuteExW(&sInfo)) { + delete[] commandw; + delete[] argsw; return -1; } else if (sInfo.hProcess) /* executable was launched, wait for it to finish */ @@ -188,6 +193,8 @@ int Portable::system(const char *command,const char *args,bool commandHasConsole exitCode = -1; } CloseHandle(sInfo.hProcess); + delete[] commandw; + delete[] argsw; return exitCode; } } @@ -314,9 +321,18 @@ portable_off_t Portable::ftell(FILE *f) FILE *Portable::fopen(const char *fileName,const char *mode) { #if defined(_WIN32) && !defined(__CYGWIN__) - QString fn(fileName); - QString m(mode); - return _wfopen((wchar_t*)fn.ucs2(),(wchar_t*)m.ucs2()); + uint16_t *fn = 0; + size_t fn_len = recodeUtf8StringToW(fileName,&fn); + uint16_t *m = 0; + size_t m_len = recodeUtf8StringToW(mode,&m); + FILE *result = 0; + if (fn_len!=(size_t)-1 && m_len!=(size_t)-1) + { + result = _wfopen((wchar_t*)fn,(wchar_t*)m); + } + delete[] fn; + delete[] m; + return result; #else return ::fopen(fileName,mode); #endif @@ -582,3 +598,21 @@ const char *Portable::devNull() return "/dev/null"; #endif } + +size_t Portable::recodeUtf8StringToW(const char *inputStr,uint16_t **outBuf) +{ + if (inputStr==0 || outBuf==0) return 0; // empty input or invalid output + void *handle = portable_iconv_open("UTF-16LE","UTF-8"); + if (handle==(void *)(-1)) return 0; // invalid encoding + size_t len = strlen(inputStr); + uint16_t *buf = new uint16_t[len+1]; + *outBuf = buf; + size_t inRemains = len; + size_t outRemains = len*sizeof(uint16_t)+2; // chars + \0 + portable_iconv(handle,(char**)&inputStr,&inRemains,(char**)&buf,&outRemains); + *buf=0; + portable_iconv_close(handle); + return len; +} + + |