|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_IO_PARSER_H_
2 #define GENESIS_UTILS_IO_PARSER_H_
82 auto const r =
static_cast<T
>(x);
83 if(
static_cast<size_t>(r) != x || r < 0 ) {
84 throw std::overflow_error(
85 "Numerical overflow in " + source.
source_name() +
" at " + source.
at() +
"."
104 std::is_signed<T>::value,
105 "Need signed type for parse_signed_integer()"
109 throw std::runtime_error(
110 "Expecting number in " + source.
source_name() +
" at " + source.
at() +
"."
115 if( *source ==
'-' || *source ==
'+' ) {
116 if( *source ==
'-' ) {
126 }
catch( std::overflow_error
const& ) {
128 throw std::underflow_error(
129 "Numerical overflow in " + source.
source_name() +
" at " + source.
at() +
"."
141 auto const r =
static_cast<T
>( sign *
static_cast<T
>(x) );
142 if(
static_cast<size_t>( sign * r ) != x || !( r == 0 || (sign < 0) == (r < 0) )) {
143 throw std::underflow_error(
144 "Numerical overflow in " + source.
source_name() +
" at " + source.
at() +
"."
156 return parse_signed_integer<T>(source);
181 throw std::runtime_error(
182 "Expecting float number in " + source.
source_name() +
" at " + source.
at() +
"."
188 if( *source ==
'-' ){
191 }
else if( *source ==
'+' ) {
196 bool found_mantisse =
false;
198 int y = *source -
'0';
202 found_mantisse =
true;
206 if( source && *source ==
'.' ) {
210 throw std::runtime_error(
211 "Invalid number in " + source.
source_name() +
" at " + source.
at() +
"."
218 int y = *source -
'0';
221 found_mantisse =
true;
226 if( ! found_mantisse && source ) {
228 std::numeric_limits<double>::is_iec559,
229 "Compiler does not use ISO IEC559 / IEEE754 standard."
234 double const sign = is_neg ? -1.0 : 1.0;
236 auto const buffer = source.
buffer();
237 if( buffer.second >= 8 &&
strncasecmp( buffer.first,
"infinity", 8 ) == 0 ) {
239 value = sign * std::numeric_limits<double>::infinity();
240 }
else if( buffer.second >= 3 &&
strncasecmp( buffer.first,
"inf", 3 ) == 0 ) {
242 value = sign * std::numeric_limits<double>::infinity();
244 if( buffer.second >= 3 &&
strncasecmp( buffer.first,
"nan", 3 ) == 0 ) {
246 value = sign * std::numeric_limits<double>::quiet_NaN();
254 if( value != 0.0 && ( !source || !
is_alnum( *source ))) {
260 if( ! found_mantisse ) {
261 throw std::runtime_error(
262 "Invalid number in " + source.
source_name() +
" at " + source.
at() +
"."
271 int e = parse_signed_integer<int>( source );
283 if( ( e & 1 ) == 0 ) {
352 bool use_escapes =
true,
353 bool use_twin_quotes =
false,
354 bool include_qmarks =
false
360 #endif // include guard
T parse_integer(utils::InputStream &source)
Alias for parse_signed_integer().
T parse_float(utils::InputStream &source)
Read a floating point number from a stream and return it.
constexpr bool is_alnum(char c) noexcept
Return whether a char is a letter (a-z or A-Z) or a digit (0-9), ASCII-only.
int strncasecmp(char const *s1, char const *s2, size_t n)
Compares up to n chars of two strings, ignoring case differences.
size_t parse_unsigned_integer_size_t(utils::InputStream &source)
Parse the input source as an unsigned int into a size_t.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
T parse_unsigned_integer(utils::InputStream &source)
Read an unsigned integer from a stream and return it.
constexpr bool is_digit(char c) noexcept
Return whether a char is a digit (0-9), ASCII-only.
T parse_signed_integer(utils::InputStream &source)
Read a signed integer from a stream and return it.
constexpr char to_lower(char c) noexcept
Return the lower case version of a letter, ASCII-only.
std::string parse_quoted_string(utils::InputStream &source, bool use_escapes, bool use_twin_quotes, bool include_qmarks)
Read a string in quotation marks from a stream and return it.
std::string parse_number_string(utils::InputStream &source)
Read a general number string from an input stream.