[libcamera-devel] [RFC PATCH v2 12/14] libcamera: yaml_parser: Add get() specializations for 16-bit integers

Laurent Pinchart laurent.pinchart at ideasonboard.com
Sat Jun 4 20:59:37 CEST 2022


Extend the YamlObject::get() function template to support 16-bit
integers.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 include/libcamera/internal/yaml_parser.h |  2 +
 src/libcamera/yaml_parser.cpp            | 61 ++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
index d0ddb5f682b2..eebfff2c8a8a 100644
--- a/include/libcamera/internal/yaml_parser.h
+++ b/include/libcamera/internal/yaml_parser.h
@@ -163,6 +163,8 @@ public:
 		 typename std::enable_if_t<
 			 std::is_same<bool, T>::value ||
 			 std::is_same<double, T>::value ||
+			 std::is_same<int16_t, T>::value ||
+			 std::is_same<uint16_t, T>::value ||
 			 std::is_same<int32_t, T>::value ||
 			 std::is_same<uint32_t, T>::value ||
 			 std::is_same<std::string, T>::value ||
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index 3fb6fec695ee..3faebe61d338 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -137,6 +137,67 @@ bool YamlObject::get(const bool &defaultValue, bool *ok) const
 	return defaultValue;
 }
 
+template<>
+int16_t YamlObject::get(const int16_t &defaultValue, bool *ok) const
+{
+	setOk(ok, false);
+
+	if (type_ != Type::Value)
+		return defaultValue;
+
+	if (value_ == "")
+		return defaultValue;
+
+	char *end;
+
+	errno = 0;
+	int16_t value = std::strtol(value_.c_str(), &end, 10);
+
+	if ('\0' != *end || errno == ERANGE ||
+	    value < std::numeric_limits<int16_t>::min() ||
+	    value > std::numeric_limits<int16_t>::max())
+		return defaultValue;
+
+	setOk(ok, true);
+	return value;
+}
+
+template<>
+uint16_t YamlObject::get(const uint16_t &defaultValue, bool *ok) const
+{
+	setOk(ok, false);
+
+	if (type_ != Type::Value)
+		return defaultValue;
+
+	if (value_ == "")
+		return defaultValue;
+
+	/*
+	 * 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 defaultValue;
+
+	char *end;
+
+	errno = 0;
+	uint16_t value = std::strtoul(value_.c_str(), &end, 10);
+
+	if ('\0' != *end || errno == ERANGE ||
+	    value < std::numeric_limits<uint16_t>::min() ||
+	    value > std::numeric_limits<uint16_t>::max())
+		return defaultValue;
+
+	setOk(ok, true);
+	return value;
+}
+
 template<>
 int32_t YamlObject::get(const int32_t &defaultValue, bool *ok) const
 {
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list