EGS Brachy
An egs++ user code for rapid brachytherapy calculations
Loading...
Searching...
No Matches
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
38namespace GZSTREAM_NAMESPACE {
39#endif
40
41// ----------------------------------------------------------------------------
42// Internal classes to implement gzstream. See below for user classes.
43// ----------------------------------------------------------------------------
44
45class gzstreambuf : public std::streambuf {
46private:
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();
56public:
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() {
65 return opened;
66 }
67 gzstreambuf *open(const char *name, int open_mode);
68 gzstreambuf *close();
70 close();
71 }
72
73 virtual int overflow(int c = EOF);
74 virtual int underflow();
75 virtual int sync();
76};
77
78class gzstreambase : virtual public std::ios {
79protected:
81public:
83 init(&buf);
84 }
85 gzstreambase(const char *name, int open_mode);
87 void open(const char *name, int open_mode);
88 void close();
90 return &buf;
91 }
92};
93
94// ----------------------------------------------------------------------------
95// User classes. Use igzstream and ogzstream analogously to ifstream and
96// ofstream respectively. They read and write files based on the gz*
97// function interface of the zlib. Files are compatible with gzip compression.
98// ----------------------------------------------------------------------------
99
100class igzstream : public gzstreambase, public std::istream {
101public:
102 igzstream() : std::istream(&buf) {}
103 igzstream(const char *name, int open_mode = std::ios::in)
104 : gzstreambase(name, open_mode), std::istream(&buf) {}
106 return gzstreambase::rdbuf();
107 }
108 void open(const char *name, int open_mode = std::ios::in) {
109 gzstreambase::open(name, open_mode);
110 }
111};
112
113class ogzstream : public gzstreambase, public std::ostream {
114public:
115 ogzstream() : std::ostream(&buf) {}
116 ogzstream(const char *name, int mode = std::ios::out)
117 : gzstreambase(name, mode), std::ostream(&buf) {}
119 return gzstreambase::rdbuf();
120 }
121 void open(const char *name, int open_mode = std::ios::out) {
122 gzstreambase::open(name, open_mode);
123 }
124};
125
126#ifdef GZSTREAM_NAMESPACE
127} // namespace GZSTREAM_NAMESPACE
128#endif
129
130#endif // GZSTREAM_H
131// ============================================================================
132// EOF //
133
gzstreambuf buf
Definition gzstream.h:80
gzstreambuf * rdbuf()
Definition gzstream.h:89
void open(const char *name, int open_mode)
Definition gzstream.C:149
char opened
Definition gzstream.h:52
int is_open()
Definition gzstream.h:64
gzFile file
Definition gzstream.h:50
igzstream(const char *name, int open_mode=std::ios::in)
Definition gzstream.h:103
void open(const char *name, int open_mode=std::ios::in)
Definition gzstream.h:108
gzstreambuf * rdbuf()
Definition gzstream.h:105
gzstreambuf * rdbuf()
Definition gzstream.h:118
ogzstream(const char *name, int mode=std::ios::out)
Definition gzstream.h:116
void open(const char *name, int open_mode=std::ios::out)
Definition gzstream.h:121