RuleSet.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include <cstring>
00018 #include <algorithm>
00019 
00020 #include "Glob.h"
00021 #include "RuleSet.h"
00022 
00023 namespace oasys {
00024 
00025 //----------------------------------------------------------------------------
00026 RuleSet::RuleSet(RuleStorage* rs)
00027     : rules_(rs), num_rules_(0)
00028 {}
00029 
00030 //----------------------------------------------------------------------------
00031 RuleStorage::Item*  
00032 RuleSet::match_rule(char* rule)
00033 {
00034     RuleStorage::Item* match = 0;
00035     int cur_priority = -1000;
00036 
00037     for (unsigned int i = 0; i < num_rules_; ++i)
00038     {
00039         RuleStorage::Item* item = &rules_->items_[i];
00040         if (do_match(rule, item) && item->priority_ > cur_priority) 
00041         {
00042             match        = item;
00043             cur_priority = item->priority_;
00044         }
00045     }
00046 
00047     return match;
00048 }
00049 
00050 //----------------------------------------------------------------------------
00051 void
00052 RuleSet::add_prefix_rule(char* rule, int log_level)
00053 {
00054     add_rule(PREFIX, rule, log_level, strlen(rule));
00055 }
00056 
00057 //----------------------------------------------------------------------------
00058 void
00059 RuleSet::add_glob_rule(char* rule, int log_level, int priority)
00060 {
00061     add_rule(GLOB, rule, log_level, priority);
00062 }
00063 
00064 //----------------------------------------------------------------------------
00065 void 
00066 RuleSet::add_rule(int flags, char* rule, int log_level, int priority)
00067 { 
00068     // Just ignore if we have too many rules
00069     if (num_rules_ > rules_->MAX_RULES) {
00070         return;
00071     }    
00072 
00073     RuleStorage::Item* item;
00074 
00075     item = &rules_->items_[num_rules_];
00076     memcpy(item->rule_, rule, std::min(rules_->MAX_RULE_LENGTH, strlen(rule)));
00077     item->flags_     = flags;
00078     item->log_level_ = log_level;
00079     item->priority_  = priority;
00080 
00081     num_rules_++;
00082 }
00083   
00084 //----------------------------------------------------------------------------
00085 bool 
00086 RuleSet::do_match(char* rule, RuleStorage::Item* item)
00087 {
00088     switch (item->flags_) {
00089     case PREFIX:
00090         if (strstr(rule, item->rule_) == rule) {
00091             return true;
00092         }
00093         break;
00094     case GLOB:
00095         if (Glob::fixed_glob(item->rule_, rule)) {
00096             return true;
00097         }
00098         break;
00099     }
00100 
00101     return false;
00102 }   
00103     
00104 } // namespace oasys
00105 
00106 #if 0
00107 
00108 #include <stdio.h>
00109 
00110 void
00111 test(oasys::RuleSet* rs, char* rule) 
00112 {
00113     oasys::RuleStorage::Item* item;
00114 
00115     item = rs->match_rule(rule);
00116     if (item == 0) { 
00117         printf("%s - no match\n", rule);
00118     } else {
00119         printf("%s:%s - %d\n", rule, item->rule_, item->log_level_);
00120     }
00121 }
00122 
00123 int
00124 main()
00125 {
00126     oasys::RuleStorage s;
00127     oasys::RuleSet rs(&s);
00128 
00129     rs.add_prefix_rule("/foo", 1);
00130     rs.add_prefix_rule("/foo/bar", 2);
00131     rs.add_glob_rule("*baz", 3, 1000);
00132     rs.add_glob_rule("*middle*", 4, 1000);
00133 
00134     test(&rs, "/foo");
00135     test(&rs, "/foo/gar");
00136     test(&rs, "/foo/bar"); 
00137     test(&rs, "/foo/bart"); 
00138     test(&rs, "/foo/bar/boo"); 
00139     test(&rs, "/foo/baz");
00140     test(&rs, "/this/is/a");
00141     test(&rs, "/this/is/a/long/baz");
00142     test(&rs, "/thismiddle");
00143 }
00144 
00145 #endif

Generated on Thu Jun 7 12:54:29 2007 for DTN Reference Implementation by  doxygen 1.5.1