bzstream.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // ============================================================================
  2. // bzstream, 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 : bzstream.C
  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. #include <core/basics/bzstream.h>
  29. #include "CrossplatformDefines.h"
  30. #ifdef NICE_USELIB_BZLIB
  31. #include <iostream>
  32. #include <string.h> // for memcpy
  33. namespace NICE {
  34. // ----------------------------------------------------------------------------
  35. // Internal classes to implement bzstream. See header file for user classes.
  36. // ----------------------------------------------------------------------------
  37. // --------------------------------------
  38. // class bzstreambuf:
  39. // --------------------------------------
  40. bzstreambuf* bzstreambuf::open( const char* name, int open_mode) {
  41. if ( is_open())
  42. return (bzstreambuf*)0;
  43. mode = open_mode;
  44. // no append nor read/write mode
  45. if ((mode & std::ios::ate) || (mode & std::ios::app)
  46. || ((mode & std::ios::in) && (mode & std::ios::out)))
  47. return (bzstreambuf*)0;
  48. char fmode[10];
  49. char* fmodeptr = fmode;
  50. if ( mode & std::ios::in)
  51. *fmodeptr++ = 'r';
  52. else if ( mode & std::ios::out)
  53. *fmodeptr++ = 'w';
  54. *fmodeptr++ = 'b';
  55. *fmodeptr = '\0';
  56. file = 0;
  57. file = BZ2_bzopen( name, fmode);
  58. if (file == 0)
  59. return (bzstreambuf*)0;
  60. opened = 1;
  61. return this;
  62. }
  63. bzstreambuf * bzstreambuf::close() {
  64. if ( is_open()) {
  65. sync();
  66. opened = 0;
  67. BZ2_bzclose(file);
  68. return this;
  69. }
  70. return (bzstreambuf*)0;
  71. }
  72. int bzstreambuf::underflow() { // used for input buffer only
  73. if ( gptr() && ( gptr() < egptr()))
  74. return * reinterpret_cast<unsigned char *>( gptr());
  75. if ( ! (mode & std::ios::in) || ! opened)
  76. return EOF;
  77. // Josuttis' implementation of inbuf
  78. int n_putback = gptr() - eback();
  79. if ( n_putback > 4)
  80. n_putback = 4;
  81. memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
  82. int num = 0;
  83. num = BZ2_bzread( file, buffer+4, bufferSize-4);
  84. if (num <= 0) // ERROR or EOF
  85. return EOF;
  86. // reset buffer pointers
  87. setg( buffer + (4 - n_putback), // beginning of putback area
  88. buffer + 4, // read position
  89. buffer + 4 + num); // end of buffer
  90. // return next character
  91. return * reinterpret_cast<unsigned char *>( gptr());
  92. }
  93. int bzstreambuf::flush_buffer() {
  94. // Separate the writing of the buffer from overflow() and
  95. // sync() operation.
  96. int w = pptr() - pbase();
  97. if ( BZ2_bzwrite( file, pbase(), w) != w)
  98. return EOF;
  99. pbump( -w);
  100. return w;
  101. }
  102. int bzstreambuf::overflow( int c) { // used for output buffer only
  103. if ( ! ( mode & std::ios::out) || ! opened)
  104. return EOF;
  105. if (c != EOF) {
  106. *pptr() = c;
  107. pbump(1);
  108. }
  109. if ( flush_buffer() == EOF)
  110. return EOF;
  111. return c;
  112. }
  113. int bzstreambuf::sync() {
  114. // Changed to use flush_buffer() instead of overflow( EOF)
  115. // which caused improper behavior with std::endl and flush(),
  116. // bug reported by Vincent Ricard.
  117. if ( pptr() && pptr() > pbase()) {
  118. if ( flush_buffer() == EOF)
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. // --------------------------------------
  124. // class bzstreambase:
  125. // --------------------------------------
  126. bzstreambase::bzstreambase( const char* name, int mode) {
  127. init( &buf);
  128. open( name, mode);
  129. }
  130. bzstreambase::~bzstreambase() {
  131. buf.close();
  132. }
  133. void bzstreambase::open( const char* name, int open_mode) {
  134. if ( ! buf.open( name, open_mode))
  135. clear( rdstate() | std::ios::badbit);
  136. }
  137. void bzstreambase::close() {
  138. if ( buf.is_open())
  139. if ( ! buf.close())
  140. clear( rdstate() | std::ios::badbit);
  141. }
  142. } // namespace
  143. #else
  144. #pragma message NICE_WARNING("bzstream will not be compiled.")
  145. #endif
  146. // ============================================================================
  147. // EOF //