]> git.lyx.org Git - lyx.git/blob - src/support/gzstream.h
Consistent use of preprocessor guards;
[lyx.git] / src / support / gzstream.h
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.2 $
22 // Revision_date : $Date: 2005/04/26 10:30:24 $
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 #ifdef HAVE_ZLIB_H
36 # include <zlib.h>
37 #endif
38
39 // For LyX
40 #define GZSTREAM_NAMESPACE gz
41
42 #ifdef GZSTREAM_NAMESPACE
43 namespace GZSTREAM_NAMESPACE {
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // Internal classes to implement gzstream. See below for user classes.
48 // ----------------------------------------------------------------------------
49
50 class gzstreambuf : public std::streambuf {
51 private:
52     static const int bufferSize = 47+256;    // size of data buff
53     // totals 512 bytes under g++ for igzstream at the end.
54
55     gzFile           file;               // file handle for compressed file
56     char             buffer[bufferSize]; // data buffer
57     char             opened;             // open/close state of stream
58     int              mode;               // I/O mode
59
60     int flush_buffer();
61 public:
62     gzstreambuf() : opened(0) {
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 protected:
81     gzstreambuf buf;
82 public:
83     gzstreambase() { init(&buf); }
84     gzstreambase( const char* name, int open_mode);
85     ~gzstreambase();
86     void open( const char* name, int open_mode);
87     void close();
88     gzstreambuf* rdbuf() { return &buf; }
89 };
90
91 // ----------------------------------------------------------------------------
92 // User classes. Use igzstream and ogzstream analogously to ifstream and
93 // ofstream respectively. They read and write files based on the gz*
94 // function interface of the zlib. Files are compatible with gzip compression.
95 // ----------------------------------------------------------------------------
96
97 class igzstream : public gzstreambase, public std::istream {
98 public:
99     igzstream() : std::istream( &buf) {}
100     igzstream( const char* name, int open_mode = std::ios::in)
101         : gzstreambase( name, open_mode), std::istream( &buf) {}
102     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
103     void open( const char* name, int open_mode = std::ios::in) {
104         gzstreambase::open( name, open_mode);
105     }
106 };
107
108 class ogzstream : public gzstreambase, public std::ostream {
109 public:
110     ogzstream() : std::ostream( &buf) {}
111     ogzstream( const char* name, int mode = std::ios::out)
112         : gzstreambase( name, mode), std::ostream( &buf) {}
113     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
114     void open( const char* name, int open_mode = std::ios::out) {
115         gzstreambase::open( name, open_mode);
116     }
117 };
118
119 #ifdef GZSTREAM_NAMESPACE
120 } // namespace GZSTREAM_NAMESPACE
121 #endif
122
123 #endif // GZSTREAM_H
124 // ============================================================================
125 // EOF //