www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - bug: Console buffer not flushed at right time

reply "Kevin Brogan" <kevin nospam.brogan.ca> writes:
==========================================================
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/  =-
Feb 07 2012
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
Please report bugs to d.puremagic.com/issues. This list is not supposed to be 
posted to directly by users. You sign up for it if you want to see all of the 
updates to bugs on Bugzilla.

- Jonathan M Davis

On Tuesday, February 07, 2012 15:49:19 Kevin Brogan wrote:
 ==========================================================
 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/ =-
Feb 07 2012