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