fix8  version 1.4.0
Open Source C++ FIX Framework
gzstream.hpp
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 #include <fix8/f8config.h>
33 #ifdef FIX8_HAVE_ZLIB_H
34 
35 // standard C++ with new header file names and std:: namespace
36 #include <iostream>
37 #include <fstream>
38 #include <zlib.h>
39 
40 #ifdef GZSTREAM_NAMESPACE
41 namespace GZSTREAM_NAMESPACE {
42 #endif
43 
44 // ----------------------------------------------------------------------------
45 // Internal classes to implement gzstream. See below for user classes.
46 // ----------------------------------------------------------------------------
47 
48 class gzstreambuf : public std::streambuf
49 {
50 private:
51  enum { bufferSize = 47+256 }; // size of data buff
52  // totals 512 bytes under g++ for igzstream at the end.
53 
54  gzFile file; // file handle for compressed file
55  char buffer[bufferSize]; // data buffer
56  char opened; // open/close state of stream
57  int mode; // I/O mode
58 
59  int flush_buffer();
60 public:
61  gzstreambuf() : opened(0)
62  {
63  setp( buffer, buffer + (bufferSize-1));
64  setg( buffer + 4, // beginning of putback area
65  buffer + 4, // read position
66  buffer + 4); // end position
67  // ASSERT: both input & output capabilities will not be used together
68  }
69  int is_open() { return opened; }
70  gzstreambuf* open( const char* name, int open_mode);
71  gzstreambuf* close();
72  ~gzstreambuf() { close(); }
73 
74  virtual int overflow( int c = EOF);
75  virtual int underflow();
76  virtual int sync();
77 };
78 
79 class gzstreambase : virtual public std::ios
80 {
81 protected:
82  gzstreambuf buf;
83 public:
84  gzstreambase() { init(&buf); }
85  gzstreambase( const char* name, int open_mode);
86  ~gzstreambase();
87  void open( const char* name, int open_mode);
88  void close();
89  gzstreambuf* rdbuf() { return &buf; }
90 };
91 
92 class ogzstream : public gzstreambase, public std::ostream
93 {
94 public:
95  ogzstream() : std::ostream( &buf) {}
96  ogzstream( const char* name, int mode = std::ios::out)
97  : gzstreambase( name, mode), std::ostream( &buf) {}
98  gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
99  void open( const char* name, int open_mode = std::ios::out)
100  {
101  gzstreambase::open( name, open_mode);
102  }
103 
104  bool operator!() const { return this->fail(); }
105 };
106 
107 #ifdef GZSTREAM_NAMESPACE
108 } // namespace GZSTREAM_NAMESPACE
109 #endif
110 
111 #endif // FIX8_HAVE_ZLIB_H
112 #endif // GZSTREAM_H
113 // ============================================================================
114 // EOF //
115