/* * Copyright (c) 2015-2017 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 * */ /** * @file * Wind River - Titanium Cloud - Regex Utilty Module */ #include #include #include using namespace std; #include "nodeBase.h" #include "regexUtil.h" /********************************************************************** * * Name : regexUtil_label_match * * Purpose : Verify that the rule matches the pattern * * Description: Loop over the rule extracting the labels that are * delimited by ':'s. Then for each label, walk the pattern * verifying that the label exists while maintaining a * forward position search. If all the rule labels are * found (in order) in the pattern then return a true. * Otherwise, return false. * **********************************************************************/ bool regexUtil_label_match ( string hostname, string pattern, string rule ) { bool match = false ; if ( rule.empty() ) { wlog ("%s empty rule string\n", hostname.c_str() ); } else { int i ; size_t pos = 0 ; size_t new_pos = 0 ; size_t run_pos = 0 ; int colins = 0 ; int len = (int)rule.length() ; char * ptr = (char*)rule.data(); for ( int i = 0 ; i < len ; i++ ) { if ( *ptr++ == ':' ) colins++ ; } dlog ("%s there are %d colins in the rule (%s)\n", hostname.c_str() , colins, rule.c_str()); match = true ; i = 0 ; do { string label ; if ( colins ) { /* get the label from the current position up to the next colin */ new_pos = rule.find( ':' , pos ); if ( new_pos != std::string::npos ) { label = rule.substr(pos, new_pos-pos) ; dlog2 ("%s %d-%s (%ld-%ld)\n", hostname.c_str() , i, label.c_str(), pos, new_pos); /* move beyond the ':' */ pos = ++new_pos ; } } else { label = rule.substr(pos) ; dlog ("%s %d:%s\n", hostname.c_str(), i, label.c_str()); } if ( label.empty() ) { wlog ("%s label not found\n", hostname.c_str()); match = false ; } else { run_pos = pattern.find( label, run_pos ) ; if ( run_pos != std::string::npos ) { dlog1 ("%s '%s' found in pattern (pos:%ld)\n", hostname.c_str(), label.c_str(),run_pos); run_pos++ ; } else { dlog3 ("%s '%s' NOT found in pattern \n", hostname.c_str(), label.c_str()); match = false ; } } i++ ; } while ( ( colins--) && ( match == true ) ) ; } return (match); } bool regexUtil_pattern_match ( std::string pattern , std::string rule, int type ) { bool result = false ; if ( type == 1 ) { //std::cmatch cm; // same as std::match_results cm; //std::regex e (rule.data()) ; //result = std::regex_match (pattern,cm, e , std::regex_constants::match_not_bol ); if ( result ) { std::cout << "Flagged String match\n"; } } else { result = std::regex_match (pattern, std::regex(rule)) ; if ( result ) { std::cout << "String literal matched\n"; std::smatch sm; // same as std::match_results sm; std::regex_match (pattern,sm,std::regex(rule)); std::cout << "String object with " << sm.size() << " matches\n"; std::cout << "The matches are: "; for (unsigned i=0; i cm; std::regex_match (cstr,cm,e); std::cout << "string literal with " << cm.size() << " matches\n"; std::smatch sm; // same as std::match_results sm; std::regex_match (s,sm,e); std::cout << "string object with " << sm.size() << " matches\n"; std::regex_match ( s.cbegin(), s.cend(), sm, e); std::cout << "range with " << sm.size() << " matches\n"; // using explicit flags: std::regex_match ( cstr, cm, e, std::regex_constants::match_default ); std::cout << "the matches were: "; for (unsigned i=0; i