A library for working with phylogenetic and population genetic data.
v0.32.0
input_source.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_INPUT_SOURCE_H_
2 #define GENESIS_UTILS_IO_INPUT_SOURCE_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 
42 
43 #include <iostream>
44 #include <memory>
45 #include <string>
46 #include <vector>
47 
48 namespace genesis {
49 namespace utils {
50 
51 // =================================================================================================
52 // Input Source Convenience Functions
53 // =================================================================================================
54 
68 inline std::shared_ptr<BaseInputSource> from_file(
69  std::string const& file_name,
70  bool detect_compression = true
71 ) {
72  if( detect_compression && is_gzip_compressed_file( file_name )) {
73  return std::make_shared<utils::GzipInputSource>(
74  std::make_shared< FileInputSource >( file_name )
75  );
76  } else {
77  return std::make_shared< FileInputSource >( file_name );
78  }
79 }
80 
91 inline std::vector<std::shared_ptr<BaseInputSource>> from_files(
92  std::vector<std::string> const& file_names,
93  bool detect_compression = true
94 ) {
95  std::vector<std::shared_ptr<BaseInputSource>> ret;
96  for( size_t i = 0; i < file_names.size(); ++i ) {
97  ret.emplace_back( from_file( file_names[i], detect_compression ));
98  }
99  return ret;
100 }
101 
111 template<typename InputIterator>
112 inline std::vector<std::shared_ptr<BaseInputSource>> from_files(
113  InputIterator first,
114  InputIterator last,
115  bool detect_compression = true
116 ) {
117  std::vector<std::shared_ptr<BaseInputSource>> ret;
118  while( first != last ) {
119  ret.emplace_back( from_file( *first, detect_compression ));
120  ++first;
121  }
122  return ret;
123 }
124 
134 inline std::shared_ptr<BaseInputSource> from_string(
135  std::string const& input_string
136 ) {
137  return std::make_shared< StringInputSource >( input_string );
138 }
139 
148 inline std::vector<std::shared_ptr<BaseInputSource>> from_strings(
149  std::vector<std::string> const& input_strings
150 ) {
151  std::vector<std::shared_ptr<BaseInputSource>> ret;
152  for( size_t i = 0; i < input_strings.size(); ++i ) {
153  ret.emplace_back( from_string( input_strings[i] ));
154  }
155  return ret;
156 }
157 
166 template<typename InputIterator>
167 inline std::vector<std::shared_ptr<BaseInputSource>> from_strings(
168  InputIterator first,
169  InputIterator last
170 ) {
171  std::vector<std::shared_ptr<BaseInputSource>> ret;
172  while( first != last ) {
173  ret.emplace_back( from_string( *first ));
174  ++first;
175  }
176  return ret;
177 }
178 
189 inline std::shared_ptr<BaseInputSource> from_stream(
190  std::istream& input_stream
191 ) {
192  return std::make_shared< StreamInputSource >( input_stream );
193 }
194 
205 inline std::shared_ptr<BaseInputSource> from_stdin() {
206  return std::make_shared< StreamInputSource >( std::cin );
207 }
208 
209 } // namespace utils
210 } // namespace genesis
211 
212 #endif // include guard
gzip_input_source.hpp
genesis::utils::from_strings
std::vector< std::shared_ptr< BaseInputSource > > from_strings(std::vector< std::string > const &input_strings)
Obtain a set of input sources for reading from strings.
Definition: input_source.hpp:148
genesis::utils::from_file
std::shared_ptr< BaseInputSource > from_file(std::string const &file_name, bool detect_compression=true)
Obtain an input source for reading from a file.
Definition: input_source.hpp:68
genesis::utils::from_files
std::vector< std::shared_ptr< BaseInputSource > > from_files(std::vector< std::string > const &file_names, bool detect_compression=true)
Obtain a set of input sources for reading from files.
Definition: input_source.hpp:91
base_input_source.hpp
gzip.hpp
genesis::utils::from_stream
std::shared_ptr< BaseInputSource > from_stream(std::istream &input_stream)
Obtain an input source for reading from a stream.
Definition: input_source.hpp:189
std.hpp
Provides some valuable additions to STD.
genesis::utils::from_string
std::shared_ptr< BaseInputSource > from_string(std::string const &input_string)
Obtain an input source for reading from a string.
Definition: input_source.hpp:134
genesis::utils::is_gzip_compressed_file
bool is_gzip_compressed_file(std::string const &file_name)
Return whether a given file is gzip-compressed.
Definition: gzip.cpp:58
genesis::utils::from_stdin
std::shared_ptr< BaseInputSource > from_stdin()
Obtain an input source for reading from standard input (i.e., stdin or cin).
Definition: input_source.hpp:205
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
file_input_source.hpp
string_input_source.hpp
stream_input_source.hpp