Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.
osc_patternmatch.c@0:439354122597, 2010-03-17 (annotated)
- Committer:
- pehrhovey
- Date:
- Wed Mar 17 03:17:38 2010 +0000
- Revision:
- 0:439354122597
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pehrhovey | 0:439354122597 | 1 | /* Pehr Hovey - mBed OSC |
pehrhovey | 0:439354122597 | 2 | * This Code is from the Make Controller firmware repository |
pehrhovey | 0:439354122597 | 3 | * http://makingthings.com |
pehrhovey | 0:439354122597 | 4 | */ |
pehrhovey | 0:439354122597 | 5 | /********************************************************************************* |
pehrhovey | 0:439354122597 | 6 | |
pehrhovey | 0:439354122597 | 7 | Copyright 2006-2009 MakingThings |
pehrhovey | 0:439354122597 | 8 | |
pehrhovey | 0:439354122597 | 9 | Licensed under the Apache License, |
pehrhovey | 0:439354122597 | 10 | Version 2.0 (the "License"); you may not use this file except in compliance |
pehrhovey | 0:439354122597 | 11 | with the License. You may obtain a copy of the License at |
pehrhovey | 0:439354122597 | 12 | |
pehrhovey | 0:439354122597 | 13 | http://www.apache.org/licenses/LICENSE-2.0 |
pehrhovey | 0:439354122597 | 14 | |
pehrhovey | 0:439354122597 | 15 | Unless required by applicable law or agreed to in writing, software distributed |
pehrhovey | 0:439354122597 | 16 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
pehrhovey | 0:439354122597 | 17 | CONDITIONS OF ANY KIND, either express or implied. See the License for |
pehrhovey | 0:439354122597 | 18 | the specific language governing permissions and limitations under the License. |
pehrhovey | 0:439354122597 | 19 | |
pehrhovey | 0:439354122597 | 20 | *********************************************************************************/ |
pehrhovey | 0:439354122597 | 21 | |
pehrhovey | 0:439354122597 | 22 | /* |
pehrhovey | 0:439354122597 | 23 | osc_patternmatch.c |
pehrhovey | 0:439354122597 | 24 | |
pehrhovey | 0:439354122597 | 25 | Adapted from OSC-pattern-match.c, by Matt Wright |
pehrhovey | 0:439354122597 | 26 | Adapted from oscpattern.c, by Matt Wright and Amar Chaudhury |
pehrhovey | 0:439354122597 | 27 | */ |
pehrhovey | 0:439354122597 | 28 | |
pehrhovey | 0:439354122597 | 29 | |
pehrhovey | 0:439354122597 | 30 | |
pehrhovey | 0:439354122597 | 31 | |
pehrhovey | 0:439354122597 | 32 | static const char *theWholePattern; /* Just for warning messages */ |
pehrhovey | 0:439354122597 | 33 | |
pehrhovey | 0:439354122597 | 34 | static bool MatchBrackets (const char *pattern, const char *test); |
pehrhovey | 0:439354122597 | 35 | static bool MatchList (const char *pattern, const char *test); |
pehrhovey | 0:439354122597 | 36 | |
pehrhovey | 0:439354122597 | 37 | bool Osc_PatternMatch(const char * pattern, const char * test) |
pehrhovey | 0:439354122597 | 38 | { |
pehrhovey | 0:439354122597 | 39 | theWholePattern = pattern; |
pehrhovey | 0:439354122597 | 40 | |
pehrhovey | 0:439354122597 | 41 | if (pattern == 0 || pattern[0] == 0) |
pehrhovey | 0:439354122597 | 42 | { |
pehrhovey | 0:439354122597 | 43 | return test[0] == 0; |
pehrhovey | 0:439354122597 | 44 | } |
pehrhovey | 0:439354122597 | 45 | |
pehrhovey | 0:439354122597 | 46 | if (test[0] == 0) |
pehrhovey | 0:439354122597 | 47 | { |
pehrhovey | 0:439354122597 | 48 | if (pattern[0] == '*') |
pehrhovey | 0:439354122597 | 49 | return Osc_PatternMatch(pattern+1,test); |
pehrhovey | 0:439354122597 | 50 | else |
pehrhovey | 0:439354122597 | 51 | return false; |
pehrhovey | 0:439354122597 | 52 | } |
pehrhovey | 0:439354122597 | 53 | |
pehrhovey | 0:439354122597 | 54 | switch (pattern[0]) |
pehrhovey | 0:439354122597 | 55 | { |
pehrhovey | 0:439354122597 | 56 | case 0: |
pehrhovey | 0:439354122597 | 57 | return test[0] == 0; |
pehrhovey | 0:439354122597 | 58 | case '?': |
pehrhovey | 0:439354122597 | 59 | return Osc_PatternMatch(pattern + 1, test + 1); |
pehrhovey | 0:439354122597 | 60 | case '*': |
pehrhovey | 0:439354122597 | 61 | if (Osc_PatternMatch(pattern+1, test)) |
pehrhovey | 0:439354122597 | 62 | return true; |
pehrhovey | 0:439354122597 | 63 | else |
pehrhovey | 0:439354122597 | 64 | return Osc_PatternMatch(pattern, test+1); |
pehrhovey | 0:439354122597 | 65 | case ']': |
pehrhovey | 0:439354122597 | 66 | case '}': |
pehrhovey | 0:439354122597 | 67 | // OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern); |
pehrhovey | 0:439354122597 | 68 | return false; |
pehrhovey | 0:439354122597 | 69 | case '[': |
pehrhovey | 0:439354122597 | 70 | return MatchBrackets (pattern,test); |
pehrhovey | 0:439354122597 | 71 | case '{': |
pehrhovey | 0:439354122597 | 72 | return MatchList (pattern,test); |
pehrhovey | 0:439354122597 | 73 | case '\\': |
pehrhovey | 0:439354122597 | 74 | if (pattern[1] == 0) |
pehrhovey | 0:439354122597 | 75 | return test[0] == 0; |
pehrhovey | 0:439354122597 | 76 | else |
pehrhovey | 0:439354122597 | 77 | { |
pehrhovey | 0:439354122597 | 78 | if (pattern[1] == test[0]) |
pehrhovey | 0:439354122597 | 79 | return Osc_PatternMatch(pattern+2,test+1); |
pehrhovey | 0:439354122597 | 80 | else |
pehrhovey | 0:439354122597 | 81 | return false; |
pehrhovey | 0:439354122597 | 82 | } |
pehrhovey | 0:439354122597 | 83 | default: |
pehrhovey | 0:439354122597 | 84 | if (pattern[0] == test[0]) |
pehrhovey | 0:439354122597 | 85 | return Osc_PatternMatch(pattern+1,test+1); |
pehrhovey | 0:439354122597 | 86 | else |
pehrhovey | 0:439354122597 | 87 | return false; |
pehrhovey | 0:439354122597 | 88 | } |
pehrhovey | 0:439354122597 | 89 | } |
pehrhovey | 0:439354122597 | 90 | |
pehrhovey | 0:439354122597 | 91 | |
pehrhovey | 0:439354122597 | 92 | /* we know that pattern[0] == '[' and test[0] != 0 */ |
pehrhovey | 0:439354122597 | 93 | |
pehrhovey | 0:439354122597 | 94 | static bool MatchBrackets (const char *pattern, const char *test) |
pehrhovey | 0:439354122597 | 95 | { |
pehrhovey | 0:439354122597 | 96 | bool result; |
pehrhovey | 0:439354122597 | 97 | bool negated = false; |
pehrhovey | 0:439354122597 | 98 | const char *p = pattern; |
pehrhovey | 0:439354122597 | 99 | |
pehrhovey | 0:439354122597 | 100 | if (pattern[1] == 0) |
pehrhovey | 0:439354122597 | 101 | { |
pehrhovey | 0:439354122597 | 102 | // OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern); |
pehrhovey | 0:439354122597 | 103 | return false; |
pehrhovey | 0:439354122597 | 104 | } |
pehrhovey | 0:439354122597 | 105 | |
pehrhovey | 0:439354122597 | 106 | if (pattern[1] == '!') |
pehrhovey | 0:439354122597 | 107 | { |
pehrhovey | 0:439354122597 | 108 | negated = true; |
pehrhovey | 0:439354122597 | 109 | p++; |
pehrhovey | 0:439354122597 | 110 | } |
pehrhovey | 0:439354122597 | 111 | |
pehrhovey | 0:439354122597 | 112 | while (*p != ']') |
pehrhovey | 0:439354122597 | 113 | { |
pehrhovey | 0:439354122597 | 114 | if (*p == 0) |
pehrhovey | 0:439354122597 | 115 | { |
pehrhovey | 0:439354122597 | 116 | //OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern); |
pehrhovey | 0:439354122597 | 117 | return false; |
pehrhovey | 0:439354122597 | 118 | } |
pehrhovey | 0:439354122597 | 119 | if (p[1] == '-' && p[2] != 0) |
pehrhovey | 0:439354122597 | 120 | { |
pehrhovey | 0:439354122597 | 121 | if (test[0] >= p[0] && test[0] <= p[2]) |
pehrhovey | 0:439354122597 | 122 | { |
pehrhovey | 0:439354122597 | 123 | result = !negated; |
pehrhovey | 0:439354122597 | 124 | goto advance; |
pehrhovey | 0:439354122597 | 125 | } |
pehrhovey | 0:439354122597 | 126 | } |
pehrhovey | 0:439354122597 | 127 | if (p[0] == test[0]) |
pehrhovey | 0:439354122597 | 128 | { |
pehrhovey | 0:439354122597 | 129 | result = !negated; |
pehrhovey | 0:439354122597 | 130 | goto advance; |
pehrhovey | 0:439354122597 | 131 | } |
pehrhovey | 0:439354122597 | 132 | p++; |
pehrhovey | 0:439354122597 | 133 | } |
pehrhovey | 0:439354122597 | 134 | |
pehrhovey | 0:439354122597 | 135 | result = negated; |
pehrhovey | 0:439354122597 | 136 | |
pehrhovey | 0:439354122597 | 137 | advance: |
pehrhovey | 0:439354122597 | 138 | |
pehrhovey | 0:439354122597 | 139 | if (!result) |
pehrhovey | 0:439354122597 | 140 | return false; |
pehrhovey | 0:439354122597 | 141 | |
pehrhovey | 0:439354122597 | 142 | while (*p != ']') |
pehrhovey | 0:439354122597 | 143 | { |
pehrhovey | 0:439354122597 | 144 | if (*p == 0) |
pehrhovey | 0:439354122597 | 145 | { |
pehrhovey | 0:439354122597 | 146 | //OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern); |
pehrhovey | 0:439354122597 | 147 | return false; |
pehrhovey | 0:439354122597 | 148 | } |
pehrhovey | 0:439354122597 | 149 | p++; |
pehrhovey | 0:439354122597 | 150 | } |
pehrhovey | 0:439354122597 | 151 | |
pehrhovey | 0:439354122597 | 152 | return Osc_PatternMatch(p+1,test+1); |
pehrhovey | 0:439354122597 | 153 | } |
pehrhovey | 0:439354122597 | 154 | |
pehrhovey | 0:439354122597 | 155 | static bool MatchList (const char *pattern, const char *test) |
pehrhovey | 0:439354122597 | 156 | { |
pehrhovey | 0:439354122597 | 157 | |
pehrhovey | 0:439354122597 | 158 | const char *restOfPattern, *tp = test; |
pehrhovey | 0:439354122597 | 159 | |
pehrhovey | 0:439354122597 | 160 | for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) |
pehrhovey | 0:439354122597 | 161 | { |
pehrhovey | 0:439354122597 | 162 | if (*restOfPattern == 0) |
pehrhovey | 0:439354122597 | 163 | { |
pehrhovey | 0:439354122597 | 164 | //OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern); |
pehrhovey | 0:439354122597 | 165 | return false; |
pehrhovey | 0:439354122597 | 166 | } |
pehrhovey | 0:439354122597 | 167 | } |
pehrhovey | 0:439354122597 | 168 | |
pehrhovey | 0:439354122597 | 169 | restOfPattern++; /* skip close curly brace */ |
pehrhovey | 0:439354122597 | 170 | |
pehrhovey | 0:439354122597 | 171 | pattern++; /* skip open curly brace */ |
pehrhovey | 0:439354122597 | 172 | |
pehrhovey | 0:439354122597 | 173 | while (1) |
pehrhovey | 0:439354122597 | 174 | { |
pehrhovey | 0:439354122597 | 175 | if (*pattern == ',') |
pehrhovey | 0:439354122597 | 176 | { |
pehrhovey | 0:439354122597 | 177 | if (Osc_PatternMatch(restOfPattern, tp)) |
pehrhovey | 0:439354122597 | 178 | return true; |
pehrhovey | 0:439354122597 | 179 | else |
pehrhovey | 0:439354122597 | 180 | { |
pehrhovey | 0:439354122597 | 181 | tp = test; |
pehrhovey | 0:439354122597 | 182 | ++pattern; |
pehrhovey | 0:439354122597 | 183 | } |
pehrhovey | 0:439354122597 | 184 | } |
pehrhovey | 0:439354122597 | 185 | else |
pehrhovey | 0:439354122597 | 186 | { |
pehrhovey | 0:439354122597 | 187 | if (*pattern == '}') |
pehrhovey | 0:439354122597 | 188 | return Osc_PatternMatch(restOfPattern, tp); |
pehrhovey | 0:439354122597 | 189 | else |
pehrhovey | 0:439354122597 | 190 | { |
pehrhovey | 0:439354122597 | 191 | if (*pattern == *tp) |
pehrhovey | 0:439354122597 | 192 | { |
pehrhovey | 0:439354122597 | 193 | ++pattern; |
pehrhovey | 0:439354122597 | 194 | ++tp; |
pehrhovey | 0:439354122597 | 195 | } |
pehrhovey | 0:439354122597 | 196 | else |
pehrhovey | 0:439354122597 | 197 | { |
pehrhovey | 0:439354122597 | 198 | tp = test; |
pehrhovey | 0:439354122597 | 199 | while (*pattern != ',' && *pattern != '}') { |
pehrhovey | 0:439354122597 | 200 | pattern++; |
pehrhovey | 0:439354122597 | 201 | } |
pehrhovey | 0:439354122597 | 202 | if (*pattern == ',') |
pehrhovey | 0:439354122597 | 203 | pattern++; |
pehrhovey | 0:439354122597 | 204 | } |
pehrhovey | 0:439354122597 | 205 | } |
pehrhovey | 0:439354122597 | 206 | } |
pehrhovey | 0:439354122597 | 207 | } |
pehrhovey | 0:439354122597 | 208 | } |
pehrhovey | 0:439354122597 | 209 | |
pehrhovey | 0:439354122597 | 210 | |
pehrhovey | 0:439354122597 | 211 | |
pehrhovey | 0:439354122597 | 212 |