A toolkit for working with phylogenetic data.
v0.24.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 
126  size_t read_( char* buffer, size_t size ) override;
127 
131  std::string source_name_() const override
132  {
133  return format_name_ + "-compressed " + input_source_->source_name();
134  }
135 
140  std::string source_string_() const override;
141 
145  int get_format_( Format format ) const;
146 
150  std::string translate_format_( Format format ) const;
151 
152  // -------------------------------------------------------------
153  // Member Variables
154  // -------------------------------------------------------------
155 
156  std::shared_ptr<BaseInputSource> input_source_;
157  std::string format_name_;
158 
159  // We want to avoid including the zlib header in this header,
160  // because it totally pollutes everything.
161  // So, we use a PIMPL-like data hiding technique for the zlib related members.
162  struct ZlibData;
163 
164  // Store the implementation and the implementation's deleter as well.
165  // Deleter is a pointer to a function with signature `void func(ZlibData *)`.
166  // This way, we can use the default move constructor and default move assignment
167  // for this class without implementing them on our own.
168  std::unique_ptr<ZlibData, void (*)(ZlibData *)> zlib_data_;
169 
170 };
171 
172 } // namespace utils
173 } // namespace genesis
174 
175 #endif // include guard
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Input source for reading byte data from a gzip/zlib-compressed source.
GzipInputSource(std::shared_ptr< BaseInputSource > input_source, Format format=Format::kAutomatic)
Construct the input source using another input source (FileInputSource, StringInputSource, StreamInputSource, etc), and add gzip/zlib decompression on top, using the specified GzipInputSource::Format.
GzipInputSource & operator=(GzipInputSource const &)=delete
Abstract base class for reading byte data from input sources.
Use a pure deflate decompression.
Enable automatic header detection, allowing either gzip or zlib.
Format
Format used by gzip/zlib for decompression.