iipsrv  0.9.9
Writer.h
1 /*
2  IIP Generic Output Writer Classes
3 
4  Copyright (C) 2006-2010 Ruven Pillay.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 
21 
22 #ifndef _WRITER_H
23 #define _WRITER_H
24 
25 
26 #include <fcgiapp.h>
27 #include <cstdio>
28 
29 
31 class Writer {
32 
33  public:
34 
35  virtual ~Writer() = 0;
36 
38 
41  virtual int putStr( const char* msg, int len ) = 0;
42 
44 
45  virtual int putS( const char* msg ) = 0;
46 
48 
49  virtual int printf( const char* msg ) = 0;
50 
52  virtual int flush() = 0;
53 
54 };
55 
56 
57 
59 class FCGIWriter {
60 
61  private:
62 
63 
64  FCGX_Stream *out;
65  static const unsigned int bufsize = 65536;
66 
68  void cpy2buf( const char* msg, size_t len ){
69  if( sz+len > bufsize ) buffer = (char*) realloc( buffer, sz+len );
70  memcpy( &buffer[sz], msg, len );
71  sz += len;
72  };
73 
74 
75  public:
76 
77  char* buffer;
78  size_t sz;
79 
81  FCGIWriter( FCGX_Stream* o ){
82  out = o;
83  buffer = (char*) malloc(bufsize);
84  sz = 0;
85  };
86 
88  ~FCGIWriter(){ if(buffer) free(buffer); };
89 
90  int putStr( const char* msg, int len ){
91  cpy2buf( msg, len );
92  return FCGX_PutStr( msg, len, out );
93  };
94  int putS( const char* msg ){
95  cpy2buf( msg, strlen(msg) );
96  return FCGX_PutS( msg, out );
97  }
98  int printf( const char* msg ){
99  cpy2buf( msg, strlen(msg) );
100  return FCGX_FPrintF( out, msg );
101  };
102  int flush(){
103  return FCGX_FFlush( out );
104  };
105 
106 };
107 
108 
109 
111 class FileWriter {
112 
113  private:
114 
115  FILE* out;
116 
117  public:
118 
119  FileWriter( FILE* o ){ out = o; };
120 
121  int putStr( const char* msg, int len ){
122  return fwrite( (void*) msg, sizeof(char), len, out );
123  };
124  int putS( const char* msg ){
125  return fputs( msg, out );
126  }
127  int printf( const char* msg ){
128  return fprintf( out, "%s", msg );
129  };
130  int flush(){
131  return fflush( out );
132  };
133 
134 };
135 
136 
137 
138 #endif
Virtual base class for various writers.
Definition: Writer.h:31
virtual int printf(const char *msg)=0
Write out a string.
virtual int putS(const char *msg)=0
Write out a string.
virtual int flush()=0
Flush the output buffer.
File Writer Class.
Definition: Writer.h:111
~FCGIWriter()
Destructor.
Definition: Writer.h:88
virtual int putStr(const char *msg, int len)=0
Write out a binary string.
FCGI Writer Class.
Definition: Writer.h:59
FCGIWriter(FCGX_Stream *o)
Constructor.
Definition: Writer.h:81