gzfstream.h

説明を見る。
00001 // -*-c++-*-
00002 
00008 /*
00009  *Copyright:
00010 
00011  Copyright (C) Hidehisa Akiyama
00012 
00013  This code is free software; you can redistribute it and/or
00014  modify it under the terms of the GNU Lesser General Public
00015  License as published by the Free Software Foundation; either
00016  version 2.1 of the License, or (at your option) any later version.
00017 
00018  This library is distributed in the hope that it will be useful,
00019  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  Lesser General Public License for more details.
00022 
00023  You should have received a copy of the GNU Lesser General Public
00024  License along with this library; if not, write to the Free Software
00025  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027  *EndCopyright:
00028  */
00029 
00031 
00032 #ifndef RCSC_GZ_GZFSTREAM_H
00033 #define RCSC_GZ_GZFSTREAM_H
00034 
00035 #include <boost/scoped_ptr.hpp>
00036 
00037 #include <iostream>
00038 #include <string>
00039 
00040 namespace rcsc {
00041 
00042 struct gzfilebuf_impl;
00043 
00053 class gzfilebuf
00054     : public std::streambuf {
00055 public:
00065     enum CompressionLevel {
00066         DEFAULT_COMPRESSION = -1,
00067         NO_COMPRESSION = 0,
00068         BEST_SPEED = 1,
00069         BEST_COMPRESSION = 9,
00070     };
00071 
00083     enum Strategy {
00084         DEFAULT_STRATEGY = 0,
00085         FILTERED = 1,
00086         HUFFMAN_ONLY = 2,
00087         RLE = 3,
00088     };
00089 
00090 private:
00091 
00093     boost::scoped_ptr< gzfilebuf_impl > M_impl;
00094 
00096     std::size_t M_buf_size;
00098     char_type * M_buf;
00099 
00101     int M_remained_size;
00103     char_type M_remained_char;
00104 
00105 
00107     gzfilebuf( const gzfilebuf & );
00109     gzfilebuf & operator=( const gzfilebuf & );
00110 
00111 public:
00118     gzfilebuf();
00119 
00126     virtual
00127     ~gzfilebuf();
00128 
00133     bool is_open();
00134 
00149     gzfilebuf * open( const char * path,
00150                       std::ios_base::openmode mode,
00151                       int level = DEFAULT_COMPRESSION,
00152                       int strategy = DEFAULT_STRATEGY );
00153 
00158     gzfilebuf * close() throw();
00159 
00160 
00161 private:
00162 
00169     bool flushBuf();
00170 
00181     std::string makeModeString( std::ios_base::openmode mode,
00182                                 int level,
00183                                 int strategy );
00184 
00191     void destroyInternalBuffer() throw();
00192 
00193 protected:
00194     //virtual
00195     //void imbue( const locale& loc );
00196     //virtual
00197     //int_type pbackfail( int_type c );
00198     //virtual
00199     //std::streambuf* setbuf( char_type* p, std::streamsize n );
00200 
00213     virtual
00214     std::streampos seekoff( std::streamoff off,
00215                             std::ios_base::seekdir way,
00216                             std::ios_base::openmode mode );
00217 
00225     virtual
00226     std::streampos seekpos( std::streampos pos,
00227                             std::ios_base::openmode mode );
00228 
00234     virtual
00235     std::streamsize showmanyc();
00236 
00242     virtual
00243     int sync();
00244 
00250     virtual
00251     std::streambuf::int_type overflow( std::streambuf::int_type c );
00252 
00253     // Not need to override because super class works sufficiently.
00254     //virtual
00255     //int_type uflow();
00256 
00261     virtual
00262     std::streambuf::int_type underflow();
00263 
00264     // Not need to override because super class works sufficiently.
00265     //virtual
00266     //std::streamsize xsgetn( char_type* s, std::streamsize n );
00267 
00268     // Not need to override because super class works sufficiently.
00269     //virtual
00270     //std::streamsize xsputn( char_type * s, std::streamsize n );
00271 
00272     /*   All virtual methods defined in std::streambuf.
00273          See: http://www.cplusplus.com/ref/iostream/
00274       XXX imbue Imbue locale [virtual]
00275       OK overflow Put character at current position [virtual]
00276       XXX pbackfail Put character back [virtual]
00277       N/A seekoff Set relative position of internal position pointer [virtual]
00278       OK seekpos  Set absolute position of internal position pointer [virtual]
00279       XXX setbuf  Set buffer [virtual]
00280       OK showmanyc  Get number of characters availbale in input sequence [virtual]
00281       OK sync Synchronize stream buffer [virtual]
00282       OK uflow  Get current character [virtual]
00283       OK underflow  Get current character [virtual]
00284       OK xsgetn Get some characters [virtual]
00285       OK xsputn Write some characters [virtual]
00286     */
00287 };
00288 
00289 
00290 /*-------------------------------------------------------------------*/
00291 /*-------------------------------------------------------------------*/
00292 /*-------------------------------------------------------------------*/
00293 
00294 
00302 class gzifstream
00303     : public std::istream {
00304 private:
00306     gzfilebuf M_file_buf;
00307 public:
00311     gzifstream();
00312 
00317     explicit
00318     gzifstream( const char * path );
00319 
00324     gzfilebuf * rdbuf() const
00325       {
00326           return const_cast< gzfilebuf * >( &M_file_buf );
00327       }
00328 
00334     bool is_open()
00335       {
00336           return M_file_buf.is_open();
00337       }
00338 
00350     void open( const char * path );
00351 
00357     void close();
00358 };
00359 
00360 /*****************************************************************************/
00361 
00369 class gzofstream
00370     : public std::ostream {
00371 private:
00373     gzfilebuf M_file_buf;
00374 
00375 public:
00381     gzofstream();
00382 
00391     explicit
00392     gzofstream( const char* path,
00393                 int level = gzfilebuf::DEFAULT_COMPRESSION,
00394                 int strategy = gzfilebuf::DEFAULT_STRATEGY );
00395 
00400     gzfilebuf * rdbuf() const
00401       {
00402           return const_cast< gzfilebuf * >( &M_file_buf );
00403       }
00404 
00410     bool is_open()
00411       {
00412           return M_file_buf.is_open();
00413       }
00414 
00428     void  open( const char * path,
00429                 int level = gzfilebuf::DEFAULT_COMPRESSION,
00430                 int strategy = gzfilebuf::DEFAULT_STRATEGY );
00431 
00437     void close();
00438 };
00439 
00440 } // end namespace
00441 
00442 #endif // include guard

librcscに対してThu May 1 15:41:20 2008に生成されました。  doxygen 1.5.0