1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "CustomVarsDictFilter.h"
27 #include <unordered_map>
29 #include "CustomVarsDictColumn.h"
33 #include "StringUtils.h"
35 CustomVarsDictFilter::CustomVarsDictFilter(Kind kind
,
36 const CustomVarsDictColumn
&column
,
37 RelationalOperator relOp
,
38 const std::string
&value
)
39 : ColumnFilter(kind
, column
, relOp
, value
), _column(column
) {
40 // Filter for custom_variables:
41 // Filter: custom_variables = PATH /hirni.mk
42 // The variable name is part of the value and separated with spaces
43 std::tie(_ref_varname
, _ref_string
) = mk::nextField(value
);
44 _ref_string
= mk::lstrip(_ref_string
);
45 _regExp
= makeRegExpFor(oper(), _ref_string
);
48 bool CustomVarsDictFilter::accepts(
49 Row row
, const contact
* /* auth_user */,
50 std::chrono::seconds
/* timezone_offset */) const {
51 auto cvm
= _column
.getValue(row
);
52 auto it
= cvm
.find(_ref_varname
);
53 auto act_string
= it
== cvm
.end() ? "" : it
->second
;
55 case RelationalOperator::equal
:
56 case RelationalOperator::equal_icase
:
57 return _regExp
->match(act_string
);
58 case RelationalOperator::not_equal
:
59 case RelationalOperator::not_equal_icase
:
60 return !_regExp
->match(act_string
);
61 case RelationalOperator::matches
:
62 case RelationalOperator::matches_icase
:
63 return _regExp
->search(act_string
);
64 case RelationalOperator::doesnt_match
:
65 case RelationalOperator::doesnt_match_icase
:
66 return !_regExp
->search(act_string
);
67 // FIXME: The cases below are nonsense for UTF-8...
68 case RelationalOperator::less
:
69 return act_string
< _ref_string
;
70 case RelationalOperator::greater_or_equal
:
71 return act_string
>= _ref_string
;
72 case RelationalOperator::greater
:
73 return act_string
> _ref_string
;
74 case RelationalOperator::less_or_equal
:
75 return act_string
<= _ref_string
;
77 return false; // unreachable
80 std::unique_ptr
<Filter
> CustomVarsDictFilter::copy() const {
81 return std::make_unique
<CustomVarsDictFilter
>(*this);
84 std::unique_ptr
<Filter
> CustomVarsDictFilter::negate() const {
85 return std::make_unique
<CustomVarsDictFilter
>(
86 kind(), _column
, negateRelationalOperator(oper()), value());