From 07eba457f37fffd6ddb91c5a0fddb6a84885cc4d Mon Sep 17 00:00:00 2001 From: Thorsten Engel Date: Thu, 12 May 2005 14:36:43 +0000 Subject: [PATCH] rewrote to use ReadConsole/WriteConsole instead of cwprinf and cwgets on win32 git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@2024 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/lib/winapi.c | 12 +---- bacula/src/lib/winapi.h | 5 -- bacula/src/win32/compat/compat.cpp | 87 ++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/bacula/src/lib/winapi.c b/bacula/src/lib/winapi.c index 9a5f6532cf..b7bf9d613e 100644 --- a/bacula/src/lib/winapi.c +++ b/bacula/src/lib/winapi.c @@ -50,9 +50,6 @@ t_wunlink p_wunlink = NULL; t_wmkdir p_wmkdir = NULL; t_wopen p_wopen = NULL; -t_cgetws p_cgetws = NULL; -t_cwprintf p_cwprintf = NULL; - t_GetFileAttributesA p_GetFileAttributesA = NULL; t_GetFileAttributesW p_GetFileAttributesW = NULL; @@ -149,14 +146,7 @@ InitWinAPIWrapper() /* wopen */ p_wopen = (t_wopen) GetProcAddress(hLib, "_wopen"); - - /* cgetws */ - p_cgetws = (t_cgetws) - GetProcAddress (hLib, "_cgetws"); - /* cwprintf */ - p_cwprintf = (t_cwprintf) - GetProcAddress (hLib, "_cwprintf"); - + FreeLibrary(hLib); } diff --git a/bacula/src/lib/winapi.h b/bacula/src/lib/winapi.h index b57dbb6802..de5ebf5507 100644 --- a/bacula/src/lib/winapi.h +++ b/bacula/src/lib/winapi.h @@ -66,15 +66,10 @@ typedef int (__cdecl * t_wunlink) (const wchar_t *); typedef int (__cdecl * t_wmkdir) (const wchar_t *); typedef int (__cdecl * t_wopen) (const wchar_t *, int, ...); -typedef wchar_t* (__cdecl *t_cgetws) (wchar_t *); -typedef int (__cdecl *t_cwprintf) (const wchar_t *, ...); - extern t_wunlink p_wunlink; extern t_wmkdir p_wmkdir; extern t_wopen p_wopen; -extern t_cgetws p_cgetws; -extern t_cwprintf p_cwprintf; /* In KERNEL32.DLL */ typedef BOOL (WINAPI * t_GetFileAttributesExA)(LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID); diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index 96c2131960..d8c89056c7 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -930,15 +930,31 @@ win32_getcwd(char *buf, int maxlen) int win32_fputs(const char *string, FILE *stream) { - /* we use _cwprintf (console printf) + /* we use WriteConsoleA / WriteConsoleA so we can be sure that unicode support works on win32. - this works under nt and 98 (95 and me not tested) - */ + with fallback if something fails + */ + + HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE); + if (hOut && (hOut != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte && + p_MultiByteToWideChar && (stream == stdout)) { + + WCHAR wszBuf[MAX_PATH_UNICODE]; + char szBuf[MAX_PATH_UNICODE]; + DWORD dwCharsWritten; + DWORD dwChars; + + dwChars = UTF8_2_wchar(wszBuf, string, MAX_PATH_UNICODE); + + /* try WriteConsoleW */ + if (WriteConsoleW (hOut, wszBuf, dwChars-1, &dwCharsWritten, NULL)) + return dwCharsWritten; + + /* convert to local codepage and try WriteConsoleA */ + dwChars = p_WideCharToMultiByte(GetConsoleOutputCP(),0,wszBuf,-1,szBuf,MAX_PATH_UNICODE,NULL,NULL); + if (WriteConsoleA (hOut, szBuf, dwChars-1, &dwCharsWritten, NULL)) + return dwCharsWritten; - if (p_MultiByteToWideChar && (stream == stdout) && p_cwprintf) { - WCHAR szBuf[MAX_PATH_UNICODE]; - UTF8_2_wchar(szBuf, string, MAX_PATH_UNICODE); - return p_cwprintf (szBuf); } return fputs(string, stream); @@ -947,27 +963,52 @@ win32_fputs(const char *string, FILE *stream) char* win32_cgets (char* buffer, int len) { - /* we use console gets / getws to be able to read unicode - from the win32 console */ + /* we use console ReadConsoleA / ReadConsoleW to be able to read unicode + from the win32 console and fallback if seomething fails */ + + HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE); + if (hIn && (hIn != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte && p_MultiByteToWideChar) { + DWORD dwRead; + WCHAR wszBuf[1024]; + char szBuf[1024]; + + /* nt and unicode conversion */ + if (ReadConsoleW (hIn, wszBuf, 1024, &dwRead, NULL)) { + + /* null terminate at end */ + if (wszBuf[dwRead-1] == L'\n') { + wszBuf[dwRead-1] = L'\0'; + dwRead --; + } - /* nt and unicode conversion */ - if ((g_platform_id == VER_PLATFORM_WIN32_NT) && p_WideCharToMultiByte && p_cgetws) { - WCHAR szBuf[260]; - szBuf[0] = min (255, len); /* max len, must be smaller than buffer */ - if (p_cgetws(szBuf) && wchar_2_UTF8(buffer, &szBuf[2], len)) + if (wszBuf[dwRead-1] == L'\r') { + wszBuf[dwRead-1] = L'\0'; + dwRead --; + } + + wchar_2_UTF8(buffer, wszBuf, len); return buffer; - } + } + + /* win 9x and unicode conversion */ + if (ReadConsoleA (hIn, szBuf, 1024, &dwRead, NULL)) { - /* win 9x and unicode conversion */ - if ((g_platform_id == VER_PLATFORM_WIN32_WINDOWS) && p_WideCharToMultiByte && p_MultiByteToWideChar) { - char szBuf[260]; - szBuf[0] = min(255, len); /* max len, must be smaller than buffer */ - if (_cgets(szBuf)) { - WCHAR wszBuf[260]; - p_MultiByteToWideChar(CP_OEMCP, 0, &szBuf[2], -1, wszBuf,260); + /* null terminate at end */ + if (szBuf[dwRead-1] == L'\n') { + szBuf[dwRead-1] = L'\0'; + dwRead --; + } + + if (szBuf[dwRead-1] == L'\r') { + szBuf[dwRead-1] = L'\0'; + dwRead --; + } + /* convert from ansii to WCHAR */ + p_MultiByteToWideChar(GetConsoleCP(), 0, szBuf, -1, wszBuf,1024); + /* convert from WCHAR to UTF-8 */ if (wchar_2_UTF8(buffer, wszBuf, len)) - return buffer; + return buffer; } } -- 2.39.5