Fix another case of defined %hash.
[catagits/fcgi2.git] / include / fcgio.h
1 //
2 // Provides support for FastCGI via C++ iostreams.
3 //
4 // $Id: fcgio.h,v 1.15 2002/02/25 13:16:11 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>
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 #if ! HAVE_STREAMBUF_CHAR_TYPE
46 typedef char char_type;
47 #endif
48
49 /*
50  *  fcgi_streambuf
51  */
52 class DLLAPI fcgi_streambuf : public std::streambuf
53 {
54 public:
55
56     // Note that if no buf is assigned (the default), iostream methods
57     // such as peek(), unget() and putback() will fail.  If a buf is
58     // assigned, I/O is a bit less effecient and output streams will
59     // have to be flushed (or the streambuf destroyed) before the next 
60     // call to "accept".
61     fcgi_streambuf(FCGX_Stream * fcgx, char * buf, int len);
62     
63     fcgi_streambuf(char_type * buf, std::streamsize len);
64     
65     fcgi_streambuf(FCGX_Stream * fcgx = 0);
66
67     ~fcgi_streambuf(void);
68
69     int attach(FCGX_Stream * fcgx);
70
71 protected:
72
73     // Consume the put area (if buffered) and c (if c is not EOF).
74     virtual int overflow(int);
75
76     // Flush the put area (if buffered) and the FCGX buffer to the client.
77     virtual int sync();
78
79     // Remove and return the current character.
80     virtual int uflow();
81
82     // Fill the get area (if buffered) and return the current character.
83     virtual int underflow();
84
85     // Use a buffer.  The only reasons that a buffer would be useful is
86     // to support the use of the unget()/putback() or seek() methods.  Using
87     // a buffer will result in less efficient I/O.  Note: the underlying
88     // FastCGI library (FCGX) maintains its own input and output buffers.  
89     virtual std::streambuf * setbuf(char_type * buf, std::streamsize len);
90
91     virtual std::streamsize xsgetn(char_type * s, std::streamsize n);
92     virtual std::streamsize xsputn(const char_type * s, std::streamsize n);
93
94 private:
95
96     FCGX_Stream * fcgx;
97
98     // buf is just handy to have around
99     char_type * buf;
100
101     // this isn't kept by the base class
102     std::streamsize bufsize;
103     
104     void init(FCGX_Stream * fcgx, char_type * buf, std::streamsize bufsize);
105
106     void reset(void);
107 };
108
109 /*
110  *  fcgi_istream - deprecated
111  */
112 class DLLAPI fcgi_istream : public std::istream
113 {
114 public:
115
116     // deprecated
117     fcgi_istream(FCGX_Stream * fcgx = 0);
118     
119     // deprecated
120     ~fcgi_istream(void) {}
121
122     // deprecated
123     virtual void attach(FCGX_Stream * fcgx);
124
125 private:
126
127     fcgi_streambuf fcgi_strmbuf;
128 };
129
130 /*
131  *  fcgi_ostream - deprecated
132  */
133 class DLLAPI fcgi_ostream : public std::ostream
134 {
135 public:
136     
137     // deprecated
138     fcgi_ostream(FCGX_Stream * fcgx = 0);
139     
140     // deprecated
141     ~fcgi_ostream(void) {}
142
143     // deprecated
144     virtual void attach(FCGX_Stream *fcgx);
145
146 private:
147
148     fcgi_streambuf fcgi_strmbuf;
149 };
150
151 #endif /* FCGIO_H */