On Tue, 10 May 2011 13:30:26 +0100
Ben Summers <[email protected]> wrote:

[...]
> You should let Windows do all the hard work for you at the process
> boundaries.
> 
> Compile the command line utility in 'unicode mode' (#define UNICODE
> command line equivalent), which gets you an API which just gives you
> UTF16.
> 
> To get your command line arguments in UTF16, use 
> 
>   int _tmain(int argc, TCHAR *argv[])
> 
> instead of the conventional main(), which will expand to the right
> thing depending whether UNICODE is defined.
> 
>   http://msdn.microsoft.com/en-us/library/4t912wf5(v=VS.71).aspx
> 
> Convert the arguments using WideCharToMultiByte(CP_UTF8, ...) and
> MultiByteToWideChar(CP_UTF8, ...) to get your internal UTF8
> representation.
AFAIK, working with wmain (_tmain with _UNICODE defined) is broken in
MinGW, and requires a hack like this [1] to work.  I did not verify this
myself, but this has been discussed on the msysgit ML quite recently,
so I think the situation is still the same.

> To get the output right, call SetConsoleOutputCP(CP_UTF8) and write
> UTF8 to stdout.
> 
>   http://msdn.microsoft.com/en-us/library/ms686036(VS.85).aspx
That's not gonna work.
First, this messes the console code page up--the code page stays at what
SetConsoleOutputCP() set it to after the process exits which is
unacceptable.
Second, outputting a simple sequence of 5 Cyrillic characters ("абвгд")
encoded in UTF-8 using this method simply does not work--puts() outputs
one character not related to the set mentioned above.
A quick demonstration example is attached (I compiled it using VC6, my
Windows uses code page 866 for console output).

1. http://www.coderforlife.com/projects/utilities/
   (See "MinGW Unicode support" there).
#include <windows.h>
#include <stdio.h>

int main(void)
{
	const char utf8[] = {
		0x0d, 0x0b,
		0x0d, 0x1b,
		0x0d, 0x2b,
		0x0d, 0x3b,
		0x0d, 0x4b,
		0x00 };
	int ok;

#if 1
	ok = SetConsoleOutputCP(65001);
	if (!ok) {
		fputs("Failed to set console CP to UTF-8", stderr);
		return 1;
	}
#endif
	puts(utf8);
	return 0;
}

_______________________________________________
fossil-users mailing list
[email protected]
http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users

Reply via email to