Pehr Hovey / Mbed 2 deprecated mbed_osc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers osc_patternmatch.c Source File

osc_patternmatch.c

00001 /* Pehr Hovey - mBed OSC
00002  * This Code is from the Make Controller firmware repository
00003  * http://makingthings.com
00004  */
00005 /*********************************************************************************
00006 
00007  Copyright 2006-2009 MakingThings
00008 
00009  Licensed under the Apache License,
00010  Version 2.0 (the "License"); you may not use this file except in compliance
00011  with the License. You may obtain a copy of the License at
00012 
00013  http://www.apache.org/licenses/LICENSE-2.0
00014 
00015  Unless required by applicable law or agreed to in writing, software distributed
00016  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
00017  CONDITIONS OF ANY KIND, either express or implied. See the License for
00018  the specific language governing permissions and limitations under the License.
00019 
00020 *********************************************************************************/
00021 
00022 /*
00023     osc_patternmatch.c
00024 
00025     Adapted from OSC-pattern-match.c, by Matt Wright
00026     Adapted from oscpattern.c, by Matt Wright and Amar Chaudhury
00027  */
00028 
00029 
00030 
00031 
00032 static const char *theWholePattern;    /* Just for warning messages */
00033 
00034 static bool MatchBrackets (const char *pattern, const char *test);
00035 static bool MatchList (const char *pattern, const char *test);
00036 
00037 bool Osc_PatternMatch(const char *  pattern, const char * test)
00038 {
00039   theWholePattern = pattern;
00040 
00041   if (pattern == 0 || pattern[0] == 0)
00042   {
00043     return test[0] == 0;
00044   }
00045 
00046   if (test[0] == 0)
00047   {
00048     if (pattern[0] == '*')
00049       return Osc_PatternMatch(pattern+1,test);
00050     else
00051       return false;
00052   }
00053 
00054   switch (pattern[0])
00055   {
00056     case 0:
00057       return test[0] == 0;
00058     case '?':
00059       return Osc_PatternMatch(pattern + 1, test + 1);
00060     case '*':
00061       if (Osc_PatternMatch(pattern+1, test))
00062         return true;
00063       else
00064           return Osc_PatternMatch(pattern, test+1);
00065     case ']':
00066     case '}':
00067       // OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
00068       return false;
00069     case '[':
00070       return MatchBrackets (pattern,test);
00071     case '{':
00072       return MatchList (pattern,test);
00073     case '\\':
00074       if (pattern[1] == 0)
00075           return test[0] == 0;
00076       else
00077       {
00078         if (pattern[1] == test[0])
00079           return Osc_PatternMatch(pattern+2,test+1);
00080         else
00081           return false;
00082       }
00083     default:
00084       if (pattern[0] == test[0])
00085           return Osc_PatternMatch(pattern+1,test+1);
00086       else
00087           return false;
00088   }
00089 }
00090 
00091 
00092 /* we know that pattern[0] == '[' and test[0] != 0 */
00093 
00094 static bool MatchBrackets (const char *pattern, const char *test)
00095 {
00096   bool result;
00097   bool negated = false;
00098   const char *p = pattern;
00099 
00100   if (pattern[1] == 0)
00101   {
00102     // OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00103     return false;
00104   }
00105 
00106   if (pattern[1] == '!')
00107   {
00108     negated = true;
00109     p++;
00110   }
00111 
00112   while (*p != ']')
00113   {
00114     if (*p == 0)
00115     {
00116       //OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00117       return false;
00118     }
00119     if (p[1] == '-' && p[2] != 0)
00120     {
00121       if (test[0] >= p[0] && test[0] <= p[2])
00122       {
00123           result = !negated;
00124           goto advance;
00125       }
00126     }
00127     if (p[0] == test[0])
00128     {
00129       result = !negated;
00130       goto advance;
00131     }
00132     p++;
00133   }
00134 
00135   result = negated;
00136 
00137 advance:
00138 
00139   if (!result)
00140     return false;
00141 
00142   while (*p != ']')
00143   {
00144     if (*p == 0)
00145     {
00146       //OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
00147       return false;
00148     }
00149     p++;
00150   }
00151 
00152   return Osc_PatternMatch(p+1,test+1);
00153 }
00154 
00155 static bool MatchList (const char *pattern, const char *test)
00156 {
00157 
00158  const char *restOfPattern, *tp = test;
00159 
00160  for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++)
00161  {
00162    if (*restOfPattern == 0)
00163    {
00164      //OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
00165      return false;
00166    }
00167  }
00168 
00169  restOfPattern++; /* skip close curly brace */
00170 
00171  pattern++; /* skip open curly brace */
00172 
00173  while (1)
00174  {
00175    if (*pattern == ',')
00176    {
00177      if (Osc_PatternMatch(restOfPattern, tp))
00178        return true;
00179      else
00180      {
00181        tp = test;
00182        ++pattern;
00183      }
00184    }
00185    else
00186    {
00187      if (*pattern == '}')
00188        return Osc_PatternMatch(restOfPattern, tp);
00189      else
00190      {
00191        if (*pattern == *tp)
00192        {
00193          ++pattern;
00194          ++tp;
00195        }
00196        else
00197        {
00198          tp = test;
00199          while (*pattern != ',' && *pattern != '}') {
00200            pattern++;
00201        }
00202        if (*pattern == ',')
00203          pattern++;
00204       }
00205      }
00206    }
00207  }
00208 }
00209 
00210 
00211 
00212