iipsrv  0.9.9
Writer.h
00001 /*
00002     IIP Generic Output Writer Classes
00003 
00004     Copyright (C) 2006-2010 Ruven Pillay.
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #ifndef _WRITER_H
00023 #define _WRITER_H
00024 
00025 
00026 #include <fcgiapp.h>
00027 #include <cstdio>
00028 
00029 
00031 class Writer {
00032 
00033  public:
00034 
00035   virtual ~Writer() = 0;
00036 
00038 
00041   virtual int putStr( const char* msg, int len ) = 0;
00042 
00044 
00045   virtual int putS( const char* msg ) = 0;
00046 
00048 
00049   virtual int printf( const char* msg ) = 0;
00050 
00052   virtual int flush() = 0;
00053 
00054 };
00055 
00056 
00057 
00059 class FCGIWriter {
00060 
00061  private:
00062 
00063   
00064   FCGX_Stream *out;
00065   static const unsigned int bufsize = 65536;
00066 
00068   void cpy2buf( const char* msg, size_t len ){
00069     if( sz+len > bufsize ) buffer = (char*) realloc( buffer, sz+len );
00070     memcpy( &buffer[sz], msg, len );
00071     sz += len;
00072   };
00073 
00074 
00075  public:
00076 
00077   char* buffer;
00078   size_t sz;
00079 
00081   FCGIWriter( FCGX_Stream* o ){
00082     out = o;
00083     buffer = (char*) malloc(bufsize);
00084     sz = 0;
00085   };
00086 
00088   ~FCGIWriter(){ if(buffer) free(buffer); };
00089 
00090   int putStr( const char* msg, int len ){
00091     cpy2buf( msg, len );
00092     return FCGX_PutStr( msg, len, out );
00093   };
00094   int putS( const char* msg ){
00095     cpy2buf( msg, strlen(msg) );
00096     return FCGX_PutS( msg, out );
00097   }
00098   int printf( const char* msg ){
00099     cpy2buf( msg, strlen(msg) );
00100     return FCGX_FPrintF( out, msg );
00101   };
00102   int flush(){
00103     return FCGX_FFlush( out );
00104   };
00105 
00106 };
00107 
00108 
00109 
00111 class FileWriter {
00112 
00113  private:
00114 
00115   FILE* out;
00116 
00117  public:
00118 
00119   FileWriter( FILE* o ){ out = o; };
00120 
00121   int putStr( const char* msg, int len ){
00122     return fwrite( (void*) msg, sizeof(char), len, out );
00123   };
00124   int putS( const char* msg ){
00125     return fputs( msg, out );
00126   }
00127   int printf( const char* msg ){
00128     return fprintf( out, "%s", msg );
00129   };
00130   int flush(){
00131     return fflush( out );
00132   };
00133 
00134 };
00135   
00136 
00137 
00138 #endif