ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
keywordEx.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_dpm_libmerge
4 * @copyright ESO - European Southern Observatory
5 */
6#ifndef DAQ_DPM_KEYWORD_EX_HPP
7#define DAQ_DPM_KEYWORD_EX_HPP
8#include <algorithm>
9#include <regex>
10#include <string>
11#include <string_view>
12#include <vector>
13
14#include <daq/fits/keyword.hpp>
15
16namespace daq::dpm {
17namespace detail {
18
19/**
20 * Rule operator
21 */
22enum class Operator : std::uint8_t { Include, Exclude };
23
24/**
25 * Rule scope.
26 */
27enum class Scope : std::uint8_t { Any, Value, Eso, Commentary };
28
29/**
30 * Represents a keyword rule expression.
31 */
32struct Rule {
35 std::string pattern;
36};
37
38/**
39 * Parse expression of the form documented in KeywordEx
40 *
41 * @throw std::invalid_argument if expression @a ex is not valid.
42 */
43Rule ParseEx(std::string_view ex);
44
45} // namespace detail
46
47/**
48 * Create keyword expression that memoize the provided string pattern.
49 *
50 * Each rule string is defined by:
51 *
52 * <rule> ::= <operator><scope> " " <pattern> | <operator> " " <pattern>
53 * <operator> ::= "+" | "-"
54 * <scope> ::= "v" | "e" | "c"
55 * <pattern> ::= <ascii-text> | <wildcard> | <pattern>
56 * <ascii-text> ::= [#x20 - #x7e]
57 * <wildcard> ::= "*" | "?" | "[" <ascii-text> "]"
58 */
59class KeywordEx {
60public:
61 KeywordEx() = default;
62
63 /**
64 * Construct expresssion from single rule.
65 */
66 explicit KeywordEx(std::string_view rule);
67
68 /**
69 * Construct from initilizer_list of c-strings.
70 *
71 * @param rules Initializer list of rules.
72 */
73 explicit KeywordEx(std::initializer_list<char const*> rules) {
74 std::transform(
75 std::begin(rules), std::end(rules), std::back_inserter(m_rules), [](auto&& value) {
76 return detail::ParseEx(std::string_view(value));
77 });
78 }
79
80 /**
81 * Construct from a pair of iterators.
82 *
83 * @param begin beginning of sequence.
84 * @param end end of sequence.
85 */
86 template <class ForwardIt,
87 typename = typename std::enable_if_t<
88 std::is_constructible_v<std::string_view, typename ForwardIt::value_type>>>
89 KeywordEx(ForwardIt begin, ForwardIt end) {
90 std::transform(begin, end, std::back_inserter(m_rules), [](auto&& value) {
91 return detail::ParseEx(std::string_view(value));
92 });
93 }
94
95protected:
96 constexpr std::vector<detail::Rule> const& GetRules() const noexcept {
97 return m_rules;
98 }
99
100 friend bool KeywordMatch(fits::KeywordVariant const& keyword, KeywordEx const& ex);
101
102private:
103 std::vector<detail::Rule> m_rules;
104};
105
106/**
107 *
108 * @tparam Iterator An iterator type to a type that can be converted to a std::string_view, such as
109 * char const**, std::vector<std::string>::const_iterator,
110 * std::list<std::string_view>::const_iterator.
111 */
112bool KeywordMatch(fits::KeywordVariant const& keyword, KeywordEx const& ex);
113
114/**
115 * Transforms keyword name using regex
116 *
117 */
119KeywordTransform(fits::KeywordVariant const& keyword, std::regex const& re, char const* fmt);
120
122KeywordTransform(fits::ValueKeyword const& keyword, std::regex const& re, char const* fmt);
123
125KeywordTransform(fits::EsoKeyword const& keyword, std::regex const& re, char const* fmt);
126
128KeywordTransform(fits::LiteralKeyword const& keyword, std::regex const& re, char const* fmt);
129
130} // namespace daq::dpm
131
132#endif // #ifndef DAQ_DPM_KEYWORD_EX_HPP
Create keyword expression that memoize the provided string pattern.
Definition: keywordEx.hpp:59
friend bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
KeywordEx(ForwardIt begin, ForwardIt end)
Construct from a pair of iterators.
Definition: keywordEx.hpp:89
KeywordEx(std::initializer_list< char const * > rules)
Construct from initilizer_list of c-strings.
Definition: keywordEx.hpp:73
constexpr std::vector< detail::Rule > const & GetRules() const noexcept
Definition: keywordEx.hpp:96
Represents the literal 80-character FITS keyword record.
Definition: keyword.hpp:129
Contains data structure for FITS keywords.
Scope
Rule scope.
Definition: keywordEx.hpp:27
Operator
Rule operator.
Definition: keywordEx.hpp:22
Rule ParseEx(std::string_view ex)
Parse expression of the form documented in KeywordEx.
Definition: keywordEx.cpp:17
Represents a keyword rule expression.
Definition: keywordEx.hpp:32
fits::KeywordVariant KeywordTransform(fits::KeywordVariant const &keyword, std::regex const &re, char const *fmt)
Transforms keyword name using regex.
Definition: keywordEx.cpp:164
bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
Definition: keywordEx.cpp:150
std::variant< ValueKeyword, EsoKeyword, LiteralKeyword > KeywordVariant
The different variants of keywords that are supported.
Definition: keyword.hpp:409
A type safe version of LiteralKeyword that consist of the three basic components of a FITS keyword ke...
Definition: keyword.hpp:275