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