ARTS  2.2.66
gzstream.h
Go to the documentation of this file.
1 // ============================================================================
2 // gzstream, C++ iostream classes wrapping the zlib compression library.
3 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ============================================================================
19 //
20 // File : gzstream.h
21 // Revision : $Revision: 1.5 $
22 // Revision_date : $Date: 2002/04/26 23:30:15 $
23 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
24 //
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
28 
29 #ifndef GZSTREAM_H
30 #define GZSTREAM_H 1
31 
32 // standard C++ with new header file names and std:: namespace
33 #include <iostream>
34 #include <fstream>
35 #include <zlib.h>
36 
37 #ifdef GZSTREAM_NAMESPACE
38 namespace GZSTREAM_NAMESPACE {
39 #endif
40 
41 // ----------------------------------------------------------------------------
42 // Internal classes to implement gzstream. See below for user classes.
43 // ----------------------------------------------------------------------------
44 
45 class gzstreambuf : public std::streambuf {
46 private:
47  static const int bufferSize = 47+256; // size of data buff
48  // totals 512 bytes under g++ for igzstream at the end.
49 
50  gzFile file; // file handle for compressed file
51  char buffer[bufferSize]; // data buffer
52  char opened; // open/close state of stream
53  int mode; // I/O mode
54 
55  int flush_buffer();
56 public:
57  gzstreambuf() : opened(0) {
58  setp( buffer, buffer + (bufferSize-1));
59  setg( buffer + 4, // beginning of putback area
60  buffer + 4, // read position
61  buffer + 4); // end position
62  // ASSERT: both input & output capabilities will not be used together
63  }
64  int is_open() { return opened; }
65  gzstreambuf* open( const char* name, int open_mode);
66  gzstreambuf* close();
67  ~gzstreambuf() { close(); }
68 
69  virtual int overflow( int c = EOF);
70  virtual int underflow();
71  virtual int sync();
72 };
73 
74 class gzstreambase : virtual public std::ios {
75 protected:
77 public:
78  gzstreambase() { init(&buf); }
79  gzstreambase( const char* name, int open_mode);
80  ~gzstreambase();
81  void open( const char* name, int open_mode);
82  void close();
83  gzstreambuf* rdbuf() { return &buf; }
84 };
85 
86 // ----------------------------------------------------------------------------
87 // User classes. Use igzstream and ogzstream analogously to ifstream and
88 // ofstream respectively. They read and write files based on the gz*
89 // function interface of the zlib. Files are compatible with gzip compression.
90 // ----------------------------------------------------------------------------
91 
92 class igzstream : public gzstreambase, public std::istream {
93 public:
94  igzstream() : std::istream( &buf) {}
95  igzstream( const char* name, int gz_open_mode = std::ios::in)
96  : gzstreambase( name, gz_open_mode), std::istream( &buf) {}
98  void open( const char* name, int gz_open_mode = std::ios::in) {
99  gzstreambase::open( name, gz_open_mode);
100  }
101 };
102 
103 class ogzstream : public gzstreambase, public std::ostream {
104 public:
105  ogzstream() : std::ostream( &buf) {}
106  ogzstream( const char* name, int mode = std::ios::out)
107  : gzstreambase( name, mode), std::ostream( &buf) {}
109  void open( const char* name, int gz_open_mode = std::ios::out) {
110  gzstreambase::open( name, gz_open_mode);
111  }
112 };
113 
114 #ifdef GZSTREAM_NAMESPACE
115 } // namespace GZSTREAM_NAMESPACE
116 #endif
117 
118 #endif // GZSTREAM_H
119 // ============================================================================
120 // EOF //
121 
ogzstream::rdbuf
gzstreambuf * rdbuf()
Definition: gzstream.h:108
gzstreambuf::gzstreambuf
gzstreambuf()
Definition: gzstream.h:57
igzstream
Definition: gzstream.h:92
gzstreambuf::opened
char opened
Definition: gzstream.h:52
gzstreambuf::file
gzFile file
Definition: gzstream.h:50
igzstream::igzstream
igzstream()
Definition: gzstream.h:94
gzstreambuf
Definition: gzstream.h:45
ogzstream
Definition: gzstream.h:103
igzstream::rdbuf
gzstreambuf * rdbuf()
Definition: gzstream.h:97
ogzstream::open
void open(const char *name, int gz_open_mode=std::ios::out)
Definition: gzstream.h:109
gzstreambase::gzstreambase
gzstreambase()
Definition: gzstream.h:78
gzstreambuf::~gzstreambuf
~gzstreambuf()
Definition: gzstream.h:67
gzstreambuf::is_open
int is_open()
Definition: gzstream.h:64
igzstream::open
void open(const char *name, int gz_open_mode=std::ios::in)
Definition: gzstream.h:98
gzstreambuf::mode
int mode
Definition: gzstream.h:53
ogzstream::ogzstream
ogzstream(const char *name, int mode=std::ios::out)
Definition: gzstream.h:106
gzstreambase::buf
gzstreambuf buf
Definition: gzstream.h:76
ogzstream::ogzstream
ogzstream()
Definition: gzstream.h:105
gzstreambase::rdbuf
gzstreambuf * rdbuf()
Definition: gzstream.h:83
gzstreambase::open
void open(const char *name, int open_mode)
Definition: gzstream.cc:148
igzstream::igzstream
igzstream(const char *name, int gz_open_mode=std::ios::in)
Definition: gzstream.h:95
gzstreambase
Definition: gzstream.h:74