00001
00011 #include <fstream>
00012
00013 #include <utility>
00014
00015
00016 #include "config.h"
00017
00018 #include "filter.h"
00019
00020 namespace ace {
00021
00031 bool match_single_rule(const std::string& rule, const std::string& value_to_match) {
00032 for ( std::string::size_type i = 0; (i < value_to_match.size()) && (i < rule.size()); ++i ) {
00033 if ( (rule[i] != constants::insignificant_position_mark) && (rule[i] != value_to_match[i]) ) {
00034 return false;
00035 }
00036 }
00037
00038 return true;
00039 }
00040
00041
00043
00044
00045
00046 void Filter::Stats::reset(void) {
00047 unmatched = 0;
00048
00049 std::fill(matched.begin(), matched.end(), 0);
00050 }
00051
00052
00054
00055
00056
00057 void UniRulesFilter::add_rule(unirule_t& rule) {
00058 _rules.push_back(rule);
00059 _stats_so_far.matched.push_back(0);
00060 _recent_stats.matched.push_back(0);
00061 }
00062
00063
00064
00065 bool UniRulesFilter::match(const unirule_t& value, bool include_in_stats) {
00066
00067 for ( rules_t::const_iterator i_rule = _rules.begin(); i_rule != _rules.end(); ++i_rule ) {
00068
00069 if ( match_single_rule(*i_rule, value) ) {
00070 if ( include_in_stats ) {
00071 ++(_stats_so_far.matched[i_rule - _rules.begin()]);
00072 ++(_recent_stats.matched[i_rule - _rules.begin()]);
00073 }
00074 return true;
00075 }
00076 }
00077 if ( include_in_stats ) {
00078 ++_stats_so_far.unmatched;
00079 ++_recent_stats.unmatched;
00080 }
00081 return false;
00082 }
00083
00084
00085
00086 bool UniRulesFilter::read_rules(const char *rules_filename) {
00087
00088 std::ifstream rules_list(rules_filename);
00089
00090 if ( !rules_list ) {
00091 return false;
00092 }
00093
00094 unirule_t rule;
00095
00096 while ( rules_list ) {
00097
00098 std::getline(rules_list, rule);
00099
00100 if ( rule.empty() ) {
00101 continue;
00102 }
00103
00104 add_rule(rule);
00105 }
00106
00107 return true;
00108 }
00109
00110
00112
00113
00114
00115 void MultiRulesFilter::add_rule(multirule_t& rule) {
00116 _rules.push_back(rule);
00117 _stats_so_far.matched.push_back(0);
00118 _recent_stats.matched.push_back(0);
00119 }
00120
00121
00122
00123 bool MultiRulesFilter::match(const multirule_t& value, bool include_in_stats) {
00124
00125 for ( rules_t::const_iterator i_rule = _rules.begin(); i_rule != _rules.end(); ++i_rule ) {
00126
00127 if ( i_rule->size() != value.size() ) {
00128
00129 continue;
00130 }
00131 unsigned i = 0;
00132 for ( ; i < i_rule->size(); ++i ) {
00133
00134 if ( !match_single_rule((*i_rule)[i], value[i]) ) {
00135
00136 break;
00137 }
00138 }
00139 if ( i == i_rule->size() ) {
00140
00141 if ( include_in_stats ) {
00142 ++(_stats_so_far.matched[i_rule - _rules.begin()]);
00143 ++(_recent_stats.matched[i_rule - _rules.begin()]);
00144 }
00145 return true;
00146 }
00147 }
00148
00149
00150 if ( include_in_stats ) {
00151 ++_stats_so_far.unmatched;
00152 ++_recent_stats.unmatched;
00153 }
00154 return false;
00155 }
00156
00157
00158
00159 bool MultiRulesFilter::read_rules(const char *rules_filename, size_t only_of_size) {
00160
00161 std::ifstream rules_list(rules_filename);
00162
00163 if ( !rules_list ) {
00164 return false;
00165 }
00166
00167 multirule_t rule;
00168 std::string str;
00169
00170 while ( rules_list ) {
00171
00172 std::getline(rules_list, str);
00173
00174 if ( str.empty() ) {
00175 continue;
00176 }
00177
00178 std::string::size_type start = 0, end = str.find(' ');
00179
00180 while ( end != std::string::npos ) {
00181 rule.push_back(str.substr(start, end - start));
00182 start = end + 1;
00183 end = str.find(' ', start);
00184 }
00185
00186 if ( start < str.size() ) {
00187 rule.push_back(str.substr(start));
00188 }
00189 if ( (only_of_size == 0) || (rule.size() == only_of_size) ) {
00190
00191 add_rule(rule);
00192 }
00193
00194
00195 rule.clear();
00196 }
00197
00198 return true;
00199
00200 }
00201
00202 }