ARTS  2.3.1285(git:92a29ea9-dirty)
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 <zlib.h>
34 #include <fstream>
35 #include <iostream>
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 
57  public:
58  gzstreambuf() : opened(0) {
59  setp(buffer, buffer + (bufferSize - 1));
60  setg(buffer + 4, // beginning of putback area
61  buffer + 4, // read position
62  buffer + 4); // end position
63  // ASSERT: both input & output capabilities will not be used together
64  }
65  int is_open() { return opened; }
66  gzstreambuf* open(const char* name, int open_mode);
67  gzstreambuf* close();
68  ~gzstreambuf() { close(); }
69 
70  virtual int overflow(int c = EOF);
71  virtual int underflow();
72  virtual int sync();
73 };
74 
75 class gzstreambase : virtual public std::ios {
76  protected:
78 
79  public:
80  gzstreambase() { init(&buf); }
81  gzstreambase(const char* name, int open_mode);
82  ~gzstreambase();
83  void open(const char* name, int open_mode);
84  void close();
85  gzstreambuf* rdbuf() { return &buf; }
86 };
87 
88 // ----------------------------------------------------------------------------
89 // User classes. Use igzstream and ogzstream analogously to ifstream and
90 // ofstream respectively. They read and write files based on the gz*
91 // function interface of the zlib. Files are compatible with gzip compression.
92 // ----------------------------------------------------------------------------
93 
94 class igzstream : public gzstreambase, public std::istream {
95  public:
96  igzstream() : std::istream(&buf) {}
97  igzstream(const char* name, int gz_open_mode = std::ios::in)
98  : gzstreambase(name, gz_open_mode), std::istream(&buf) {}
100  void open(const char* name, int gz_open_mode = std::ios::in) {
101  gzstreambase::open(name, gz_open_mode);
102  }
103 };
104 
105 class ogzstream : public gzstreambase, public std::ostream {
106  public:
107  ogzstream() : std::ostream(&buf) {}
108  ogzstream(const char* name, int mode = std::ios::out)
109  : gzstreambase(name, mode), std::ostream(&buf) {}
111  void open(const char* name, int gz_open_mode = std::ios::out) {
112  gzstreambase::open(name, gz_open_mode);
113  }
114 };
115 
116 #ifdef GZSTREAM_NAMESPACE
117 } // namespace GZSTREAM_NAMESPACE
118 #endif
119 
120 #endif // GZSTREAM_H
121 // ============================================================================
122 // EOF //
gzstreambuf()
Definition: gzstream.h:58
gzFile file
Definition: gzstream.h:50
char opened
Definition: gzstream.h:52
void open(const char *name, int gz_open_mode=std::ios::in)
Definition: gzstream.h:100
void open(const char *name, int open_mode)
Definition: gzstream.cc:139
gzstreambuf * rdbuf()
Definition: gzstream.h:85
ogzstream()
Definition: gzstream.h:107
gzstreambuf * rdbuf()
Definition: gzstream.h:110
int is_open()
Definition: gzstream.h:65
igzstream()
Definition: gzstream.h:96
igzstream(const char *name, int gz_open_mode=std::ios::in)
Definition: gzstream.h:97
~gzstreambuf()
Definition: gzstream.h:68
gzstreambase()
Definition: gzstream.h:80
void open(const char *name, int gz_open_mode=std::ios::out)
Definition: gzstream.h:111
Workspace & init(Workspace &ws)
int mode
Definition: gzstream.h:53
gzstreambuf * rdbuf()
Definition: gzstream.h:99
gzstreambuf buf
Definition: gzstream.h:77
ogzstream(const char *name, int mode=std::ios::out)
Definition: gzstream.h:108