MeteoIODoc 20260726.8efb7964
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
GZstream.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2026 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef GZSTREAM_H
21#define GZSTREAM_H
22
23#include <iostream>
24#include <string>
25#include <memory>
26#include <zlib.h>
27
28namespace mio {
29
34class GZstreambuf : public std::streambuf {
35public:
37 ~GZstreambuf() override;
38
45 bool open(const std::string& filename, const char* mode);
46
50 void close();
51
55 bool is_open() const;
56
60 std::streampos tell() const;
61
62private:
63 gzFile file; // The gzipped file handle
64 static const int buffer_size = 1024*64; // Buffer size - compromise between size and speed
65 char* buffer;
66 bool opened; // Whether the file is currently open
67
68 // Prevent copying
69 GZstreambuf(const GZstreambuf&) = delete;
70 GZstreambuf& operator=(const GZstreambuf&) = delete;
71
72protected:
73 // Override virtual functions from std::streambuf
74 virtual int_type underflow() override;
75 virtual int_type pbackfail(int_type c) override;
76 virtual std::streamsize showmanyc() override;
77 virtual std::streamsize xsgetn(char_type* s, std::streamsize n) override;
78 virtual int_type overflow(int_type c) override;
79 virtual int sync() override;
80 virtual std::streamsize xsputn(const char_type* s, std::streamsize n) override;
81};
82
87class IGZstream : public std::istream {
88public:
89 IGZstream();
90 ~IGZstream() override;
91
96 explicit IGZstream(const std::string& filename);
97
102 void open(const std::string& filename);
103
104 void close();
105
110 bool is_open() const { return buf && buf->is_open(); }
111
112private:
113 std::unique_ptr<GZstreambuf> buf; // The stream buffer
114};
115
121class OGZstream : public std::ostream {
122public:
123 OGZstream();
124 ~OGZstream() override;
125
130 explicit OGZstream(const std::string& filename);
131
136 void open(const std::string& filename);
137
138 void close();
139
144 bool is_open() const { return buf && buf->is_open(); }
145
146private:
147 std::unique_ptr<GZstreambuf> buf; // The stream buffer
148};
149
150} // namespace mio
151
152#endif // GZSTREAM_H
A stream buffer for compressed (gzipped) file I/O using zlib with compression level 9.
Definition GZstream.h:34
bool open(const std::string &filename, const char *mode)
Open a gzipped file.
Definition GZstream.cc:40
virtual std::streamsize showmanyc() override
Definition GZstream.cc:142
virtual int sync() override
Definition GZstream.cc:181
virtual int_type overflow(int_type c) override
Definition GZstream.cc:157
virtual std::streamsize xsputn(const char_type *s, std::streamsize n) override
Definition GZstream.cc:198
void close()
Close the file.
Definition GZstream.cc:60
std::streampos tell() const
Get the current position.
Definition GZstream.cc:84
virtual int_type pbackfail(int_type c) override
Definition GZstream.cc:106
bool is_open() const
Check if the stream is open.
Definition GZstream.cc:79
~GZstreambuf() override
Definition GZstream.cc:34
virtual std::streamsize xsgetn(char_type *s, std::streamsize n) override
Definition GZstream.cc:148
virtual int_type underflow() override
Definition GZstream.cc:91
GZstreambuf()
Definition GZstream.cc:27
A class that provides gzipped input stream functionality using zlib.
Definition GZstream.h:87
~IGZstream() override
Definition GZstream.cc:219
IGZstream()
Definition GZstream.cc:211
void close()
Definition GZstream.cc:237
bool is_open() const
Check if the stream is open.
Definition GZstream.h:110
void open(const std::string &filename)
Open a gzipped file for reading.
Definition GZstream.cc:224
A class that provides gzipped output stream functionality using zlib. Uses compression level 9 (best ...
Definition GZstream.h:121
void open(const std::string &filename)
Open a gzipped file for writing.
Definition GZstream.cc:263
void close()
Definition GZstream.cc:279
OGZstream()
Definition GZstream.cc:250
~OGZstream() override
Definition GZstream.cc:258
bool is_open() const
Check if the stream is open.
Definition GZstream.h:144
Definition Config.cc:34