iipsrv 1.2
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-2022 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// Windows vcpkg requires prefix for include
26#ifdef WIN32
27#include <fastcgi/fcgiapp.h>
28#else
29#include <fcgiapp.h>
30#endif
31
32#include <cstdio>
33#include <cstring>
34
35
37class Writer {
38
39 public:
40
41 virtual ~Writer() = 0;
42
44
47 virtual int putStr( const char* msg, int len ) = 0;
48
50
51 virtual int putS( const char* msg ) = 0;
52
54
55 virtual int printf( const char* msg ) = 0;
56
58 virtual int flush() = 0;
59
60};
61
62
63
66
67 private:
68
69
70 FCGX_Stream *out;
71 static const unsigned int bufsize = 65536;
72
74 void cpy2buf( const char* msg, size_t len ){
75 if( sz+len > bufsize ) buffer = (char*) realloc( buffer, sz+len );
76 if( buffer ){
77 memcpy( &buffer[sz], msg, len );
78 sz += len;
79 }
80 };
81
82
83 public:
84
85 char* buffer;
86 size_t sz;
87
89 FCGIWriter( FCGX_Stream* o ){
90 out = o;
91 buffer = (char*) malloc(bufsize);
92 sz = 0;
93 };
94
96 ~FCGIWriter(){ if(buffer) free(buffer); };
97
98 int putStr( const char* msg, int len ){
99 cpy2buf( msg, len );
100 return FCGX_PutStr( msg, len, out );
101 };
102 int putS( const char* msg ){
103 int len = (int) strlen( msg );
104 cpy2buf( msg, len );
105 if( FCGX_PutStr( msg, len, out ) != len ) return -1;
106 return len;
107 }
108 int printf( const char* msg ){
109 cpy2buf( msg, strlen(msg) );
110 return FCGX_FPrintF( out, msg );
111 };
112 int flush(){
113 return FCGX_FFlush( out );
114 };
115
116};
117
118
119
122
123 private:
124
125 FILE* out;
126
127 public:
128
129 FileWriter( FILE* o ){ out = o; };
130
131 int putStr( const char* msg, int len ){
132 return fwrite( (void*) msg, sizeof(char), len, out );
133 };
134 int putS( const char* msg ){
135 return fputs( msg, out );
136 }
137 int printf( const char* msg ){
138 return fprintf( out, "%s", msg );
139 };
140 int flush(){
141 return fflush( out );
142 };
143
144};
145
146
147
148#endif
FCGI Writer Class.
Definition: Writer.h:65
~FCGIWriter()
Destructor.
Definition: Writer.h:96
FCGIWriter(FCGX_Stream *o)
Constructor.
Definition: Writer.h:89
File Writer Class.
Definition: Writer.h:121
Virtual base class for various writers.
Definition: Writer.h:37
virtual int flush()=0
Flush the output buffer.
virtual int putStr(const char *msg, int len)=0
Write out a binary string.
virtual int putS(const char *msg)=0
Write out a string.
virtual int printf(const char *msg)=0
Write out a string.