From patchwork Sat Jun 4 18:59:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16167 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 7329EC327C for ; Sat, 4 Jun 2022 19:00:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0E2E06563F; Sat, 4 Jun 2022 21:00:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654369219; bh=TqHiVr4QGQxzOpjf0XHtdsxLe3op6/BtgX5oVoQx3UM=; 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=lBmfT72x//xbgBDzpesbhnZVgzzX5SnARlpCI9THg8drZxqpLXdi1TZcGjfroXXoa fmKwulcaMtN8LZLYWnlNDbySIeH1KzrBXzUmF3LCIaRs/JzkmqbmSmpMFLag3U9K4z MpkfoUqDsejxVPIRRvni4I2DP0sOv9yO2mlMCchxL8fYvpBYIIrZHdXDnGTKTUto5L f+l2wkACqF4C/18w2aB1KJulrpVlRJpDHFWobNs3nZQ0UWpIG4wT5uCyWWEuY/yEgq AyebuxqBKD4lEhfssk9QcXpGidWvNnTjT1BW8AR/phhHboERzQR9pnTJ/GW5Ysai9l 8GxZz4nFOV6Lg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9A3A165644 for ; Sat, 4 Jun 2022 21:00:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EyHhAKPI"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (85-76-79-203-nat.elisa-mobile.fi [85.76.79.203]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 662F36D4; Sat, 4 Jun 2022 21:00:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1654369217; bh=TqHiVr4QGQxzOpjf0XHtdsxLe3op6/BtgX5oVoQx3UM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EyHhAKPIqo9QWObfkxWrGc54i9Lxh6zwzRT5YpXEX+vRt1DKSW5Sq6I40xeoUbHsA HlbPSnPFafGT4pFh8r9Ey26NcrOKaY3H21FzQYKXo3osZ+baseo/aVsHV/6eNamF2H ofgtQoJtPvIorATAU/GJwFrjLOPTXkR97fMULsS4= To: libcamera-devel@lists.libcamera.org Date: Sat, 4 Jun 2022 21:59:37 +0300 Message-Id: <20220604185939.29163-13-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220604185939.29163-1-laurent.pinchart@ideasonboard.com> References: <20220604185939.29163-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH v2 12/14] libcamera: yaml_parser: Add get() specializations for 16-bit integers 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" Extend the YamlObject::get() function template to support 16-bit integers. Signed-off-by: Laurent Pinchart --- 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::value || std::is_same::value || + std::is_same::value || + std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::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::min() || + value > std::numeric_limits::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::min() || + value > std::numeric_limits::max()) + return defaultValue; + + setOk(ok, true); + return value; +} + template<> int32_t YamlObject::get(const int32_t &defaultValue, bool *ok) const {