gzstream.h

Go to the documentation of this file.
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.h
00021 // Revision      : $Revision: 1.1 $
00022 // Revision_date : $Date: 2007/07/31 20:09:14 $
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_H
00030 #define GZSTREAM_H 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 class gzstreambuf : public std::streambuf {
00046 private:
00047     static const int bufferSize = 47+256;    // size of data buff
00048     // totals 512 bytes under g++ for igzstream at the end.
00049 
00050     gzFile           file;               // file handle for compressed file
00051     char             buffer[bufferSize]; // data buffer
00052     char             opened;             // open/close state of stream
00053     int              mode;               // I/O mode
00054 
00055     int flush_buffer();
00056 public:
00057     gzstreambuf() : opened(0) {
00058         setp( buffer, buffer + (bufferSize-1));
00059         setg( buffer + 4,     // beginning of putback area
00060               buffer + 4,     // read position
00061               buffer + 4);    // end position      
00062         // ASSERT: both input & output capabilities will not be used together
00063     }
00064     int is_open() { return opened; }
00065     gzstreambuf* open( const char* name, int open_mode);
00066     gzstreambuf* close();
00067     ~gzstreambuf() { close(); }
00068     
00069     virtual int     overflow( int c = EOF);
00070     virtual int     underflow();
00071     virtual int     sync();
00072 };
00073 
00074 class gzstreambase : virtual public std::ios {
00075 protected:
00076     gzstreambuf buf;
00077 public:
00078     gzstreambase() { init(&buf); }
00079     gzstreambase( const char* name, int open_mode);
00080     ~gzstreambase();
00081     void open( const char* name, int open_mode);
00082     void close();
00083     gzstreambuf* rdbuf() { return &buf; }
00084 };
00085 
00086 // ----------------------------------------------------------------------------
00087 // User classes. Use igzstream and ogzstream analogously to ifstream and
00088 // ofstream respectively. They read and write files based on the gz* 
00089 // function interface of the zlib. Files are compatible with gzip compression.
00090 // ----------------------------------------------------------------------------
00091 
00092 class igzstream : public gzstreambase, public std::istream {
00093 public:
00094     igzstream() : std::istream( &buf) {} 
00095     igzstream( const char* name, int open_mode = std::ios::in)
00096         : gzstreambase( name, open_mode), std::istream( &buf) {}  
00097     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00098     void open( const char* name, int open_mode = std::ios::in) {
00099         gzstreambase::open( name, open_mode);
00100     }
00101 };
00102 
00103 class ogzstream : public gzstreambase, public std::ostream {
00104 public:
00105     ogzstream() : std::ostream( &buf) {}
00106     ogzstream( const char* name, int mode = std::ios::out)
00107         : gzstreambase( name, mode), std::ostream( &buf) {}  
00108     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00109     void open( const char* name, int open_mode = std::ios::out) {
00110         gzstreambase::open( name, open_mode);
00111     }
00112 };
00113 
00114 #ifdef GZSTREAM_NAMESPACE
00115 } // namespace GZSTREAM_NAMESPACE
00116 #endif
00117 
00118 #endif // GZSTREAM_H
00119 // ============================================================================
00120 // EOF //
00121 

Generated on Wed Dec 15 10:46:15 2010 for Hazy_System by  doxygen 1.4.7