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

bzstream.h

00001 // ============================================================================ 00002 // bzstream, C++ iostream classes wrapping the bz2lib compression library. 00003 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner 00004 // Copyright (C) 2003 stephan beal <stephan@s11n.net> 00005 // 00006 // This code is basically a copy/paste of gzstream, adapted for libbz2 00007 // by stephan@s11n.net (2 Oct 2003) 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // ============================================================================ 00023 // 00024 // File : bzstream.h 00025 // Revision : $Revision: 1.1.1.1 $ 00026 // Revision_date : $Date: 2004/03/18 00:25:55 $ 00027 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner 00028 // 00029 // Standard streambuf implementation following Nicolai Josuttis, "The 00030 // Standard C++ Library". 00031 // ============================================================================ 00032 00033 // Changes by stephan@s11n.net: 00034 // October, 2003: 00035 // - added zlevel() to gzstreambase and gzstreambuf. Only works for ostreams. 00036 // - replaced gzip code with bzip code. 00037 00038 #ifndef s11n_BZSTREAM_H_INCLUDED 00039 #define s11n_BZSTREAM_H_INCLUDED 1 00040 00041 00042 // standard C++ with new header file names and std:: namespace 00043 #include <iostream> 00044 #include <fstream> 00045 00046 #include <bzlib.h> // libbz2 header 00047 00048 00049 #ifndef ZSTREAM_NAMESPACE 00050 #define ZSTREAM_NAMESPACE s11n 00051 #endif 00052 00053 #ifdef ZSTREAM_NAMESPACE 00054 namespace ZSTREAM_NAMESPACE 00055 { 00056 #endif 00057 00058 // ---------------------------------------------------------------------------- 00059 // Internal classes to implement bzstream. See below for user classes. 00060 // ---------------------------------------------------------------------------- 00061 class bzstreambuf:public std::streambuf 00062 { 00063 private: 00064 static const int bufferSize = 47 + 256; // size of data buff 00065 // totals 512 bytes under g++ for ibzstream at the end. 00066 00067 BZFILE * file; // file handle for compressed file 00068 char buffer[bufferSize]; // data buffer 00069 char opened; // open/close state of stream 00070 int mode; // I/O mode 00071 00072 int flush_buffer( ); 00073 public: 00074 bzstreambuf( ):opened( 0 ), m_zlevel(-1) 00075 { 00076 setp( buffer, buffer + ( bufferSize - 1 ) ); 00077 setg( buffer + 4, // beginning of putback area 00078 buffer + 4, // read position 00079 buffer + 4 ); // end position 00080 // ASSERT: both input & output capabilities will not be used together 00081 } 00082 int is_open( ) 00083 { 00084 return opened; 00085 } 00086 bzstreambuf *open( const char *name, int open_mode ); 00087 bzstreambuf *close( ); 00088 ~bzstreambuf( ) 00089 { 00090 close( ); 00091 } 00092 00093 virtual int overflow( int c = EOF ); 00094 virtual int underflow( ); 00095 virtual int sync( ); 00096 00097 /** 00098 Defines the compression level, 0 - 9. A value of -1 00099 means use the system-wide zlib default (normally 3). 00100 */ 00101 void zlevel( int z ) { this->m_zlevel = ( z<0 ? -1 : (z>9?9:z) ) ; } 00102 /** 00103 Returns the compression level for this stream. 00104 */ 00105 int zlevel() const { return this->m_zlevel; } 00106 private: 00107 int m_zlevel; 00108 }; 00109 00110 /** 00111 Base stream class inherited by obzstream/igzstream. Any 00112 given instance may not be used for both input and output. 00113 00114 Uses bzip2 compression, and is compatible with files compressed 00115 using bzip2, or any number of other tools which support bz2lib. 00116 00117 */ 00118 class bzstreambase:virtual public std::ios 00119 { 00120 private: 00121 int m_zlevel; 00122 protected: 00123 bzstreambuf buf; 00124 public: 00125 bzstreambase( ) 00126 { 00127 init( &buf ); 00128 } 00129 bzstreambase( const char *name, int open_mode ); 00130 ~bzstreambase( ); 00131 void open( const char *name, int open_mode ); 00132 void close( ); 00133 00134 void zlevel( int z ) { buf.zlevel(z); } 00135 int zlevel() const { return buf.zlevel(); } 00136 00137 bzstreambuf *rdbuf( ) 00138 { 00139 return &buf; 00140 } 00141 }; 00142 00143 /** 00144 An input stream which decompresses it's input. Used 00145 identically to a std::ifstream. 00146 00147 <pre> 00148 igzstream ifs( "myfile.bz2" ); 00149 </pre> 00150 */ 00151 class ibzstream:public bzstreambase, public std::istream 00152 { 00153 public: 00154 ibzstream( ):std::istream( &buf ) 00155 { 00156 } 00157 ibzstream( const char *name, int open_mode = std::ios::in ):bzstreambase( name, open_mode ), std::istream( &buf ) 00158 { 00159 } 00160 bzstreambuf *rdbuf( ) 00161 { 00162 return bzstreambase::rdbuf( ); 00163 } 00164 void open( const char *name, int open_mode = std::ios::in ) 00165 { 00166 bzstreambase::open( name, open_mode ); 00167 } 00168 }; 00169 00170 /** 00171 An output stream which compresses it's output. Used identically to a std::ofstream: 00172 00173 <pre> 00174 ogzstream of( "myfile.bz2" ); 00175 of << "hello, world!" << std::endl; 00176 </pre> 00177 */ 00178 00179 class obzstream:public bzstreambase, public std::ostream 00180 { 00181 public: 00182 obzstream( ):std::ostream( &buf ) 00183 { 00184 } 00185 obzstream( const char *name, int mode = std::ios::out ):bzstreambase( name, mode ), std::ostream( &buf ) 00186 { 00187 } 00188 bzstreambuf *rdbuf( ) 00189 { 00190 return bzstreambase::rdbuf( ); 00191 } 00192 void open( const char *name, int open_mode = std::ios::out ) 00193 { 00194 bzstreambase::open( name, open_mode ); 00195 } 00196 }; 00197 00198 #ifdef ZSTREAM_NAMESPACE 00199 } // namespace ZSTREAM_NAMESPACE 00200 #endif 00201 00202 #endif // s11n_BZSTREAM_H_INCLUDED 00203 // ============================================================================ 00204 // EOF //

Generated on Wed Jul 28 16:04:14 2004 for s11n by doxygen 1.3.7