Aw: filenames on MS Windows: follow-up

Bastian Märkisch <[email protected]>
Newsgroups gmane.comp.graphics.gnuplot.devel
Message-ID <trinity-4edfbbed-435c-492c-844c-d3601126be22-1606988976911@3c-app-webde-bs62>
Dear Allin,

sorry for being slow to work on this. There is a fundamental issue with
the approach, which I am working on how to resolve best. Translating all
the command line arguments to UTF-8 is a great step forward and the 
loading of files works fine as you point out. But this change also 
affects and potentially breaks the -e option (try e.g. 
-d -e "print 'öäü'"). 

The best step in my opinion might be to just change gnuplot's default 
encoding to utf8. This will break backward compatibility, though. A
remedy could be new command line options -a/-u which choose the system's
default encoding ("ANSI") or the utf8 encoding.

Please find attached a simplified version of your patch, which also 
works for console mode gnuplot and other compilers than MinGW. It does
not yet address the issue discussed above, though.

Bastian

> Gesendet: Samstag, 14. November 2020 um 22:41 Uhr
> Von: "Allin Cottrell" <[email protected]>
> An: "gnuplot-beta" <[email protected]>
> Betreff: filenames on MS Windows: follow-up
>
> I'm attaching a patch against gnuplot git master which does what I 
> mentioned in 
> https://sourceforge.net/p/gnuplot/mailman/message/37141539/ That is, 
> it allows wgnuplot.exe to accept via the Windows command-line 
> unicode filenames that cannot be represented in the user's "system 
> codepage", and to successfully to open such files.
> 
> This iteration of my patch uses native win32 APIs to perform the 
> necessary recoding of filenames, as opposed to the previous 
> iteration which used GLib.
> 
> I gave evidence in 
> https://sourceforge.net/p/gnuplot/mailman/message/37143153/
> that the patch is not disruptive of gnuplot's ability to open files 
> named via the "load" command, in which case the encoding specified 
> in the gnuplot script must be respected.
> 
> The patch is activated only when compiling the program for Windows, 
> and then only when the symbol WIDE_ARGS is defined.
> 
> -- 
> Allin Cottrell
> Department of Economics
> Wake Forest University_______________________________________________
> gnuplot-beta mailing list
> [email protected]
> Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
>

_______________________________________________
gnuplot-beta mailing list
[email protected]
Membership management via: https://lists.sourceforge.net/lists/listinfo/gnuplot-beta
wide-args-3.patch (text/plain, 2.5 KB)
diff --git a/src/misc.c b/src/misc.c
index b1fd32c5d..5907e9a14 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -255,7 +255,7 @@ load_file(FILE *fp, char *name, int calltype)
 	    } else {
 		/* Either we successfully read a line from input file fp
 		 * or we are about to copy a line from a datablock.
-		 * Either way we have to process line-ending '\' as a
+		 * Either way we have to process line-ending '\' as a
 		 * continuation request.
 		 */
 		if (!fp && datablock_input_line) {
diff --git a/src/win/winmain.c b/src/win/winmain.c
index 0fd3de027..a78ccc1c8 100644
--- a/src/win/winmain.c
+++ b/src/win/winmain.c
@@ -425,6 +425,41 @@ ReadMainIni(LPTSTR file, LPTSTR section)
 }


+static int
+alt_winargs(int *pargc, char ***pargv)
+{
+    int argc_w = 0;
+    LPWSTR *argv_w;
+    int err = 0;
+
+    /* get args as UTF-16 */
+    argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
+
+    if (argv_w == NULL) {
+	err = 1;
+    } else {
+	/* convert args to UTF-8, so they can be passed
+	   and read as char * by gnu_main()
+	*/
+	char **argv_u8 = calloc(argc_w, sizeof *argv_u8);
+	int i;
+
+	for (i = 0; i < argc_w && !err; i++) {
+	    argv_u8[i] = AnsiText(argv_w[i], S_ENC_UTF8);
+	    if (argv_u8[i] == NULL)
+		err = 1;
+	}
+	*pargc = argc_w;
+	*pargv = argv_u8;
+
+	/* we're done with this */
+	LocalFree(argv_w);
+    }
+
+    return err;
+}
+
+
 #ifndef WGP_CONSOLE
 int WINAPI
 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
@@ -440,15 +475,23 @@ main(int argc, char **argv)
     int i;
 #endif

+#if 0
 #ifndef WGP_CONSOLE
-# if defined( __MINGW32__) && !defined(_W64)
+# if defined(__MINGW32__) && !defined(_W64)
 #  define argc _argc
 #  define argv _argv
-# else /* MSVC, WATCOM, MINGW-W64 */
+# else /* MSVC, WATCOM */
 #  define argc __argc
 #  define argv __argv
 # endif
 #endif /* WGP_CONSOLE */
+#else
+#  define argc argc_u
+#  define argv argv_u
+    int argc_u = 0;
+    char **argv_u = NULL;
+    alt_winargs(&argc_u, &argv_u);
+#endif

     szModuleName = (LPTSTR) malloc((MAXSTR + 1) * sizeof(TCHAR));
     CheckMemory(szModuleName);
@@ -1572,6 +1615,12 @@ win_fopen(const char *filename, const char *mode)
     LPWSTR wfilename = UnicodeText(filename, encoding);
     LPWSTR wmode = UnicodeText(mode, encoding);
     file = _wfopen(wfilename, wmode);
+    if (file == NULL) {
+	/* "encoding" didn't work, try UTF-8 instead */
+	free(wfilename);
+	wfilename = UnicodeText(filename, S_ENC_UTF8);
+	file = _wfopen(wfilename, wmode);
+    }
     free(wfilename);
     free(wmode);
     return file;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.