2018-02-03 03:51:57 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
2018-02-06 22:03:38 +01:00
|
|
|
#define HAVE_IRCD_UTIL_PARAMS_H
|
2018-02-03 03:51:57 +01:00
|
|
|
|
2018-02-06 22:03:38 +01:00
|
|
|
// This file is not part of the standard include stack. It is included
|
|
|
|
// manually as needed.
|
|
|
|
|
2019-12-11 02:49:09 +01:00
|
|
|
namespace ircd
|
2018-02-06 22:03:38 +01:00
|
|
|
{
|
2019-12-11 02:49:09 +01:00
|
|
|
inline namespace util
|
|
|
|
{
|
|
|
|
struct params;
|
|
|
|
}
|
|
|
|
}
|
2018-02-06 22:03:38 +01:00
|
|
|
|
2019-03-10 22:25:10 +01:00
|
|
|
struct ircd::util::params
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
2019-12-11 02:49:09 +01:00
|
|
|
static const size_t MAX
|
|
|
|
{
|
|
|
|
12
|
|
|
|
};
|
|
|
|
|
2019-03-10 22:25:10 +01:00
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
IRCD_EXCEPTION(error, missing)
|
|
|
|
IRCD_EXCEPTION(error, invalid)
|
|
|
|
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view in;
|
2019-12-11 03:22:26 +01:00
|
|
|
string_view prefix;
|
|
|
|
const char *sep {" "};
|
2019-12-11 02:49:09 +01:00
|
|
|
std::array<string_view, MAX> names;
|
2018-02-03 03:51:57 +01:00
|
|
|
|
2020-11-28 01:56:56 +01:00
|
|
|
bool for_each_pararg(const token_view &) const;
|
|
|
|
bool for_each_posarg(const token_view &) const;
|
2019-12-11 02:49:09 +01:00
|
|
|
string_view name(const size_t &i) const;
|
2018-08-16 07:49:15 +02:00
|
|
|
size_t name(const string_view &) const;
|
2018-02-03 03:51:57 +01:00
|
|
|
|
|
|
|
public:
|
2019-12-11 03:22:26 +01:00
|
|
|
size_t count_pararg() const;
|
2018-02-03 03:51:57 +01:00
|
|
|
size_t count() const;
|
2018-08-16 07:49:15 +02:00
|
|
|
|
2019-12-11 03:22:26 +01:00
|
|
|
bool has(const string_view ¶m) const;
|
|
|
|
string_view get(const string_view ¶m) const;
|
|
|
|
|
2018-08-16 07:49:15 +02:00
|
|
|
// Get positional argument by position index
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view operator[](const size_t &i) const; // returns empty
|
2018-02-03 03:51:57 +01:00
|
|
|
template<class T> T at(const size_t &i, const T &def) const; // throws invalid
|
|
|
|
template<class T> T at(const size_t &i) const; // throws missing or invalid
|
2018-02-06 22:03:38 +01:00
|
|
|
string_view at(const size_t &i) const; // throws missing
|
2018-02-03 03:51:57 +01:00
|
|
|
|
2018-08-16 07:49:15 +02:00
|
|
|
// Get positional argument by name->index convenience.
|
|
|
|
string_view operator[](const string_view &) const;
|
|
|
|
template<class T> T at(const string_view &, const T &def) const;
|
|
|
|
template<class T> T at(const string_view &) const;
|
|
|
|
string_view at(const string_view &) const;
|
|
|
|
|
2018-02-06 22:03:38 +01:00
|
|
|
params(const string_view &in,
|
2018-02-03 03:51:57 +01:00
|
|
|
const char *const &sep,
|
2019-12-11 02:49:09 +01:00
|
|
|
const std::array<string_view, MAX> &names = {});
|
2019-12-11 03:22:26 +01:00
|
|
|
|
|
|
|
params(const string_view &in,
|
|
|
|
const std::pair<const char *, const char *> &sep,
|
|
|
|
const std::array<string_view, MAX> &names = {});
|
2018-02-03 03:51:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::params(const string_view &in,
|
|
|
|
const char *const &sep,
|
2019-12-11 02:49:09 +01:00
|
|
|
const std::array<string_view, MAX> &names)
|
2018-02-03 03:51:57 +01:00
|
|
|
:in{in}
|
|
|
|
,sep{sep}
|
|
|
|
,names{names}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-11 03:22:26 +01:00
|
|
|
inline
|
|
|
|
ircd::util::params::params(const string_view &in,
|
|
|
|
const std::pair<const char *, const char *> &sep,
|
|
|
|
const std::array<string_view, MAX> &names)
|
|
|
|
:in{in}
|
|
|
|
,prefix{sep.second}
|
|
|
|
,sep{sep.first}
|
|
|
|
,names{names}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-16 07:49:15 +02:00
|
|
|
template<class T>
|
|
|
|
T
|
|
|
|
ircd::util::params::at(const string_view &name,
|
|
|
|
const T &def)
|
2019-12-11 03:22:26 +01:00
|
|
|
const try
|
2018-08-16 07:49:15 +02:00
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
const auto &val(get(name));
|
|
|
|
return val? lex_cast<T>(val): def;
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &)
|
|
|
|
{
|
|
|
|
return def;
|
2018-08-16 07:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
|
|
|
ircd::util::params::at(const string_view &name)
|
2019-12-11 03:22:26 +01:00
|
|
|
const try
|
2018-08-16 07:49:15 +02:00
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
return lex_cast<T>(get(name));
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw invalid
|
|
|
|
{
|
|
|
|
"parameter <%s> :%s",
|
|
|
|
name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-08-16 07:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::util::params::at(const string_view &name)
|
|
|
|
const
|
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
get(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw missing
|
|
|
|
{
|
|
|
|
"required parameter <%s>",
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2018-08-16 07:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::util::params::operator[](const string_view &name)
|
|
|
|
const
|
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
return get(name);
|
2018-08-16 07:49:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 03:51:57 +01:00
|
|
|
template<class T>
|
|
|
|
T
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i,
|
|
|
|
const T &def)
|
2018-02-03 03:51:57 +01:00
|
|
|
const try
|
|
|
|
{
|
|
|
|
return count() > i? at<T>(i) : def;
|
|
|
|
}
|
2018-02-06 22:03:38 +01:00
|
|
|
catch(const bad_lex_cast &e)
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
return def;
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const try
|
|
|
|
{
|
2018-02-06 22:03:38 +01:00
|
|
|
return lex_cast<T>(at(i));
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
2018-02-06 22:03:38 +01:00
|
|
|
catch(const bad_lex_cast &e)
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
2018-08-16 07:49:15 +02:00
|
|
|
throw invalid
|
|
|
|
{
|
|
|
|
"parameter #%zu <%s>", i, name(i)
|
|
|
|
};
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::at(const size_t &i)
|
2019-12-11 03:22:26 +01:00
|
|
|
const
|
2018-02-03 03:51:57 +01:00
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
const string_view ret
|
2018-08-16 07:49:15 +02:00
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
this->operator[](i)
|
2018-08-16 07:49:15 +02:00
|
|
|
};
|
2019-12-11 03:22:26 +01:00
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw missing
|
|
|
|
{
|
|
|
|
"required parameter #%zu <%s>",
|
|
|
|
i,
|
|
|
|
name(i)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::operator[](const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
size_t j{i};
|
|
|
|
string_view ret;
|
|
|
|
for_each_posarg([&ret, &j]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
if(j-- == 0)
|
|
|
|
{
|
|
|
|
ret = token;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::string_view
|
|
|
|
ircd::util::params::get(const string_view &arg)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(prefix && startswith(arg, prefix))
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
for_each_pararg([&ret, &arg]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
if(startswith(token, arg))
|
|
|
|
{
|
|
|
|
ret = token;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else return operator[](this->name(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::util::params::has(const string_view &arg)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(prefix && startswith(arg, prefix))
|
|
|
|
return !for_each_pararg([&arg]
|
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
return !startswith(token, arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
return count() > this->name(arg);
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::count()
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
2019-12-11 03:22:26 +01:00
|
|
|
size_t ret{0};
|
|
|
|
for_each_posarg([&ret](const string_view &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ircd::util::params::count_pararg()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each_pararg([&ret](const string_view &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
|
|
|
|
2018-08-16 07:49:15 +02:00
|
|
|
inline size_t
|
|
|
|
ircd::util::params::name(const string_view &name)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return util::index(begin(names), end(names), name);
|
|
|
|
}
|
|
|
|
|
2019-12-11 02:49:09 +01:00
|
|
|
inline ircd::string_view
|
2018-02-06 22:03:38 +01:00
|
|
|
ircd::util::params::name(const size_t &i)
|
2018-02-03 03:51:57 +01:00
|
|
|
const
|
|
|
|
{
|
2019-12-11 02:49:09 +01:00
|
|
|
return names.size() > i?
|
|
|
|
*std::next(begin(names), i):
|
|
|
|
"<unnamed>";
|
2018-02-03 03:51:57 +01:00
|
|
|
}
|
2019-12-11 03:22:26 +01:00
|
|
|
|
|
|
|
inline bool
|
2020-11-28 01:56:56 +01:00
|
|
|
ircd::util::params::for_each_posarg(const token_view &closure)
|
2019-12-11 03:22:26 +01:00
|
|
|
const
|
|
|
|
{
|
2020-11-28 01:56:56 +01:00
|
|
|
return tokens(in, sep, [this, &closure]
|
2019-12-11 03:22:26 +01:00
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
return !prefix || !startswith(token, prefix)?
|
|
|
|
closure(token):
|
|
|
|
true;
|
2020-11-28 01:56:56 +01:00
|
|
|
});
|
2019-12-11 03:22:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2020-11-28 01:56:56 +01:00
|
|
|
ircd::util::params::for_each_pararg(const token_view &closure)
|
2019-12-11 03:22:26 +01:00
|
|
|
const
|
|
|
|
{
|
2020-11-28 01:56:56 +01:00
|
|
|
return tokens(in, sep, [this, &closure]
|
2019-12-11 03:22:26 +01:00
|
|
|
(const string_view &token)
|
|
|
|
{
|
|
|
|
return prefix && startswith(token, prefix)?
|
|
|
|
closure(token):
|
|
|
|
true;
|
2020-11-28 01:56:56 +01:00
|
|
|
});
|
2019-12-11 03:22:26 +01:00
|
|
|
}
|