00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00195
00196
00197
00198
00199
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
00254
00255
00256
00261 virtual
00262 std::streambuf::int_type underflow();
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
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 }
00441
00442 #endif // include guard