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.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_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 <istream>
00036 #include <ios>
00037 #include <zlib.h>
00038 
00039 #ifdef _MSC_VER
00040 using std::ostream;
00041 using std::istream;
00042 #endif
00043 
00044 
00045 namespace Base {
00046 
00047 
00048 #define BUFFERSIZE 47+256
00049 //const int bufferSize = 47+256;    // size of data buff
00050 
00051 
00052 // ----------------------------------------------------------------------------
00053 // Internal classes to implement gzstream. See below for user classes.
00054 // ----------------------------------------------------------------------------
00055 
00056 class BaseExport gzstreambuf : public std::streambuf {
00057 private:
00058     static const int bufferSize;    // size of data buff
00059     // totals 512 bytes under g++ for igzstream at the end.
00060 
00061     gzFile           file;               // file handle for compressed file
00062     char             buffer[BUFFERSIZE]; // data buffer
00063     char             opened;             // open/close state of stream
00064     int              mode;               // I/O mode
00065 
00066     int flush_buffer();
00067 public:
00068     gzstreambuf() : opened(0) {
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() { return opened; }
00076     gzstreambuf* open( const char* name, int open_mode, int comp);
00077     gzstreambuf* close();
00078     ~gzstreambuf() { close(); }
00079     
00080     virtual int     overflow( int c = EOF);
00081     virtual int     underflow();
00082     virtual int     sync();
00083 };
00084 
00085 class BaseExport gzstreambase : virtual public std::ios {
00086 protected:
00087     gzstreambuf buf;
00088 public:
00089     gzstreambase() { init(&buf); }
00090     gzstreambase( const char* name, int open_mode, int comp);
00091     ~gzstreambase();
00092     void open( const char* name, int open_mode, int comp);
00093     void close();
00094     gzstreambuf* rdbuf() { return &buf; }
00095 };
00096 
00097 // ----------------------------------------------------------------------------
00098 // User classes. Use igzstream and ogzstream analogously to ifstream and
00099 // ofstream respectively. They read and write files based on the gz* 
00100 // function interface of the zlib. Files are compatible with gzip compression.
00101 // ----------------------------------------------------------------------------
00102 
00103 class BaseExport igzstream : public gzstreambase, public std::istream {
00104 public:
00105     igzstream()
00106 #ifdef _MSC_VER
00107       : istream( &buf) {} 
00108 #else
00109       : std::istream( &buf) {} 
00110 #endif
00111     igzstream( const char* name, int open_mode = std::ios_base::in, int comp = 1)
00112 #ifdef _MSC_VER
00113       : gzstreambase( name, open_mode, comp ), istream( &buf) {}  
00114 #else
00115       : gzstreambase( name, open_mode, comp), std::istream( &buf) {}  
00116 #endif
00117     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00118     void open( const char* name, int open_mode = std::ios_base::in, int comp = 1) {
00119         gzstreambase::open( name, open_mode, comp);
00120     }
00121 };
00122 
00123 class BaseExport ogzstream : public gzstreambase, public std::ostream {
00124 public:
00125     ogzstream()
00126 #ifdef _MSC_VER
00127       : ostream( &buf) {}
00128 #else
00129       : std::ostream( &buf) {}
00130 #endif
00131     ogzstream( const char* name, int mode = std::ios_base::out, int comp = 1)
00132 #ifdef _MSC_VER
00133       : gzstreambase( name, mode, comp), ostream( &buf) {}  
00134 #else
00135       : gzstreambase( name, mode, comp), std::ostream( &buf) {}  
00136 #endif
00137     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
00138     void open( const char* name, int open_mode = std::ios_base::out, int comp = 1) {
00139         gzstreambase::open( name, open_mode, comp);
00140     }
00141 };
00142 
00143 } // namespace BAse
00144 
00145 #endif // GZSTREAM_H
00146 // ============================================================================
00147 // EOF //
00148 

Generated on Wed Nov 23 19:00:17 2011 for FreeCAD by  doxygen 1.6.1