Tcl Source Code

Artifact [c81deed647]
Login

Artifact c81deed6471750e4d1f2615ab4e5dcdcc07690a9:

Attachment "tcl-winunicon2.patch" to ticket [1256872fff] added by a_kovalenko 2005-08-24 05:43:27.
Index: win/tclWin32Dll.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWin32Dll.c,v
retrieving revision 1.46
diff -u -r1.46 tclWin32Dll.c
--- win/tclWin32Dll.c	24 Jul 2005 22:56:46 -0000	1.46
+++ win/tclWin32Dll.c	23 Aug 2005 22:38:17 -0000
@@ -118,8 +118,13 @@
     /* deleted (int (__cdecl*)(CONST TCHAR *, struct _utimbuf *)) _utime, */
     NULL,
     NULL,
+    /* getLongPathNameProc */ 
+    NULL,
     /* Security SDK - not available on 95,98,ME */
-    NULL, NULL, NULL, NULL, NULL, NULL
+    NULL, NULL, NULL, NULL, NULL, NULL,
+    /* ReadConsole and WriteConsole */
+    (BOOL (WINAPI *)(HANDLE, LPVOID, DWORD, LPDWORD, LPVOID)) ReadConsoleA,
+    (BOOL (WINAPI *)(HANDLE, const VOID*, DWORD, LPDWORD, LPVOID)) WriteConsoleA
 };
 
 static TclWinProcs unicodeProcs = {
@@ -171,8 +176,13 @@
     /* deleted (int (__cdecl*)(CONST TCHAR *, struct _utimbuf *)) _wutime, */
     NULL,
     NULL,
+    /* getLongPathNameProc */
+    NULL, 
     /* Security SDK - will be filled in on NT,XP,2000,2003 */
-    NULL, NULL, NULL, NULL, NULL, NULL
+    NULL, NULL, NULL, NULL, NULL, NULL,
+    /* ReadConsole and WriteConsole */
+    (BOOL (WINAPI *)(HANDLE, LPVOID, DWORD, LPDWORD, LPVOID)) ReadConsoleW,
+    (BOOL (WINAPI *)(HANDLE, const VOID*, DWORD, LPDWORD, LPVOID)) WriteConsoleW
 };
 
 TclWinProcs *tclWinProcs;
Index: win/tclWinConsole.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinConsole.c,v
retrieving revision 1.15
diff -u -r1.15 tclWinConsole.c
--- win/tclWinConsole.c	24 Jul 2005 22:56:47 -0000	1.15
+++ win/tclWinConsole.c	23 Aug 2005 22:38:17 -0000
@@ -54,6 +54,7 @@
  * This structure describes per-instance data for a console based channel.
  */
 
+
 typedef struct ConsoleInfo {
     HANDLE handle;
     int type;
@@ -187,6 +188,45 @@
 
 /*
  *----------------------------------------------------------------------
+ * 
+ * readConsoleBytes, writeConsoleBytes --
+ * Wrapper for ReadConsole{A,W}, that takes and returns number of bytes
+ * instead of number of TCHARS
+ */
+static BOOL readConsoleBytes(HANDLE hConsole,
+  LPVOID lpBuffer,
+  DWORD nbytes,
+  LPDWORD nbytesread)
+{
+    DWORD ntchars;
+    BOOL result;
+    int tcharsize;
+    tcharsize = tclWinProcs->useWide? 2 : 1;
+    result = tclWinProcs->readConsoleProc(
+	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);
+    if (nbytesread) 
+	*nbytesread = (ntchars*tcharsize);
+    return result;
+}
+
+static BOOL writeConsoleBytes(HANDLE hConsole,
+  const VOID *lpBuffer,
+  DWORD nbytes,
+  LPDWORD nbyteswritten)
+{
+    DWORD ntchars;
+    BOOL result;
+    int tcharsize;
+    tcharsize = tclWinProcs->useWide? 2 : 1;
+    result = tclWinProcs->writeConsoleProc(
+	    hConsole, lpBuffer, nbytes / tcharsize, &ntchars, NULL);
+    if (nbyteswritten) 
+	*nbyteswritten = (ntchars*tcharsize);
+    return result;
+}
+
+/*
+ *----------------------------------------------------------------------
  *
  * ConsoleInit --
  *
@@ -701,8 +741,8 @@
      * byte is available or an EOF occurs.
      */
 
-    if (ReadConsole(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &count,
-	    (LPOVERLAPPED) NULL) == TRUE) {
+    if (readConsoleBytes(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &count) 
+	    == TRUE) {
 	buf[count] = '\0';
 	return count;
     }
@@ -788,8 +828,8 @@
 	 * avoids an unnecessary copy.
 	 */
 
-	if (WriteConsole(infoPtr->handle, buf, toWrite, &bytesWritten,
-		NULL) == FALSE) {
+	if (writeConsoleBytes(infoPtr->handle, buf, toWrite, &bytesWritten)
+		== FALSE) {
 	    TclWinConvertError(GetLastError());
 	    goto error;
 	}
@@ -1133,8 +1173,8 @@
 	 * not KEY_EVENTs.
 	 */
 
-	if (ReadConsoleA(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
-		(LPDWORD) &infoPtr->bytesRead, NULL) != FALSE) {
+	if (readConsoleBytes(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
+		(LPDWORD) &infoPtr->bytesRead) != FALSE) {
 	    /*
 	     * Data was stored in the buffer.
 	     */
@@ -1233,7 +1273,7 @@
 	 */
 
 	while (toWrite > 0) {
-	    if (WriteConsole(handle, buf, toWrite, &count, NULL) == FALSE) {
+	    if (writeConsoleBytes(handle, buf, toWrite, &count) == FALSE) {
 		infoPtr->writeError = GetLastError();
 		break;
 	    } else {
@@ -1363,7 +1403,10 @@
     
     Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
     Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");
-    Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", encoding);
+    if (tclWinProcs->useWide)
+	Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", "unicode");
+    else
+	Tcl_SetChannelOption(NULL, infoPtr->channel, "-encoding", encoding);
 
     return infoPtr->channel;
 }
Index: win/tclWinInt.h
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinInt.h,v
retrieving revision 1.28
diff -u -r1.28 tclWinInt.h
--- win/tclWinInt.h	3 Nov 2004 00:26:59 -0000	1.28
+++ win/tclWinInt.h	23 Aug 2005 22:38:17 -0000
@@ -127,6 +127,23 @@
 		    LPDWORD PrivilegeSetLength,
 		    LPDWORD GrantedAccess,
 		    LPBOOL AccessStatus);
+    /*
+     * Unicode console support. WriteConsole and ReadConsole
+     */
+    BOOL (WINAPI *readConsoleProc)(
+      HANDLE hConsoleInput,
+      LPVOID lpBuffer,
+      DWORD nNumberOfCharsToRead,
+      LPDWORD lpNumberOfCharsRead,
+      LPVOID lpReserved
+    );
+    BOOL (WINAPI *writeConsoleProc)(
+      HANDLE hConsoleOutput,
+      const VOID* lpBuffer,
+      DWORD nNumberOfCharsToWrite,
+      LPDWORD lpNumberOfCharsWritten,
+      LPVOID lpReserved
+    );
 } TclWinProcs;
 
 MODULE_SCOPE TclWinProcs *tclWinProcs;