Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

gzstream.cpp

Go to the documentation of this file.
00001 // ============================================================================
00002 // gzstream, C++ iostream classes wrapping the zlib compression library.
00003 // Copyright (C) 2001  Deepak Bandyopadhyay, Lutz Kettner
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 // ============================================================================
00019 //
00020 // File          : gzstream.C
00021 // Revision      : $Revision: 1.1 $
00022 // Revision_date : $Date: 2002/10/17 08:18:48 $
00023 // Author(s)     : Deepak Bandyopadhyay, Lutz Kettner
00024 // 
00025 // Standard streambuf implementation following Nicolai Josuttis, "The 
00026 // Standard C++ Library".
00027 // ============================================================================
00028 
00029 //#include <gzstream.h>
00030 #include "gzstream.h"
00031 #include <iostream>
00032 #include <string.h>  // for memcpy
00033 
00034 #ifdef GZSTREAM_NAMESPACE
00035 namespace GZSTREAM_NAMESPACE {
00036 #endif
00037 
00038 // ----------------------------------------------------------------------------
00039 // Internal classes to implement gzstream. See header file for user classes.
00040 // ----------------------------------------------------------------------------
00041 
00042 // --------------------------------------
00043 // class gzstreambuf:
00044 // --------------------------------------
00045 
00046 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
00047     if ( is_open())
00048         return (gzstreambuf*)0;
00049     mode = open_mode;
00050     // no append nor read/write mode
00051     if ((mode & std::ios::ate) || (mode & std::ios::app)
00052         || ((mode & std::ios::in) && (mode & std::ios::out)))
00053         return (gzstreambuf*)0;
00054     char  fmode[10];
00055     char* fmodeptr = fmode;
00056     if ( mode & std::ios::in)
00057         *fmodeptr++ = 'r';
00058     else if ( mode & std::ios::out)
00059         *fmodeptr++ = 'w';
00060     *fmodeptr++ = 'b';
00061     *fmodeptr = '\0';
00062     file = gzopen( name, fmode);
00063     if (file == 0)
00064         return (gzstreambuf*)0;
00065     opened = 1;
00066     return this;
00067 }
00068 
00069 gzstreambuf * gzstreambuf::close() {
00070     if ( is_open()) {
00071         sync();
00072         opened = 0;
00073         if ( gzclose( file) == Z_OK)
00074             return this;
00075     }
00076     return (gzstreambuf*)0;
00077 }
00078 
00079 int gzstreambuf::underflow() { // used for input buffer only
00080     if ( gptr() && ( gptr() < egptr()))
00081         return * reinterpret_cast<unsigned char *>( gptr());
00082 
00083     if ( ! (mode & std::ios::in) || ! opened)
00084         return EOF;
00085     // Josuttis' implementation of inbuf
00086     int n_putback = gptr() - eback();
00087     if ( n_putback > 4)
00088         n_putback = 4;
00089     memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
00090 
00091     int num = gzread( file, buffer+4, bufferSize-4);
00092     if (num <= 0) // ERROR or EOF
00093         return EOF;
00094 
00095     // reset buffer pointers
00096     setg( buffer + (4 - n_putback),   // beginning of putback area
00097           buffer + 4,                 // read position
00098           buffer + 4 + num);          // end of buffer
00099 
00100     // return next character
00101     return * reinterpret_cast<unsigned char *>( gptr());    
00102 }
00103 
00104 int gzstreambuf::flush_buffer() {
00105     // Separate the writing of the buffer from overflow() and
00106     // sync() operation.
00107     int w = pptr() - pbase();
00108     if ( gzwrite( file, pbase(), w)) {
00109         setp( pbase(), epptr() - 1);
00110         return w;
00111     }
00112     return EOF;
00113 }
00114 
00115 int gzstreambuf::overflow( int c) { // used for output buffer only
00116     if ( ! ( mode & std::ios::out) || ! opened)
00117         return EOF;
00118     if (c != EOF) {
00119         *pptr() = c;
00120         pbump(1);
00121     }
00122     if ( flush_buffer() == EOF)
00123         return EOF;
00124     return c;
00125 }
00126 
00127 int gzstreambuf::sync() {
00128     // Changed to use flush_buffer() instead of overflow( EOF)
00129     // which caused improper behavior with std::endl and flush(),
00130     // bug reported by Vincent Ricard.
00131     if ( pptr() && pptr() > pbase()) {
00132         if ( flush_buffer() == EOF)
00133             return -1;
00134     }
00135     return 0;
00136 }
00137 
00138 // --------------------------------------
00139 // class gzstreambase:
00140 // --------------------------------------
00141 
00142 gzstreambase::gzstreambase( const char* name, int mode) {
00143     init( &buf);
00144     open( name, mode);
00145 }
00146 
00147 gzstreambase::~gzstreambase() {
00148     buf.close();
00149 }
00150 
00151 void gzstreambase::open( const char* name, int open_mode) {
00152     if ( ! buf.open( name, open_mode))
00153         clear( rdstate() & std::ios::badbit);
00154 }
00155 
00156 void gzstreambase::close() {
00157     if ( buf.is_open())
00158         if ( ! buf.close())
00159             clear( rdstate() & std::ios::badbit);
00160 }
00161 
00162 #ifdef GZSTREAM_NAMESPACE
00163 } // namespace GZSTREAM_NAMESPACE
00164 #endif
00165 
00166 // ============================================================================
00167 // EOF //

Generated on Tue Nov 19 12:32:57 2002 for AIDA_XMLStore by doxygen1.2.16