From patchwork Fri Jan 25 21:21:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 395 X-Patchwork-Delegate: niklas.soderlund@ragnatech.se Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6291A60C6A for ; Fri, 25 Jan 2019 22:22:13 +0100 (CET) X-Halon-ID: 4460ee51-20e7-11e9-9adf-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (unknown [89.233.230.99]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 4460ee51-20e7-11e9-9adf-005056917a89; Fri, 25 Jan 2019 22:22:06 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 25 Jan 2019 22:21:53 +0100 Message-Id: <20190125212154.26950-2-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190125212154.26950-1-niklas.soderlund@ragnatech.se> References: <20190125212154.26950-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC 1/2] cam: options: add parser for suboptions X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Jan 2019 21:22:13 -0000 The cam utility will make use of lists of key/value pairs from the command line arguments. This is needed so that a user can among other things describe complex stream format descriptions, which consists of a set of parameters. Signed-off-by: Niklas Söderlund --- src/cam/options.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++ src/cam/options.h | 24 ++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 55c42540f92478e6..3299cdfed69703fa 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -180,3 +180,71 @@ void OptionsParser::Options::clear() { values_.clear(); } + +void SubOptionsParser::addToken(const char *name) +{ + if (!name) + return; + + /* Reject duplicate options. */ + for (const char *option : options_) + if (!strcmp(name, option)) + return; + + options_.push_back(name); +} + +SubOptionsParser::Options SubOptionsParser::parse(const char *argc) +{ + const char **tokens; + char *dupe, *subs, *value; + Options options; + + tokens = new const char *[options_.size() + 1](); + for (unsigned int i = 0; i < options_.size(); i++) + tokens[i] = options_.at(i); + + dupe = strdup(argc); + subs = dupe; + while (*subs != '\0') { + int opt = getsubopt(&subs, (char *const *)tokens, &value); + + if (opt == -1 || !value || value[0] == '\0') { + if (opt == -1) + std::cerr << "Invalid option " << value << std::endl; + else + std::cerr << "Missing argument for option " + << tokens[opt] << std::endl; + + options.clear(); + break; + } + + options.values_[tokens[opt]] = value; + } + + free(dupe); + delete[] tokens; + + return options; +} + +bool SubOptionsParser::Options::valid() const +{ + return !values_.empty(); +} + +bool SubOptionsParser::Options::isSet(std::string opt) const +{ + return values_.find(opt) != values_.end(); +} + +const std::string &SubOptionsParser::Options::operator[](std::string opt) const +{ + return values_.find(opt)->second; +} + +void SubOptionsParser::Options::clear() +{ + values_.clear(); +} diff --git a/src/cam/options.h b/src/cam/options.h index f99ea7300a71c24f..653dda0c76e52251 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -57,4 +57,28 @@ private: std::map optionsMap_; }; +class SubOptionsParser +{ +public: + class Options + { + public: + bool valid() const; + bool isSet(std::string opt) const; + const std::string &operator[](std::string opt) const; + + private: + friend class SubOptionsParser; + std::map values_; + void clear(); + }; + + void addToken(const char *token); + + Options parse(const char *argc); + +private: + std::vector options_; +}; + #endif /* __CAM_OPTIONS_H__ */