gzstream.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ============================================================================
  2. // gzstream, C++ iostream classes wrapping the zlib compression library.
  3. // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. // ============================================================================
  19. //
  20. // File : gzstream.h
  21. // Revision : $Revision: 1.2 $
  22. // Revision_date : $Date: 2009/05/28 11:36:30 $
  23. // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
  24. //
  25. // Standard streambuf implementation following Nicolai Josuttis, "The
  26. // Standard C++ Library".
  27. // ============================================================================
  28. #ifndef GZSTREAM_H
  29. #define GZSTREAM_H 1
  30. #ifdef NICE_USELIB_ZLIB
  31. // standard C++ with new header file names and std:: namespace
  32. #include <iostream>
  33. #include <fstream>
  34. #include <zlib.h>
  35. namespace NICE{
  36. // ----------------------------------------------------------------------------
  37. // Internal classes to implement gzstream. See below for user classes.
  38. // ----------------------------------------------------------------------------
  39. class gzstreambuf : public std::streambuf {
  40. private:
  41. static const int bufferSize = 47+256; // size of data buff
  42. // totals 512 bytes under g++ for igzstream at the end.
  43. gzFile file; // file handle for compressed file
  44. char buffer[bufferSize]; // data buffer
  45. char opened; // open/close state of stream
  46. int mode; // I/O mode
  47. int flush_buffer();
  48. public:
  49. gzstreambuf() : opened(0) {
  50. setp( buffer, buffer + (bufferSize-1));
  51. setg( buffer + 4, // beginning of putback area
  52. buffer + 4, // read position
  53. buffer + 4); // end position
  54. // ASSERT: both input & output capabilities will not be used together
  55. }
  56. int is_open() { return opened; }
  57. gzstreambuf* open( const char* name, int open_mode);
  58. gzstreambuf* close();
  59. ~gzstreambuf() { close(); }
  60. virtual int overflow( int c = EOF);
  61. virtual int underflow();
  62. virtual int sync();
  63. };
  64. class gzstreambase : virtual public std::ios {
  65. protected:
  66. gzstreambuf buf;
  67. public:
  68. gzstreambase() { init(&buf); }
  69. gzstreambase( const char* name, int open_mode);
  70. ~gzstreambase();
  71. void open( const char* name, int open_mode);
  72. void close();
  73. gzstreambuf* rdbuf() { return &buf; }
  74. };
  75. // ----------------------------------------------------------------------------
  76. // User classes. Use igzstream and ogzstream analogously to ifstream and
  77. // ofstream respectively. They read and write files based on the gz*
  78. // function interface of the zlib. Files are compatible with gzip compression.
  79. // ----------------------------------------------------------------------------
  80. /**
  81. * Input gzip stream class.
  82. * Supports reading of gzip files.
  83. *
  84. * @author Deepak Bandyopadhyay, Lutz Kettner
  85. */
  86. class igzstream : public gzstreambase, public std::istream {
  87. public:
  88. igzstream() : std::istream( &buf) {}
  89. igzstream( const char* name, int open_mode = std::ios::in)
  90. : gzstreambase( name, open_mode), std::istream( &buf) {}
  91. gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
  92. void open( const char* name, int open_mode = std::ios::in) {
  93. gzstreambase::open( name, open_mode);
  94. }
  95. };
  96. /**
  97. * Output gzip stream class.
  98. * Supports writing of gzip files.
  99. *
  100. * @author Deepak Bandyopadhyay, Lutz Kettner
  101. */
  102. class ogzstream : public gzstreambase, public std::ostream {
  103. public:
  104. ogzstream() : std::ostream( &buf) {}
  105. ogzstream( const char* name, int mode = std::ios::out)
  106. : gzstreambase( name, mode), std::ostream( &buf) {}
  107. gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
  108. void open( const char* name, int open_mode = std::ios::out) {
  109. gzstreambase::open( name, open_mode);
  110. }
  111. };
  112. } // namespace
  113. #endif
  114. #endif // GZSTREAM_H
  115. // ============================================================================
  116. // EOF //