A library for working with phylogenetic and population genetic data.
v0.27.0
output_target.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_OUTPUT_TARGET_H_
2 #define GENESIS_UTILS_IO_OUTPUT_TARGET_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 
40 
43 
44 #include <memory>
45 #include <stdexcept>
46 #include <string>
47 #include <vector>
48 
49 namespace genesis {
50 namespace utils {
51 
52 // =================================================================================================
53 // Output Target Convenience Functions
54 // =================================================================================================
55 
80 inline std::shared_ptr<BaseOutputTarget> to_file(
81  std::string const& file_name,
82  GzipCompressionLevel compression_level,
83  // GzipCompressionLevel compression_level = GzipCompressionLevel::kNoCompression,
84  bool auto_adjust_filename = true
85 ) {
86  auto fn = file_name;
87  auto const ext = file_extension( fn );
88 
89  // With compression
90  if( compression_level != GzipCompressionLevel::kNoCompression ) {
91  if( auto_adjust_filename && ext != "gz" ) {
92  fn += ".gz";
93  }
94  return std::make_shared<GzipOutputTarget>(
95  std::make_shared< FileOutputTarget >( fn, std::ios_base::out | std::ios_base::binary ),
96  compression_level
97  );
98  }
99 
100  // Without compression. Not in the `else` branch, to not confuse old compilers.
101  if( auto_adjust_filename && ext == "gz" ) {
102  fn.erase( fn.size() - 3 );
103  }
104  return std::make_shared< FileOutputTarget >( fn );
105 }
106 
116 inline std::shared_ptr<BaseOutputTarget> to_file(
117  std::string const& file_name,
118  std::ios_base::openmode mode = std::ios_base::out
119 ) {
120  return std::make_shared< FileOutputTarget >( file_name, mode );
121 }
122 
136 inline std::shared_ptr<BaseOutputTarget> to_gzip_block_file(
137  std::string const& file_name,
138  std::size_t block_size = GzipBlockOStream::GZIP_DEFAULT_BLOCK_SIZE,
140  std::size_t num_threads = 0,
141  bool auto_adjust_filename = true
142 ) {
143  if( compression_level == GzipCompressionLevel::kNoCompression ) {
144  throw std::invalid_argument(
145  "Cannot use compression level kNoCompression with a gzip block output."
146  );
147  }
148 
149  // Adjust filename if needed and wanted
150  auto fn = file_name;
151  auto const ext = file_extension( fn );
152  if( auto_adjust_filename && ext != "gz" ) {
153  fn += ".gz";
154  }
155 
156  // Return the wrapped streams
157  return std::make_shared<GzipBlockOutputTarget>(
158  std::make_shared< FileOutputTarget >( fn, std::ios_base::out | std::ios_base::binary ),
159  block_size,
160  compression_level,
161  num_threads
162  );
163 }
164 
176 inline std::shared_ptr<BaseOutputTarget> to_stream(
177  std::ostream& target_stream,
179 ) {
180  if( compression_level != GzipCompressionLevel::kNoCompression ) {
181  return std::make_shared<GzipOutputTarget>(
182  std::make_shared< StreamOutputTarget >( target_stream ),
183  compression_level
184  );
185  }
186  return std::make_shared< StreamOutputTarget >( target_stream );
187 }
188 
195 inline std::shared_ptr<BaseOutputTarget> to_string(
196  std::string& target_string
197 ) {
198  return std::make_shared< StringOutputTarget >( target_string );
199 }
200 
201 } // namespace utils
202 } // namespace genesis
203 
204 #endif // include guard
genesis::utils::to_string
std::shared_ptr< BaseOutputTarget > to_string(std::string &target_string)
Obtain an output target for writing to a string.
Definition: output_target.hpp:195
genesis::utils::GzipBlockOStream::GZIP_DEFAULT_BLOCK_SIZE
static const std::size_t GZIP_DEFAULT_BLOCK_SIZE
Definition: gzip_block_ostream.hpp:111
fs.hpp
Provides functions for accessing the file system.
std.hpp
Provides some valuable additions to STD.
genesis::utils::GzipCompressionLevel
GzipCompressionLevel
List of possible compression levels used for GzipOStream.
Definition: gzip_stream.hpp:100
gzip_output_target.hpp
file_output_target.hpp
gzip_stream.hpp
string_output_target.hpp
genesis::utils::to_stream
std::shared_ptr< BaseOutputTarget > to_stream(std::ostream &target_stream, GzipCompressionLevel compression_level=GzipCompressionLevel::kNoCompression)
Obtain an output target for writing to a stream.
Definition: output_target.hpp:176
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::to_gzip_block_file
std::shared_ptr< BaseOutputTarget > to_gzip_block_file(std::string const &file_name, std::size_t block_size=GzipBlockOStream::GZIP_DEFAULT_BLOCK_SIZE, GzipCompressionLevel compression_level=GzipCompressionLevel::kDefaultCompression, std::size_t num_threads=0, bool auto_adjust_filename=true)
Obtain an output target for writing gzip block compressed data to a file.
Definition: output_target.hpp:136
genesis::utils::GzipCompressionLevel::kNoCompression
@ kNoCompression
genesis::utils::to_file
std::shared_ptr< BaseOutputTarget > to_file(std::string const &file_name, GzipCompressionLevel compression_level, bool auto_adjust_filename=true)
Obtain an output target for writing to a file.
Definition: output_target.hpp:80
genesis::utils::GzipCompressionLevel::kDefaultCompression
@ kDefaultCompression
stream_output_target.hpp
genesis::utils::file_extension
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:720
base_output_target.hpp