2 // ============================================================================
3 // gzstream, C++ iostream classes wrapping the zlib compression library.
4 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // ============================================================================
22 // Revision : $Revision: 1.7 $
23 // Revision_date : $Date: 2003/01/08 14:41:27 $
24 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
26 // Standard streambuf implementation following Nicolai Josuttis, "The
27 // Standard C++ Library".
28 // ============================================================================
32 #include <string.h> // for memcpy
34 #ifdef GZSTREAM_NAMESPACE
35 namespace GZSTREAM_NAMESPACE
{
38 // ----------------------------------------------------------------------------
39 // Internal classes to implement gzstream. See header file for user classes.
40 // ----------------------------------------------------------------------------
42 // --------------------------------------
44 // --------------------------------------
46 gzstreambuf
* gzstreambuf::open( const char* name
, int open_mode
) {
48 return (gzstreambuf
*)0;
50 // no append nor read/write mode
51 if ((mode
& std::ios::ate
) || (mode
& std::ios::app
)
52 || ((mode
& std::ios::in
) && (mode
& std::ios::out
)))
53 return (gzstreambuf
*)0;
55 char* fmodeptr
= fmode
;
56 if ( mode
& std::ios::in
)
58 else if ( mode
& std::ios::out
)
62 file
= gzopen( name
, fmode
);
64 return (gzstreambuf
*)0;
69 gzstreambuf
* gzstreambuf::close() {
73 if ( gzclose( file
) == Z_OK
)
76 return (gzstreambuf
*)0;
79 int gzstreambuf::underflow() { // used for input buffer only
80 if ( gptr() && ( gptr() < egptr()))
81 return * reinterpret_cast<unsigned char *>( gptr());
83 if ( ! (mode
& std::ios::in
) || ! opened
)
85 // Josuttis' implementation of inbuf
86 int n_putback
= gptr() - eback();
89 memcpy( buffer
+ (4 - n_putback
), gptr() - n_putback
, n_putback
);
91 int num
= gzread( file
, buffer
+4, bufferSize
-4);
92 if (num
<= 0) // ERROR or EOF
95 // reset buffer pointers
96 setg( buffer
+ (4 - n_putback
), // beginning of putback area
97 buffer
+ 4, // read position
98 buffer
+ 4 + num
); // end of buffer
100 // return next character
101 return * reinterpret_cast<unsigned char *>( gptr());
104 int gzstreambuf::flush_buffer() {
105 // Separate the writing of the buffer from overflow() and
107 int w
= pptr() - pbase();
108 if ( gzwrite( file
, pbase(), w
) != w
)
114 int gzstreambuf::overflow( int c
) { // used for output buffer only
115 if ( ! ( mode
& std::ios::out
) || ! opened
)
121 if ( flush_buffer() == EOF
)
126 int gzstreambuf::sync() {
127 // Changed to use flush_buffer() instead of overflow( EOF)
128 // which caused improper behavior with std::endl and flush(),
129 // bug reported by Vincent Ricard.
130 if ( pptr() && pptr() > pbase()) {
131 if ( flush_buffer() == EOF
)
137 gzstreambuf
& gzstreambuf::operator =(const gzstreambuf
&gzs
)
142 // --------------------------------------
143 // class gzstreambase:
144 // --------------------------------------
146 gzstreambase::gzstreambase( const char* name
, int mode
) {
151 gzstreambase::~gzstreambase() {
155 void gzstreambase::open( const char* name
, int open_mode
) {
156 if ( ! buf
.open( name
, open_mode
))
157 clear( rdstate() | std::ios::badbit
);
160 void gzstreambase::close() {
163 clear( rdstate() | std::ios::badbit
);
165 gzstreambase
& gzstreambase::operator =(const gzstreambase
&gzst
)
171 // --------------------------------------
173 // --------------------------------------
177 //const ogfstream& ogzfstream::operator = (const ogfstream &ogzcon)
179 // consensus = ogzcon.consensus;
184 #ifdef GZSTREAM_NAMESPACE
185 } // namespace GZSTREAM_NAMESPACE
188 // ============================================================================