00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef ZFSTREAM_H
00034 #define ZFSTREAM_H
00035
00036 #ifdef HAVE_ZLIB
00037
00038 #include <iosfwd>
00039
00040 #include "zlib.h"
00041
00042
00043
00052 class gzfilebuf : public std::streambuf
00053 {
00054 public:
00055
00056 gzfilebuf();
00057
00058
00059 virtual
00060 ~gzfilebuf();
00061
00073 int
00074 setcompression(int comp_level,
00075 int comp_strategy = Z_DEFAULT_STRATEGY);
00076
00081 bool
00082 is_open() const { return (file != 0); }
00083
00090 gzfilebuf*
00091 open(const char* name,
00092 std::ios_base::openmode mode);
00093
00100 gzfilebuf*
00101 attach(int fd,
00102 std::ios_base::openmode mode);
00103
00108 gzfilebuf*
00109 close();
00110
00111 protected:
00116 bool
00117 open_mode(std::ios_base::openmode mode,
00118 char* c_mode) const;
00119
00127 virtual std::streamsize
00128 showmanyc();
00129
00137 virtual int_type
00138 underflow();
00139
00149 virtual int_type
00150 overflow(int_type c = traits_type::eof());
00151
00160 virtual std::streambuf*
00161 setbuf(char_type* p,
00162 std::streamsize n);
00163
00170 virtual int
00171 sync();
00172
00178 virtual pos_type
00179 seekoff(off_type off, std::ios_base::seekdir way,
00180 std::ios_base::openmode mode =
00181 std::ios_base::in|std::ios_base::out);
00182
00188 virtual pos_type
00189 seekpos(pos_type sp, std::ios_base::openmode mode =
00190 std::ios_base::in|std::ios_base::out);
00191
00192 virtual int_type
00193 pbackfail (int_type c = traits_type::eof());
00194
00195
00196
00197
00198
00199
00200
00201 private:
00210 void
00211 enable_buffer();
00212
00220 void
00221 disable_buffer();
00222
00226 gzFile file;
00227
00231 std::ios_base::openmode io_mode;
00232
00239 bool own_fd;
00240
00247 char_type* buffer;
00248
00255 std::streamsize buffer_size;
00256
00263 bool own_buffer;
00264 };
00265
00266
00267
00274 class gzifstream : public std::istream
00275 {
00276 public:
00277
00278 gzifstream();
00279
00285 explicit
00286 gzifstream(const char* name,
00287 std::ios_base::openmode mode = std::ios_base::in);
00288
00294 explicit
00295 gzifstream(int fd,
00296 std::ios_base::openmode mode = std::ios_base::in);
00297
00301 gzfilebuf*
00302 rdbuf() const
00303 { return const_cast<gzfilebuf*>(&sb); }
00304
00309 bool
00310 is_open() { return sb.is_open(); }
00311
00324 void
00325 open(const char* name,
00326 std::ios_base::openmode mode = std::ios_base::in);
00327
00336 void
00337 attach(int fd,
00338 std::ios_base::openmode mode = std::ios_base::in);
00339
00345 void
00346 close();
00347
00348 private:
00352 gzfilebuf sb;
00353 };
00354
00355
00356
00363 class gzofstream : public std::ostream
00364 {
00365 public:
00366
00367 gzofstream();
00368
00374 explicit
00375 gzofstream(const char* name,
00376 std::ios_base::openmode mode = std::ios_base::out);
00377
00383 explicit
00384 gzofstream(int fd,
00385 std::ios_base::openmode mode = std::ios_base::out);
00386
00390 gzfilebuf*
00391 rdbuf() const
00392 { return const_cast<gzfilebuf*>(&sb); }
00393
00398 bool
00399 is_open() { return sb.is_open(); }
00400
00413 void
00414 open(const char* name,
00415 std::ios_base::openmode mode = std::ios_base::out);
00416
00425 void
00426 attach(int fd,
00427 std::ios_base::openmode mode = std::ios_base::out);
00428
00434 void
00435 close();
00436
00437 private:
00441 gzfilebuf sb;
00442 };
00443
00444
00445
00452 template<typename T1, typename T2>
00453 class gzomanip2
00454 {
00455 public:
00456
00457 template <typename Ta, typename Tb>
00458 friend gzofstream&
00459 operator<<(gzofstream&,
00460 const gzomanip2<Ta,Tb>&);
00461
00462
00463 gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
00464 T1 v1,
00465 T2 v2);
00466 private:
00467
00468 gzofstream&
00469 (*func)(gzofstream&, T1, T2);
00470
00471
00472 T1 val1;
00473 T2 val2;
00474 };
00475
00476
00477
00478
00479 inline gzofstream&
00480 setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
00481 {
00482 (gzs.rdbuf())->setcompression(l, s);
00483 return gzs;
00484 }
00485
00486
00487 template<typename T1, typename T2>
00488 inline
00489 gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),
00490 T1 v1,
00491 T2 v2)
00492 : func(f), val1(v1), val2(v2)
00493 { }
00494
00495
00496 template<typename T1, typename T2>
00497 inline gzofstream&
00498 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
00499 { return (*m.func)(s, m.val1, m.val2); }
00500
00501
00502 inline gzomanip2<int,int>
00503 setcompression(int l, int s = Z_DEFAULT_STRATEGY)
00504 { return gzomanip2<int,int>(&setcompression, l, s); }
00505
00506 #endif // HAVE_ZLIB
00507
00508 #endif // ZFSTREAM_H
00509
00510
00511
00512
00513
00514
00515