[fpc-pascal] TFileStream and stdin - how to make this work?
Honza
befelemepeseveze at gmail.com
Sun Oct 3 15:21:18 CEST 2010
2010/10/3 Bernd Kreuss <prof7bit at googlemail.com>:
> this might be a stupid question but I find myself again struggling with
> file IO and stdin/stdout. I am trying to do the following:
>
> StdIn := TFileStream.Create('/dev/stdin', fmOpenRead);
>
> and later on i want to poll whether there is something to read. I want
> to try reading binary data that will be piped to my application and i
> need to poll (non blocking!) and in case of incoming data read the first
> byte to determine the type of message that has arrived and then read the
> rest.
>
> The Stream seems to be able to read when there is something to read on
> stdin but I cannot find any way to actually test whether the ReadByte()
> will block (nothing to read) or whether there is data available.
>
> the properties size and position both are always $ffffffffffffffff and
> the stream has no eof property.
>
> Can this be done at all? And is my way of opening the stream correct?
> What would be a more platform independent way of opening the stdin as a
> stream to read binary data or is there a different and better way to do
> this, maybe a stream is not the correct thing for this at all? How do I
> poll and read binary data from stdin?
I don't think it's possible to succeed in this using TFileStream.
Additionally, the stdin thing on *nix is IMO designed to be normally
used in a blocking manner.
OTOH, it's likely, that on *nix you can find some syscall/libc
functionality, which will provide the "file descriptor is ready for
reading" information. But you asked for platform independence and I
don't know much about Windows. Neither if there is or is not something
like that being cross platform somewhere in the RTL/FCL already.
--
bflm
freepascal-bits.blogspot.com
More information about the fpc-pascal
mailing list