gzstream.cpp 5.0 KB

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