]> git.lyx.org Git - lyx.git/blob - src/support/gzstream.h
qt5: Fix use of zlib
[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 #include <QtCore>
36 #if (defined Q_OS_WIN && QT_VERSION >= 0x050000)
37 #define Z_PREFIX 1
38 #endif
39 #include <zlib.h>
40
41 // For LyX
42 #define GZSTREAM_NAMESPACE gz
43
44 #ifdef GZSTREAM_NAMESPACE
45 namespace GZSTREAM_NAMESPACE {
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // Internal classes to implement gzstream. See below for user classes.
50 // ----------------------------------------------------------------------------
51
52 class gzstreambuf : public std::streambuf {
53 private:
54     static const int bufferSize = 47+256;    // size of data buff
55     // totals 512 bytes under g++ for igzstream at the end.
56
57     gzFile           file;               // file handle for compressed file
58     char             buffer[bufferSize]; // data buffer
59     char             opened;             // open/close state of stream
60     int              mode;               // I/O mode
61
62     int flush_buffer();
63 public:
64     gzstreambuf() : opened(0) {
65         setp( buffer, buffer + (bufferSize-1));
66         setg( buffer + 4,     // beginning of putback area
67               buffer + 4,     // read position
68               buffer + 4);    // end position
69         // ASSERT: both input & output capabilities will not be used together
70     }
71     int is_open() { return opened; }
72     gzstreambuf* open( const char* name, int open_mode);
73     gzstreambuf* close();
74     ~gzstreambuf() { close(); }
75
76     virtual int     overflow( int c = EOF);
77     virtual int     underflow();
78     virtual int     sync();
79 };
80
81 class gzstreambase : virtual public std::ios {
82 protected:
83     gzstreambuf buf;
84 public:
85     gzstreambase() { init(&buf); }
86     gzstreambase( const char* name, int open_mode);
87     ~gzstreambase();
88     void open( const char* name, int open_mode);
89     void close();
90     gzstreambuf* rdbuf() { return &buf; }
91 };
92
93 // ----------------------------------------------------------------------------
94 // User classes. Use igzstream and ogzstream analogously to ifstream and
95 // ofstream respectively. They read and write files based on the gz*
96 // function interface of the zlib. Files are compatible with gzip compression.
97 // ----------------------------------------------------------------------------
98
99 class igzstream : public gzstreambase, public std::istream {
100 public:
101     igzstream() : std::istream( &buf) {}
102     igzstream( const char* name, int open_mode = std::ios::in)
103         : gzstreambase( name, open_mode), std::istream( &buf) {}
104     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
105     void open( const char* name, int open_mode = std::ios::in) {
106         gzstreambase::open( name, open_mode);
107     }
108 };
109
110 class ogzstream : public gzstreambase, public std::ostream {
111 public:
112     ogzstream() : std::ostream( &buf) {}
113     ogzstream( const char* name, int mode = std::ios::out)
114         : gzstreambase( name, mode), std::ostream( &buf) {}
115     gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
116     void open( const char* name, int open_mode = std::ios::out) {
117         gzstreambase::open( name, open_mode);
118     }
119 };
120
121 #ifdef GZSTREAM_NAMESPACE
122 } // namespace GZSTREAM_NAMESPACE
123 #endif
124
125 #endif // GZSTREAM_H
126 // ============================================================================
127 // EOF //