- Main Page
- Namespaces
- Classes
- Files

# proto/Source/filter.cpp

Go to the documentation of this file.

    00001
    00011 #include <fstream>
    00012 // Utility std::fill needed.
    00013 #include <utility>
    00014
    00015 // We are interested in value of INSIGNIFICANT_POSITION_MARK.
    00016 #include "config.h"
    00017 // Self header.
    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     // Hola!:)
    00038     return true;
    00039 }
    00040
    00041
    00043
    00044 /*  Interface item implementation - for info see description in header file.
    00045  */
    00046 void Filter::Stats::reset(void) {
    00047     unmatched = 0;
    00048     // Reset doesn't mean clear!
    00049     std::fill(matched.begin(), matched.end(), 0);
    00050 }
    00051
    00052
    00054
    00055 /*  Interface item implementation - for info see description in header file.
    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 /*  Interface item implementation - for info see description in header file.
    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         // Loop through every rule and compare to given value.
    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 /*  Interface item implementation - for info see description in header file.
    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         // Get next line == get next rule.
    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 /*  Interface item implementation - for info see description in header file.
    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 /*  Interface item implementation - for info see description in header file.
    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         // Loop through sets of rules...
    00127         if ( i_rule->size() != value.size() ) {
    00128             // Different sized rules cannot match in any way.
    00129             continue;
    00130         }
    00131         unsigned i = 0; // Needed after loop also.
    00132         for ( ; i < i_rule->size(); ++i ) {
    00133             // Loop through every member of the rule and match
    00134             if ( !match_single_rule((*i_rule)[i], value[i]) ) {
    00135                 // This rule doesn't match. Try next one.
    00136                 break;
    00137             }
    00138         }
    00139         if ( i == i_rule->size() ) {
    00140             // Ok, matched!
    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     // No matching rule found.
    00150     if ( include_in_stats ) {
    00151         ++_stats_so_far.unmatched;
    00152         ++_recent_stats.unmatched;
    00153     }
    00154     return false;
    00155 }
    00156
    00157 /*  Interface item implementation - for info see description in header file.
    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         // Get next line...
    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         // Final piece...
    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             // (0 -> store all rules)
    00191             add_rule(rule);
    00192         }
    00193
    00194         // Prepare for next loop!
    00195         rule.clear();
    00196     }
    00197
    00198     return true;
    00199
    00200 }
    00201
    00202 } // namespace ace