A library for working with phylogenetic and population genetic data.
v0.27.0
convert.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TEXT_CONVERT_H_
2 #define GENESIS_UTILS_TEXT_CONVERT_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 
35 
36 #include <iostream>
37 #include <sstream>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Generic Conversion
47 // =================================================================================================
48 
57 template <typename T>
58 T convert_from_string( std::string const& str, bool trim = false )
59 {
60  T value;
61 
62  // Generic parser that just uses a string stream.
63  // Convert, and fail if there is more in the string that we expect.
64  bool good = true;
65  try {
66  std::stringstream ss( trim ? utils::trim(str) : str );
67  ss >> std::noskipws >> value;
68  good = ss.eof();
69  } catch(...) {
70  good = false;
71  }
72 
73  // If we are here, either the string stream conversion itself failed (that's what the catch
74  // is for), or there was more data in the stream that we could convert. Either way, throw.
75  if( !good ) {
76  throw std::invalid_argument( "Cannot convert string '" + str + "' to specified type." );
77  }
78  return value;
79 }
80 
84 template <>
85 inline std::string convert_from_string<std::string>( std::string const& str, bool trim )
86 {
87  // We need special treatment of strings here, as the stringstream would only give
88  // us the first word of the input otherwise.
89  return (trim ? utils::trim(str) : str);
90 }
91 
92 // =================================================================================================
93 // Bool Text Conversion
94 // =================================================================================================
95 
96 bool convert_to_bool( std::string const& str );
97 bool is_convertible_to_bool( std::string const& str );
98 
99 template<typename ForwardIterator>
100 std::vector<bool> convert_to_bool(
101  ForwardIterator first,
102  ForwardIterator last,
103  size_t size = 0
104 ) {
105  // Prep.
106  std::vector<bool> ret;
107  ret.reserve( size );
108 
109  // Add all values. Throws on error.
110  while( first != last ) {
111  ret.push_back( convert_to_bool( *first ));
112  ++first;
113  }
114  return ret;
115 }
116 
117 template<typename ForwardIterator>
119  ForwardIterator first,
120  ForwardIterator last
121 ) {
122  while( first != last ) {
123  if( !is_convertible_to_bool( *first )) {
124  return false;
125  }
126  ++first;
127  }
128  return true;
129 }
130 
131 // =================================================================================================
132 // Bool Double Text Conversion
133 // =================================================================================================
134 
135 double convert_to_bool_double( std::string const& str );
136 bool is_convertible_to_bool_double( std::string const& str );
137 
138 template<typename ForwardIterator>
139 std::vector<double> convert_to_bool_double(
140  ForwardIterator first,
141  ForwardIterator last,
142  size_t size = 0
143 ) {
144  // Prep.
145  std::vector<double> ret;
146  ret.reserve( size );
147 
148  // Add all values. Throws on error.
149  while( first != last ) {
150  ret.push_back( convert_to_bool_double( *first ));
151  ++first;
152  }
153  return ret;
154 }
155 
156 template<typename ForwardIterator>
158  ForwardIterator first,
159  ForwardIterator last
160 ) {
161  while( first != last ) {
162  if( !is_convertible_to_bool_double( *first )) {
163  return false;
164  }
165  ++first;
166  }
167  return true;
168 }
169 
170 // =================================================================================================
171 // Double Text Conversion
172 // =================================================================================================
173 
174 double convert_to_double( std::string const& str );
175 bool is_convertible_to_double( std::string const& str );
176 
177 template<typename ForwardIterator>
178 std::vector<double> convert_to_double(
179  ForwardIterator first,
180  ForwardIterator last,
181  size_t size = 0
182 ) {
183  // Prep.
184  std::vector<double> ret;
185  ret.reserve( size );
186 
187  // Add all values. Throws on error.
188  while( first != last ) {
189  ret.push_back( convert_to_double( *first ));
190  ++first;
191  }
192  return ret;
193 }
194 
195 template<typename ForwardIterator>
197  ForwardIterator first,
198  ForwardIterator last
199 ) {
200  while( first != last ) {
201  if( !is_convertible_to_double( *first )) {
202  return false;
203  }
204  ++first;
205  }
206  return true;
207 }
208 
209 } // namespace utils
210 } // namespace genesis
211 
212 #endif // include guard
genesis::utils::is_convertible_to_bool_double
bool is_convertible_to_bool_double(std::string const &str)
Definition: convert.cpp:122
genesis::utils::convert_to_bool_double
double convert_to_bool_double(std::string const &str)
Definition: convert.cpp:113
genesis::utils::is_convertible_to_bool
bool is_convertible_to_bool(Dataframe const &df, size_t col_index)
Definition: utils/containers/dataframe/operators.cpp:61
genesis::utils::trim
std::string trim(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with trimmed white spaces.
Definition: string.cpp:602
genesis::utils::convert_to_double
void convert_to_double(Dataframe &df, size_t col_index)
Definition: utils/containers/dataframe/operators.cpp:192
string.hpp
Provides some commonly used string utility functions.
genesis::utils::convert_from_string
T convert_from_string(std::string const &str, bool trim=false)
Generic conversion from string to any data type that is supported by std::stringsteam operator >>.
Definition: convert.hpp:58
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::is_convertible_to_double
bool is_convertible_to_double(Dataframe const &df, size_t col_index)
Definition: utils/containers/dataframe/operators.cpp:84
genesis::utils::convert_to_bool
void convert_to_bool(Dataframe &df, size_t col_index)
Definition: utils/containers/dataframe/operators.cpp:156