zfstream.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef zfstream_h
  2. #define zfstream_h
  3. #include <fstream.h>
  4. #include "zlib.h"
  5. class gzfilebuf : public streambuf {
  6. public:
  7. gzfilebuf( );
  8. virtual ~gzfilebuf();
  9. gzfilebuf *open( const char *name, int io_mode );
  10. gzfilebuf *attach( int file_descriptor, int io_mode );
  11. gzfilebuf *close();
  12. int setcompressionlevel( int comp_level );
  13. int setcompressionstrategy( int comp_strategy );
  14. inline int is_open() const { return (file !=NULL); }
  15. virtual streampos seekoff( streamoff, ios::seek_dir, int );
  16. virtual int sync();
  17. protected:
  18. virtual int underflow();
  19. virtual int overflow( int = EOF );
  20. private:
  21. gzFile file;
  22. short mode;
  23. short own_file_descriptor;
  24. int flushbuf();
  25. int fillbuf();
  26. };
  27. class gzfilestream_common : virtual public ios {
  28. friend class gzifstream;
  29. friend class gzofstream;
  30. friend gzofstream &setcompressionlevel( gzofstream &, int );
  31. friend gzofstream &setcompressionstrategy( gzofstream &, int );
  32. public:
  33. virtual ~gzfilestream_common();
  34. void attach( int fd, int io_mode );
  35. void open( const char *name, int io_mode );
  36. void close();
  37. protected:
  38. gzfilestream_common();
  39. private:
  40. gzfilebuf *rdbuf();
  41. gzfilebuf buffer;
  42. };
  43. class gzifstream : public gzfilestream_common, public istream {
  44. public:
  45. gzifstream();
  46. gzifstream( const char *name, int io_mode = ios::in );
  47. gzifstream( int fd, int io_mode = ios::in );
  48. virtual ~gzifstream();
  49. };
  50. class gzofstream : public gzfilestream_common, public ostream {
  51. public:
  52. gzofstream();
  53. gzofstream( const char *name, int io_mode = ios::out );
  54. gzofstream( int fd, int io_mode = ios::out );
  55. virtual ~gzofstream();
  56. };
  57. template<class T> class gzomanip {
  58. friend gzofstream &operator<<(gzofstream &, const gzomanip<T> &);
  59. public:
  60. gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { }
  61. private:
  62. gzofstream &(*func)(gzofstream &, T);
  63. T val;
  64. };
  65. template<class T> gzofstream &operator<<(gzofstream &s, const gzomanip<T> &m)
  66. {
  67. return (*m.func)(s, m.val);
  68. }
  69. inline gzofstream &setcompressionlevel( gzofstream &s, int l )
  70. {
  71. (s.rdbuf())->setcompressionlevel(l);
  72. return s;
  73. }
  74. inline gzofstream &setcompressionstrategy( gzofstream &s, int l )
  75. {
  76. (s.rdbuf())->setcompressionstrategy(l);
  77. return s;
  78. }
  79. inline gzomanip<int> setcompressionlevel(int l)
  80. {
  81. return gzomanip<int>(&setcompressionlevel,l);
  82. }
  83. inline gzomanip<int> setcompressionstrategy(int l)
  84. {
  85. return gzomanip<int>(&setcompressionstrategy,l);
  86. }
  87. #endif