www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Read from stdin without blocking

reply "Benjamin Thaut" <code benjamin-thaut.de> writes:
I want to read all input from stdin without blocking. That means 
I simply want to read the remaining input from stdin. All ways I 
tried so far always end up in me waiting for the user to enter 
additional input, which is not what I want.

I tried around a lot with D's files / streams but couldn't find 
any way to do that.

Any help is appreciated.

Kind Regards
Benjamin Thaut
Jan 13 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
The operating system does line buffering, so you'll need to turn 
that off. The function is tcssetattr() on Posix and 
SetConsoleMode on Windows.

My terminal.d does this in struct ctors and dtors:


example usage:

The RealTimeConsoleInput struct turns off buffering and has 
functions like getch() and kbhit() to fetch individual characters 
from it.

That's not quite the same as getting everything that's available, 
but might be useful to you.

Most efficient way I think though is to turn off buffering with 
the OS calls, then you can set stdin to be nonblocking and read 
chunks off it with ordinary read() or ReadFile calls.
Jan 13 2015