A toolkit for working with phylogenetic data.
v0.24.0
gzip_stream.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_GZIP_STREAM_H_
2 #define GENESIS_UTILS_IO_GZIP_STREAM_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
27 /*
28  The code in this file as well as the according header file lib/utils/io/gzip_stream.hpp are
29  adapted from the excellent zstr library (C++ header-only ZLib wrapper" classes) by Matei David,
30  see https://github.com/mateidavid/zstr
31 
32  We adapted the original code by renaming all classes and variables to our standards,
33  moving much of the implementation into a source file (so that the header does not clutter
34  its callers with zlib-internal symbols), and refining some functionality.
35 
36  For this and the according source file, we need to include the following original license:
37 
38  The MIT License (MIT)
39 
40  Copyright (c) 2015 Matei David, Ontario Institute for Cancer Research
41 
42  Permission is hereby granted, free of charge, to any person obtaining a copy
43  of this software and associated documentation files (the "Software"), to deal
44  in the Software without restriction, including without limitation the rights
45  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
46  copies of the Software, and to permit persons to whom the Software is
47  furnished to do so, subject to the following conditions:
48 
49  The above copyright notice and this permission notice shall be included in all
50  copies or substantial portions of the Software.
51 
52  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
53  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
55  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
57  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
58  SOFTWARE.
59  */
60 
69 
71 
72 #include <cassert>
73 #include <fstream>
74 #include <sstream>
75 #include <string>
76 
77 namespace genesis {
78 namespace utils {
79 
80 // ================================================================================================
81 // Gzip Settings
82 // ================================================================================================
83 
100 enum class GzipCompressionLevel : int
101 {
102  kDefaultCompression = -1,
103  kNoCompression = 0,
104  kBestSpeed = 1,
105  kBestCompression = 9
106 };
107 
116 static const std::size_t GZIP_DEFAULT_BUFFER_SIZE = (std::size_t) 1 << 20;
117 
118 // ================================================================================================
119 // Gzip Input Stream
120 // ================================================================================================
121 
149  : public std::istream
150 {
151 public:
152 
153  GzipIStream(
154  std::istream& is,
155  bool auto_detect = true,
156  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
157  );
158 
159  explicit GzipIStream(
160  std::streambuf * sbuf_p,
161  bool auto_detect = true,
162  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
163  );
164 
165  virtual ~GzipIStream();
166 };
167 
168 // ================================================================================================
169 // Gzip Output Stream
170 // ================================================================================================
171 
194  : public std::ostream
195 {
196 public:
197 
198  GzipOStream(
199  std::ostream& os,
201  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
202  );
203 
204  explicit GzipOStream(
205  std::streambuf* sbuf_p,
207  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
208  );
209 
210  virtual ~GzipOStream();
211 };
212 
213 // ================================================================================================
214 // Strict File Stream Holder
215 // ================================================================================================
216 
232 template < typename FStreamType >
234 {
235  StrictFStreamHolder( std::string const& filename, std::ios_base::openmode mode = std::ios_base::in )
236  : file_stream_(filename, mode)
237  {}
238 
239  FStreamType file_stream_;
240 };
241 
242 // ================================================================================================
243 // Gzip Input File Stream
244 // ================================================================================================
245 
277  : private StrictFStreamHolder<StrictIFStream>
278  , public std::istream
279 {
280 public:
281 
282  explicit GzipIFStream(
283  std::string const& filename,
284  std::ios_base::openmode mode = std::ios_base::in,
285  bool auto_detect = true,
286  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
287  );
288 
289  virtual ~GzipIFStream();
290 };
291 
292 // ================================================================================================
293 // Gzip Output File Stream
294 // ================================================================================================
295 
314  : private StrictFStreamHolder<StrictOFStream>
315  , public std::ostream
316 {
317 public:
318 
319  explicit GzipOFStream(
320  std::string const& filename,
321  std::ios_base::openmode mode = std::ios_base::out,
323  std::size_t buffer_size = GZIP_DEFAULT_BUFFER_SIZE
324  );
325 
326  virtual ~GzipOFStream();
327 
331  GzipOFStream& flush();
332 };
333 
334 } // namespace utils
335 } // namespace genesis
336 
337 #endif // include guard
Output stream that offers on-the-fly gzip-compression.
GzipCompressionLevel
List of possible compression levels used for GzipOStream.
Helper class template for managing the construction order between stream classes. ...
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
static const std::size_t GZIP_DEFAULT_BUFFER_SIZE
Default buffer size for all gzip (de)compression buffers.
Input file stream that offers on-the-fly gzip-decompression if needed.
Input stream that offers on-the-fly gzip-decompression if needed.
Out file stream that offers on-the-fly gzip-compression.
StrictFStreamHolder(std::string const &filename, std::ios_base::openmode mode=std::ios_base::in)