Stream Support

Dirk Elmendorf (delmendo@cymitar.net)
Fri, 8 Aug 1997 12:01:43 -0600

Message-Id: <v03110700b0110e1fbf18@[207.71.45.18]>
Date: Fri, 8 Aug 1997 12:01:43 -0600
To: fastcgi-developers@OpenMarket.com
From: Dirk Elmendorf <delmendo@cymitar.net>
Subject: Stream Support

The following should work. I've been using it in my programs.

int main()
{
	//Declare the streams
	fcgi_buf CGI_BUF(FCGI_stdout);
	ostream INIT_BUF(&CGI_BUF);

	//Now Sync them
	    cout = INIT_BUF;
	    cout.sync_with_stdio();
	//Now cout & printf both work.
}
fcgi_buf::fcgi_buf(FCGI_FILE *fp)
{
    out = fp;
}

fcgi_buf::fcgi_buf(FCGX_Stream *st)
{
    out = &holder;
    holder.stdio_stream = NULL;
    holder.fcgx_stream = st;
}

int fcgi_buf::sync ()
{
    streamsize n = pptr() - pbase();
    return((n && (int)FCGI_fwrite(pbase(), n, 1, out) != n) ? (-1 /*EOF*/)
: 0);
}

int fcgi_buf::overflow (int ch)
{
    streamsize n = pptr() - pbase();
    if (n && sync ())
        return EOF;
    if (ch != EOF)
    {
        char cbuf[1];
        cbuf[0] = ch;
        if (FCGI_fwrite(cbuf, 1, 1, out) != 1)
            return EOF;
    }
    pbump(-n);  // Reset pptr().
    return 0;
}

streamsize fcgi_buf::xsputn (char* text, streamsize n)
{
    return((sync () == EOF) ? 0 : FCGI_fwrite(text, n, 1, out));
}

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Dirk Elmendorf	Head of Software Design & Development
Cymitar Network Services
San Antonio, TX
www.cymitar.net