diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index de452844f..8c7916565 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -224,7 +224,7 @@ private: Empty, }; - template + template struct Getter { std::optional get(const YamlObject &obj) const; }; diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 7c0b341a3..db256ec5b 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -7,6 +7,7 @@ #include "libcamera/internal/yaml_parser.h" +#include #include #include #include @@ -146,151 +147,38 @@ YamlObject::Getter::get(const YamlObject &obj) const return std::nullopt; } -namespace { - -bool parseSignedInteger(const std::string &str, long min, long max, - long *result) +template +struct YamlObject::Getter || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v>> { - if (str == "") - return false; + std::optional get(const YamlObject &obj) const + { + if (obj.type_ != Type::Value) + return std::nullopt; - char *end; + const std::string &str = obj.value_; + T value; - errno = 0; - long value = std::strtol(str.c_str(), &end, 10); + auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), + value); + if (ptr != str.data() + str.size() || ec != std::errc()) + return std::nullopt; - if ('\0' != *end || errno == ERANGE || value < min || value > max) - return false; + return value; + } +}; - *result = value; - return true; -} - -bool parseUnsignedInteger(const std::string &str, unsigned long max, - unsigned long *result) -{ - if (str == "") - return false; - - /* - * strtoul() accepts strings representing a negative number, in which - * case it negates the converted value. We don't want to silently accept - * negative values and return a large positive number, so check for a - * minus sign (after optional whitespace) and return an error. - */ - std::size_t found = str.find_first_not_of(" \t"); - if (found != std::string::npos && str[found] == '-') - return false; - - char *end; - - errno = 0; - unsigned long value = std::strtoul(str.c_str(), &end, 10); - - if ('\0' != *end || errno == ERANGE || value > max) - return false; - - *result = value; - return true; -} - -} /* namespace */ - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - long value; - - if (!parseSignedInteger(obj.value_, std::numeric_limits::min(), - std::numeric_limits::max(), &value)) - return std::nullopt; - - return value; -} - -template<> -std::optional -YamlObject::Getter::get(const YamlObject &obj) const -{ - if (obj.type_ != Type::Value) - return std::nullopt; - - unsigned long value; - - if (!parseUnsignedInteger(obj.value_, std::numeric_limits::max(), - &value)) - return std::nullopt; - - return value; -} +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; +template struct YamlObject::Getter; template<> std::optional