From a8113fb3a89984cc65d51436480cee45b60543e8 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 20 Oct 2022 01:25:45 +0300 Subject: apps: Share common source between applications Multiple source files in the src/apps/cam/ directory are used by cam, qcam and lc-compliance. They are compiled separately for each application. Move them to a new src/apps/common/ directory and compile them in a static library to decrease the number of compilation operations. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Kieran Bingham --- src/apps/common/options.h | 157 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 src/apps/common/options.h (limited to 'src/apps/common/options.h') diff --git a/src/apps/common/options.h b/src/apps/common/options.h new file mode 100644 index 00000000..4ddd4987 --- /dev/null +++ b/src/apps/common/options.h @@ -0,0 +1,157 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * options.h - cam - Options parsing + */ + +#pragma once + +#include +#include +#include +#include +#include + +class KeyValueParser; +class OptionValue; +struct Option; + +enum OptionArgument { + ArgumentNone, + ArgumentRequired, + ArgumentOptional, +}; + +enum OptionType { + OptionNone, + OptionInteger, + OptionString, + OptionKeyValue, +}; + +template +class OptionsBase +{ +public: + OptionsBase() : valid_(false) {} + + bool empty() const; + bool valid() const; + bool isSet(const T &opt) const; + const OptionValue &operator[](const T &opt) const; + + void invalidate(); + +private: + friend class KeyValueParser; + friend class OptionsParser; + + bool parseValue(const T &opt, const Option &option, const char *value); + + std::map values_; + bool valid_; +}; + +class KeyValueParser +{ +public: + class Options : public OptionsBase + { + }; + + KeyValueParser(); + virtual ~KeyValueParser(); + + bool addOption(const char *name, OptionType type, const char *help, + OptionArgument argument = ArgumentNone); + + virtual Options parse(const char *arguments); + +private: + KeyValueParser(const KeyValueParser &) = delete; + KeyValueParser &operator=(const KeyValueParser &) = delete; + + friend class OptionsParser; + unsigned int maxOptionLength() const; + void usage(int indent); + + std::map optionsMap_; +}; + +class OptionsParser +{ +public: + class Options : public OptionsBase + { + }; + + OptionsParser(); + ~OptionsParser(); + + bool addOption(int opt, OptionType type, const char *help, + const char *name = nullptr, + OptionArgument argument = ArgumentNone, + const char *argumentName = nullptr, bool array = false, + int parent = 0); + bool addOption(int opt, KeyValueParser *parser, const char *help, + const char *name = nullptr, bool array = false, + int parent = 0); + + Options parse(int argc, char *argv[]); + void usage(); + +private: + OptionsParser(const OptionsParser &) = delete; + OptionsParser &operator=(const OptionsParser &) = delete; + + void usageOptions(const std::list