From a69958fcd6c7bf394859f5c5177c4a685f512f45 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 15 Aug 2022 23:38:19 +0300 Subject: libcamera: yaml_parser: Enable YamlObject::get() for int8_t and uint8_t The YamlObject::get() function template is implemented for 16-bit and 32-bit integers. Add an 8-bit specialization that will be used in the rkisp1 IPA module, and extend the unit tests accordingly. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Jacopo Mondi --- src/libcamera/yaml_parser.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'src') diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index f928b723..85a52c05 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -131,6 +131,61 @@ std::optional YamlObject::get() const return std::nullopt; } +template<> +std::optional YamlObject::get() const +{ + if (type_ != Type::Value) + return std::nullopt; + + if (value_ == "") + return std::nullopt; + + char *end; + + errno = 0; + long value = std::strtol(value_.c_str(), &end, 10); + + if ('\0' != *end || errno == ERANGE || + value < std::numeric_limits::min() || + value > std::numeric_limits::max()) + return std::nullopt; + + return value; +} + +template<> +std::optional YamlObject::get() const +{ + if (type_ != Type::Value) + return std::nullopt; + + if (value_ == "") + return std::nullopt; + + /* + * libyaml parses all scalar values as strings. When a string has + * leading spaces before a minus sign, for example " -10", strtoul + * skips leading spaces, accepts the leading minus sign, and the + * calculated digits are negated as if by unary minus. Rule it out in + * case the user gets a large number when the value is negative. + */ + std::size_t found = value_.find_first_not_of(" \t"); + if (found != std::string::npos && value_[found] == '-') + return std::nullopt; + + char *end; + + errno = 0; + unsigned long value = std::strtoul(value_.c_str(), &end, 10); + + if ('\0' != *end || errno == ERANGE || + value < std::numeric_limits::min() || + value > std::numeric_limits::max()) + return std::nullopt; + + return value; +} + template<> std::optional YamlObject::get() const { @@ -310,6 +365,8 @@ template || std::is_same_v || + std::is_same_v || + std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || @@ -336,6 +393,8 @@ std::optional> YamlObject::getList() const template std::optional> YamlObject::getList() const; template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; template std::optional> YamlObject::getList() const; template std::optional> YamlObject::getList() const; template std::optional> YamlObject::getList() const; -- cgit v1.2.1