Patch Detail
Show a patch.
GET /api/patches/16158/?format=api
{ "id": 16158, "url": "https://patchwork.libcamera.org/api/patches/16158/?format=api", "web_url": "https://patchwork.libcamera.org/patch/16158/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20220604185939.29163-4-laurent.pinchart@ideasonboard.com>", "date": "2022-06-04T18:59:28", "name": "[libcamera-devel,RFC,v2,03/14] libcamera: yaml_parser: Switch from FILE to File", "commit_ref": null, "pull_url": null, "state": "rfc", "archived": false, "hash": "da12e6e9feed20579818530c6a7d7e185e684b59", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/16158/mbox/", "series": [ { "id": 3161, "url": "https://patchwork.libcamera.org/api/series/3161/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3161", "date": "2022-06-04T18:59:25", "name": "Replace boost JSON parser with libyaml in Raspberry Pi IPA", "version": 2, "mbox": "https://patchwork.libcamera.org/series/3161/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/16158/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/16158/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 6B149C3274\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 4 Jun 2022 19:00:04 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B280165644;\n\tSat, 4 Jun 2022 21:00:03 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8C65C6563F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 4 Jun 2022 21:00:01 +0200 (CEST)", "from pendragon.ideasonboard.com (85-76-79-203-nat.elisa-mobile.fi\n\t[85.76.79.203])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3A13387A;\n\tSat, 4 Jun 2022 21:00:00 +0200 (CEST)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654369203;\n\tbh=gh83vO2M0GAuJE7wr6hbUrqmm2P+xGfstsTSz+FOkRs=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=Nq7gSFJgnoMZvi64PxxL+dyP0/2ibYiqYxbRXkkLB/EEGep49ZsTLRy9lDsziGhgB\n\tdJGS1FUnkQhD/bOAuO+B+4glhXjVD+vA8vsFMfHTCsj6x5TUy2NDcm6SjUVU3nZRzn\n\tuzXlhdun3ZWPTfHA8oz5YAGVauepN/cKj8wfQ5xt8UoOyhNWRUwlnQTRmow71H05Ym\n\t4FhJlQa+IWVufIi1/KncWtPu7nkfj92IVitQrQ05XYHOZr/0Ne+O8OHqMR8IbA7pxt\n\tYcaWh9e+86wpi1Qwpf4Tvbn+EwWMYQ9YdBqoysXf69atA2nj7vVTIyUkg2GK3TBn5h\n\tIqQrw4MdYIkdw==", "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654369201;\n\tbh=gh83vO2M0GAuJE7wr6hbUrqmm2P+xGfstsTSz+FOkRs=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Zi9VxCigGJO7MygHot/a12hdle/wZqu6ya6xe1nKg/HMxm0T9x1JbiK1HqpmT3fqn\n\t2/MD94ThNdWa3VyQNJP8zmQrxnIfnnLPyvecjdHCYiaQKaKlm8B1k4jXEsKx61T/Hr\n\tPnnSZQG/McaG1uujWT6V+0VUDgc242l9mqYhnlow=" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Zi9VxCig\"; dkim-atps=neutral", "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", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [RFC PATCH v2 03/14] libcamera: yaml_parser:\n\tSwitch from FILE to File", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "From": "Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>", "Reply-To": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "THe FILE object isn't very user-friendly as it requires manual close.\nReplace it with File to provide RAII-style resource management in the\nYamlParser API.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/yaml_parser.h | 4 +--\n src/android/camera_hal_config.cpp | 16 +++++-----\n src/libcamera/yaml_parser.cpp | 39 +++++++++++++++++-------\n test/yaml-parser.cpp | 18 +++++------\n 4 files changed, 47 insertions(+), 30 deletions(-)", "diff": "diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex b4f852b1ce54..be5f0914703f 100644\n--- a/include/libcamera/internal/yaml_parser.h\n+++ b/include/libcamera/internal/yaml_parser.h\n@@ -7,7 +7,6 @@\n \n #pragma once\n \n-#include <cstdio>\n #include <map>\n #include <string>\n #include <vector>\n@@ -18,6 +17,7 @@\n \n namespace libcamera {\n \n+class File;\n class YamlParserContext;\n \n class YamlObject\n@@ -82,7 +82,7 @@ private:\n class YamlParser final\n {\n public:\n-\tstatic std::unique_ptr<YamlObject> parse(std::FILE *fh);\n+\tstatic std::unique_ptr<YamlObject> parse(File &file);\n };\n \n } /* namespace libcamera */\ndiff --git a/src/android/camera_hal_config.cpp b/src/android/camera_hal_config.cpp\nindex 8ba8738cc6b6..ac484b8df1bd 100644\n--- a/src/android/camera_hal_config.cpp\n+++ b/src/android/camera_hal_config.cpp\n@@ -10,6 +10,7 @@\n #include <stdlib.h>\n #include <string>\n \n+#include <libcamera/base/file.h>\n #include <libcamera/base/log.h>\n \n #include \"libcamera/internal/yaml_parser.h\"\n@@ -27,7 +28,7 @@ class CameraHalConfig::Private : public Extensible::Private\n public:\n \tPrivate();\n \n-\tint parseConfigFile(FILE *fh, std::map<std::string, CameraConfigData> *cameras);\n+\tint parseConfigFile(File &file, std::map<std::string, CameraConfigData> *cameras);\n \n private:\n \tint parseCameraConfigData(const std::string &cameraId, const YamlObject &);\n@@ -41,7 +42,7 @@ CameraHalConfig::Private::Private()\n {\n }\n \n-int CameraHalConfig::Private::parseConfigFile(FILE *fh,\n+int CameraHalConfig::Private::parseConfigFile(File &file,\n \t\t\t\t\t std::map<std::string, CameraConfigData> *cameras)\n {\n \t/*\n@@ -65,7 +66,7 @@ int CameraHalConfig::Private::parseConfigFile(FILE *fh,\n \n \tcameras_ = cameras;\n \n-\tstd::unique_ptr<YamlObject> root = YamlParser::parse(fh);\n+\tstd::unique_ptr<YamlObject> root = YamlParser::parse(file);\n \tif (!root)\n \t\treturn -EINVAL;\n \n@@ -169,9 +170,9 @@ int CameraHalConfig::parseConfigurationFile()\n \t\treturn -ENOENT;\n \t}\n \n-\tFILE *fh = fopen(filePath.c_str(), \"r\");\n-\tif (!fh) {\n-\t\tint ret = -errno;\n+\tFile file(filePath);\n+\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n+\t\tint ret = file.error();\n \t\tLOG(HALConfig, Error) << \"Failed to open configuration file \"\n \t\t\t\t << filePath << \": \" << strerror(-ret);\n \t\treturn ret;\n@@ -179,8 +180,7 @@ int CameraHalConfig::parseConfigurationFile()\n \n \texists_ = true;\n \n-\tint ret = _d()->parseConfigFile(fh, &cameras_);\n-\tfclose(fh);\n+\tint ret = _d()->parseConfigFile(file, &cameras_);\n \tif (ret)\n \t\treturn -EINVAL;\n \ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 5b872dbb0a2d..85f6694f5fde 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -11,6 +11,7 @@\n #include <errno.h>\n #include <functional>\n \n+#include <libcamera/base/file.h>\n #include <libcamera/base/log.h>\n \n #include <yaml.h>\n@@ -345,7 +346,7 @@ public:\n \tYamlParserContext();\n \t~YamlParserContext();\n \n-\tint init(std::FILE *fh);\n+\tint init(File &file);\n \tint parseContent(YamlObject &yamlObject);\n \n private:\n@@ -358,6 +359,9 @@ private:\n \t};\n \tusing EventPtr = std::unique_ptr<yaml_event_t, EventDeleter>;\n \n+\tstatic int yamlRead(void *data, unsigned char *buffer, size_t size,\n+\t\t\t size_t *sizeRead);\n+\n \tEventPtr nextEvent();\n \n \tvoid readValue(std::string &value, EventPtr event);\n@@ -399,13 +403,13 @@ YamlParserContext::~YamlParserContext()\n * \\param[in] fh The YAML file to parse\n *\n * Prior to parsing the YAML content, the YamlParserContext must be initialized\n- * with an opened FILE to create an internal parser. The FILE needs to stay\n- * valid during the process.\n+ * with a file to create an internal parser. The file needs to stay valid until\n+ * parsing completes.\n *\n * \\return 0 on success or a negative error code otherwise\n * \\retval -EINVAL The parser has failed to initialize\n */\n-int YamlParserContext::init(std::FILE *fh)\n+int YamlParserContext::init(File &file)\n {\n \t/* yaml_parser_initialize returns 1 when it succeededs */\n \tif (!yaml_parser_initialize(&parser_)) {\n@@ -413,11 +417,25 @@ int YamlParserContext::init(std::FILE *fh)\n \t\treturn -EINVAL;\n \t}\n \tparserValid_ = true;\n-\tyaml_parser_set_input_file(&parser_, fh);\n+\tyaml_parser_set_input(&parser_, &YamlParserContext::yamlRead, &file);\n \n \treturn 0;\n }\n \n+int YamlParserContext::yamlRead(void *data, unsigned char *buffer, size_t size,\n+\t\t\t\tsize_t *sizeRead)\n+{\n+\tFile *file = static_cast<File *>(data);\n+\n+\tSpan<unsigned char> buf{ buffer, size };\n+\tssize_t ret = file->read(buf);\n+\tif (ret < 0)\n+\t\treturn 0;\n+\n+\t*sizeRead = ret;\n+\treturn 1;\n+}\n+\n /**\n * \\fn YamlParserContext::nextEvent()\n * \\brief Get the next event\n@@ -655,21 +673,20 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\n */\n \n /**\n- * \\fn YamlParser::parse()\n * \\brief Parse a YAML file as a YamlObject\n- * \\param[in] fh The YAML file to parse\n+ * \\param[in] file The YAML file to parse\n *\n- * The YamlParser::parse() function takes an open FILE, parses its contents, and\n+ * The YamlParser::parse() function takes a file, parses its contents, and\n * returns a pointer to a YamlObject corresponding to the root node of the YAML\n- * document. The caller is responsible for closing the file.\n+ * document.\n *\n * \\return Pointer to result YamlObject on success or nullptr otherwise\n */\n-std::unique_ptr<YamlObject> YamlParser::parse(std::FILE *fh)\n+std::unique_ptr<YamlObject> YamlParser::parse(File &file)\n {\n \tYamlParserContext context;\n \n-\tif (context.init(fh))\n+\tif (context.init(file))\n \t\treturn nullptr;\n \n \tstd::unique_ptr<YamlObject> root(new YamlObject());\ndiff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp\nindex c5b4ddbb19e5..e75f8fe8f642 100644\n--- a/test/yaml-parser.cpp\n+++ b/test/yaml-parser.cpp\n@@ -9,6 +9,8 @@\n #include <string>\n #include <unistd.h>\n \n+#include <libcamera/base/file.h>\n+\n #include \"libcamera/internal/yaml_parser.h\"\n \n #include \"test.h\"\n@@ -69,29 +71,27 @@ protected:\n \tint run()\n \t{\n \t\t/* Test invalid YAML file */\n-\t\tFILE *fh = fopen(invalidYamlFile_.c_str(), \"r\");\n-\t\tif (!fh) {\n+\t\tFile file{ invalidYamlFile_ };\n+\t\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n \t\t\tcerr << \"Fail to open invalid YAML file\" << std::endl;\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\tstd::unique_ptr<YamlObject> root = YamlParser::parse(fh);\n-\t\tfclose(fh);\n-\n+\t\tstd::unique_ptr<YamlObject> root = YamlParser::parse(file);\n \t\tif (root) {\n \t\t\tcerr << \"Invalid YAML file parse successfully\" << std::endl;\n \t\t\treturn TestFail;\n \t\t}\n \n \t\t/* Test YAML file */\n-\t\tfh = fopen(testYamlFile_.c_str(), \"r\");\n-\t\tif (!fh) {\n+\t\tfile.close();\n+\t\tfile.setFileName(testYamlFile_);\n+\t\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n \t\t\tcerr << \"Fail to open test YAML file\" << std::endl;\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\troot = YamlParser::parse(fh);\n-\t\tfclose(fh);\n+\t\troot = YamlParser::parse(file);\n \n \t\tif (!root) {\n \t\t\tcerr << \"Fail to parse test YAML file: \" << std::endl;\n", "prefixes": [ "libcamera-devel", "RFC", "v2", "03/14" ] }