2 // $Id: fcgio.cpp,v 1.12 2001/12/04 00:22:06 robs Exp $
4 // Allows you communicate with FastCGI streams using C++ iostreams
6 // ORIGINAL AUTHOR: George Feinberg
7 // REWRITTEN BY: Michael Richards 06/20/1999
8 // REWRITTEN AGAIN BY: Michael Shell 02/23/2000
9 // REWRITTEN AGAIN BY: Rob Saccoccio 11 Nov 2001
11 // Copyright (c) 2000 Tux the Linux Penguin
13 // You are free to use this software without charge or royalty
14 // as long as this notice is not removed or altered, and recognition
15 // is given to the author(s)
17 // This code is offered as-is without any warranty either expressed or
18 // implied; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.
22 #define DLLAPI __declspec(dllexport)
27 fcgi_streambuf::fcgi_streambuf(FCGX_Stream * fs, char * b, int bs)
32 fcgi_streambuf::fcgi_streambuf(char * b, int bs)
37 fcgi_streambuf::fcgi_streambuf(FCGX_Stream * fs)
42 fcgi_streambuf::~fcgi_streambuf(void)
45 // FCGX_Finish()/FCGX_Accept() will flush and close
48 void fcgi_streambuf::init(FCGX_Stream * fs, char * b, int bs)
56 int fcgi_streambuf::overflow(int c)
60 int plen = pptr() - pbase();
64 if (FCGX_PutStr(pbase(), plen, this->fcgx) != plen) return EOF;
71 if (FCGX_PutChar(c, this->fcgx) != c) return EOF;
77 // default base class behaviour seems to be inconsistent
78 int fcgi_streambuf::sync()
80 if (overflow(EOF)) return EOF;
81 if (FCGX_FFlush(this->fcgx)) return EOF;
85 // uflow() removes the char, underflow() doesn't
86 int fcgi_streambuf::uflow()
89 if (this->bufsize) gbump(1);
93 // Note that the expected behaviour when there is no buffer varies
94 int fcgi_streambuf::underflow()
100 int glen = FCGX_GetStr(eback(), this->bufsize, this->fcgx);
101 if (glen <= 0) return EOF;
103 setg(eback(), eback(), eback() + glen);
106 return (unsigned char) *gptr();
110 return FCGX_GetChar(this->fcgx);
114 void fcgi_streambuf::reset(void)
116 // it should be ok to set up both the get and put areas
117 setg(this->buf, this->buf, this->buf);
118 setp(this->buf, this->buf + this->bufsize);
121 streambuf * fcgi_streambuf::setbuf(char * b, int bs)
123 // XXX support moving data from an old buffer
124 if (this->bufsize) return 0;
129 // the base setbuf() *has* to be called
130 streambuf::setbuf(b, bs);
137 int fcgi_streambuf::attach(FCGX_Stream * fs)
149 int fcgi_streambuf::xsgetn(char * s, int n)
151 return (this->bufsize)
152 ? streambuf::xsgetn(s, n)
153 : FCGX_GetStr(s, n, this->fcgx);
156 int fcgi_streambuf::xsputn(const char * s, int n)
158 return (this->bufsize)
159 ? streambuf::xsputn(s, n)
160 : FCGX_PutStr(s, n, this->fcgx);
164 fcgi_istream::fcgi_istream(FCGX_Stream * fs) :
165 istream(&fcgi_strmbuf)
167 fcgi_strmbuf.attach(fs);
171 void fcgi_istream::attach(FCGX_Stream * fs)
173 fcgi_strmbuf.attach(fs);
177 fcgi_ostream::fcgi_ostream(FCGX_Stream * fs) :
178 ostream(&fcgi_strmbuf)
180 fcgi_strmbuf.attach(fs);
184 void fcgi_ostream::attach(FCGX_Stream * fs)
186 fcgi_strmbuf.attach(fs);