From patchwork Tue Aug 16 01:54:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17132 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2CA35C3272 for ; Tue, 16 Aug 2022 01:54:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EBC2E61FD2; Tue, 16 Aug 2022 03:54:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660614878; bh=MnNYIntuyymR7unE1fXXD+PG9BX+3m1CqgAiFujFWp0=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=g0ih25sGKpz4xFlkF3LOVhhS0jkd6xcx+h1CYmEc9gD8I2ZW3XYbhfJHM9cOwD2wg IdR+j5OTybHnYRgc9D0P7adwoKRTornTir8xxa5UwRj7Gfea9UfyijI9xF5P7FsmXu 83szzY1uo7cTnNkpdtXtf89GLkw3DSipv0u9c1deLK1cMKv+ZPqmR0l3ITONgF0Uyz MHVjHjalaH8o31abRYflLJZi2+Oh9MCjWXcSNnI7BmWY/kRZVP2iveczbX6418fi4I oOGvH38YkbvvWqTcJh3g/C8Micqe5fNlTWBucYfSe5lUtZlzKftXfyfFOvj4GVY7q7 /bhhjYcfyGOCw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5DB0E61FC4 for ; Tue, 16 Aug 2022 03:54:35 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="S1OJDx29"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F1CB9496; Tue, 16 Aug 2022 03:54:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1660614875; bh=MnNYIntuyymR7unE1fXXD+PG9BX+3m1CqgAiFujFWp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S1OJDx29WJsjxCN2rDZIsbL2yFxiuNzGFaJXfRZDbWFIRp39+Fzq5UPFzNI4ZSHk0 E1v9xmfpYUIs1/+vAwrH2NAPY8iAprMxyBGu9HM4CYahD44bPzOarHSXVw03UMXMpw adTPAEHQXd9swXQYdwqAsZDh9hXmnppRtb0lYLVQ= To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Aug 2022 04:54:10 +0300 Message-Id: <20220816015414.7462-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> References: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 5/9] libcamera: yaml_parser: Enable YamlObject::get() for int8_t and uint8_t X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- include/libcamera/internal/yaml_parser.h | 4 ++ src/libcamera/yaml_parser.cpp | 59 ++++++++++++++++ test/yaml-parser.cpp | 86 ++++++++++++++++++++---- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 5ba777d364fa..8ca71df867ab 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -166,6 +166,8 @@ public: std::enable_if_t< std::is_same_v || std::is_same_v || + std::is_same_v || + std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || @@ -188,6 +190,8 @@ public: std::enable_if_t< std::is_same_v || std::is_same_v || + std::is_same_v || + std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v || diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index f928b7238a19..85a52c05e682 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; diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 28f8cc8822b3..2d92463aed98 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -24,6 +24,8 @@ using namespace std; static const string testYaml = "string: libcamera\n" "double: 3.14159\n" + "int8_t: -100\n" + "uint8_t: 100\n" "int16_t: -1000\n" "uint16_t: 1000\n" "int32_t: -100000\n" @@ -76,6 +78,8 @@ protected: enum class Type { String, + Int8, + UInt8, Int16, UInt16, Int32, @@ -90,10 +94,13 @@ protected: { bool isList = type == Type::List || type == Type::Size; bool isScalar = !isList && type != Type::Dictionary; + bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; bool isInteger32 = type == Type::Int32 || type == Type::UInt32; - bool isInteger = isInteger16 || isInteger32; - bool isSigned = type == Type::Int16 || type == Type::Int32; + bool isIntegerUpTo16 = isInteger8 || isInteger16; + bool isIntegerUpTo32 = isIntegerUpTo16 || isInteger32; + bool isSigned = type == Type::Int8 || type == Type::Int16 || + type == Type::Int32; if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) { std::cerr @@ -124,35 +131,49 @@ protected: return TestFail; } - if (!isInteger16 && obj.get()) { + if (!isInteger8 && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "int8_t" << std::endl; + return TestFail; + } + + if ((!isInteger8 || isSigned) && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "uint8_t" << std::endl; + return TestFail; + } + + if (!isIntegerUpTo16 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int16_t" << std::endl; return TestFail; } - if ((!isInteger16 || isSigned) && obj.get()) { + if ((!isIntegerUpTo16 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint16_t" << std::endl; return TestFail; } - if (!isInteger && obj.get()) { + if (!isIntegerUpTo32 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int32_t" << std::endl; return TestFail; } - if ((!isInteger || isSigned) && obj.get()) { + if ((!isIntegerUpTo32 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint32_t" << std::endl; return TestFail; } - if (!isInteger && type != Type::Double && obj.get()) { + if (!isIntegerUpTo32 && type != Type::Double && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "double" << std::endl; @@ -174,8 +195,10 @@ protected: { uint64_t unsignedValue = static_cast(value); std::string strValue = std::to_string(value); + bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; - bool isSigned = type == Type::Int16 || type == Type::Int32; + bool isSigned = type == Type::Int8 || type == Type::Int16 || + type == Type::Int32; /* All integers can be parsed as strings or double. */ @@ -195,7 +218,27 @@ protected: return TestFail; } - if (isInteger16) { + if (isInteger8) { + if (obj.get().value_or(0) != value || + obj.get(0) != value) { + std::cerr + << "Object " << name << " failed to parse as " + << "int8_t" << std::endl; + return TestFail; + } + } + + if (isInteger8 && !isSigned) { + if (obj.get().value_or(0) != unsignedValue || + obj.get(0) != unsignedValue) { + std::cerr + << "Object " << name << " failed to parse as " + << "uint8_t" << std::endl; + return TestFail; + } + } + + if (isInteger8 || isInteger16) { if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr @@ -205,7 +248,7 @@ protected: } } - if (isInteger16 && !isSigned) { + if ((isInteger8 || isInteger16) && !isSigned) { if (obj.get().value_or(0) != unsignedValue || obj.get(0) != unsignedValue) { std::cerr @@ -272,8 +315,9 @@ protected: } std::vector rootElemNames = { - "string", "double", "int16_t", "uint16_t", "int32_t", - "uint32_t", "size", "list", "dictionary", "level1", + "string", "double", "int8_t", "uint8_t", "int16_t", + "uint16_t", "int32_t", "uint32_t", "size", "list", + "dictionary", "level1", }; for (const char *name : rootElemNames) { @@ -296,6 +340,24 @@ protected: return TestFail; } + /* Test int8_t object */ + auto &int8Obj = (*root)["int8_t"]; + + if (testObjectType(int8Obj, "int8_t", Type::Int8) != TestPass) + return TestFail; + + if (testIntegerObject(int8Obj, "int8_t", Type::Int8, -100) != TestPass) + return TestFail; + + /* Test uint8_t object */ + auto &uint8Obj = (*root)["uint8_t"]; + + if (testObjectType(uint8Obj, "uint8_t", Type::UInt8) != TestPass) + return TestFail; + + if (testIntegerObject(uint8Obj, "uint8_t", Type::UInt8, 100) != TestPass) + return TestFail; + /* Test int16_t object */ auto &int16Obj = (*root)["int16_t"];