iipsrv  1.1
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
Writer.h
1 /*
2  IIP Generic Output Writer Classes
3 
4  Copyright (C) 2006-2013 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 3 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 Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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  if( buffer ){
71  memcpy( &buffer[sz], msg, len );
72  sz += len;
73  }
74  };
75 
76 
77  public:
78 
79  char* buffer;
80  size_t sz;
81 
83  FCGIWriter( FCGX_Stream* o ){
84  out = o;
85  buffer = (char*) malloc(bufsize);
86  sz = 0;
87  };
88 
90  ~FCGIWriter(){ if(buffer) free(buffer); };
91 
92  int putStr( const char* msg, int len ){
93  cpy2buf( msg, len );
94  return FCGX_PutStr( msg, len, out );
95  };
96  int putS( const char* msg ){
97  cpy2buf( msg, strlen(msg) );
98  return FCGX_PutS( msg, out );
99  }
100  int printf( const char* msg ){
101  cpy2buf( msg, strlen(msg) );
102  return FCGX_FPrintF( out, msg );
103  };
104  int flush(){
105  return FCGX_FFlush( out );
106  };
107 
108 };
109 
110 
111 
113 class FileWriter {
114 
115  private:
116 
117  FILE* out;
118 
119  public:
120 
121  FileWriter( FILE* o ){ out = o; };
122 
123  int putStr( const char* msg, int len ){
124  return fwrite( (void*) msg, sizeof(char), len, out );
125  };
126  int putS( const char* msg ){
127  return fputs( msg, out );
128  }
129  int printf( const char* msg ){
130  return fprintf( out, "%s", msg );
131  };
132  int flush(){
133  return fflush( out );
134  };
135 
136 };
137 
138 
139 
140 #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:113
~FCGIWriter()
Destructor.
Definition: Writer.h:90
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:83