define HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
[catagits/fcgi2.git] / include / fcgio.h
1 //
2 // Provides support for FastCGI via C++ iostreams.
3 //
4 // $Id: fcgio.h,v 1.13 2001/12/04 00:22:04 robs Exp $
5 //
6 // This work is based on routines written by George Feinberg. They
7 // have been mostly re-written and extensively changed by
8 // Michael Richards.
9 //
10 // Rewritten again with bug fixes and numerous enhancements by
11 // Michael Shell.
12 // 
13 // And rewritten again by Rob Saccoccio. 
14 //
15 // Special Thanks to Dietmar Kuehl for his help and the numerous custom
16 // streambuf examples on his web site.
17 //
18 // Copyright (c) 2000 Tux the Linux Penguin
19 // Copyright (c) 2001 Rob Saccoccio and Chelsea Networks
20 //
21 // You are free to use this software without charge or royalty
22 // as long as this notice is not removed or altered, and recognition
23 // is given to the author(s)
24 //
25 // This code is offered as-is without any warranty either expressed or
26 // implied; without even the implied warranty of MERCHANTABILITY or
27 // FITNESS FOR A PARTICULAR PURPOSE.  If it breaks, you get to keep 
28 // both halves.
29
30 #ifndef FCGIO_H
31 #define FCGIO_H
32
33 #include <iostream.h>
34
35 #include "fcgiapp.h"
36
37 #ifndef DLLAPI
38 #ifdef _WIN32
39 #define DLLAPI __declspec(dllimport)
40 #else
41 #define DLLAPI
42 #endif
43 #endif
44
45 /*
46  *  fcgi_streambuf
47  */
48 class fcgi_streambuf : public streambuf
49 {
50 public:
51
52     // Note that if no buf is assigned (the default), iostream methods
53     // such as peek(), unget() and putback() will fail.  If a buf is
54     // assigned, I/O is a bit less effecient and output streams will
55     // have to be flushed (or the streambuf destroyed) before the next 
56     // call to "accept".
57     DLLAPI fcgi_streambuf(FCGX_Stream * fcgx, char * buf, int len);
58     
59     DLLAPI fcgi_streambuf(char * buf, int len);
60     
61     DLLAPI fcgi_streambuf(FCGX_Stream * fcgx = 0);
62
63     DLLAPI ~fcgi_streambuf(void);
64
65     DLLAPI int attach(FCGX_Stream * fcgx);
66
67 protected:
68
69     // Consume the put area (if buffered) and c (if c is not EOF).
70     DLLAPI virtual int overflow(int);
71
72     // Flush the put area (if buffered) and the FCGX buffer to the client.
73     DLLAPI virtual int sync();
74
75     // Remove and return the current character.
76     DLLAPI virtual int uflow();
77
78     // Fill the get area (if buffered) and return the current character.
79     DLLAPI virtual int underflow();
80
81     // Use a buffer.  The only reasons that a buffer would be useful is
82     // to support the use of the unget()/putback() or seek() methods.  Using
83     // a buffer will result in less efficient I/O.  Note: the underlying
84     // FastCGI library (FCGX) maintains its own input and output buffers.  
85     DLLAPI virtual streambuf * setbuf(char * buf, int len);
86
87     DLLAPI virtual int xsgetn(char * s, int n);
88     DLLAPI virtual int xsputn(const char * s, int n);
89
90 private:
91
92     FCGX_Stream * fcgx;
93
94     // buf is just handy to have around
95     char * buf;
96
97     // this isn't kept by the base class
98     int bufsize;
99     
100     void init(FCGX_Stream * fcgx, char * buf, int bufsize);
101
102     void reset(void);
103 };
104
105 /*
106  *  fcgi_istream - deprecated
107  */
108 class fcgi_istream : public istream
109 {
110 public:
111
112     // deprecated
113     DLLAPI fcgi_istream(FCGX_Stream * fcgx = 0);
114     
115     // deprecated
116     DLLAPI ~fcgi_istream(void) {}
117
118     // deprecated
119     DLLAPI virtual void attach(FCGX_Stream * fcgx);
120
121 private:
122
123     fcgi_streambuf fcgi_strmbuf;
124 };
125
126 /*
127  *  fcgi_ostream - deprecated
128  */
129 class fcgi_ostream : public ostream
130 {
131 public:
132     
133     // deprecated
134     DLLAPI fcgi_ostream(FCGX_Stream * fcgx = 0);
135     
136     // deprecated
137     DLLAPI ~fcgi_ostream(void) {}
138
139     // deprecated
140     DLLAPI virtual void attach(FCGX_Stream *fcgx);
141
142 private:
143
144     fcgi_streambuf fcgi_strmbuf;
145 };
146
147 #endif /* FCGIO_H */