Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:19:50

0001 // ============================================================================
0002 // gzstream, C++ iostream classes wrapping the zlib compression library.
0003 // Copyright (C) 2001  Deepak Bandyopadhyay, Lutz Kettner
0004 //
0005 // This library is free software; you can redistribute it and/or
0006 // modify it under the terms of the GNU Lesser General Public
0007 // License as published by the Free Software Foundation; either
0008 // version 2.1 of the License, or (at your option) any later version.
0009 //
0010 // This library is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013 // Lesser General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public
0016 // License along with this library; if not, write to the Free Software
0017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0018 // ============================================================================
0019 //
0020 // File          : gzstream.C
0021 // Revision      : $Revision: 1.7 $
0022 // Revision_date : $Date: 2003/01/08 14:41:27 $
0023 // Author(s)     : Deepak Bandyopadhyay, Lutz Kettner
0024 // 
0025 // Standard streambuf implementation following Nicolai Josuttis, "The 
0026 // Standard C++ Library".
0027 // ============================================================================
0028 
0029 #include <gzstream.h>
0030 #include <iostream>
0031 #include <string.h>  // for memcpy
0032 
0033 #ifdef GZSTREAM_NAMESPACE
0034 namespace GZSTREAM_NAMESPACE {
0035 #endif
0036 
0037 // ----------------------------------------------------------------------------
0038 // Internal classes to implement gzstream. See header file for user classes.
0039 // ----------------------------------------------------------------------------
0040 
0041 // --------------------------------------
0042 // class gzstreambuf:
0043 // --------------------------------------
0044 
0045 gzstreambuf* gzstreambuf::open( const char* name, int open_mode) {
0046     if ( is_open())
0047         return (gzstreambuf*)0;
0048     mode = open_mode;
0049     // no append nor read/write mode
0050     if ((mode & std::ios::ate) || (mode & std::ios::app)
0051         || ((mode & std::ios::in) && (mode & std::ios::out)))
0052         return (gzstreambuf*)0;
0053     char  fmode[10];
0054     char* fmodeptr = fmode;
0055     if ( mode & std::ios::in)
0056         *fmodeptr++ = 'r';
0057     else if ( mode & std::ios::out)
0058         *fmodeptr++ = 'w';
0059     *fmodeptr++ = 'b';
0060     *fmodeptr = '\0';
0061     file = gzopen( name, fmode);
0062     if (file == 0)
0063         return (gzstreambuf*)0;
0064     opened = 1;
0065     return this;
0066 }
0067 
0068 gzstreambuf * gzstreambuf::close() {
0069     if ( is_open()) {
0070         sync();
0071         opened = 0;
0072         if ( gzclose( file) == Z_OK)
0073             return this;
0074     }
0075     return (gzstreambuf*)0;
0076 }
0077 
0078 int gzstreambuf::underflow() { // used for input buffer only
0079     if ( gptr() && ( gptr() < egptr()))
0080         return * reinterpret_cast<unsigned char *>( gptr());
0081 
0082     if ( ! (mode & std::ios::in) || ! opened)
0083         return EOF;
0084     // Josuttis' implementation of inbuf
0085     int n_putback = gptr() - eback();
0086     if ( n_putback > 4)
0087         n_putback = 4;
0088     memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
0089 
0090     int num = gzread( file, buffer+4, bufferSize-4);
0091     if (num <= 0) // ERROR or EOF
0092         return EOF;
0093 
0094     // reset buffer pointers
0095     setg( buffer + (4 - n_putback),   // beginning of putback area
0096           buffer + 4,                 // read position
0097           buffer + 4 + num);          // end of buffer
0098 
0099     // return next character
0100     return * reinterpret_cast<unsigned char *>( gptr());    
0101 }
0102 
0103 int gzstreambuf::flush_buffer() {
0104     // Separate the writing of the buffer from overflow() and
0105     // sync() operation.
0106     int w = pptr() - pbase();
0107     if ( gzwrite( file, pbase(), w) != w)
0108         return EOF;
0109     pbump( -w);
0110     return w;
0111 }
0112 
0113 int gzstreambuf::overflow( int c) { // used for output buffer only
0114     if ( ! ( mode & std::ios::out) || ! opened)
0115         return EOF;
0116     if (c != EOF) {
0117         *pptr() = c;
0118         pbump(1);
0119     }
0120     if ( flush_buffer() == EOF)
0121         return EOF;
0122     return c;
0123 }
0124 
0125 int gzstreambuf::sync() {
0126     // Changed to use flush_buffer() instead of overflow( EOF)
0127     // which caused improper behavior with std::endl and flush(),
0128     // bug reported by Vincent Ricard.
0129     if ( pptr() && pptr() > pbase()) {
0130         if ( flush_buffer() == EOF)
0131             return -1;
0132     }
0133     return 0;
0134 }
0135 
0136 // --------------------------------------
0137 // class gzstreambase:
0138 // --------------------------------------
0139 
0140 gzstreambase::gzstreambase( const char* name, int mode) {
0141     init( &buf);
0142     open( name, mode);
0143 }
0144 
0145 gzstreambase::~gzstreambase() {
0146     buf.close();
0147 }
0148 
0149 void gzstreambase::open( const char* name, int open_mode) {
0150     if ( ! buf.open( name, open_mode))
0151         clear( rdstate() | std::ios::badbit);
0152 }
0153 
0154 void gzstreambase::close() {
0155     if ( buf.is_open())
0156         if ( ! buf.close())
0157             clear( rdstate() | std::ios::badbit);
0158 }
0159 
0160 #ifdef GZSTREAM_NAMESPACE
0161 } // namespace GZSTREAM_NAMESPACE
0162 #endif
0163 
0164 // ============================================================================
0165 // EOF //