]> git.sur5r.net Git - bacula/bacula/commitdiff
rewrote to use ReadConsole/WriteConsole instead of cwprinf and cwgets on win32
authorThorsten Engel <thorsten.engel@matrix-computer.com>
Thu, 12 May 2005 14:36:43 +0000 (14:36 +0000)
committerThorsten Engel <thorsten.engel@matrix-computer.com>
Thu, 12 May 2005 14:36:43 +0000 (14:36 +0000)
git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@2024 91ce42f0-d328-0410-95d8-f526ca767f89

bacula/src/lib/winapi.c
bacula/src/lib/winapi.h
bacula/src/win32/compat/compat.cpp

index 9a5f6532cf936323d2e279ad51b7b4e39a35142a..b7bf9d613ea8e4e3ac2af8ac0872e59c5d8e8e0a 100644 (file)
@@ -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);
    }
    
index b57dbb6802abe8405ac7e8e996bafd98495b1248..de5ebf55078cff235fb30469dc38a77c547e24e7 100644 (file)
@@ -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);
index 96c21319601df516948bf35a5e43098ab0e8d7ed..d8c89056c71d154c2cfd24ea42ca1679b5d23052 100644 (file)
@@ -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;         
       }
    }