[Github-comments] [geany/geany] Provide utf-8 command line arguments on Windows (#1258)

Colomban Wendling notifications at xxxxx
Wed Nov 9 16:16:39 UTC 2016


b4n commented on this pull request.



> @@ -1032,4 +1032,31 @@ gchar *win32_get_user_config_dir(void)
 	return g_build_filename(g_get_user_config_dir(), "geany", NULL);
 }
 
+
+void win32_make_argc_and_argv_in_utf8(gint *pargc, gchar ***pargv)
+{
+	int num_arg;
+	LPWSTR *szarglist = CommandLineToArgvW(GetCommandLineW(), &num_arg);
+	char **utf8argv = g_new0(char *, num_arg + 1);
+	int i = num_arg;
+	while(i)
+	{
+		i--;
+		utf8argv[i] = g_utf16_to_utf8((gunichar2 *)szarglist[i], -1, NULL, NULL, NULL);

Well it's totally doable, but it involves calling `WideCharToMultiByte()` twice (or over-alloc `len*4+1` bytes), allocating manually and the like.  Indeed nothing complicated, but at least 4 lines or so worth of WINAPI code, where we basically have access to a 1-line thing.  So if it gives us something, yeah sure, otherwise I'm not really sure it's worth it.

Hell, let's see how it'd look:
```C
static gchar *wcstr_to_utf8(wchar_t *wcstr)
{
	int len = WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, NULL, 0, NULL, NULL);
	gchar *utf8str = g_malloc(len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wcstr, -1, utf8str, len + 1, NULL, NULL);
	utf8str[len] = 0; // FIXME: is that useful?
	return utf8str;
}
```
And according to the [docs](https://msdn.microsoft.com/en-us/library/windows/desktop/dd374130%28v=vs.85%29.aspx) it probably doesn't really help:
> **WC_ERR_INVALID_CHARS**: **Windows Vista and later:** Fail if an invalid input character is encountered. If this flag is not set, the function silently drops illegal code points. A call to GetLastError returns ERROR_NO_UNICODE_TRANSLATION. Note that this flag only applies when CodePage is specified as CP_UTF8 or 54936 (for Windows Vista and later). It cannot be used with other code page values.

So my understanding would be that in case of invalid UTF-16 (as it is possible in filenames), it would result in uninteresting UTF-8, while we'd actually want "WTF-8".

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/pull/1258
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20161109/90423e45/attachment.html>


More information about the Github-comments mailing list