Tk Source Code

Artifact [d3ce1f5f]
Login

Artifact d3ce1f5f032e7bb811e05dd2f6ba2227575080cc:

Attachment "fileFilters.diff" to ticket [1083878f] added by vincentdarley 2004-12-14 20:54:01.
? fileFilters.diff
? library/v_menu.tcl
Index: generic/tkFileFilter.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkFileFilter.c,v
retrieving revision 1.5
diff -b -u -r1.5 tkFileFilter.c
--- generic/tkFileFilter.c	27 Jan 2002 11:10:50 -0000	1.5
+++ generic/tkFileFilter.c	14 Dec 2004 13:50:22 -0000
@@ -17,7 +17,7 @@
 
 static int		AddClause _ANSI_ARGS_((
 			    Tcl_Interp * interp, FileFilter * filterPtr,
-			    CONST char * patternsStr, CONST char * ostypesStr,
+			    Tcl_Obj * patternsObj, Tcl_Obj * ostypesObj,
 			    int isWindows));
 static void		FreeClauses _ANSI_ARGS_((FileFilter * filterPtr));
 static void		FreeGlobPatterns _ANSI_ARGS_((
@@ -79,22 +79,26 @@
  *----------------------------------------------------------------------
  */
 int
-TkGetFileFilters(interp, flistPtr, string, isWindows)
+TkGetFileFilters(interp, flistPtr, types, isWindows)
     Tcl_Interp *interp;		/* Interpreter to use for error reporting. */
     FileFilterList * flistPtr;	/* Stores the list of file filters. */
-    char * string;		/* Value of the -filetypes option. */
+    Tcl_Obj* types;		/* Value of the -filetypes option. */
     int isWindows;		/* True if we are running on Windows. */
 {
-    int listArgc;
-    CONST char ** listArgv = NULL;
-    CONST char ** typeInfo = NULL;
+    int listObjc;
+    Tcl_Obj ** listObjv = NULL;
+    Tcl_Obj ** typeInfo = NULL;
     int code = TCL_OK;
     int i;
 
-    if (Tcl_SplitList(interp, string, &listArgc, &listArgv) != TCL_OK) {
+    if (types == NULL) {
+        goto done;
+    }
+    
+    if (Tcl_ListObjGetElements(interp, types, &listObjc, &listObjv) != TCL_OK) {
 	return TCL_ERROR;
     }
-    if (listArgc == 0) {
+    if (listObjc == 0) {
 	goto done;
     }
 
@@ -105,7 +109,7 @@
      */
     TkFreeFileFilters(flistPtr);
 
-    for (i = 0; i<listArgc; i++) {
+    for (i = 0; i<listObjc; i++) {
 	/*
 	 * Each file type should have two or three elements: the first one
 	 * is the name of the type and the second is the filter of the type.
@@ -114,45 +118,38 @@
 	int count;
 	FileFilter * filterPtr;
 
-	if (Tcl_SplitList(interp, listArgv[i], &count, &typeInfo) != TCL_OK) {
+	if (Tcl_ListObjGetElements(interp, listObjv[i], &count, 
+				   &typeInfo) != TCL_OK) {
 	    code = TCL_ERROR;
 	    goto done;
 	}
 	
 	if (count != 2 && count != 3) {
-	    Tcl_AppendResult(interp, "bad file type \"", listArgv[i], "\", ",
+	    Tcl_AppendResult(interp, "bad file type \"", 
+		Tcl_GetString(listObjv[i]), "\", ",
 		"should be \"typeName {extension ?extensions ...?} ",
 		"?{macType ?macTypes ...?}?\"",	NULL);
 	    code = TCL_ERROR;
 	    goto done;
 	}
 
-	filterPtr = GetFilter(flistPtr, typeInfo[0]);
+	filterPtr = GetFilter(flistPtr, Tcl_GetString(typeInfo[0]));
 
 	if (count == 2) {
-	    code = AddClause(interp, filterPtr, typeInfo[1], NULL,
-		isWindows);
+	    code = AddClause(interp, filterPtr, typeInfo[1], 
+			     NULL, isWindows);
 	} else {
-	    code = AddClause(interp, filterPtr, typeInfo[1], typeInfo[2],
-		isWindows);
+	    code = AddClause(interp, filterPtr, typeInfo[1], 
+			     typeInfo[2], isWindows);
 	}
 	if (code != TCL_OK) {
 	    goto done;
 	}
 
-        if (typeInfo) {
-	    ckfree((char*)typeInfo);
-	}
 	typeInfo = NULL;
     }
 
   done:
-    if (typeInfo) {
-       ckfree((char*)typeInfo);
-    }
-    if (listArgv) {
-	ckfree((char*)listArgv);
-    }
     return code;
 }
 
@@ -203,38 +200,77 @@
  *----------------------------------------------------------------------
  */
 
-static int AddClause(interp, filterPtr, patternsStr, ostypesStr, isWindows)
+static int AddClause(interp, filterPtr, patternsObj, ostypesObj, isWindows)
     Tcl_Interp * interp;	/* Interpreter to use for error reporting. */
     FileFilter * filterPtr;	/* Stores the new filter clause */
-    CONST char * patternsStr;		/* A TCL list of glob patterns. */
-    CONST char * ostypesStr;		/* A TCL list of Mac OSType strings. */
+    Tcl_Obj * patternsObj;	/* A Tcl list of glob patterns. */
+    Tcl_Obj * ostypesObj;	/* A Tcl list of Mac OSType strings. */
     int isWindows;		/* True if we are running on Windows; False
 				 * if we are running on the Mac; Glob
 				 * patterns need to be processed differently
 				 * on these two platforms */
 {
-    CONST char ** globList = NULL;
+    Tcl_Obj ** globList = NULL;
     int globCount;
-    CONST char ** ostypeList = NULL;
+    Tcl_Obj ** ostypeList = NULL;
     int ostypeCount;
     FileFilterClause * clausePtr;
     int i;
     int code = TCL_OK;
+    Tcl_Encoding macRoman = NULL;
 
-    if (Tcl_SplitList(interp, patternsStr, &globCount, &globList)!= TCL_OK) {
+    if (Tcl_ListObjGetElements(interp, patternsObj, &globCount, &globList)
+	    != TCL_OK) {
 	code = TCL_ERROR;
 	goto done;
     }
-    if (ostypesStr != NULL) {
-	if (Tcl_SplitList(interp, ostypesStr, &ostypeCount, &ostypeList)
+    if (ostypesObj != NULL) {
+	if (Tcl_ListObjGetElements(interp, ostypesObj, &ostypeCount, &ostypeList)
 		!= TCL_OK) {
 	    code = TCL_ERROR;
 	    goto done;
 	}
+	/* 
+	 * Might be cleaner to use 'Tcl_GetOSTypeFromObj' but that is
+	 * actually static to the MacOS X/Darwin version of Tcl, and
+	 * would therefore require further code refactoring.
+	 */
 	for (i=0; i<ostypeCount; i++) {
-	    if (strlen(ostypeList[i]) != 4) {
+	    int len;
+	    CONST char *strType = Tcl_GetStringFromObj(ostypeList[i], &len);
+	    /* 
+	     * If len is < 4, it is definitely an error.  If equal or
+	     * longer, we need to use the macRoman encoding to determine
+	     * the correct length (assuming there may be non-ascii
+	     * characters, eg., embedded nulls or accented characters in
+	     * the string, the macRoman length will be different).
+	     */
+	    if (len >= 4) {
+		if (macRoman == NULL) {
+		    macRoman = Tcl_GetEncoding(NULL, "macRoman");
+		}
+		/* 
+		 * If we couldn't load the encoding, then we can't
+		 * actually check the correct length.  But here we
+		 * assume we're probably operating on unix/windows
+		 * with a minimal set of encodings and so don't
+		 * care about MacOS types.  So we won't signal
+		 * an error.
+		 */
+		if (macRoman != NULL) {
+		    Tcl_DString osTypeDS;
+		    /* 
+		     * Convert utf to macRoman, since MacOS types are 
+		     * defined to be 4 macRoman characters long
+		     */
+		    Tcl_UtfToExternalDString(macRoman, strType, len, &osTypeDS);
+		    len = Tcl_DStringLength(&osTypeDS);
+		    Tcl_DStringFree(&osTypeDS);
+		}
+	    }
+	    if (len != 4) {
 		Tcl_AppendResult(interp, "bad Macintosh file type \"",
-		    ostypeList[i], "\"", NULL);
+		    Tcl_GetString(ostypeList[i]), "\"", NULL);
 		code = TCL_ERROR;
 		goto done;
 	    }
@@ -264,22 +300,23 @@
 	    GlobPattern * globPtr = (GlobPattern*)ckalloc(sizeof(GlobPattern));
 	    int len;
 	    
-	    len = (strlen(globList[i]) + 1) * sizeof(char);
+	    CONST char *str = Tcl_GetStringFromObj(globList[i], &len);
+	    len = (len + 1) * sizeof(char);
 
-	    if (globList[i][0] && globList[i][0] != '*') {
+	    if (str[0] && str[0] != '*') {
 		/*
 		 * Prepend a "*" to patterns that do not have a leading "*"
 		 */
 		globPtr->pattern = (char*)ckalloc((unsigned int) len+1);
 		globPtr->pattern[0] = '*';
-		strcpy(globPtr->pattern+1, globList[i]);
+		strcpy(globPtr->pattern+1, str);
 	    }
 	    else if (isWindows) {
-		if (strcmp(globList[i], "*") == 0) {
+		if (strcmp(str, "*") == 0) {
 		    globPtr->pattern = (char*)ckalloc(4*sizeof(char));
 		    strcpy(globPtr->pattern, "*.*");
 		}
-		else if (strcmp(globList[i], "") == 0) {
+		else if (strcmp(str, "") == 0) {
 		    /*
 		     * An empty string means "match all files with no
 		     * extensions"
@@ -290,11 +327,11 @@
 		}
 		else {
 		    globPtr->pattern = (char*)ckalloc((unsigned int) len);
-		    strcpy(globPtr->pattern, globList[i]);
+		    strcpy(globPtr->pattern, str);
 		}
 	    } else {
 		globPtr->pattern = (char*)ckalloc((unsigned int) len);
-		strcpy(globPtr->pattern, globList[i]);
+		strcpy(globPtr->pattern, str);
 	    }
 
 	    /*
@@ -311,10 +348,22 @@
 	}
     }
     if (ostypeCount > 0 && ostypeList != NULL) {
+	Tcl_DString osTypeDS;
+	if (macRoman == NULL) {
+	    macRoman = Tcl_GetEncoding(NULL, "macRoman");
+	}
 	for (i=0; i<ostypeCount; i++) {
+	    int len;
 	    MacFileType * mfPtr = (MacFileType*)ckalloc(sizeof(MacFileType));
+	    CONST char *strType = Tcl_GetStringFromObj(ostypeList[i], &len);
 
-	    memcpy(&mfPtr->type, ostypeList[i], sizeof(OSType));
+	    /* 
+	     * Convert utf to macRoman, since MacOS types are 
+	     * defined to be 4 macRoman characters long
+	     */
+	    Tcl_UtfToExternalDString(macRoman, strType, len, &osTypeDS);
+
+	    memcpy(&mfPtr->type, Tcl_DStringValue(&osTypeDS), sizeof(OSType));
 
 	    /*
 	     * Add the Mac type pattern into the list of Mac types
@@ -327,16 +376,14 @@
 	    }
 	    mfPtr->next = NULL;
 	}
+	Tcl_DStringFree(&osTypeDS);
     }
 
   done:
-    if (globList) {
-	ckfree((char*)globList);
-    }
-    if (ostypeList) {
-	ckfree((char*)ostypeList);
-    }
 
+    if (macRoman != NULL) {
+	Tcl_FreeEncoding(macRoman);
+    }
     return code;
 }	
 
Index: generic/tkFileFilter.h
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkFileFilter.h,v
retrieving revision 1.5
diff -b -u -r1.5 tkFileFilter.h
--- generic/tkFileFilter.h	17 Mar 2004 18:15:43 -0000	1.5
+++ generic/tkFileFilter.h	14 Dec 2004 13:50:22 -0000
@@ -79,7 +79,7 @@
 EXTERN void		TkInitFileFilters _ANSI_ARGS_((
 			    FileFilterList * flistPtr));
 EXTERN int		TkGetFileFilters _ANSI_ARGS_ ((Tcl_Interp *interp,
-    			    FileFilterList * flistPtr, char * string,
+    			    FileFilterList * flistPtr, Tcl_Obj *valuePtr,
 			    int isWindows));
 
 # undef TCL_STORAGE_CLASS
Index: generic/tkTextDisp.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/generic/tkTextDisp.c,v
retrieving revision 1.45
diff -b -u -r1.45 tkTextDisp.c
--- generic/tkTextDisp.c	26 Oct 2004 12:51:45 -0000	1.45
+++ generic/tkTextDisp.c	14 Dec 2004 13:50:44 -0000
@@ -6567,7 +6567,7 @@
     int noCharsYet;		/* Non-zero means no characters have been
 				 * assigned to this display line yet. */
     TkWrapMode wrapMode;	/* How to handle line wrapping: 
-                        	 * TEXT_WRAPMODE_CHAR, * TEXT_WRAPMODE_NONE, 
+                        	 * TEXT_WRAPMODE_CHAR, TEXT_WRAPMODE_NONE, 
                         	 * or TEXT_WRAPMODE_WORD. */
     register TkTextDispChunk *chunkPtr;
 				/* Structure to fill in with information
Index: macosx/tkMacOSXDialog.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/macosx/tkMacOSXDialog.c,v
retrieving revision 1.10
diff -b -u -r1.10 tkMacOSXDialog.c
--- macosx/tkMacOSXDialog.c	24 May 2004 21:21:50 -0000	1.10
+++ macosx/tkMacOSXDialog.c	14 Dec 2004 13:50:49 -0000
@@ -323,8 +323,7 @@
             case OPEN_DEFAULT:
                 break;
             case OPEN_FILETYPES:
-                choice = Tcl_GetStringFromObj(objv[i + 1], NULL);
-                if (TkGetFileFilters(interp, &ofd.fl, choice, 0) 
+                if (TkGetFileFilters(interp, &ofd.fl, objv[i + 1], 0) 
                         != TCL_OK) {
                     result = TCL_ERROR;
                     goto end;
Index: tests/filebox.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/filebox.test,v
retrieving revision 1.17
diff -b -u -r1.17 filebox.test
--- tests/filebox.test	8 Dec 2004 03:02:53 -0000	1.17
+++ tests/filebox.test	14 Dec 2004 13:50:50 -0000
@@ -13,6 +13,20 @@
 eval tcltest::configure $argv
 tcltest::loadTestedCommands
 
+test fileDialog-0.1 {GetFileName: file types: MakeFilter() fails} {
+    # MacOS type that is too long
+
+    set res [list [catch {tk_getSaveFile -filetypes {{"foo" .foo {\0\0\0\0\0}}}} msg] $msg]
+    regsub -all "\0" $res {\\0}
+} {1 {bad Macintosh file type "\0\0\0\0\0"}}
+test fileDialog-0.2 {GetFileName: file types: MakeFilter() fails} {
+    # MacOS type that is too short, but looks ok in utf (4 bytes).
+
+    set x [catch {tk_getSaveFile -filetypes {{"foo" .foo {\0\0}}}} msg]
+    regsub -all "\0" $msg {\\0} msg
+    list $x $msg
+} {1 {bad Macintosh file type "\0\0"}}
+
 set tk_strictMotif_old $tk_strictMotif
 
 #----------------------------------------------------------------------
Index: tests/winDialog.test
===================================================================
RCS file: /cvsroot/tktoolkit/tk/tests/winDialog.test,v
retrieving revision 1.11
diff -b -u -r1.11 winDialog.test
--- tests/winDialog.test	17 Jun 2004 22:38:57 -0000	1.11
+++ tests/winDialog.test	14 Dec 2004 13:50:51 -0000
@@ -257,6 +257,24 @@
     }
     set x
 } {c:/12x 457}
+test winDialog-5.25 {GetFileName: file types: MakeFilter() succeeds} {nt} {
+    # MacOS type that is correct, but has embedded nulls.
+
+    start {set x [catch {tk_getSaveFile -filetypes {{"foo" .foo {\0\0\0\0}}}}]}
+    then {
+	Click 2
+    }
+    set x
+} {0}
+test winDialog-5.26 {GetFileName: file types: MakeFilter() succeeds} {nt} {
+    # MacOS type that is correct, but has embedded high-bit chars.
+
+    start {set x [catch {tk_getSaveFile -filetypes {{"foo" .foo {\u2022\u2022\u2022\u2022}}}}]}
+    then {
+	Click 2
+    }
+    set x
+} {0}
 
 test winDialog-6.1 {MakeFilter} {emptyTest nt} {} {}
 
Index: win/tkWinDialog.c
===================================================================
RCS file: /cvsroot/tktoolkit/tk/win/tkWinDialog.c,v
retrieving revision 1.36
diff -b -u -r1.36 tkWinDialog.c
--- win/tkWinDialog.c	20 Aug 2004 00:58:52 -0000	1.36
+++ win/tkWinDialog.c	14 Dec 2004 13:50:58 -0000
@@ -175,7 +175,7 @@
 static int 		GetFileNameW(ClientData clientData, 
 			    Tcl_Interp *interp, int objc, 
 			    Tcl_Obj *CONST objv[], int isOpen);
-static int 		MakeFilter(Tcl_Interp *interp, char *string, 
+static int 		MakeFilter(Tcl_Interp *interp, Tcl_Obj *valuePtr, 
 			    Tcl_DString *dsPtr);
 static UINT APIENTRY	OFNHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, 
 			    LPARAM lParam);
@@ -651,7 +651,7 @@
 	    }
 	    case FILE_TYPES: {
 		Tcl_DStringFree(&utfFilterString);
-		if (MakeFilter(interp, string, &utfFilterString) != TCL_OK) {
+		if (MakeFilter(interp, valuePtr, &utfFilterString) != TCL_OK) {
 		    goto end;
 		}
 		filter = Tcl_DStringValue(&utfFilterString);
@@ -698,7 +698,7 @@
     }
 
     if (filter == NULL) {
-	if (MakeFilter(interp, "", &utfFilterString) != TCL_OK) {
+	if (MakeFilter(interp, NULL, &utfFilterString) != TCL_OK) {
 	    goto end;
 	}
     }
@@ -1127,7 +1127,7 @@
 	    }
 	    case FILE_TYPES: {
 		Tcl_DStringFree(&utfFilterString);
-		if (MakeFilter(interp, string, &utfFilterString) != TCL_OK) {
+		if (MakeFilter(interp, valuePtr, &utfFilterString) != TCL_OK) {
 		    goto end;
 		}
 		filter = Tcl_DStringValue(&utfFilterString);
@@ -1174,7 +1174,7 @@
     }
 
     if (filter == NULL) {
-	if (MakeFilter(interp, "", &utfFilterString) != TCL_OK) {
+	if (MakeFilter(interp, NULL, &utfFilterString) != TCL_OK) {
 	    goto end;
 	}
     }
@@ -1516,9 +1516,9 @@
  *----------------------------------------------------------------------
  */
 static int 
-MakeFilter(interp, string, dsPtr) 
+MakeFilter(interp, valuePtr, dsPtr) 
     Tcl_Interp *interp;		/* Current interpreter. */
-    char *string;		/* String value of the -filetypes option */
+    Tcl_Obj *valuePtr;		/* Value of the -filetypes option */
     Tcl_DString *dsPtr;		/* Filled with windows filter string. */
 {
     char *filterStr;
@@ -1528,7 +1528,7 @@
     FileFilter *filterPtr;
 
     TkInitFileFilters(&flist);
-    if (TkGetFileFilters(interp, &flist, string, 1) != TCL_OK) {
+    if (TkGetFileFilters(interp, &flist, valuePtr, 1) != TCL_OK) {
 	return TCL_ERROR;
     }
 
@@ -1552,6 +1552,13 @@
 	*p = '\0';
 
     } else {
+	int len;
+	if (valuePtr == NULL) {
+	    len = 0;
+	} else {
+	    CONST char* string = Tcl_GetStringFromObj(valuePtr, &len);
+	}
+	
 	/* We format the filetype into a string understood by Windows:
 	 * {"Text Documents" {.doc .txt} {TEXT}} becomes
 	 * "Text Documents (*.doc,*.txt)\0*.doc;*.txt\0"
@@ -1564,7 +1571,7 @@
 	 * Since we may only add asterisks (*) to the filter, we need at most
 	 * twice the size of the string to format the filter
 	 */
-	filterStr = ckalloc((unsigned int) strlen(string) * 3);
+	filterStr = ckalloc((unsigned int) len * 3);
 
 	for (filterPtr = flist.filters, p = filterStr; filterPtr;
 	        filterPtr = filterPtr->next) {