bzstream.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ============================================================================
  2. // bzstream, C++ iostream classes wrapping the zlib compression library.
  3. // Copyright (C) 2006 Frank Mattern
  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 : bzstream.h
  21. // Revision : $Revision: 1.2 $
  22. // Revision_date : $Date: 2009/05/28 11:36:30 $
  23. // Author(s) : Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern
  24. //
  25. // Standard streambuf implementation following Nicolai Josuttis, "The
  26. // Standard C++ Library".
  27. // ============================================================================
  28. #ifndef BZSTREAM_H
  29. #define BZSTREAM_H 1
  30. #ifdef NICE_USELIB_BZLIB
  31. // standard C++ with new header file names and std:: namespace
  32. #include <iostream>
  33. #include <fstream>
  34. #include <bzlib.h>
  35. namespace NICE{
  36. // ----------------------------------------------------------------------------
  37. // Internal classes to implement bzstream. See below for user classes.
  38. // ----------------------------------------------------------------------------
  39. class bzstreambuf : public std::streambuf {
  40. private:
  41. static const int bufferSize = 47+256; // size of data buff
  42. // totals 512 bytes under g++ for ibzstream at the end.
  43. BZFILE *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. bzstreambuf() : 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. bzstreambuf* open( const char* name, int open_mode);
  58. bzstreambuf* close();
  59. ~bzstreambuf() { close(); }
  60. virtual int overflow( int c = EOF);
  61. virtual int underflow();
  62. virtual int sync();
  63. };
  64. class bzstreambase : virtual public std::ios {
  65. protected:
  66. bzstreambuf buf;
  67. public:
  68. bzstreambase() { init(&buf); }
  69. bzstreambase( const char* name, int open_mode);
  70. ~bzstreambase();
  71. void open( const char* name, int open_mode);
  72. void close();
  73. bzstreambuf* rdbuf() { return &buf; }
  74. };
  75. // ----------------------------------------------------------------------------
  76. // User classes. Use ibzstream and obzstream analogously to ifstream and
  77. // ofstream respectively. They read and write files based on the bz*
  78. // function interface of the zlib. Files are compatible with bzip compression.
  79. // ----------------------------------------------------------------------------
  80. /**
  81. * Input bzip stream class.
  82. * Supports reading of bzip files.
  83. *
  84. * @author Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern
  85. */
  86. class ibzstream : public bzstreambase, public std::istream {
  87. public:
  88. ibzstream() : std::istream( &buf) {}
  89. ibzstream( const char* name, int open_mode = std::ios::in)
  90. : bzstreambase( name, open_mode), std::istream( &buf) {}
  91. bzstreambuf* rdbuf() { return bzstreambase::rdbuf(); }
  92. void open( const char* name, int open_mode = std::ios::in) {
  93. bzstreambase::open( name, open_mode);
  94. }
  95. };
  96. /**
  97. * Output bzip stream class.
  98. * Supports writing of bzip files.
  99. *
  100. * @author Deepak Bandyopadhyay, Lutz Kettner, Frank Mattern
  101. */
  102. class obzstream : public bzstreambase, public std::ostream {
  103. public:
  104. obzstream() : std::ostream( &buf) {}
  105. obzstream( const char* name, int mode = std::ios::out)
  106. : bzstreambase( name, mode), std::ostream( &buf) {}
  107. bzstreambuf* rdbuf() { return bzstreambase::rdbuf(); }
  108. void open( const char* name, int open_mode = std::ios::out) {
  109. bzstreambase::open( name, open_mode);
  110. }
  111. };
  112. } // namespace
  113. #endif
  114. #endif // BZSTREAM_H
  115. // ============================================================================
  116. // EOF //