A toolkit for working with phylogenetic data.
v0.24.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 
77 inline std::shared_ptr<BaseOutputTarget> to_file(
78  std::string const& file_name,
80  bool auto_adjust_filename = true
81 ) {
82  auto fn = file_name;
83  auto const ext = file_extension( fn );
84 
85  // With compression
86  if( compression_level != GzipCompressionLevel::kNoCompression ) {
87  if( auto_adjust_filename && ext != "gz" ) {
88  fn += ".gz";
89  }
90  return std::make_shared<GzipOutputTarget>(
91  std::make_shared< FileOutputTarget >( fn, std::ios_base::out | std::ios_base::binary ),
92  compression_level
93  );
94  }
95 
96  // Without compression. Not in the `else` branch, to not confuse old compilers.
97  if( auto_adjust_filename && ext == "gz" ) {
98  fn.erase( fn.size() - 2 );
99  }
100  return std::make_shared< FileOutputTarget >( fn );
101 }
102 
114 inline std::shared_ptr<BaseOutputTarget> to_stream(
115  std::ostream& target_stream,
117 ) {
118  if( compression_level != GzipCompressionLevel::kNoCompression ) {
119  return std::make_shared<GzipOutputTarget>(
120  std::make_shared< StreamOutputTarget >( target_stream ),
121  compression_level
122  );
123  }
124  return std::make_shared< StreamOutputTarget >( target_stream );
125 }
126 
133 inline std::shared_ptr<BaseOutputTarget> to_string(
134  std::string& target_string
135 ) {
136  return std::make_shared< StringOutputTarget >( target_string );
137 }
138 
139 } // namespace utils
140 } // namespace genesis
141 
142 #endif // include guard
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:707
GzipCompressionLevel
List of possible compression levels used for GzipOStream.
std::shared_ptr< BaseOutputTarget > to_file(std::string const &file_name, GzipCompressionLevel compression_level=GzipCompressionLevel::kNoCompression, bool auto_adjust_filename=true)
Obtain an output target for writing to a file.
std::shared_ptr< BaseOutputTarget > to_stream(std::ostream &target_stream, GzipCompressionLevel compression_level=GzipCompressionLevel::kNoCompression)
Obtain an output target for writing to a stream.
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Provides some valuable additions to STD.
Provides functions for accessing the file system.
std::shared_ptr< BaseOutputTarget > to_string(std::string &target_string)
Obtain an output target for writing to a string.