X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=libfcgi%2Ffcgio.cpp;h=5a54c1153c9dce8ad07412e23dc8c6729ecc44ca;hb=b123beeec36dd9eea9e3da208fbdc42e15ede2d4;hp=5edd3633bcf8436f8998a6753006a54645a35160;hpb=af4a4f30576c355081a0284b743338803c809f43;p=catagits%2Ffcgi2.git diff --git a/libfcgi/fcgio.cpp b/libfcgi/fcgio.cpp index 5edd363..5a54c11 100644 --- a/libfcgi/fcgio.cpp +++ b/libfcgi/fcgio.cpp @@ -1,5 +1,5 @@ // -// $Id: fcgio.cpp,v 1.7 2001/11/20 03:24:19 robs Exp $ +// $Id: fcgio.cpp,v 1.14 2003/06/22 00:51:27 robs Exp $ // // Allows you communicate with FastCGI streams using C++ iostreams // @@ -22,21 +22,42 @@ #define DLLAPI __declspec(dllexport) #endif +#include #include "fcgio.h" -fcgi_streambuf::fcgi_streambuf(FCGX_Stream * strm) +using std::streambuf; +using std::istream; +using std::ostream; +using std::streamsize; + +fcgi_streambuf::fcgi_streambuf(FCGX_Stream * fs, char * b, int bs) +{ + init(fs, b, bs); +} + +fcgi_streambuf::fcgi_streambuf(char_type * b, streamsize bs) +{ + init(0, b, bs); +} + +fcgi_streambuf::fcgi_streambuf(FCGX_Stream * fs) { - this->fcgx = strm; - this->buf = NULL; - this->bufsize = 0; - setbuf(NULL, 0); + init(fs, 0, 0); } fcgi_streambuf::~fcgi_streambuf(void) { overflow(EOF); // FCGX_Finish()/FCGX_Accept() will flush and close -} +} + +void fcgi_streambuf::init(FCGX_Stream * fs, char_type * b, streamsize bs) +{ + this->fcgx = fs; + this->buf = 0; + this->bufsize = 0; + setbuf(b, bs); +} int fcgi_streambuf::overflow(int c) { @@ -59,14 +80,29 @@ int fcgi_streambuf::overflow(int c) return 0; } -// default base class behaviour seems to be inconsistent +// default base class behaviour seems to be inconsistent int fcgi_streambuf::sync() { - if (overflow(EOF)) return EOF; + if (overflow(EOF)) return EOF; if (FCGX_FFlush(this->fcgx)) return EOF; return 0; } +// uflow() removes the char, underflow() doesn't +int fcgi_streambuf::uflow() +{ + if (this->bufsize) + { + int c = underflow(); + gbump(1); + return c; + } + else + { + return FCGX_GetChar(this->fcgx); + } +} + int fcgi_streambuf::underflow() { if (this->bufsize) @@ -83,82 +119,83 @@ int fcgi_streambuf::underflow() } else { - return FCGX_GetChar(this->fcgx); + return FCGX_UnGetChar(FCGX_GetChar(this->fcgx), this->fcgx); } } -void fcgi_streambuf::reset(void) -{ - // it should be ok to set up both the get and put areas - char * end = this->buf + this->bufsize; - this->setg(this->buf, this->buf, end); - this->setb(this->buf, end); -} - -streambuf * fcgi_streambuf::setbuf(char * buf, int len) +void fcgi_streambuf::reset(void) +{ + // it should be ok to set up both the get and put areas + setg(this->buf, this->buf, this->buf); + setp(this->buf, this->buf + this->bufsize); +} + +std::streambuf * fcgi_streambuf::setbuf(char_type * b, streamsize bs) { - // XXX support moving data from an old buffer - if (this->bufsize) return NULL; - - this->buf = buf; - this->bufsize = len; - - // the base setbuf() *has* to be called - streambuf::setbuf(buf, len); + // XXX support moving data from an old buffer + if (this->bufsize) return 0; + + this->buf = b; + this->bufsize = bs; - reset(); + // the base setbuf() *has* to be called + streambuf::setbuf(b, bs); + + reset(); return this; -} - -int fcgi_streambuf::attach(FCGX_Stream * strm) -{ - this->fcgx = strm; - - if (this->bufsize) - { - reset(); - } - - return 0; -} - -int fcgi_streambuf::xsgetn(char * s, int n) -{ - return (this->bufsize) - ? streambuf::xsgetn(s, n) - : FCGX_GetStr(s, n, this->fcgx); -} - -int fcgi_streambuf::xsputn(const char * s, int n) -{ - return (this->bufsize) - ? streambuf::xsputn(s, n) - : FCGX_PutStr(s, n, this->fcgx); -} - -// deprecated -fcgi_istream::fcgi_istream(FCGX_Stream * strm) : +} + +int fcgi_streambuf::attach(FCGX_Stream * fs) +{ + this->fcgx = fs; + + if (this->bufsize) + { + reset(); + } + + return 0; +} + +streamsize fcgi_streambuf::xsgetn(char_type * s, streamsize n) +{ + if (n > INT_MAX) return 0; + return (this->bufsize) + ? streambuf::xsgetn(s, n) + : (streamsize) FCGX_GetStr((char *) s, (int) n, this->fcgx); +} + +streamsize fcgi_streambuf::xsputn(const char_type * s, streamsize n) +{ + if (n > INT_MAX) return 0; + return (this->bufsize) + ? streambuf::xsputn(s, n) + : (streamsize) FCGX_PutStr((char *) s, (int) n, this->fcgx); +} + +// deprecated +fcgi_istream::fcgi_istream(FCGX_Stream * fs) : istream(&fcgi_strmbuf) { - fcgi_strmbuf.attach(strm); + fcgi_strmbuf.attach(fs); } -// deprecated -void fcgi_istream::attach(FCGX_Stream * strm) +// deprecated +void fcgi_istream::attach(FCGX_Stream * fs) { - fcgi_strmbuf.attach(strm); + fcgi_strmbuf.attach(fs); } -// deprecated -fcgi_ostream::fcgi_ostream(FCGX_Stream * strm) : - ostream(&fcgi_strmbuf) +// deprecated +fcgi_ostream::fcgi_ostream(FCGX_Stream * fs) : + ostream(&fcgi_strmbuf) { - fcgi_strmbuf.attach(strm); + fcgi_strmbuf.attach(fs); } -// deprecated -void fcgi_ostream::attach(FCGX_Stream * strm) +// deprecated +void fcgi_ostream::attach(FCGX_Stream * fs) { - fcgi_strmbuf.attach(strm); + fcgi_strmbuf.attach(fs); }