head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.42; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.02.00; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.06; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.04; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# zfstream.cxx

    00001 //  A C++ I/O streams interface to the zlib gz* functions
    00002 //
    00003 // Written by Bernie Bright, 1998
    00004 // Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@@rodin.wustl.edu>
    00005 //
    00006 // Copyright (C) 1998  Bernie Bright - bbright@@c031.aone.net.au
    00007 //
    00008 // This library is free software; you can redistribute it and/or
    00009 // modify it under the terms of the GNU Library General Public
    00010 // License as published by the Free Software Foundation; either
    00011 // version 2 of the License, or (at your option) any later version.
    00012 //
    00013 // This library is distributed in the hope that it will be useful,
    00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00016 // Library General Public License for more details.
    00017 //
    00018 // You should have received a copy of the GNU Library General Public
    00019 // License along with this library; if not, write to the
    00020 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00021 // Boston, MA  02111-1307, USA.
    00022 //
    00023 // $Id: zfstream.cxx,v 1.7 2001/05/23 19:25:28 curt Exp $
    00024
    00025 #include <simgear/compiler.h>
    00026
    00027 #ifdef SG_HAVE_STD_INCLUDES
    00028 # include <cerrno>
    00029 #else
    00030 # include <errno.h>
    00031 #endif
    00032 #include <memory.h>
    00033 #include <stdio.h>
    00034
    00035 #include "zfstream.hxx"
    00036
    00037 //
    00038 // Construct a gzfilebuf object.
    00039 // Allocate memory for 'get' buffer and zero all buffer pointers.
    00040 //
    00041 gzfilebuf::gzfilebuf()
    00042     : streambuf(),
    00043       file(NULL),
    00044 #if defined( __MWERKS__ ) || __GNUC__ > 2
    00045       mode(ios_openmode(0)),
    00046 #else
    00047       mode(0),
    00048 #endif
    00049       own_file_descriptor(false),
    00050       ibuf_size(0),
    00051       ibuffer(0)
    00052 {
    00053 //     try {
    00054     ibuf_size = page_size / sizeof(char);
    00055     ibuffer = new char [ibuf_size];
    00056 //     } catch (...) {
    00057 //      delete [] ibuffer;
    00058 //     }
    00059
    00060     // Null get and set pointers.
    00061     this->setg(0,0,0);
    00062     this->setp(0,0);
    00063 }
    00064
    00065 gzfilebuf::~gzfilebuf()
    00066 {
    00067     sync();
    00068     if ( own_file_descriptor )
    00069         this->close();
    00070     delete [] ibuffer;
    00071 }
    00072
    00073 void
    00074 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
    00075 {
    00076 //     memset( char_mode, '\0', 10 );
    00077 //     char* p = char_mode;
    00078
    00079     if ( io_mode & ios_in )
    00080     {
    00081         mode = ios_in;
    00082         *p++ = 'r';
    00083     }
    00084     else if ( io_mode & ios_app )
    00085     {
    00086         mode = ios_app;
    00087         *p++ = 'a';
    00088     }
    00089     else
    00090     {
    00091         mode = ios_out;
    00092         *p++ = 'w';
    00093     }
    00094
    00095     if ( io_mode & ios_binary )
    00096     {
    00097         mode |= ios_binary;
    00098         *p++ = 'b';
    00099     }
    00100
    00101     // Hard code the compression level
    00102     if ( io_mode & (ios_out | ios_app) )
    00103     {
    00104         *p++ = '9';
    00105     }
    00106
    00107     *p = '\0';
    00108 }
    00109
    00110 gzfilebuf*
    00111 gzfilebuf::open( const char *name, ios_openmode io_mode )
    00112 {
    00113     if ( is_open() )
    00114         return NULL;
    00115
    00116     char char_mode[10];
    00117     cvt_iomode( char_mode, io_mode );
    00118     if ( (file = gzopen(name, char_mode)) == NULL ) {
    00119         // perror( "gzfilebuf::open(): " );
    00120         errno = 0;
    00121         return NULL;
    00122     }
    00123
    00124     own_file_descriptor = true;
    00125
    00126     return this;
    00127 }
    00128
    00129 gzfilebuf*
    00130 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
    00131 {
    00132     if ( is_open() )
    00133         return NULL;
    00134
    00135     char char_mode[10];
    00136     cvt_iomode( char_mode, io_mode );
    00137     if ( (file = gzdopen(file_descriptor, char_mode)) == NULL ) {
    00138         perror( "gzfilebuf::attach(): " );
    00139         errno = 0;
    00140         return NULL;
    00141     }
    00142
    00143     own_file_descriptor = false;
    00144
    00145     return this;
    00146 }
    00147
    00148 gzfilebuf*
    00149 gzfilebuf::close()
    00150 {
    00151     // cout << "closing ..." ;
    00152     if ( is_open() )
    00153     {
    00154         sync();
    00155         gzclose( file );
    00156         file = NULL;
    00157         // cout << "done" << endl;
    00158     } else {
    00159         // cout << "error" << endl;
    00160     }
    00161
    00162     return this;
    00163 }
    00164
    00165 // int
    00166 // gzfilebuf::setcompressionlevel( int comp_level )
    00167 // {
    00168 //     return gzsetparams(file, comp_level, -2);
    00169 // }
    00170
    00171 // int
    00172 // gzfilebuf::setcompressionstrategy( int comp_strategy )
    00173 // {
    00174 //     return gzsetparams(file, -2, comp_strategy);
    00175 // }
    00176
    00177
    00178 streampos
    00179 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
    00180 {
    00181     return streampos(EOF);
    00182 }
    00183
    00184 gzfilebuf::int_type
    00185 gzfilebuf::overflow( int_type )
    00186 {
    00187 #if 0
    00188     if ( !is_open() || !(mode & ios::out) )
    00189         return EOF;
    00190
    00191     if ( !base() )
    00192     {
    00193         if ( allocate() == EOF )
    00194             return EOF;
    00195         setg(0,0,0);
    00196     }
    00197     else
    00198     {
    00199         if (in_avail())
    00200         {
    00201             return EOF;
    00202         }
    00203
    00204         if (out_waiting())
    00205         {
    00206             if (flushbuf() == EOF)
    00207                 return EOF;
    00208         }
    00209     }
    00210
    00211     int bl = blen();
    00212     setp( base(), base() + bl);
    00213
    00214     if ( c != EOF )
    00215     {
    00216         *pptr() = c;
    00217         pbump(1);
    00218     }
    00219 #endif
    00220     return 0;
    00221 }
    00222
    00223 int
    00224 gzfilebuf::sync()
    00225 {
    00226     if ( !is_open() )
    00227         return EOF;
    00228
    00229     if ( pptr() != 0 && pptr() > pbase() )
    00230         return flushbuf();
    00231
    00232     return 0;
    00233 }
    00234
    00235 gzfilebuf::int_type
    00236 gzfilebuf::flushbuf()
    00237 {
    00238     char* q = pbase();
    00239     int n = pptr() - q;
    00240
    00241     if ( gzwrite( file, q, n) < n )
    00242         return traits_type::eof();
    00243
    00244     setp(0,0);
    00245
    00246     return 0;
    00247 }
    00248
    00249 gzfilebuf::int_type
    00250 gzfilebuf::underflow()
    00251 {
    00252 //     cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
    00253     // Error if the file not open for reading.
    00254     if ( !is_open() || !(mode & ios_in) )
    00255         return traits_type::eof();
    00256
    00257     // If the input buffer is empty then try to fill it.
    00258     if ( gptr() != 0 && gptr() < egptr() )
    00259     {
    00260         return int_type(*gptr());
    00261     }
    00262     else
    00263     {
    00264         return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
    00265     }
    00266 }
    00267
    00268 //
    00269 // Load the input buffer from the underlying gz file.
    00270 // Returns number of characters read, or EOF.
    00271 //
    00272 int
    00273 gzfilebuf::fillbuf()
    00274 {
    00275     int t = gzread( file, ibuffer, ibuf_size );
    00276     if ( t <= 0)
    00277     {
    00278         // disable get area
    00279         setg(0,0,0);
    00280         return EOF;
    00281     }
    00282
    00283     // Set the input (get) pointers
    00284     setg( ibuffer, ibuffer, ibuffer+t );
    00285
    00286 //     cerr << "gzfilebuf::fillbuf():"
    00287 //       << " t=" << t
    00288 //       << ", ibuffer=" << (void*)ibuffer
    00289 //       << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
    00290
    00291     return t;
    00292 }
    00293
    00294 #if 0
    00295 gzifstream::gzifstream()
    00296     : istream(&buffer), buffer()
    00297 {
    00298     clear( ios_badbit );
    00299 }
    00300
    00301 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
    00302     : istream(&buffer), buffer()
    00303 {
    00304     this->open( name, io_mode );
    00305 }
    00306
    00307 gzifstream::gzifstream( int fd, ios_openmode io_mode )
    00308     : istream(&buffer), buffer()
    00309 {
    00310     buffer.attach( fd, io_mode );
    00311 }
    00312
    00313 gzifstream::~gzifstream()
    00314 {
    00315 }
    00316
    00317 void
    00318 gzifstream::open( const char *name, ios_openmode io_mode )
    00319 {
    00320     if ( !buffer.open( name, io_mode ) )
    00321         clear( ios_failbit | ios_badbit );
    00322     else
    00323         clear();
    00324 }
    00325
    00326 void
    00327 gzifstream::close()
    00328 {
    00329     if ( !buffer.close() )
    00330         clear( ios_failbit | ios_badbit );
    00331 }
    00332 #endif
    00333

@ 1.3 log @Oops, try again. @ text @d342 1 a342 1