A library for working with phylogenetic and population genetic data.
v0.32.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-2024 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@sund.ku.dk>
23  University of Copenhagen, Globe Institute, Section for GeoGenetics
24  Oster Voldgade 5-7, 1350 Copenhagen K, Denmark
25 */
26 
40 
43 
44 #include <iostream>
45 #include <memory>
46 #include <stdexcept>
47 #include <string>
48 #include <vector>
49 
50 namespace genesis {
51 namespace utils {
52 
53 // =================================================================================================
54 // Output Target Convenience Functions
55 // =================================================================================================
56 
81 inline std::shared_ptr<BaseOutputTarget> to_file(
82  std::string const& file_name,
83  GzipCompressionLevel compression_level,
84  // GzipCompressionLevel compression_level = GzipCompressionLevel::kNoCompression,
85  bool auto_adjust_filename = true
86 ) {
87  auto fn = file_name;
88  auto const ext = file_extension( fn );
89 
90  // With compression
91  if( compression_level != GzipCompressionLevel::kNoCompression ) {
92  if( auto_adjust_filename && ext != "gz" ) {
93  fn += ".gz";
94  }
95  return std::make_shared<GzipOutputTarget>(
96  std::make_shared< FileOutputTarget >( fn, std::ios_base::out | std::ios_base::binary ),
97  compression_level
98  );
99  }
100 
101  // Without compression. Not in the `else` branch, to not confuse old compilers.
102  if( auto_adjust_filename && ext == "gz" ) {
103  fn.erase( fn.size() - 3 );
104  }
105  return std::make_shared< FileOutputTarget >( fn );
106 }
107 
117 inline std::shared_ptr<BaseOutputTarget> to_file(
118  std::string const& file_name,
119  std::ios_base::openmode mode = std::ios_base::out
120 ) {
121  return std::make_shared< FileOutputTarget >( file_name, mode );
122 }
123 
132 inline std::shared_ptr<BaseOutputTarget> to_gzip_file(
133  std::string const& file_name
134 ) {
136 }
137 
151 inline std::shared_ptr<BaseOutputTarget> to_gzip_block_file(
152  std::string const& file_name,
153  std::size_t block_size = GzipBlockOStream::GZIP_DEFAULT_BLOCK_SIZE,
155  std::shared_ptr<ThreadPool> thread_pool = nullptr,
156  bool auto_adjust_filename = true
157 ) {
158  if( compression_level == GzipCompressionLevel::kNoCompression ) {
159  throw std::invalid_argument(
160  "Cannot use compression level kNoCompression with a gzip block output."
161  );
162  }
163 
164  // Adjust filename if needed and wanted
165  auto fn = file_name;
166  auto const ext = file_extension( fn );
167  if( auto_adjust_filename && ext != "gz" ) {
168  fn += ".gz";
169  }
170 
171  // Return the wrapped streams
172  return std::make_shared<GzipBlockOutputTarget>(
173  std::make_shared< FileOutputTarget >( fn, std::ios_base::out | std::ios_base::binary ),
174  block_size,
175  compression_level,
176  thread_pool
177  );
178 }
179 
186 inline std::shared_ptr<BaseOutputTarget> to_string(
187  std::string& target_string
188 ) {
189  return std::make_shared< StringOutputTarget >( target_string );
190 }
191 
203 inline std::shared_ptr<BaseOutputTarget> to_stream(
204  std::ostream& target_stream,
206 ) {
207  if( compression_level != GzipCompressionLevel::kNoCompression ) {
208  return std::make_shared<GzipOutputTarget>(
209  std::make_shared< StreamOutputTarget >( target_stream ),
210  compression_level
211  );
212  }
213  return std::make_shared< StreamOutputTarget >( target_stream );
214 }
215 
222 inline std::shared_ptr<BaseOutputTarget> to_stdout() {
223  return std::make_shared< StreamOutputTarget >( std::cout );
224 }
225 
232 inline std::shared_ptr<BaseOutputTarget> to_stderr() {
233  return std::make_shared< StreamOutputTarget >( std::cerr );
234 }
235 
236 } // namespace utils
237 } // namespace genesis
238 
239 #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:186
genesis::utils::GzipBlockOStream::GZIP_DEFAULT_BLOCK_SIZE
static const std::size_t GZIP_DEFAULT_BLOCK_SIZE
Definition: gzip_block_ostream.hpp:110
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
genesis::utils::to_gzip_file
std::shared_ptr< BaseOutputTarget > to_gzip_file(std::string const &file_name)
Obtain an output target for writing to a gzip-compressed file.
Definition: output_target.hpp:132
gzip_output_target.hpp
file_output_target.hpp
gzip_stream.hpp
string_output_target.hpp
genesis::utils::to_stderr
std::shared_ptr< BaseOutputTarget > to_stderr()
Obtain an output target for writing to standard error (i.e., stderr or cerr).
Definition: output_target.hpp:232
genesis::utils::to_stdout
std::shared_ptr< BaseOutputTarget > to_stdout()
Obtain an output target for writing to standard output (i.e., stdout or cout).
Definition: output_target.hpp:222
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:203
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::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:81
genesis::utils::GzipCompressionLevel::kDefaultCompression
@ kDefaultCompression
stream_output_target.hpp
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::shared_ptr< ThreadPool > thread_pool=nullptr, bool auto_adjust_filename=true)
Obtain an output target for writing gzip block compressed data to a file.
Definition: output_target.hpp:151
genesis::utils::file_extension
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:821
base_output_target.hpp