X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fcxx%2FConfigKey_methods.cpp;h=d899ddcc2a18d2053470a346051e6f4e5ccca27f;hb=HEAD;hp=7f8c201ed5a8f1e649b4f4e9a452b5c8344e1dd3;hpb=6c6dd7328a83f7beb7280d57cc5e255294aba2fc;p=libsigrok.git diff --git a/bindings/cxx/ConfigKey_methods.cpp b/bindings/cxx/ConfigKey_methods.cpp index 7f8c201e..d899ddcc 100644 --- a/bindings/cxx/ConfigKey_methods.cpp +++ b/bindings/cxx/ConfigKey_methods.cpp @@ -70,6 +70,50 @@ static inline double stod( const std::string& str ) } #endif +#ifndef HAVE_STOUL + +/* Fallback implementation of stoul. */ + +#include +#include +#include +#include + +static inline unsigned long stoul(const std::string &str) +{ + char *endptr; + unsigned long ret; + errno = 0; + ret = std::strtoul(str.c_str(), &endptr, 10); + if (endptr == str.c_str()) + throw std::invalid_argument("stoul"); + /* + * TODO Convert to a larger/wider intermediate data type? + * Because after conversion into the very target type, the + * range check is assumed to be ineffective. + */ + if (errno == ERANGE || + ret < std::numeric_limits::min() || + ret > std::numeric_limits::max()) + throw std::out_of_range("stoul"); + return ret; +} +#endif + +// Conversion from text to uint32_t, including a range check. +// This is sigrok specific, _not_ part of any C++ standard library. +static uint32_t stou32(const std::string &str) +{ + unsigned long ret; + errno = 0; + ret = stoul(str); + if (errno == ERANGE) + throw std::out_of_range("stou32"); + if (ret > std::numeric_limits::max()) + throw std::out_of_range("stou32"); + return ret; +} + Glib::VariantBase ConfigKey::parse_string(std::string value, enum sr_datatype dt) { GVariant *variant; @@ -109,6 +153,13 @@ Glib::VariantBase ConfigKey::parse_string(std::string value, enum sr_datatype dt throw Error(SR_ERR_ARG); } break; + case SR_T_UINT32: + try { + variant = g_variant_new_uint32(stou32(value)); + } catch (invalid_argument&) { + throw Error(SR_ERR_ARG); + } + break; default: throw Error(SR_ERR_BUG); }