Re: Read all from stdin

Wan-Teh Chang <[email protected]>
Newsgroups gmane.comp.mozilla.devel.nspr
Message-ID <[email protected]>
soubok wrote:
> Hello,
> I need to read all remaining data from stdin (PR_StandardInput) but it
> seems that PR_Available do not work with stdin file.

PR_Available should work with stdin on Unix (including Linux
and Mac OS X).  On Windows PR_Available does not work with
stdin because the implementation of PR_Available uses a Winsock
function.

> Is there a way to PR_Read all datas from stdin at once ?

This depends on exactly what you want to do.  Again, Windows
presents a challenge.

1. You can put PR_StandardInput in non-blocking mode and
keep reading from PR_StandardInput until PR_Read returns
0 or -1.

This method only works on Unix (including Linux and Mac OS
X).  This method doesn't work on Windows because the function
to set a PRFileDesc in non-blocking mode calls Winsock functions.

2. If you know the maximum number of bytes that you may
read from stdin, you can allocate a buffer of that size,
and call PR_Read on stdin (in the default blocking mode).
When PR_Read returns, you have read all the data.

3. If you control how the peer sends data to your stdin,
you can arrange for the peer to send the byte count before
the data.  After you have read the byte count, you know
how many bytes of data will follow.

Wan-Teh
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.