fix8  version 1.4.0
Open Source C++ FIX Framework
gzstream.cpp
Go to the documentation of this file.
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.7
22 // Revision_date : Date: 2003/01/08 14:41:27
23 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
24 //
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
28 //
29 // DD: 05/03/2012 - changes to improve portability; ios:out append enabled;
30 //
31 
32 #include "precomp.hpp"
33 #include <fix8/f8config.h>
34 #ifdef FIX8_HAVE_ZLIB_H
35 
36 #include <fix8/gzstream.hpp>
37 
38 #ifdef GZSTREAM_NAMESPACE
39 namespace GZSTREAM_NAMESPACE {
40 #endif
41 
42 // ----------------------------------------------------------------------------
43 // Internal classes to implement gzstream. See header file for user classes.
44 // ----------------------------------------------------------------------------
45 
46 // --------------------------------------
47 // class gzstreambuf:
48 // --------------------------------------
49 
50 gzstreambuf* gzstreambuf::open(const char* name, int open_mode)
51 {
52  if (is_open())
53  return nullptr;
54  mode = open_mode;
55  // no input append nor read/write mode
56  if ((mode & std::ios::ate) || ((mode & std::ios::in) && (mode & std::ios::out)))
57  return nullptr;
58  char fmode[10], *fmodeptr = fmode;
59  if (mode & std::ios::in)
60  *fmodeptr++ = 'r';
61  else if (mode & std::ios::app)
62  *fmodeptr++ = 'a';
63  else if (mode & std::ios::out)
64  *fmodeptr++ = 'w';
65  *fmodeptr++ = 'b';
66  *fmodeptr = 0;
67  if ((file = gzopen( name, fmode)) == 0)
68  return nullptr;
69  opened = 1;
70  return this;
71 }
72 
73 gzstreambuf *gzstreambuf::close()
74 {
75  if (is_open())
76  {
77  sync();
78  opened = 0;
79  if (gzclose( file) == Z_OK)
80  return this;
81  }
82  return nullptr;
83 }
84 
85 int gzstreambuf::underflow()
86 { // used for input buffer only
87  if (gptr() && (gptr() < egptr()))
88  return * reinterpret_cast<unsigned char *>(gptr());
89 
90  if (! (mode & std::ios::in) || !opened)
91  return EOF;
92  // Josuttis' implementation of inbuf
93  int n_putback = gptr() - eback();
94  if (n_putback > 4)
95  n_putback = 4;
96  memcpy(buffer + (4 - n_putback), gptr() - n_putback, n_putback);
97 
98  int num = gzread( file, buffer+4, bufferSize-4);
99  if (num <= 0) // ERROR or EOF
100  return EOF;
101 
102  // reset buffer pointers
103  setg( buffer + (4 - n_putback), // beginning of putback area
104  buffer + 4, // read position
105  buffer + 4 + num); // end of buffer
106 
107  // return next character
108  return * reinterpret_cast<unsigned char *>( gptr());
109 }
110 
111 int gzstreambuf::flush_buffer()
112 {
113  // Separate the writing of the buffer from overflow() and
114  // sync() operation.
115  int w = pptr() - pbase();
116  if (gzwrite( file, pbase(), w) != w)
117  return EOF;
118  pbump( -w);
119  return w;
120 }
121 
122 int gzstreambuf::overflow(int c)
123 { // used for output buffer only
124  if ( !(mode & std::ios::out) || !opened)
125  return EOF;
126  if (c != EOF)
127  {
128  *pptr() = c;
129  pbump(1);
130  }
131  if (flush_buffer() == EOF)
132  return EOF;
133  return c;
134 }
135 
136 int gzstreambuf::sync()
137 {
138  // Changed to use flush_buffer() instead of overflow( EOF)
139  // which caused improper behavior with std::endl and flush(),
140  // bug reported by Vincent Ricard.
141  if (pptr() && pptr() > pbase())
142  {
143  if (flush_buffer() == EOF)
144  return -1;
145  }
146  return 0;
147 }
148 
149 // --------------------------------------
150 // class gzstreambase:
151 // --------------------------------------
152 
153 gzstreambase::gzstreambase(const char* name, int mode)
154 {
155  init( &buf);
156  open( name, mode);
157 }
158 
159 gzstreambase::~gzstreambase()
160 {
161  buf.close();
162 }
163 
164 void gzstreambase::open(const char* name, int open_mode)
165 {
166  if (!buf.open( name, open_mode))
167  clear(rdstate() | std::ios::badbit);
168 // else
169 // setstate(rdstate() | std::ios::badbit);
170 }
171 
172 void gzstreambase::close()
173 {
174  if (buf.is_open())
175  if (! buf.close())
176  clear( rdstate() | std::ios::badbit);
177 }
178 
179 #ifdef GZSTREAM_NAMESPACE
180 } // namespace GZSTREAM_NAMESPACE
181 #endif
182 #endif // FIX8_HAVE_ZLIB_H
183 
184 // ============================================================================
185 // EOF //