From patchwork Tue May 24 22:58:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16036 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 C93C0C326F for ; Tue, 24 May 2022 22:58:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A09496566D; Wed, 25 May 2022 00:58:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1653433119; bh=jAs5MXsa9CbCjWdFhEKz7i6feQcOFMCR3aVzCVIcSWU=; 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=QlsAxkiboHb5WORUtCCSF8LjEBsCaCZiNYOVbNKlRhjXtlwsYPIvS9aGNJOb/r0bV 0MlfES6S73zHF1qNOdi/nr0UThPzpSQH++Z0Q/cPu/G7ow72HndEpAXr0ip9Eu4WPu Bd96kfRitgiB51zsYefDZQQoq31+Qu4OpT6VHgICCuUoyV5vjhwEFVCwP3KyZW/NfK UrHhirinf1NwxOUqjBCBjcaAbiPQAB48R/wwJ8kuXvvQa/J9HdAqY88vPrXEcaOQ9/ yjtB25/SW9GhoulgO0Tw+D9vFtRRGVcRNY/uenAtKSWJJYVZi9yvbypRDZBbrOOLWo ubXwzcYv9nw1A== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8E27265674 for ; Wed, 25 May 2022 00:58:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="RzEepIhT"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (ip-109-40-241-133.web.vodafone.de [109.40.241.133]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D5693A58; Wed, 25 May 2022 00:58:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1653433110; bh=jAs5MXsa9CbCjWdFhEKz7i6feQcOFMCR3aVzCVIcSWU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RzEepIhTrERlSZEgQKAdZ9xFpfiwOgbw3RrhUEhV97v6Xv8SXfNIoteAwIxLqK1rz GNRoU9bluIksVy756vkTEGUzaw1KDFB/O826sg5WRbucPm9vxRZEIwLGKY57CiOYtU Z7p/xGs3ykNbUOOFj/9FmaQ+ie8HsMaVYSuEYJtE= To: libcamera-devel@lists.libcamera.org Date: Wed, 25 May 2022 01:58:14 +0300 Message-Id: <20220524225816.6830-11-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220524225816.6830-1-laurent.pinchart@ideasonboard.com> References: <20220524225816.6830-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 10/12] 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 79537ff2ea7c..dd543a138af5 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -154,6 +154,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 f0b5eb96449b..57e8b52965e8 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -138,6 +138,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 {