1 |
tim |
615 |
// ============================================================================ |
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 |
gezelter |
1442 |
// Revision : $Revision$ |
22 |
|
|
// Revision_date : $Date$ |
23 |
tim |
615 |
// Author(s) : Deepak Bandyopadhyay, Lutz Kettner |
24 |
|
|
// |
25 |
|
|
// Standard streambuf implementation following Nicolai Josuttis, "The |
26 |
|
|
// Standard C++ Library". |
27 |
|
|
// ============================================================================ |
28 |
|
|
|
29 |
|
|
#ifndef IO_GZSTREAM_HPP |
30 |
|
|
#define IO_GZSTREAM_HPP |
31 |
|
|
|
32 |
|
|
// standard C++ with new header file names and std:: namespace |
33 |
|
|
#include <iostream> |
34 |
|
|
#include <fstream> |
35 |
|
|
#include <zlib.h> |
36 |
|
|
|
37 |
gezelter |
1390 |
namespace OpenMD { |
38 |
tim |
615 |
|
39 |
|
|
|
40 |
|
|
// ---------------------------------------------------------------------------- |
41 |
|
|
// Internal classes to implement gzstream. See below for user classes. |
42 |
|
|
// ---------------------------------------------------------------------------- |
43 |
|
|
|
44 |
|
|
class gzstreambuf : public std::streambuf { |
45 |
|
|
private: |
46 |
|
|
static const int bufferSize = 47+256; // size of data buff |
47 |
|
|
// totals 512 bytes under g++ for igzstream at the end. |
48 |
|
|
|
49 |
|
|
gzFile file; // file handle for compressed file |
50 |
|
|
char buffer[bufferSize]; // data buffer |
51 |
|
|
char opened; // open/close state of stream |
52 |
|
|
int mode; // I/O mode |
53 |
|
|
|
54 |
|
|
int flush_buffer(); |
55 |
|
|
public: |
56 |
|
|
gzstreambuf() : opened(0) { |
57 |
|
|
setp( buffer, buffer + (bufferSize-1)); |
58 |
|
|
setg( buffer + 4, // beginning of putback area |
59 |
|
|
buffer + 4, // read position |
60 |
|
|
buffer + 4); // end position |
61 |
|
|
// ASSERT: both input & output capabilities will not be used together |
62 |
|
|
} |
63 |
|
|
int is_open() { return opened; } |
64 |
|
|
gzstreambuf* open( const char* name, int open_mode); |
65 |
|
|
gzstreambuf* close(); |
66 |
|
|
~gzstreambuf() { close(); } |
67 |
|
|
|
68 |
|
|
virtual int overflow( int c = EOF); |
69 |
|
|
virtual int underflow(); |
70 |
|
|
virtual int sync(); |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
class gzstreambase : virtual public std::ios { |
74 |
|
|
protected: |
75 |
|
|
gzstreambuf buf; |
76 |
|
|
public: |
77 |
|
|
gzstreambase() { init(&buf); } |
78 |
|
|
gzstreambase( const char* name, int open_mode); |
79 |
|
|
~gzstreambase(); |
80 |
|
|
void open( const char* name, int open_mode); |
81 |
|
|
void close(); |
82 |
|
|
gzstreambuf* rdbuf() { return &buf; } |
83 |
|
|
}; |
84 |
|
|
|
85 |
|
|
// ---------------------------------------------------------------------------- |
86 |
|
|
// User classes. Use igzstream and ogzstream analogously to ifstream and |
87 |
|
|
// ofstream respectively. They read and write files based on the gz* |
88 |
|
|
// function interface of the zlib. Files are compatible with gzip compression. |
89 |
|
|
// ---------------------------------------------------------------------------- |
90 |
|
|
|
91 |
|
|
class igzstream : public gzstreambase, public std::istream { |
92 |
|
|
public: |
93 |
|
|
igzstream() : std::istream( &buf) {} |
94 |
|
|
igzstream( const char* name, int open_mode = std::ios::in) |
95 |
|
|
: gzstreambase( name, open_mode), std::istream( &buf) {} |
96 |
|
|
gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); } |
97 |
|
|
void open( const char* name, int open_mode = std::ios::in) { |
98 |
|
|
gzstreambase::open( name, open_mode); |
99 |
|
|
} |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
class ogzstream : public gzstreambase, public std::ostream { |
103 |
|
|
public: |
104 |
|
|
ogzstream() : std::ostream( &buf) {} |
105 |
|
|
ogzstream( const char* name, int mode = std::ios::out) |
106 |
|
|
: gzstreambase( name, mode), std::ostream( &buf) {} |
107 |
|
|
gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); } |
108 |
|
|
void open( const char* name, int open_mode = std::ios::out) { |
109 |
|
|
gzstreambase::open( name, open_mode); |
110 |
|
|
} |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
} |
114 |
|
|
#endif |
115 |
|
|
|