From d6d0a675bfe27420d14394ef6b0da5247ea63d87 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 24 May 2022 18:29:32 +0300 Subject: libcamera: yaml_parser: Switch from FILE to File 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 --- src/android/camera_hal_config.cpp | 16 ++++++++-------- src/libcamera/yaml_parser.cpp | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/android/camera_hal_config.cpp b/src/android/camera_hal_config.cpp index 8ba8738c..ac484b8d 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 5b872dbb..85f6694f 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()); -- cgit v1.2.1