2017-07-26 15:25:29 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-07-26 15:25:29 +10:00
|
|
|
*
|
2022-06-13 15:37:58 +10:00
|
|
|
* Copyright 2017-2018, 2022 Danny Robson <danny@nerdcruft.net>
|
2017-07-26 15:25:29 +10:00
|
|
|
*/
|
|
|
|
|
2019-03-19 12:38:22 +11:00
|
|
|
#include "value.hpp"
|
2018-01-10 17:19:39 +11:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/bool.hpp>
|
|
|
|
#include <cruft/util/cast.hpp>
|
|
|
|
#include <cruft/util/preprocessor.hpp>
|
2017-07-26 15:25:29 +10:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2019-03-19 12:38:22 +11:00
|
|
|
using cruft::parse::value;
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2019-02-02 13:48:48 +11:00
|
|
|
namespace {
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// A trait class that records the C string parsing function for a given
|
|
|
|
/// type.
|
|
|
|
///
|
|
|
|
/// Not all types will necessarily have a corresponding parsing function.
|
|
|
|
template <typename ValueT> struct parse_traits { };
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <> struct parse_traits<long> { static constexpr auto value = strtol; };
|
|
|
|
template <> struct parse_traits<long long> { static constexpr auto value = strtoll; };
|
|
|
|
template <> struct parse_traits<unsigned long> { static constexpr auto value = strtoul; };
|
|
|
|
template <> struct parse_traits<unsigned long long> { static constexpr auto value = strtoull; };
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <> struct parse_traits<float> { static constexpr auto value = strtof; };
|
|
|
|
template <> struct parse_traits<double> { static constexpr auto value = strtod; };
|
|
|
|
template <> struct parse_traits<long double> { static constexpr auto value = strtold; };
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-03-16 18:17:18 +11:00
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename IteratorT,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_same_v<
|
|
|
|
std::remove_cv_t<IteratorT>,
|
|
|
|
char
|
|
|
|
>
|
|
|
|
>
|
|
|
|
>
|
2019-02-02 13:48:48 +11:00
|
|
|
ValueT
|
2019-03-16 18:17:18 +11:00
|
|
|
c_iparse (cruft::view<IteratorT*> &data)
|
2019-02-02 13:48:48 +11:00
|
|
|
{
|
|
|
|
auto head = const_cast<char *> (data.begin ());
|
|
|
|
auto tail = head;
|
|
|
|
auto val = parse_traits<ValueT>::value (head, &tail, 0);
|
|
|
|
|
|
|
|
if (tail == head)
|
|
|
|
throw std::invalid_argument ("unable to parse integer");
|
|
|
|
|
|
|
|
data = data.consume (tail);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2019-03-16 18:17:18 +11:00
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename IteratorT,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_same_v<
|
|
|
|
std::remove_cv_t<IteratorT>,
|
|
|
|
char
|
|
|
|
>
|
|
|
|
>
|
|
|
|
>
|
2019-02-02 13:48:48 +11:00
|
|
|
ValueT
|
2019-03-16 18:17:18 +11:00
|
|
|
c_fparse (cruft::view<IteratorT*> &data)
|
2019-02-02 13:48:48 +11:00
|
|
|
{
|
|
|
|
auto head = const_cast<char *> (data.begin ());
|
|
|
|
auto tail = head;
|
|
|
|
auto val = parse_traits<ValueT>::value (head, &tail);
|
|
|
|
|
|
|
|
if (tail == head)
|
|
|
|
throw std::invalid_argument ("unable to parse float");
|
|
|
|
|
|
|
|
data = data.consume (tail);
|
|
|
|
return val;
|
|
|
|
}
|
2017-07-26 15:25:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2019-02-02 13:44:04 +11:00
|
|
|
/// Specialise cruft::parse for the type KLASS and dispatch the actual parsing
|
|
|
|
/// to safe template wrappers around the C parsing functions.
|
2019-03-16 18:17:18 +11:00
|
|
|
#define C_PARSE_ITERATOR(ITERATOR,PREFIX, KLASS)\
|
2019-02-02 13:44:04 +11:00
|
|
|
template <> \
|
|
|
|
KLASS \
|
2019-03-19 12:38:22 +11:00
|
|
|
cruft::parse::value<KLASS> ( \
|
2019-03-16 18:17:18 +11:00
|
|
|
cruft::view<ITERATOR> &str \
|
2019-02-02 13:44:04 +11:00
|
|
|
) { \
|
|
|
|
return c_ ## PREFIX ## parse<KLASS> (str); \
|
2017-07-26 15:25:29 +10:00
|
|
|
}
|
|
|
|
|
2019-03-16 18:17:18 +11:00
|
|
|
#define C_PARSE(PREFIX,KLASS) \
|
|
|
|
C_PARSE_ITERATOR(char *, PREFIX, KLASS) \
|
|
|
|
C_PARSE_ITERATOR(char const *, PREFIX, KLASS)
|
|
|
|
|
2017-07-26 15:25:29 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2019-02-02 13:44:04 +11:00
|
|
|
MAP1(C_PARSE, i,
|
|
|
|
unsigned long,
|
|
|
|
unsigned long long,
|
|
|
|
long,
|
|
|
|
long long
|
|
|
|
)
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2019-02-02 13:44:04 +11:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
MAP1(C_PARSE, f,
|
|
|
|
float,
|
|
|
|
double,
|
|
|
|
long double
|
|
|
|
)
|
2017-12-19 18:12:35 +11:00
|
|
|
|
|
|
|
|
2019-02-02 13:44:04 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Specialise cruft::parse for the type FINAL and dispatch the actual parsing
|
|
|
|
/// to the safe template wrappers defined above, but use the type USED as an
|
|
|
|
/// intermediary type.
|
|
|
|
///
|
|
|
|
/// We need to parse using the type `USED` because there aren't C parsing
|
|
|
|
/// routines for all the fundamental types.
|
|
|
|
///
|
|
|
|
/// We check that the final result can safely fit within `FINAL` before
|
|
|
|
/// returning the value, else we raise an exception.
|
2018-12-16 13:26:48 +11:00
|
|
|
#define C_PARSE_WITH_CAST(FINAL, USED) \
|
|
|
|
template<> \
|
|
|
|
FINAL \
|
2019-03-19 12:38:22 +11:00
|
|
|
cruft::parse::value<FINAL> (cruft::view<char const*> &str) { \
|
2018-12-16 13:26:48 +11:00
|
|
|
auto remain = str; \
|
2019-03-19 12:38:22 +11:00
|
|
|
auto res = value<USED> (remain); \
|
2018-12-16 13:26:48 +11:00
|
|
|
if (res > std::numeric_limits<FINAL>::max ()) \
|
|
|
|
throw std::invalid_argument ("overflow during parse"); \
|
|
|
|
\
|
|
|
|
str = remain; \
|
|
|
|
return cruft::cast::narrow<FINAL> (res); \
|
2018-03-27 16:11:04 +11:00
|
|
|
}
|
2018-07-27 18:28:30 +10:00
|
|
|
|
2019-02-02 13:45:34 +11:00
|
|
|
|
2019-02-02 13:44:19 +11:00
|
|
|
C_PARSE_WITH_CAST(short, long)
|
|
|
|
C_PARSE_WITH_CAST(int, long)
|
|
|
|
C_PARSE_WITH_CAST(unsigned short, unsigned long)
|
|
|
|
C_PARSE_WITH_CAST(unsigned int, unsigned long)
|
2019-05-30 10:42:25 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <>
|
|
|
|
bool
|
|
|
|
cruft::parse::value<bool> (cruft::view<char const*> &str)
|
|
|
|
{
|
|
|
|
static constexpr struct {
|
|
|
|
char const *str;
|
|
|
|
bool res;
|
|
|
|
} VALUES[] = {
|
|
|
|
{ "yes", true },
|
|
|
|
{ "y", true },
|
|
|
|
{ "true",true },
|
|
|
|
{ "t", true },
|
|
|
|
|
|
|
|
{ "no", false, },
|
|
|
|
{ "n", false, },
|
|
|
|
{ "false", false, },
|
|
|
|
{ "f", false, },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto [key,val]: VALUES) {
|
|
|
|
if (equal (key, str)) {
|
|
|
|
str = str.consume (strlen (key));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::invalid_argument ("invalid boolean string");
|
|
|
|
}
|
2022-06-13 15:37:58 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <>
|
|
|
|
cruft::tribool
|
|
|
|
cruft::parse::value<cruft::tribool> (cruft::view< char const*> &str)
|
|
|
|
{
|
|
|
|
return tribool (parse::value<bool> (str));
|
|
|
|
}
|