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