zfstream.cpp

Go to the documentation of this file.
00001 
00002 #include "zfstream.h"
00003 
00004 gzfilebuf::gzfilebuf() :
00005   file(NULL),
00006   mode(0),
00007   own_file_descriptor(0)
00008 { }
00009 
00010 gzfilebuf::~gzfilebuf() {
00011 
00012   sync();
00013   if ( own_file_descriptor )
00014     close();
00015 
00016 }
00017 
00018 gzfilebuf *gzfilebuf::open( const char *name,
00019                             int io_mode ) {
00020 
00021   if ( is_open() )
00022     return NULL;
00023 
00024   char char_mode[10];
00025   char *p = char_mode;
00026 
00027   if ( io_mode & ios::in ) {
00028     mode = ios::in;
00029     *p++ = 'r';
00030   } else if ( io_mode & ios::app ) {
00031     mode = ios::app;
00032     *p++ = 'a';
00033   } else {
00034     mode = ios::out;
00035     *p++ = 'w';
00036   }
00037 
00038   if ( io_mode & ios::binary ) {
00039     mode |= ios::binary;
00040     *p++ = 'b';
00041   }
00042 
00043   // Hard code the compression level
00044   if ( io_mode & (ios::out|ios::app )) {
00045     *p++ = '9';
00046   }
00047 
00048   // Put the end-of-string indicator
00049   *p = '\0';
00050 
00051   if ( (file = gzopen(name, char_mode)) == NULL )
00052     return NULL;
00053 
00054   own_file_descriptor = 1;
00055 
00056   return this;
00057 
00058 }
00059 
00060 gzfilebuf *gzfilebuf::attach( int file_descriptor,
00061                               int io_mode ) {
00062 
00063   if ( is_open() )
00064     return NULL;
00065 
00066   char char_mode[10];
00067   char *p = char_mode;
00068 
00069   if ( io_mode & ios::in ) {
00070     mode = ios::in;
00071     *p++ = 'r';
00072   } else if ( io_mode & ios::app ) {
00073     mode = ios::app;
00074     *p++ = 'a';
00075   } else {
00076     mode = ios::out;
00077     *p++ = 'w';
00078   }
00079 
00080   if ( io_mode & ios::binary ) {
00081     mode |= ios::binary;
00082     *p++ = 'b';
00083   }
00084 
00085   // Hard code the compression level
00086   if ( io_mode & (ios::out|ios::app )) {
00087     *p++ = '9';
00088   }
00089 
00090   // Put the end-of-string indicator
00091   *p = '\0';
00092 
00093   if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
00094     return NULL;
00095 
00096   own_file_descriptor = 0;
00097 
00098   return this;
00099 
00100 }
00101 
00102 gzfilebuf *gzfilebuf::close() {
00103 
00104   if ( is_open() ) {
00105 
00106     sync();
00107     gzclose( file );
00108     file = NULL;
00109 
00110   }
00111 
00112   return this;
00113 
00114 }
00115 
00116 int gzfilebuf::setcompressionlevel( int comp_level ) {
00117 
00118   return gzsetparams(file, comp_level, -2);
00119 
00120 }
00121 
00122 int gzfilebuf::setcompressionstrategy( int comp_strategy ) {
00123 
00124   return gzsetparams(file, -2, comp_strategy);
00125 
00126 }
00127 
00128 
00129 streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
00130 
00131   return streampos(EOF);
00132 
00133 }
00134 
00135 int gzfilebuf::underflow() {
00136 
00137   // If the file hasn't been opened for reading, error.
00138   if ( !is_open() || !(mode & ios::in) )
00139     return EOF;
00140 
00141   // if a buffer doesn't exists, allocate one.
00142   if ( !base() ) {
00143 
00144     if ( (allocate()) == EOF )
00145       return EOF;
00146     setp(0,0);
00147 
00148   } else {
00149 
00150     if ( in_avail() )
00151       return (unsigned char) *gptr();
00152 
00153     if ( out_waiting() ) {
00154       if ( flushbuf() == EOF )
00155         return EOF;
00156     }
00157 
00158   }
00159 
00160   // Attempt to fill the buffer.
00161 
00162   int result = fillbuf();
00163   if ( result == EOF ) {
00164     // disable get area
00165     setg(0,0,0);
00166     return EOF;
00167   }
00168 
00169   return (unsigned char) *gptr();
00170 
00171 }
00172 
00173 int gzfilebuf::overflow( int c ) {
00174 
00175   if ( !is_open() || !(mode & ios::out) )
00176     return EOF;
00177 
00178   if ( !base() ) {
00179     if ( allocate() == EOF )
00180       return EOF;
00181     setg(0,0,0);
00182   } else {
00183     if (in_avail()) {
00184         return EOF;
00185     }
00186     if (out_waiting()) {
00187       if (flushbuf() == EOF)
00188         return EOF;
00189     }
00190   }
00191 
00192   int bl = blen();
00193   setp( base(), base() + bl);
00194 
00195   if ( c != EOF ) {
00196 
00197     *pptr() = c;
00198     pbump(1);
00199 
00200   }
00201 
00202   return 0;
00203 
00204 }
00205 
00206 int gzfilebuf::sync() {
00207 
00208   if ( !is_open() )
00209     return EOF;
00210 
00211   if ( out_waiting() )
00212     return flushbuf();
00213 
00214   return 0;
00215 
00216 }
00217 
00218 int gzfilebuf::flushbuf() {
00219 
00220   int n;
00221   char *q;
00222 
00223   q = pbase();
00224   n = pptr() - q;
00225 
00226   if ( gzwrite( file, q, n) < n )
00227     return EOF;
00228 
00229   setp(0,0);
00230 
00231   return 0;
00232 
00233 }
00234 
00235 int gzfilebuf::fillbuf() {
00236 
00237   int required;
00238   char *p;
00239 
00240   p = base();
00241 
00242   required = blen();
00243 
00244   int t = gzread( file, p, required );
00245 
00246   if ( t <= 0) return EOF;
00247 
00248   setg( base(), base(), base()+t);
00249 
00250   return t;
00251 
00252 }
00253 
00254 gzfilestream_common::gzfilestream_common() :
00255   ios( gzfilestream_common::rdbuf() )
00256 { }
00257 
00258 gzfilestream_common::~gzfilestream_common()
00259 { }
00260 
00261 void gzfilestream_common::attach( int fd, int io_mode ) {
00262 
00263   if ( !buffer.attach( fd, io_mode) )
00264     clear( ios::failbit | ios::badbit );
00265   else
00266     clear();
00267 
00268 }
00269 
00270 void gzfilestream_common::open( const char *name, int io_mode ) {
00271 
00272   if ( !buffer.open( name, io_mode ) )
00273     clear( ios::failbit | ios::badbit );
00274   else
00275     clear();
00276 
00277 }
00278 
00279 void gzfilestream_common::close() {
00280 
00281   if ( !buffer.close() )
00282     clear( ios::failbit | ios::badbit );
00283 
00284 }
00285 
00286 gzfilebuf *gzfilestream_common::rdbuf()
00287 {
00288   return &buffer;
00289 }
00290 
00291 gzifstream::gzifstream() :
00292   ios( gzfilestream_common::rdbuf() )
00293 {
00294   clear( ios::badbit );
00295 }
00296 
00297 gzifstream::gzifstream( const char *name, int io_mode ) :
00298   ios( gzfilestream_common::rdbuf() )
00299 {
00300   gzfilestream_common::open( name, io_mode );
00301 }
00302 
00303 gzifstream::gzifstream( int fd, int io_mode ) :
00304   ios( gzfilestream_common::rdbuf() )
00305 {
00306   gzfilestream_common::attach( fd, io_mode );
00307 }
00308 
00309 gzifstream::~gzifstream() { }
00310 
00311 gzofstream::gzofstream() :
00312   ios( gzfilestream_common::rdbuf() )
00313 {
00314   clear( ios::badbit );
00315 }
00316 
00317 gzofstream::gzofstream( const char *name, int io_mode ) :
00318   ios( gzfilestream_common::rdbuf() )
00319 {
00320   gzfilestream_common::open( name, io_mode );
00321 }
00322 
00323 gzofstream::gzofstream( int fd, int io_mode ) :
00324   ios( gzfilestream_common::rdbuf() )
00325 {
00326   gzfilestream_common::attach( fd, io_mode );
00327 }
00328 
00329 gzofstream::~gzofstream() { }

Generated on Fri Apr 14 22:57:28 2006 for minix by  doxygen 1.4.6