C++ support - provided by Michael Richards <mimiker@scifair.acadiau.ca>n(Apollo Softw...
[catagits/fcgi2.git] / include / fcgio.h
1 // fcgio.h - defines classes fcgio_buf, fcgio_ostream, fcgio_istream
2 //           and replaces cin, cout and cerr with FastCGI versions
3 //           you must include this file in any source file which 
4 //           uses cin, cout or cerr. It must also appear at the end
5 //           of your includes
6 //
7 // $Id: fcgio.h,v 1.1 1999/08/15 03:37:59 roberts Exp $
8 //
9 // This work is based on routines written by George Feinberg. They 
10 // have been mostly re-written and extensively changed by 
11 // Michael Richards.
12 //
13 // Copyright (c) 1999 by Apollo Software. All rights reserved.
14 //
15 // You are free to use this software without charge or royalty
16 // as long as this notice is not removed or altered, and recognition 
17 // is given to the author(s)
18 //
19    
20 #ifndef FCGIO_H
21 #define FCGIO_H
22    
23 #include <iostream.h>
24 #include <fcgiapp.h>
25    
26
27 // FastCGI streambuf replacement. Implements low level io to the 
28 // FastCGI c functions so our higher level iostreams will talk
29 // in the fcgi protocol
30 class fcgio_buf : public streambuf {
31   // construction
32   public:
33     fcgio_buf( FCGX_Stream *str) { fcgi_str = str; }
34     ~fcgio_buf() {}
35    
36     // from streambuf
37     // tells the stream to go flush itself
38     virtual int sync();
39     // handles allocation of buffers by failing since buffer support isn't used
40     virtual int doallocate() { return 0; }
41    
42     virtual int xsgetn(char *s, int n);
43     virtual int xsputn(const char* s, int n);
44     virtual int overflow(int);
45     virtual int underflow();
46     virtual streambuf* setbuf(char* s, int n);
47    
48   private:
49     FCGX_Stream * fcgi_str;
50 };
51
52    
53 // Here's the ostream class definition. All it has to do is
54 // call ios::init() passing an instance of fcgio_buf
55 class fcgio_ostream : public ostream {
56   public:
57     fcgio_ostream(FCGX_Stream *str);
58     ~fcgio_ostream() { buf.sync(); }
59
60   private:
61     fcgio_buf buf;
62 };
63
64
65 // Here's the istream class definition. All it has to do is
66 // call ios::init() passing an instance of fcgio_buf
67 class fcgio_istream : public istream {
68   public:
69     fcgio_istream(FCGX_Stream *str);
70     ~fcgio_istream() {}
71
72   private:
73     // FastCGI stuff used for the FCGX_ functions
74     fcgio_buf buf;
75     // buffer for getting data
76     const int buffersize=100;
77     char buffer[buffersize];
78 };
79
80
81 // replace cin and cout with our own versions
82 #ifndef USE_CIN_COUT_CERR
83   #undef cout   
84   #define cout (*FCGIX_cout)
85   extern fcgio_ostream *FCGIX_cout;
86
87   #undef cerr   
88   #define cerr (*FCGIX_cerr)
89   extern fcgio_ostream *FCGIX_cerr;
90
91   #undef cin
92   #define cin (*FCGIX_cin)
93   extern fcgio_istream *FCGIX_cin;
94 #endif
95
96 #endif __FCCIO_H__