From fcb0ea001a2d3350068410c3bd2f940694a339a0 Mon Sep 17 00:00:00 2001 From: Han-Lin Chen Date: Wed, 27 Apr 2022 22:09:27 +0800 Subject: libcamera: Introduce YamlParser as a helper to parse yaml files Introduce YamlParser as a helper to convert contents of a yaml file to a tree based structure for easier reading, and to avoid writing parser with raw yaml tokens. The class is based on libyaml, and only support reading but not writing a yaml file. The interface is inspired by Json::Value class from jsoncpp: http://jsoncpp.sourceforge.net/class_json_1_1_value.html Signed-off-by: Han-Lin Chen Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart --- include/libcamera/internal/meson.build | 1 + include/libcamera/internal/yaml_parser.h | 87 ++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 include/libcamera/internal/yaml_parser.h (limited to 'include') diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index c9e055d4..7a780d48 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -42,4 +42,5 @@ libcamera_internal_headers = files([ 'v4l2_pixelformat.h', 'v4l2_subdevice.h', 'v4l2_videodevice.h', + 'yaml_parser.h', ]) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h new file mode 100644 index 00000000..3a4f3052 --- /dev/null +++ b/include/libcamera/internal/yaml_parser.h @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Google Inc. + * + * yaml_parser.h - libcamera YAML parsing helper + */ + +#pragma once + +#include +#include +#include +#include + +#include + +#include + +namespace libcamera { + +class YamlParserContext; + +class YamlObject +{ +public: + YamlObject(); + ~YamlObject(); + + bool isValue() const + { + return type_ == Value; + } + bool isList() const + { + return type_ == List; + } + bool isDictionary() const + { + return type_ == Dictionary; + } + +#ifndef __DOXYGEN__ + template::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value> * = nullptr> +#else + template +#endif + T get(const T &defaultValue, bool *ok = nullptr) const; + + std::size_t size() const; + const YamlObject &operator[](std::size_t index) const; + + bool contains(const std::string &key) const; + const YamlObject &operator[](const std::string &key) const; + std::vector memberNames() const; + +private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(YamlObject) + + friend class YamlParserContext; + + enum Type { + Dictionary, + List, + Value, + }; + + Type type_; + + std::string value_; + std::vector> list_; + std::map> dictionary_; +}; + +class YamlParser final +{ +public: + static std::unique_ptr parse(std::FILE *fh); +}; + +} /* namespace libcamera */ -- cgit v1.2.1