Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages | Examples

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 51 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 136 of file WOscPatternMatch.cpp.

References PatternMatch().

Referenced by PatternMatch().

00137 {
00138     bool result;
00139     bool negated = false;
00140     const char *p = pattern;
00141 
00142     if (pattern[1] == 0) {
00143         /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00144          * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00145          */
00146         return false;
00147     }
00148 
00149     if (pattern[1] == '!') {
00150         negated = true;
00151         p++;
00152     }
00153 
00154     while (*p != ']') {
00155         if (*p == 0) {
00156             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00157              * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00158              */
00159             return false;
00160         }
00161         if (p[1] == '-' && p[2] != 0) {
00162             if (test[0] >= p[0] && test[0] <= p[2]) {
00163                 result = !negated;
00164                 goto advance;
00165             }
00166         }
00167         if (p[0] == test[0]) {
00168             result = !negated;
00169             goto advance;
00170         }
00171         p++;
00172     }
00173 
00174     result = negated;
00175 
00176 advance:
00177 
00178     if (!result)
00179         return false;
00180 
00181     while (*p != ']') {
00182         if (*p == 0) {
00183             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00184              * OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00185              */
00186             return false;
00187         }
00188         p++;
00189     }
00190 
00191     return PatternMatch (p+1,test+1);
00192 }

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 214 of file WOscPatternMatch.cpp.

References PatternMatch().

Referenced by PatternMatch().

00215 {
00216 
00217     const char *restOfPattern, *tp = test;
00218 
00219 
00220     for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
00221         if (*restOfPattern == 0) {
00222             /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00223              * OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
00224              */
00225             return false;
00226         }
00227     }
00228 
00229     restOfPattern++; /* skip close curly brace */
00230 
00231     pattern++; /* skip open curly brace */
00232 
00233     while (1) {
00234 
00235         if (*pattern == ',') {
00236             if (PatternMatch (restOfPattern, tp)) {
00237                 return true;
00238             } else {
00239                 tp = test;
00240                 ++pattern;
00241             }
00242         } else if (*pattern == '}') {
00243             return PatternMatch (restOfPattern, tp);
00244         } else if (*pattern == *tp) {
00245             ++pattern;
00246             ++tp;
00247         } else {
00248             tp = test;
00249             while (*pattern != ',' && *pattern != '}') {
00250                 pattern++;
00251             }
00252             if (*pattern == ',') {
00253                 pattern++;
00254             }
00255         }
00256     }
00257 }

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 270 of file WOscPatternMatch.cpp.

Referenced by WOscContainer::FindContainer().

00271 {
00272     while (*p != '/' && *p != '\0') {
00273         p++;
00274     }
00275     return p;
00276 }

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

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.

Definition at line 63 of file WOscPatternMatch.cpp.

References MatchBrackets(), MatchList(), and PatternMatch().

Referenced by MatchBrackets(), MatchList(), and PatternMatch().

00064 {
00065     // Matt's OSC-Kit:
00066     // theWholePattern = pattern;
00067 
00068     if (pattern == 0 || pattern[0] == 0) {
00069         return test[0] == 0;
00070     } 
00071 
00072     if (test[0] == 0) {
00073         if (pattern[0] == '*')
00074             return PatternMatch (pattern+1,test);
00075         else
00076             return false;
00077     }
00078 
00079     switch (pattern[0]) {
00080     case 0      : return test[0] == 0;
00081     case '?'    : return PatternMatch (pattern + 1, test + 1);
00082     case '*'    : 
00083         if (PatternMatch (pattern+1, test)) {
00084             return true;
00085         } else {
00086             return PatternMatch (pattern, test+1);
00087         }
00088     case ']'    :
00089     case '}'    :
00090         /* Can be changed to exception, if desired (does not make sense). In Matt's OSC-Kit it was:
00091         * OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
00092         */
00093         return false;
00094     case '['    :
00095         return MatchBrackets (pattern,test);
00096     case '{'    :
00097         return MatchList (pattern,test);
00098     case '\\'   :  
00099         if (pattern[1] == 0) {
00100             return test[0] == 0;
00101         } else if (pattern[1] == test[0]) {
00102             return PatternMatch (pattern+2,test+1);
00103         } else {
00104             return false;
00105         }
00106     default     :
00107         if (pattern[0] == test[0]) {
00108             return PatternMatch (pattern+1,test+1);
00109         } else {
00110             return false;
00111         }
00112     }
00113 }


The documentation for this class was generated from the following files:
Generated on Sun Nov 1 23:31:19 2009 for WOscLib by  doxygen 1.4.1