Scippy

SoPlex

Sequential object-oriented simPlex

gzstream.cpp
Go to the documentation of this file.
1 #ifdef SOPLEX_WITH_ZLIB
2 
3 // ============================================================================
4 // gzstream, C++ iostream classes wrapping the zlib compression library.
5 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // ============================================================================
21 //
22 // File : gzstream.C
23 // Revision : $Revision: 1.8 $
24 // Revision_date : $Date: 2005/12/07 18:03:25 $
25 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
26 //
27 // Standard streambuf implementation following Nicolai Josuttis, "The
28 // Standard C++ Library".
29 // ============================================================================
30 
31 #include "gzstream.h"
32 #include <iostream>
33 #include <string.h> // for memcpy
34 
35 #ifdef GZSTREAM_NAMESPACE
36 namespace GZSTREAM_NAMESPACE {
37 #endif
38 
39 // ----------------------------------------------------------------------------
40 // Internal classes to implement gzstream. See header file for user classes.
41 // ----------------------------------------------------------------------------
42 
43 // --------------------------------------
44 // class gzstreambuf:
45 // --------------------------------------
46 
47 gzstreambuf* gzstreambuf::open( const char* _name, int _open_mode) {
48  if ( is_open())
49  return 0;
50  mode = _open_mode;
51  // no append nor read/write mode
52  if ((mode & std::ios::ate) || (mode & std::ios::app)
53  || ((mode & std::ios::in) && (mode & std::ios::out)))
54  return 0;
55  char fmode[10];
56  char* fmodeptr = fmode;
57  if ( mode & std::ios::in)
58  *fmodeptr++ = 'r';
59  else if ( mode & std::ios::out)
60  *fmodeptr++ = 'w';
61  *fmodeptr++ = 'b';
62  *fmodeptr = '\0';
63  file = gzopen( _name, fmode);
64  if (file == 0)
65  return 0;
66  opened = 1;
67  return this;
68 }
69 
70 gzstreambuf * gzstreambuf::close() {
71  if ( is_open()) {
72  sync();
73  opened = 0;
74  if ( gzclose( file) == Z_OK)
75  return this;
76  }
77  return 0;
78 }
79 
80 int gzstreambuf::underflow() { // used for input buffer only
81  if ( gptr() && ( gptr() < egptr()))
82  return * reinterpret_cast<unsigned char *>( gptr());
83 
84  if ( ! (mode & std::ios::in) || ! opened)
85  return EOF;
86  // Josuttis' implementation of inbuf
87  size_t n_putback = (size_t) (gptr() - eback());
88  if ( n_putback > 4)
89  n_putback = 4;
90  memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
91 
92  int num = gzread( file, buffer+4, bufferSize-4);
93  if (num <= 0) // ERROR or EOF
94  return EOF;
95 
96  // reset buffer pointers
97  setg( buffer + (4 - n_putback), // beginning of putback area
98  buffer + 4, // read position
99  buffer + 4 + num); // end of buffer
100 
101  // return next character
102  return * reinterpret_cast<unsigned char *>( gptr());
103 }
104 
105 int gzstreambuf::flush_buffer() {
106  // Separate the writing of the buffer from overflow() and
107  // sync() operation.
108  int w = static_cast<int>(pptr() - pbase());
109  if ( gzwrite( file, pbase(), (unsigned int) w) != w)
110  return EOF;
111  pbump( -w);
112  return w;
113 }
114 
115 int gzstreambuf::overflow( int c) { // used for output buffer only
116  if ( ! ( mode & std::ios::out) || ! opened)
117  return EOF;
118  if (c != EOF) {
119  *pptr() = static_cast<char>(c);
120  pbump(1);
121  }
122  if ( flush_buffer() == EOF)
123  return EOF;
124  return c;
125 }
126 
127 int gzstreambuf::sync() {
128  // Changed to use flush_buffer() instead of overflow( EOF)
129  // which caused improper behavior with std::endl and flush(),
130  // bug reported by Vincent Ricard.
131  if ( pptr() && pptr() > pbase()) {
132  if ( flush_buffer() == EOF)
133  return -1;
134  }
135  return 0;
136 }
137 
138 // --------------------------------------
139 // class gzstreambase:
140 // --------------------------------------
141 
142 gzstreambase::gzstreambase( const char* name, int mode) {
143  init( &buf);
144  open( name, mode);
145 }
146 
147 gzstreambase::~gzstreambase() {
148  buf.close();
149 }
150 
151 void gzstreambase::open( const char* _name, int _open_mode) {
152  if ( ! buf.open( _name, _open_mode))
153  setstate(std::ios::badbit);
154  // clear( rdstate() | std::ios::badbit);
155 }
156 
157 void gzstreambase::close() {
158  if ( buf.is_open())
159  if ( ! buf.close())
160  setstate(std::ios::badbit);
161  // clear( rdstate() | std::ios::badbit);
162 }
163 
164 #ifdef GZSTREAM_NAMESPACE
165 } // namespace GZSTREAM_NAMESPACE
166 #endif
167 
168 // ============================================================================
169 // EOF //
170 
171 #endif