A library for working with phylogenetic and population genetic data.
v0.27.0
gzip_input_source.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_GZIP_INPUT_SOURCE_H_
2 #define GENESIS_UTILS_IO_GZIP_INPUT_SOURCE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
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 
35 
36 #include <memory>
37 #include <string>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Gzip Input Source
44 // =================================================================================================
45 
56 class GzipInputSource final : public BaseInputSource
57 {
58 public:
59 
60  // -------------------------------------------------------------
61  // Typedefs and Enums
62  // -------------------------------------------------------------
63 
67  enum class Format
68  {
72  kAutomatic,
73 
77  kGzip,
78 
82  kZlib,
83 
87  kDeflate
88  };
89 
90  // -------------------------------------------------------------
91  // Constructors and Rule of Five
92  // -------------------------------------------------------------
93 
99  explicit GzipInputSource(
100  std::shared_ptr<BaseInputSource> input_source,
101  Format format = Format::kAutomatic
102  );
103 
104  GzipInputSource( GzipInputSource const& ) = delete;
105  GzipInputSource( GzipInputSource&& ) = default;
106 
107  GzipInputSource& operator= ( GzipInputSource const& ) = delete;
109 
110  ~GzipInputSource() override;
111 
112  // -------------------------------------------------------------
113  // Overloaded Internal Members
114  // -------------------------------------------------------------
115 
116 private:
117 
121  static const size_t BlockLength = 1 << 20;
122 
128  void create_zstream_();
129 
135  void destroy_zstream_();
136 
140  size_t read_( char* buffer, size_t size ) override;
141 
145  std::string source_name_() const override
146  {
147  return format_name_ + "-compressed " + input_source_->source_name();
148  }
149 
154  std::string source_string_() const override;
155 
159  int get_format_( Format format ) const;
160 
164  std::string translate_format_( Format format ) const;
165 
166  // -------------------------------------------------------------
167  // Member Variables
168  // -------------------------------------------------------------
169 
170  std::shared_ptr<BaseInputSource> input_source_;
171 
172  Format format_;
173  std::string format_name_;
174 
175  // We want to avoid including the zlib header in this header,
176  // because it totally pollutes everything.
177  // So, we use a PIMPL-like data hiding technique for the zlib related members.
178  struct ZlibData;
179 
180  // Store the implementation and the implementation's deleter as well.
181  // Deleter is a pointer to a function with signature `void func(ZlibData *)`.
182  // This way, we can use the default move constructor and default move assignment
183  // for this class without implementing them on our own.
184  std::unique_ptr<ZlibData, void (*)(ZlibData *)> zlib_data_;
185 
186 };
187 
188 } // namespace utils
189 } // namespace genesis
190 
191 #endif // include guard
genesis::utils::GzipInputSource::Format::kGzip
@ kGzip
Use gzip decompression.
genesis::utils::GzipInputSource::operator=
GzipInputSource & operator=(GzipInputSource const &)=delete
base_input_source.hpp
genesis::utils::GzipInputSource::Format::kZlib
@ kZlib
Use zlib decompression.
genesis::utils::GzipInputSource::~GzipInputSource
~GzipInputSource() override
Definition: gzip_input_source.cpp:117
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::GzipInputSource::GzipInputSource
GzipInputSource(std::shared_ptr< BaseInputSource > input_source, Format format=Format::kAutomatic)
Construct the input source using another input source (FileInputSource, StringInputSource,...
Definition: gzip_input_source.cpp:101
genesis::utils::GzipInputSource
Input source for reading byte data from a gzip/zlib-compressed source.
Definition: gzip_input_source.hpp:56
genesis::utils::GzipInputSource::Format::kDeflate
@ kDeflate
Use a pure deflate decompression.
genesis::utils::GzipInputSource::Format::kAutomatic
@ kAutomatic
Enable automatic header detection, allowing either gzip or zlib.
genesis::utils::GzipInputSource::Format
Format
Format used by gzip/zlib for decompression.
Definition: gzip_input_source.hpp:67
genesis::utils::BaseInputSource
Abstract base class for reading byte data from input sources.
Definition: base_input_source.hpp:50