]> git.lyx.org Git - lyx.git/blob - src/support/gzstream.cpp
fix warning on possibly(?) unused precompiled headers due to different -fPic settings...
[lyx.git] / src / support / gzstream.cpp
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.C
21 // Revision      : $Revision: 1.4 $
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 #include <config.h>
30
31 #include "gzstream.h"
32 #include <iostream>
33 #ifdef HAVE_STRING_H
34 # include <string.h> // for memcpy
35 #endif
36
37 #ifdef GZSTREAM_NAMESPACE
38 namespace GZSTREAM_NAMESPACE {
39 #endif
40
41 // ----------------------------------------------------------------------------
42 // Internal classes to implement gzstream. See header file for user classes.
43 // ----------------------------------------------------------------------------
44
45 // --------------------------------------
46 // class gzstreambuf:
47 // --------------------------------------
48
49 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
50     if ( is_open())
51         return (gzstreambuf*)0;
52     mode = open_mode;
53     // no append nor read/write mode
54     if ((mode & std::ios::ate) || (mode & std::ios::app)
55         || ((mode & std::ios::in) && (mode & std::ios::out)))
56         return (gzstreambuf*)0;
57     char  fmode[10];
58     char* fmodeptr = fmode;
59     if ( mode & std::ios::in)
60         *fmodeptr++ = 'r';
61     else if ( mode & std::ios::out)
62         *fmodeptr++ = 'w';
63     *fmodeptr++ = 'b';
64     *fmodeptr = '\0';
65     file = gzopen( name, fmode);
66     if (file == 0)
67         return (gzstreambuf*)0;
68     opened = 1;
69     return this;
70 }
71
72 gzstreambuf * gzstreambuf::close() {
73     if ( is_open()) {
74         sync();
75         opened = 0;
76         if ( gzclose( file) == Z_OK)
77             return this;
78     }
79     return (gzstreambuf*)0;
80 }
81
82 int gzstreambuf::underflow() { // used for input buffer only
83     if ( gptr() && ( gptr() < egptr()))
84         return * reinterpret_cast<unsigned char *>( gptr());
85
86     if ( ! (mode & std::ios::in) || ! opened)
87         return EOF;
88     // Josuttis' implementation of inbuf
89     int n_putback = gptr() - eback();
90     if ( n_putback > 4)
91         n_putback = 4;
92     memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
93
94     int num = gzread( file, buffer+4, bufferSize-4);
95     if (num <= 0) // ERROR or EOF
96         return EOF;
97
98     // reset buffer pointers
99     setg( buffer + (4 - n_putback),   // beginning of putback area
100           buffer + 4,                 // read position
101           buffer + 4 + num);          // end of buffer
102
103     // return next character
104     return * reinterpret_cast<unsigned char *>( gptr());
105 }
106
107 int gzstreambuf::flush_buffer() {
108     // Separate the writing of the buffer from overflow() and
109     // sync() operation.
110     int w = pptr() - pbase();
111     if ( gzwrite( file, pbase(), w) != w)
112         return EOF;
113     pbump( -w);
114     return w;
115 }
116
117 int gzstreambuf::overflow( int c) { // used for output buffer only
118     if ( ! ( mode & std::ios::out) || ! opened)
119         return EOF;
120     if (c != EOF) {
121         *pptr() = c;
122         pbump(1);
123     }
124     if ( flush_buffer() == EOF)
125         return EOF;
126     return c;
127 }
128
129 int gzstreambuf::sync() {
130     // Changed to use flush_buffer() instead of overflow( EOF)
131     // which caused improper behavior with std::endl and flush(),
132     // bug reported by Vincent Ricard.
133     if ( pptr() && pptr() > pbase()) {
134         if ( flush_buffer() == EOF)
135             return -1;
136     }
137     return 0;
138 }
139
140 // --------------------------------------
141 // class gzstreambase:
142 // --------------------------------------
143
144 gzstreambase::gzstreambase( const char* name, int mode) {
145     init( &buf);
146     open( name, mode);
147 }
148
149 gzstreambase::~gzstreambase() {
150     buf.close();
151 }
152
153 void gzstreambase::open( const char* name, int open_mode) {
154     if ( ! buf.open( name, open_mode))
155         clear( rdstate() | std::ios::badbit);
156 }
157
158 void gzstreambase::close() {
159     if ( buf.is_open())
160         if ( ! buf.close())
161             clear( rdstate() | std::ios::badbit);
162 }
163
164 #ifdef GZSTREAM_NAMESPACE
165 } // namespace GZSTREAM_NAMESPACE
166 #endif
167
168 // ============================================================================
169 // EOF //