1 // ============================================================================
2 // gzstream, C++ iostream classes wrapping the zlib compression library.
3 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
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.
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.
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 // ============================================================================
21 // Revision : $Revision: 1.4 $
22 // Revision_date : $Date: 2005/04/26 10:30:24 $
23 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
31 #include "support/gzstream.h"
35 # include <string.h> // for memcpy
40 #ifdef GZSTREAM_NAMESPACE
41 namespace GZSTREAM_NAMESPACE {
44 // ----------------------------------------------------------------------------
45 // Internal classes to implement gzstream. See header file for user classes.
46 // ----------------------------------------------------------------------------
48 // --------------------------------------
50 // --------------------------------------
52 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
54 return (gzstreambuf*)0;
56 // no append nor read/write mode
57 if ((mode & ios::ate) || (mode & ios::app)
58 || ((mode & ios::in) && (mode & ios::out)))
59 return (gzstreambuf*)0;
61 char* fmodeptr = fmode;
64 else if ( mode & ios::out)
68 file = gzopen( name, fmode);
70 return (gzstreambuf*)0;
75 gzstreambuf * gzstreambuf::close() {
79 if ( gzclose( file) == Z_OK)
82 return (gzstreambuf*)0;
85 int gzstreambuf::underflow() { // used for input buffer only
86 if ( gptr() && ( gptr() < egptr()))
87 return * reinterpret_cast<unsigned char *>( gptr());
89 if ( ! (mode & ios::in) || ! opened)
91 // Josuttis' implementation of inbuf
92 int n_putback = gptr() - eback();
95 memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
97 int num = gzread( file, buffer+4, bufferSize-4);
98 if (num <= 0) // ERROR or EOF
101 // reset buffer pointers
102 setg( buffer + (4 - n_putback), // beginning of putback area
103 buffer + 4, // read position
104 buffer + 4 + num); // end of buffer
106 // return next character
107 return * reinterpret_cast<unsigned char *>( gptr());
110 int gzstreambuf::flush_buffer() {
111 // Separate the writing of the buffer from overflow() and
113 int w = pptr() - pbase();
114 if ( gzwrite( file, pbase(), w) != w)
120 int gzstreambuf::overflow( int c) { // used for output buffer only
121 if ( ! ( mode & ios::out) || ! opened)
127 if ( flush_buffer() == EOF)
132 int gzstreambuf::sync() {
133 // Changed to use flush_buffer() instead of overflow( EOF)
134 // which caused improper behavior with endl and flush(),
135 // bug reported by Vincent Ricard.
136 if ( pptr() && pptr() > pbase()) {
137 if ( flush_buffer() == EOF)
143 // --------------------------------------
144 // class gzstreambase:
145 // --------------------------------------
147 gzstreambase::gzstreambase( const char* name, int mode) {
152 gzstreambase::~gzstreambase() {
156 void gzstreambase::open( const char* name, int open_mode) {
157 if ( ! buf.open( name, open_mode))
158 clear( rdstate() | ios::badbit);
161 void gzstreambase::close() {
164 clear( rdstate() | ios::badbit);
167 #ifdef GZSTREAM_NAMESPACE
168 } // namespace GZSTREAM_NAMESPACE
171 // ============================================================================