A toolkit for working with phylogenetic data.
v0.24.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-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 
40 
42 
43 #include <memory>
44 #include <string>
45 #include <vector>
46 
47 namespace genesis {
48 namespace utils {
49 
50 // =================================================================================================
51 // Input Source Convenience Functions
52 // =================================================================================================
53 
67 inline std::shared_ptr<BaseInputSource> from_file(
68  std::string const& file_name,
69  bool detect_compression = true
70 ) {
71  if( detect_compression && is_gzip_compressed_file( file_name )) {
72  return std::make_shared<utils::GzipInputSource>(
73  std::make_shared< FileInputSource >( file_name )
74  );
75  } else {
76  return std::make_shared< FileInputSource >( file_name );
77  }
78 }
79 
90 inline std::vector<std::shared_ptr<BaseInputSource>> from_files(
91  std::vector<std::string> const& file_names,
92  bool detect_compression = true
93 ) {
94  std::vector<std::shared_ptr<BaseInputSource>> ret;
95  for( size_t i = 0; i < file_names.size(); ++i ) {
96  ret.emplace_back( from_file( file_names[i], detect_compression ));
97  }
98  return ret;
99 }
100 
110 template<typename InputIterator>
111 inline std::vector<std::shared_ptr<BaseInputSource>> from_files(
112  InputIterator first,
113  InputIterator last,
114  bool detect_compression = true
115 ) {
116  std::vector<std::shared_ptr<BaseInputSource>> ret;
117  while( first != last ) {
118  ret.emplace_back( from_file( *first, detect_compression ));
119  ++first;
120  }
121  return ret;
122 }
123 
133 inline std::shared_ptr<BaseInputSource> from_string(
134  std::string const& input_string
135 ) {
136  return std::make_shared< StringInputSource >( input_string );
137 }
138 
147 inline std::vector<std::shared_ptr<BaseInputSource>> from_strings(
148  std::vector<std::string> const& input_strings
149 ) {
150  std::vector<std::shared_ptr<BaseInputSource>> ret;
151  for( size_t i = 0; i < input_strings.size(); ++i ) {
152  ret.emplace_back( from_string( input_strings[i] ));
153  }
154  return ret;
155 }
156 
165 template<typename InputIterator>
166 inline std::vector<std::shared_ptr<BaseInputSource>> from_strings(
167  InputIterator first,
168  InputIterator last
169 ) {
170  std::vector<std::shared_ptr<BaseInputSource>> ret;
171  while( first != last ) {
172  ret.emplace_back( from_string( *first ));
173  ++first;
174  }
175  return ret;
176 }
177 
187 inline std::shared_ptr<BaseInputSource> from_stream(
188  std::istream& input_stream
189 ) {
190  return std::make_shared< StreamInputSource >( input_stream );
191 }
192 
193 } // namespace utils
194 } // namespace genesis
195 
196 #endif // include guard
std::shared_ptr< BaseInputSource > from_stream(std::istream &input_stream)
Obtain an input source for reading from 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.
std::shared_ptr< BaseInputSource > from_file(std::string const &file_name, bool detect_compression=true)
Obtain an input source for reading from a file.
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.
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.
bool is_gzip_compressed_file(std::string const &file_name)
Return whether a given file is gzip-compressed.
Definition: gzip.cpp:58
std::shared_ptr< BaseInputSource > from_string(std::string const &input_string)
Obtain an input source for reading from a string.