WOscPatternMatch Class Reference

#include <WOscPatternMatch.h>

Inherited by WOscContainer.

List of all members.

Static Public Member Functions

static bool PatternMatch (const char *pattern, const char *test)
static const char * NextSlashOrNull (const char *p)

Static Protected Member Functions

static bool MatchBrackets (const char *pattern, const char *test)
static bool MatchList (const char *pattern, const char *test)

Detailed Description

Pattern matching algorithms. All code stolen from Matt Wright's OSC-Kit. Handles all tasks related to checking OSC-addresses against certain patterns.

See also:
For information about the OSC address syntax see the OSC specifications.

Definition at line 52 of file WOscPatternMatch.h.


Member Function Documentation

bool WOscPatternMatch::MatchBrackets ( const char *  pattern,
const char *  test 
) [static, protected]

Checks if the pattern matches agains the test-string. Code from Matt Wright's OSC-Kit. Thanks...

Parameters:
pattern The pattern against test has to match.
test The string to be tested.
Returns:
True if both match regarding the definitions in the OSC-address-syntax and OSC pattern matching. See the OSC specifications for details.
Exceptions:
Currently nothing. Could be changed, if desired.
Todo:
Write more precise documentation.

Definition at line 165 of file WOscPatternMatch.cpp.

References PatternMatch().

Referenced by PatternMatch().

00166 {
00167     bool result;
00168     bool negated = false;
00169     const char *p = pattern;
00170 
00171     if (pattern[1] == 0) {
00172         /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00173          * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00174          */
00175         return false;
00176     }
00177 
00178     if (pattern[1] == '!') {
00179         negated = true;
00180         p++;
00181     }
00182 
00183     while (*p != ']') {
00184         if (*p == 0) {
00185             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00186              * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00187              */
00188             return false;
00189         }
00190         if (p[1] == '-' && p[2] != 0) {
00191             if (test[0] >= p[0] && test[0] <= p[2]) {
00192                 result = !negated;
00193                 goto advance;
00194             }
00195         }
00196         if (p[0] == test[0]) {
00197             result = !negated;
00198             goto advance;
00199         }
00200         p++;
00201     }
00202 
00203     result = negated;
00204 
00205 advance:
00206 
00207     if (!result)
00208         return false;
00209 
00210     while (*p != ']') {
00211         if (*p == 0) {
00212             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00213              * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00214              */
00215             return false;
00216         }
00217         p++;
00218     }
00219 
00220     return PatternMatch (p+1,test+1);
00221 }

bool WOscPatternMatch::MatchList ( const char *  pattern,
const char *  test 
) [static, protected]

Checks if the pattern matches against the test-string. Code from Matt Wright's OSC-Kit. Thanks...

Parameters:
pattern The pattern against test has to match.
test The string to be tested.
Returns:
True if both match regarding the definitions in the OSC-address-syntax and OSC pattern matching. See the OSC specifications for details.
Exceptions:
Currently nothing. Could be changed, if desired.
Todo:
Write more precise documentation.

Definition at line 243 of file WOscPatternMatch.cpp.

References PatternMatch().

Referenced by PatternMatch().

00244 {
00245 
00246     const char *restOfPattern, *tp = test;
00247 
00248 
00249     for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
00250         if (*restOfPattern == 0) {
00251             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00252              * OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
00253              */
00254             return false;
00255         }
00256     }
00257 
00258     restOfPattern++; /* skip close curly brace */
00259 
00260     pattern++; /* skip open curly brace */
00261 
00262     while (1) {
00263 
00264         if (*pattern == ',') {
00265             if (PatternMatch (restOfPattern, tp)) {
00266                 return true;
00267             } else {
00268                 tp = test;
00269                 ++pattern;
00270             }
00271         } else if (*pattern == '}') {
00272             return PatternMatch (restOfPattern, tp);
00273         } else if (*pattern == *tp) {
00274             ++pattern;
00275             ++tp;
00276         } else {
00277             tp = test;
00278             while (*pattern != ',' && *pattern != '}') {
00279                 pattern++;
00280             }
00281             if (*pattern == ',') {
00282                 pattern++;
00283             }
00284         }
00285     }
00286 }

