From patchwork Sat Jun 4 18:59:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16158 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 6B149C3274 for ; Sat, 4 Jun 2022 19:00:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B280165644; Sat, 4 Jun 2022 21:00:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654369203; bh=gh83vO2M0GAuJE7wr6hbUrqmm2P+xGfstsTSz+FOkRs=; 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=Nq7gSFJgnoMZvi64PxxL+dyP0/2ibYiqYxbRXkkLB/EEGep49ZsTLRy9lDsziGhgB dJGS1FUnkQhD/bOAuO+B+4glhXjVD+vA8vsFMfHTCsj6x5TUy2NDcm6SjUVU3nZRzn uzXlhdun3ZWPTfHA8oz5YAGVauepN/cKj8wfQ5xt8UoOyhNWRUwlnQTRmow71H05Ym 4FhJlQa+IWVufIi1/KncWtPu7nkfj92IVitQrQ05XYHOZr/0Ne+O8OHqMR8IbA7pxt YcaWh9e+86wpi1Qwpf4Tvbn+EwWMYQ9YdBqoysXf69atA2nj7vVTIyUkg2GK3TBn5h IqQrw4MdYIkdw== 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 8C65C6563F for ; Sat, 4 Jun 2022 21:00:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Zi9VxCig"; 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 3A13387A; Sat, 4 Jun 2022 21:00:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1654369201; bh=gh83vO2M0GAuJE7wr6hbUrqmm2P+xGfstsTSz+FOkRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zi9VxCigGJO7MygHot/a12hdle/wZqu6ya6xe1nKg/HMxm0T9x1JbiK1HqpmT3fqn 2/MD94ThNdWa3VyQNJP8zmQrxnIfnnLPyvecjdHCYiaQKaKlm8B1k4jXEsKx61T/Hr PnnSZQG/McaG1uujWT6V+0VUDgc242l9mqYhnlow= To: libcamera-devel@lists.libcamera.org Date: Sat, 4 Jun 2022 21:59:28 +0300 Message-Id: <20220604185939.29163-4-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 03/14] libcamera: yaml_parser: Switch from FILE to File 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 FILE object isn't very user-friendly as it requires manual close. Replace it with File to provide RAII-style resource management in the YamlParser API. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder --- include/libcamera/internal/yaml_parser.h | 4 +-- src/android/camera_hal_config.cpp | 16 +++++----- src/libcamera/yaml_parser.cpp | 39 +++++++++++++++++------- test/yaml-parser.cpp | 18 +++++------ 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index b4f852b1ce54..be5f0914703f 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -7,7 +7,6 @@ #pragma once -#include #include #include #include @@ -18,6 +17,7 @@ namespace libcamera { +class File; class YamlParserContext; class YamlObject @@ -82,7 +82,7 @@ private: class YamlParser final { public: - static std::unique_ptr parse(std::FILE *fh); + static std::unique_ptr parse(File &file); }; } /* namespace libcamera */ diff --git a/src/android/camera_hal_config.cpp b/src/android/camera_hal_config.cpp index 8ba8738cc6b6..ac484b8df1bd 100644 --- a/src/android/camera_hal_config.cpp +++ b/src/android/camera_hal_config.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include "libcamera/internal/yaml_parser.h" @@ -27,7 +28,7 @@ class CameraHalConfig::Private : public Extensible::Private public: Private(); - int parseConfigFile(FILE *fh, std::map *cameras); + int parseConfigFile(File &file, std::map *cameras); private: int parseCameraConfigData(const std::string &cameraId, const YamlObject &); @@ -41,7 +42,7 @@ CameraHalConfig::Private::Private() { } -int CameraHalConfig::Private::parseConfigFile(FILE *fh, +int CameraHalConfig::Private::parseConfigFile(File &file, std::map *cameras) { /* @@ -65,7 +66,7 @@ int CameraHalConfig::Private::parseConfigFile(FILE *fh, cameras_ = cameras; - std::unique_ptr root = YamlParser::parse(fh); + std::unique_ptr root = YamlParser::parse(file); if (!root) return -EINVAL; @@ -169,9 +170,9 @@ int CameraHalConfig::parseConfigurationFile() return -ENOENT; } - FILE *fh = fopen(filePath.c_str(), "r"); - if (!fh) { - int ret = -errno; + File file(filePath); + if (!file.open(File::OpenModeFlag::ReadOnly)) { + int ret = file.error(); LOG(HALConfig, Error) << "Failed to open configuration file " << filePath << ": " << strerror(-ret); return ret; @@ -179,8 +180,7 @@ int CameraHalConfig::parseConfigurationFile() exists_ = true; - int ret = _d()->parseConfigFile(fh, &cameras_); - fclose(fh); + int ret = _d()->parseConfigFile(file, &cameras_); if (ret) return -EINVAL; diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 5b872dbb0a2d..85f6694f5fde 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -345,7 +346,7 @@ public: YamlParserContext(); ~YamlParserContext(); - int init(std::FILE *fh); + int init(File &file); int parseContent(YamlObject &yamlObject); private: @@ -358,6 +359,9 @@ private: }; using EventPtr = std::unique_ptr; + static int yamlRead(void *data, unsigned char *buffer, size_t size, + size_t *sizeRead); + EventPtr nextEvent(); void readValue(std::string &value, EventPtr event); @@ -399,13 +403,13 @@ YamlParserContext::~YamlParserContext() * \param[in] fh The YAML file to parse * * Prior to parsing the YAML content, the YamlParserContext must be initialized - * with an opened FILE to create an internal parser. The FILE needs to stay - * valid during the process. + * with a file to create an internal parser. The file needs to stay valid until + * parsing completes. * * \return 0 on success or a negative error code otherwise * \retval -EINVAL The parser has failed to initialize */ -int YamlParserContext::init(std::FILE *fh) +int YamlParserContext::init(File &file) { /* yaml_parser_initialize returns 1 when it succeededs */ if (!yaml_parser_initialize(&parser_)) { @@ -413,11 +417,25 @@ int YamlParserContext::init(std::FILE *fh) return -EINVAL; } parserValid_ = true; - yaml_parser_set_input_file(&parser_, fh); + yaml_parser_set_input(&parser_, &YamlParserContext::yamlRead, &file); return 0; } +int YamlParserContext::yamlRead(void *data, unsigned char *buffer, size_t size, + size_t *sizeRead) +{ + File *file = static_cast(data); + + Span buf{ buffer, size }; + ssize_t ret = file->read(buf); + if (ret < 0) + return 0; + + *sizeRead = ret; + return 1; +} + /** * \fn YamlParserContext::nextEvent() * \brief Get the next event @@ -655,21 +673,20 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even */ /** - * \fn YamlParser::parse() * \brief Parse a YAML file as a YamlObject - * \param[in] fh The YAML file to parse + * \param[in] file The YAML file to parse * - * The YamlParser::parse() function takes an open FILE, parses its contents, and + * The YamlParser::parse() function takes a file, parses its contents, and * returns a pointer to a YamlObject corresponding to the root node of the YAML - * document. The caller is responsible for closing the file. + * document. * * \return Pointer to result YamlObject on success or nullptr otherwise */ -std::unique_ptr YamlParser::parse(std::FILE *fh) +std::unique_ptr YamlParser::parse(File &file) { YamlParserContext context; - if (context.init(fh)) + if (context.init(file)) return nullptr; std::unique_ptr root(new YamlObject()); diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index c5b4ddbb19e5..e75f8fe8f642 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -9,6 +9,8 @@ #include #include +#include + #include "libcamera/internal/yaml_parser.h" #include "test.h" @@ -69,29 +71,27 @@ protected: int run() { /* Test invalid YAML file */ - FILE *fh = fopen(invalidYamlFile_.c_str(), "r"); - if (!fh) { + File file{ invalidYamlFile_ }; + if (!file.open(File::OpenModeFlag::ReadOnly)) { cerr << "Fail to open invalid YAML file" << std::endl; return TestFail; } - std::unique_ptr root = YamlParser::parse(fh); - fclose(fh); - + std::unique_ptr root = YamlParser::parse(file); if (root) { cerr << "Invalid YAML file parse successfully" << std::endl; return TestFail; } /* Test YAML file */ - fh = fopen(testYamlFile_.c_str(), "r"); - if (!fh) { + file.close(); + file.setFileName(testYamlFile_); + if (!file.open(File::OpenModeFlag::ReadOnly)) { cerr << "Fail to open test YAML file" << std::endl; return TestFail; } - root = YamlParser::parse(fh); - fclose(fh); + root = YamlParser::parse(file); if (!root) { cerr << "Fail to parse test YAML file: " << std::endl;