Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

gzstream.h

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.1.1 $ 00022 // Revision_date : $Date: 2004/03/18 00:25:54 $ 00023 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner 00024 // 00025 // Standard streambuf implementation following Nicolai Josuttis, "The 00026 // Standard C++ Library". 00027 // ============================================================================ 00028 00029 // Changes by stephan@s11n.net: 00030 // October, 2003: 00031 // - added zlevel() to gzstreambase and gzstreambuf. Only works for ostreams. 00032 00033 #ifndef s11n_GZSTREAM_H_INCLUDED 00034 #define s11n_GZSTREAM_H_INCLUDED 1 00035 00036 00037 // standard C++ with new header file names and std:: namespace 00038 #include <iostream> 00039 #include <fstream> 00040 #include <zlib.h> 00041 00042 #ifndef ZSTREAM_NAMESPACE 00043 #define ZSTREAM_NAMESPACE s11n 00044 #endif 00045 00046 #ifdef ZSTREAM_NAMESPACE 00047 namespace ZSTREAM_NAMESPACE 00048 { 00049 #endif 00050 00051 // ---------------------------------------------------------------------------- 00052 // Internal classes to implement gzstream. See below for user classes. 00053 // ---------------------------------------------------------------------------- 00054 class gzstreambuf:public std::streambuf 00055 { 00056 private: 00057 static const int bufferSize = 47 + 256; // size of data buff 00058 // totals 512 bytes under g++ for igzstream at the end. 00059 00060 gzFile file; // file handle for compressed file 00061 char buffer[bufferSize]; // data buffer 00062 char opened; // open/close state of stream 00063 int mode; // I/O mode 00064 00065 int flush_buffer(); 00066 public: 00067 gzstreambuf():opened( 0 ), m_zlevel(-1) 00068 { 00069 setp( buffer, buffer + ( bufferSize - 1 ) ); 00070 setg( buffer + 4, // beginning of putback area 00071 buffer + 4, // read position 00072 buffer + 4 ); // end position 00073 // ASSERT: both input & output capabilities will not be used together 00074 } 00075 int is_open() 00076 { 00077 return opened; 00078 } 00079 gzstreambuf *open( const char *name, int open_mode ); 00080 gzstreambuf *close(); 00081 ~gzstreambuf() 00082 { 00083 close(); 00084 } 00085 00086 virtual int overflow( int c = EOF ); 00087 virtual int underflow(); 00088 virtual int sync(); 00089 00090 void zlevel( int z ) { this->m_zlevel = ( z<0 ? -1 : (z>9?9:z) ) ; } 00091 int zlevel() const { return this->m_zlevel; } 00092 private: 00093 int m_zlevel; 00094 }; 00095 00096 /** 00097 Base stream class inherited by ogzstream/igzstream. Any 00098 given instance may not be used for both input and output. 00099 00100 Uses gzlib compression, and is compatible with files compressed 00101 using gzip, or any number of other tools which support gzlib. 00102 00103 */ 00104 class gzstreambase:virtual public std::ios 00105 { 00106 private: 00107 int m_zlevel; 00108 protected: 00109 gzstreambuf buf; 00110 public: 00111 gzstreambase() 00112 { 00113 init( &buf ); 00114 } 00115 gzstreambase( const char *name, int open_mode ); 00116 ~gzstreambase(); 00117 void open( const char *name, int open_mode ); 00118 void close(); 00119 00120 void zlevel( int z ) { buf.zlevel(z); } 00121 int zlevel() const { return buf.zlevel(); } 00122 00123 gzstreambuf *rdbuf() 00124 { 00125 return &buf; 00126 } 00127 }; 00128 00129 00130 /** 00131 An input stream which decompresses it's input. Used 00132 identically to a std::ifstream. 00133 00134 <pre> 00135 igzstream ifs( "myfile.gz" ); 00136 </pre> 00137 */ 00138 class igzstream:public gzstreambase, public std::istream 00139 { 00140 public: 00141 igzstream():std::istream( &buf ) 00142 { 00143 } 00144 igzstream( const char *name, int open_mode = std::ios::in ):gzstreambase( name, open_mode ), std::istream( &buf ) 00145 { 00146 } 00147 gzstreambuf *rdbuf() 00148 { 00149 return gzstreambase::rdbuf(); 00150 } 00151 void open( const char *name, int open_mode = std::ios::in ) 00152 { 00153 gzstreambase::open( name, open_mode ); 00154 } 00155 }; 00156 00157 /** 00158 An output stream which compresses it's output. Used identically to a std::ofstream: 00159 00160 <pre> 00161 ogzstream of( "myfile.gz" ); 00162 of << "hello, world!" << std::endl; 00163 </pre> 00164 */ 00165 class ogzstream:public gzstreambase, public std::ostream 00166 { 00167 public: 00168 ogzstream():std::ostream( &buf ) 00169 { 00170 } 00171 ogzstream( const char *name, int mode = std::ios::out ):gzstreambase( name, mode ), std::ostream( &buf ) 00172 { 00173 } 00174 gzstreambuf *rdbuf() 00175 { 00176 return gzstreambase::rdbuf(); 00177 } 00178 void open( const char *name, int open_mode = std::ios::out ) 00179 { 00180 gzstreambase::open( name, open_mode ); 00181 } 00182 }; 00183 00184 #ifdef ZSTREAM_NAMESPACE 00185 } // namespace ZSTREAM_NAMESPACE 00186 #endif 00187 00188 #endif // s11n_GZSTREAM_H_INCLUDED 00189 // ============================================================================ 00190 // EOF //

Generated on Tue Jul 20 10:46:48 2004 for s11n by doxygen 1.3.7