Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:09:52

0001 #ifndef ATOOLS_Org_Gzip_Stream_H
0002 #define ATOOLS_Org_Gzip_Stream_H
0003 // adapted for Sherpa by Frank Siegert from: ==================================
0004 // gzstream, C++ iostream classes wrapping the zlib compression library.
0005 // Copyright (C) 2001  Deepak Bandyopadhyay, Lutz Kettner
0006 // (http://www.cs.unc.edu/Research/compgeom/gzstream)
0007 // ============================================================================
0008 
0009 
0010 #include <iostream>
0011 #include <fstream>
0012 #include <zlib.h>
0013 
0014 namespace ATOOLS {
0015   
0016 // ----------------------------------------------------------------------------
0017 // Internal classes to implement gzstream. See below for user classes.
0018 // ----------------------------------------------------------------------------
0019 
0020 class gzstreambuf : public std::streambuf {
0021 private:
0022     static const int bufferSize = 47+256;    // size of data buff
0023     // totals 512 bytes under g++ for igzstream at the end.
0024 
0025     gzFile           file;               // file handle for compressed file
0026     char             buffer[bufferSize]; // data buffer
0027     char             opened;             // open/close state of stream
0028     int              mode;               // I/O mode
0029 
0030     int flush_buffer();
0031 public:
0032     gzstreambuf() : opened(0) {
0033         setp( buffer, buffer + (bufferSize-1));
0034         setg( buffer + 4,     // beginning of putback area
0035               buffer + 4,     // read position
0036               buffer + 4);    // end position      
0037         // ASSERT: both input & output capabilities will not be used together
0038     }
0039     int is_open() { return opened; }
0040     gzstreambuf* open( const char* name, int open_mode);
0041     gzstreambuf* close();
0042     ~gzstreambuf() { close(); }
0043     
0044     virtual int     overflow( int c = EOF);
0045     virtual int     underflow();
0046     virtual int     sync();
0047 };
0048 
0049 class gzstreambase : virtual public std::ios {
0050 protected:
0051     gzstreambuf buf;
0052 public:
0053     gzstreambase() { init(&buf); }
0054     gzstreambase( const char* name, int open_mode);
0055     ~gzstreambase();
0056     void open( const char* name, int open_mode);
0057     void close();
0058     gzstreambuf* rdbuf() { return &buf; }
0059 };
0060 
0061 // ----------------------------------------------------------------------------
0062 // User classes. Use igzstream and ogzstream analogously to ifstream and
0063 // ofstream respectively. They read and write files based on the gz* 
0064 // function interface of the zlib. Files are compatible with gzip compression.
0065 // ----------------------------------------------------------------------------
0066 
0067 class igzstream : public gzstreambase, public std::istream {
0068 public:
0069     igzstream() : std::istream( &buf) {} 
0070     igzstream( const char* name, int open_mode = std::ios::in)
0071         : gzstreambase( name, open_mode), std::istream( &buf) {}  
0072     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
0073     void open( const char* name, int open_mode = std::ios::in) {
0074         gzstreambase::open( name, open_mode);
0075     }
0076 };
0077 
0078 class ogzstream : public gzstreambase, public std::ostream {
0079 public:
0080     ogzstream() : std::ostream( &buf) {}
0081     ogzstream( const char* name, int mode = std::ios::out)
0082         : gzstreambase( name, mode), std::ostream( &buf) {}  
0083     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
0084     void open( const char* name, int open_mode = std::ios::out) {
0085         gzstreambase::open( name, open_mode);
0086     }
0087 };
0088 
0089 } // namespace ATOOLS
0090 
0091 #endif
0092