From 61349dcf5fbfeab888ea345ebec3d887777a2782 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 8 Oct 2018 23:01:16 +0300 Subject: Add support for openssl qualified options --- bpkg/options-types.hxx | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'bpkg/options-types.hxx') diff --git a/bpkg/options-types.hxx b/bpkg/options-types.hxx index 373ef5e..3438be1 100644 --- a/bpkg/options-types.hxx +++ b/bpkg/options-types.hxx @@ -5,6 +5,10 @@ #ifndef BPKG_OPTIONS_TYPES_HXX #define BPKG_OPTIONS_TYPES_HXX +#include +#include +#include // move() + namespace bpkg { enum class auth @@ -13,6 +17,60 @@ namespace bpkg remote, all }; + + // Qualified options. + // + // An option that uses this type can have its values qualified using the + // : form, for example, '--option foo:bar' An unqualified + // value that contains a colon can be specified as qualified with an empty + // qualifier, for example, '--option :http://example.org'. Unqualified + // values apply to all the qualifiers in the order specified. + // + // The second template argument is a NULL-terminated list of valid qualifier + // strings, for example: + // + // const char* option_qualifiers[] = {"foo", "bar", nullptr}; + // + template + class qualified_option: public std::map + { + public: + using base_type = std::map; + + template + explicit + qualified_option (T v) {this->emplace ("", std::move (v));} + + qualified_option (): qualified_option (V ()) {} + + using base_type::operator[]; + + const V& + operator[] (const string& q) const + { + auto verify = [&q] () + { + for (const char** p (Q); *p != nullptr; ++p) + { + if (q == *p) + return true; + } + return q.empty (); + }; + + assert (verify ()); + + typename base_type::const_iterator i (this->find (q)); + + if (i == this->end ()) + i = this->find (""); + + assert (i != this->end ()); + return i->second; + } + }; + + extern const char* openssl_commands[]; } #endif // BPKG_OPTIONS_TYPES_HXX -- cgit v1.1