const char * WOscPatternMatch::NextSlashOrNull ( const char *  p  )  [static]

Advances in a string until a '\0' or a '/' is found.

Parameters:
p String to be scanned.
Returns:
The next position of a '\0' or a '/' in given string.

Definition at line 299 of file WOscPatternMatch.cpp.

Referenced by WOscContainer::FindContainer().

00300 {
00301     while (*p != '/' && *p != '\0') {
00302         p++;
00303     }
00304     return p;
00305 }

bool WOscPatternMatch::PatternMatch ( const char *  pattern,
const char *  test 
) [static]

Allowing wildcards in address names (containers and methods). Supported wildcards are "*" and "?". The behaviour is just tested basically in tests/patternmatch.cpp but will require much more testing. If anyone has a nice test please add it to this testbench or notify me.

Currently the address space wildcards are on by default. Checks if the pattern matches agains the test-string. Code from Matt Wright's OSC-Kit. Thanks...

Parameters:
pattern The pattern against test has to match. Usually the OSC message address.
test The string to be tested. Usually the container address.
Returns:
True if both match regarding the definitions in the OSC-address-syntax and OSC pattern matching. See the OSC specifications for details.
Exceptions:
Currently nothing. Could be changed, if desired.

Definition at line 74 of file WOscPatternMatch.cpp.

References MatchBrackets(), and MatchList().

Referenced by MatchBrackets(), and MatchList().

00075 {
00076 
00077     if (pattern == 0 || pattern[0] == 0) {
00078 #if WOSC_USE_ADDR_WILDCARDS
00079         switch (test[0]) {
00080         case '*': return PatternMatch(pattern, test+1);
00081         case 0: return true;
00082         default: return false;
00083         }
00084 #else
00085         return test[0] == 0;
00086 #endif
00087     } 
00088 
00089     // test has precedence before pattern
00090     switch ( test[0] ) {
00091     case 0:
00092         if (pattern[0] == '*')
00093             return PatternMatch (pattern+1,test);
00094         else
00095             return false;
00096 #if WOSC_USE_ADDR_WILDCARDS
00097     case '*':
00098         if (PatternMatch (pattern, test+1)) {
00099             return true;
00100         } else {
00101             return PatternMatch (pattern+1, test);
00102         }
00103     case '?'    : return PatternMatch (pattern + 1, test + 1);
00104 #endif
00105     }
00106 
00107     // test pattern (message address) against test (container address)
00108     switch (pattern[0]) {
00109     case 0      : return test[0] == 0; // already tested at the beginning
00110     case '?'    : return PatternMatch (pattern + 1, test + 1);
00111     case '*'    : 
00112         if (PatternMatch (pattern+1, test)) {
00113             return true;
00114         } else {
00115             return PatternMatch (pattern, test+1);
00116         }
00117     case ']'    :
00118     case '}'    :
00119         /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00120         * OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
00121         */
00122         return false;
00123     case '['    :
00124         return MatchBrackets (pattern,test);
00125     case '{'    :
00126         return MatchList (pattern,test);
00127     case '\\'   :  
00128         if (pattern[1] == 0) {
00129             return test[0] == 0;
00130         } else if (pattern[1] == test[0]) {
00131             return PatternMatch (pattern+2,test+1);
00132         } else {
00133             return false;
00134         }
00135     default     :
00136         if (pattern[0] == test[0]) {
00137             return PatternMatch (pattern+1,test+1);
00138         } else {
00139             return false;
00140         }
00141     }
00142 }


The documentation for this class was generated from the following files:
Generated on Sat Oct 23 03:05:59 2010 for WOscLib by  doxygen 1.6.3