summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-05-25 01:58:14 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-06-20 15:12:27 +0300
commitc7d260c03abdf8963b5b0f2a8af7481ca08d6c1a (patch)
tree2446ef856c09c86339f6d6be9e34f66d8c419227 /src
parent839c4a5a480731bfbae231797fc974f5a3749b69 (diff)
libcamera: yaml_parser: Add get() specializations for 16-bit integers
Extend the YamlObject::get() function template to support 16-bit integers. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Han-Lin Chen <hanlinchen@chromium.org>
Diffstat (limited to 'src')
-rw-r--r--src/libcamera/yaml_parser.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index bd4b501b..5c45e44e 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -139,6 +139,67 @@ bool YamlObject::get(const bool &defaultValue, bool *ok) const
}
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
{
setOk(ok, false);