gzstream.hh

00001 // ============================================================================
00002 // gzstream, C++ iostream classes wrapping the zlib compression library.
00003 // Copyright (C) 2001  Deepak Bandyopadhyay, Lutz Kettner
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 // ============================================================================
00019 //
00020 // File          : gzstream.hh
00021 // Revision      : $Revision: 1.5 $
00022 // Revision_date : $Date: 2002/04/26 23:30:15 $
00023 // Author(s)     : Deepak Bandyopadhyay, Lutz Kettner
00024 // 
00025 // Standard streambuf implementation following Nicolai Josuttis, "The 
00026 // Standard C++ Library".
00027 // ============================================================================
00028 
00029 #ifndef _GZSTREAM_HH_
00030 #define _GZSTREAM_HH_ 1
00031 
00032 // standard C++ with new header file names and std:: namespace
00033 #include <iostream>
00034 #include <fstream>
00035 #include <zlib.h>
00036 
00037 #ifdef GZSTREAM_NAMESPACE
00038 namespace GZSTREAM_NAMESPACE {
00039 #endif
00040   
00041   // ----------------------------------------------------------------------------
00042   // Internal classes to implement gzstream. See below for user classes.
00043   // ----------------------------------------------------------------------------
00044   
00045 
00046   //__________________________________________________________________
00047   class gzstreambuf : public std::streambuf {
00048 
00049   private:
00050     static const int bufferSize = 47+256;    // size of data buff
00051     // totals 512 bytes under g++ for igzstream at the end.
00052     
00053     gzFile           file;               // file handle for compressed file
00054     char             buffer[bufferSize]; // data buffer
00055     char             opened;             // open/close state of stream
00056     int              mode;               // I/O mode
00057     
00058     int flush_buffer();
00059 
00060   public:
00061     gzstreambuf() : opened(0) {
00062       setp( buffer, buffer + (bufferSize-1));
00063       setg( buffer + 4,     // beginning of putback area
00064             buffer + 4,     // read position
00065             buffer + 4);    // end position      
00066       // ASSERT: both input & output capabilities will not be used together
00067     }
00068 
00069     int is_open() { return opened; }
00070 
00071     gzstreambuf* open( const char* name, int open_mode);
00072 
00073     gzstreambuf* close();
00074 
00075     ~gzstreambuf() { close(); }
00076     
00077     virtual int     overflow( int c = EOF);
00078     virtual int     underflow();
00079     virtual int     sync();
00080   };
00081   
00082   //____________________________________________________________________
00083   class gzstreambase : virtual public std::ios {
00084 
00085   protected:
00086     gzstreambuf buf;
00087 
00088   public:
00089     gzstreambase() { init(&buf); }
00090 
00091     gzstreambase( const char* name, int open_mode);
00092 
00093     ~gzstreambase();
00094 
00095     void open( const char* name, int open_mode);
00096 
00097     void close();
00098 
00099     gzstreambuf* rdbuf() { return &buf; }
00100   };
00101 
00102   
00103   // ----------------------------------------------------------------------------
00104   // User classes. Use igzstream and ogzstream analogously to ifstream and
00105   // ofstream respectively. They read and write files based on the gz* 
00106   // function interface of the zlib. Files are compatible with gzip compression.
00107   // ----------------------------------------------------------------------------
00108   
00109 
00110   //____________________________________________________________________________
00111   class igzstream : public gzstreambase, public std::istream {
00112 
00113   public:
00114     igzstream() : std::istream( &buf) {}
00115  
00116     igzstream( const char* name, int open_mode = std::ios::in)
00117       : gzstreambase( name, open_mode), std::istream( &buf) {}  
00118 
00119     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00120 
00121     void open( const char* name, int open_mode = std::ios::in) {
00122       gzstreambase::open( name, open_mode);
00123     }
00124     
00125   };
00126 
00127 
00128   //____________________________________________________________________________  
00129   class ogzstream : public gzstreambase, public std::ostream {
00130 
00131   public:
00132     ogzstream() : std::ostream( &buf) {}
00133 
00134     ogzstream( const char* name, int mode = std::ios::out)
00135       : gzstreambase( name, mode), std::ostream( &buf) {}  
00136 
00137     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00138 
00139     void open( const char* name, int open_mode = std::ios::out) {
00140       gzstreambase::open( name, open_mode);
00141     }
00142   };
00143   
00144 #ifdef GZSTREAM_NAMESPACE
00145 } // namespace GZSTREAM_NAMESPACE
00146 #endif
00147 
00148 #endif // _GZSTREAM_HH_
00149 // ============================================================================
00150 // EOF //

Generated on Tue Nov 16 10:49:55 2010 for CUORE Software by  doxygen 1.5.6