bug: Console buffer not flushed at right time

Kevin Brogan kevin at nospam.brogan.ca
Tue Feb 7 15:49:19 PST 2012


==========================================================
Bug:
----------------------------------------------------------

1) Console output buffer is not flushed when it should be.

==========================================================
Example Code:
----------------------------------------------------------

import std.stdio;

extern (Windows): uint GetConsoleOutputCP();
extern (Windows): bool SetConsoleOutputCP(uint codePageId);

void main()
{
	auto oldCP = [GetConsoleOutputCP(),SetConsoleOutputCP(65001)];
	scope(exit)	if(oldCP[1]) SetConsoleOutputCP(oldCP[0]);
		
	string str = "H\u266a!"; 
	
	foreach  (wchar c;  str)
	{ 
		write(c,' '); 		
	}
}


==========================================================
What should happen:
----------------------------------------------------------

When main starts, the console codepage is set to UTF 8. Characters are then output to the console, and then the console codepage is reset to what it was prior to program execution. The reset is neccessary to preserve the console environment that exists prior to the program being run.

==========================================================
What happens instead:
----------------------------------------------------------

Instead of outputting the characters while the console is in the requested codepage, output is buffered. When main exits, the buffer is flushed, but after the scope(exit) statement has reset the codepage, so the flushed output is output while the wrong codepage is selected.

==========================================================
Work arounds:
----------------------------------------------------------

a) Get console input first - Buffer appears to be flushed before accepting input from the console.

b) Output a carriage return - Buffer appears to be flushed after every carriage return

c) Explicit flush in scope(exit) - import std.cstream, call dout.flush() in the scope(exit) statement

d) Patch the runtime to set the console codepage to unicode after opening a console and then set it back to what it was on program exit - Makes sense since all strings are output in unicode, and I wouldn't have to use win32 apis to get logical behavior.


--------------=  Posted using GrabIt  =----------------
------=  Binary Usenet downloading made easy =---------
-=  Get GrabIt for free from http://www.shemes.com/  =-



More information about the Digitalmars-d-bugs mailing list