Index: trunk/extensions/AbuseFilter/parser_native/filter_evaluator.cpp |
— | — | @@ -0,0 +1,53 @@ |
| 2 | +/* |
| 3 | + * Copyright (c) 2008 Andrew Garrett. |
| 4 | + * Copyright (c) 2008 River Tarnell <river@wikimedia.org> |
| 5 | + * Derived from public domain code contributed by Victor Vasiliev. |
| 6 | + * |
| 7 | + * Permission is granted to anyone to use this software for any purpose, |
| 8 | + * including commercial applications, and to alter it and redistribute it |
| 9 | + * freely. This software is provided 'as-is', without any express or |
| 10 | + * implied warranty. |
| 11 | + */ |
| 12 | + |
| 13 | +#include "filter_evaluator.h" |
| 14 | + |
| 15 | +namespace afp { |
| 16 | + |
| 17 | +filter_evaluator::filter_evaluator() |
| 18 | +{ |
| 19 | + e.add_function(make_astring<UChar32, char>("length"), af_length<UChar32>); |
| 20 | + e.add_function(make_astring<UChar32, char>("lcase"), af_lcase<UChar32>); |
| 21 | + e.add_function(make_astring<UChar32, char>("ccnorm"), af_ccnorm<UChar32>); |
| 22 | + e.add_function(make_astring<UChar32, char>("rmdoubles"), af_rmdoubles<UChar32>); |
| 23 | + e.add_function(make_astring<UChar32, char>("specialratio"), af_specialratio<UChar32>); |
| 24 | + e.add_function(make_astring<UChar32, char>("rmspecials"), af_rmspecials<UChar32>); |
| 25 | + e.add_function(make_astring<UChar32, char>("norm"), af_norm<UChar32>); |
| 26 | + e.add_function(make_astring<UChar32, char>("count"), af_count<UChar32>); |
| 27 | +} |
| 28 | + |
| 29 | +void |
| 30 | +filter_evaluator::clear_variables() |
| 31 | +{ |
| 32 | + e.clear_variables(); |
| 33 | +} |
| 34 | + |
| 35 | +bool |
| 36 | +filter_evaluator::evaluate(u32fray const &filter) const |
| 37 | +{ |
| 38 | + try { |
| 39 | + return e.evaluate(filter).toBool(); |
| 40 | + } catch (std::exception &e) { |
| 41 | + std::cerr << "can't evaluate filter: " << e.what() << '\n'; |
| 42 | + return false; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void |
| 47 | +filter_evaluator::add_variable( |
| 48 | + u32fray const &key, |
| 49 | + u32datum const &value) |
| 50 | +{ |
| 51 | + e.add_variable(key, value); |
| 52 | +} |
| 53 | + |
| 54 | +} // namespace afp |