modded version CNMAT/OSC https://github.com/CNMAT/OSC

Dependents:   CVtoOSCConverter

Fork of osc-cnmat by Asperius van Hansen

Committer:
aspeteRakete
Date:
Fri May 16 22:10:49 2014 +0000
Revision:
1:18009e3041ea
OSCMessage Portiert: ::send(Print &p) derzeit auskommentiert.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aspeteRakete 1:18009e3041ea 1 #define OSC_MATCH_ENABLE_2STARS 1
aspeteRakete 1:18009e3041ea 2 #define OSC_MATCH_ENABLE_NSTARS 1
aspeteRakete 1:18009e3041ea 3 /*
aspeteRakete 1:18009e3041ea 4 Written by John MacCallum, The Center for New Music and Audio Technologies,
aspeteRakete 1:18009e3041ea 5 University of California, Berkeley. Copyright (c) 2009, The Regents of
aspeteRakete 1:18009e3041ea 6 the University of California (Regents).
aspeteRakete 1:18009e3041ea 7 Permission to use, copy, modify, distribute, and distribute modified versions
aspeteRakete 1:18009e3041ea 8 of this software and its documentation without fee and without a signed
aspeteRakete 1:18009e3041ea 9 licensing agreement, is hereby granted, provided that the above copyright
aspeteRakete 1:18009e3041ea 10 notice, this paragraph and the following two paragraphs appear in all copies,
aspeteRakete 1:18009e3041ea 11 modifications, and distributions.
aspeteRakete 1:18009e3041ea 12
aspeteRakete 1:18009e3041ea 13 IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
aspeteRakete 1:18009e3041ea 14 SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
aspeteRakete 1:18009e3041ea 15 OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
aspeteRakete 1:18009e3041ea 16 BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aspeteRakete 1:18009e3041ea 17
aspeteRakete 1:18009e3041ea 18 REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
aspeteRakete 1:18009e3041ea 19 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
aspeteRakete 1:18009e3041ea 20 PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
aspeteRakete 1:18009e3041ea 21 HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
aspeteRakete 1:18009e3041ea 22 MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
aspeteRakete 1:18009e3041ea 23 */
aspeteRakete 1:18009e3041ea 24 #include <string.h>
aspeteRakete 1:18009e3041ea 25 #include "OSCMatch.h"
aspeteRakete 1:18009e3041ea 26
aspeteRakete 1:18009e3041ea 27 static int osc_match_star(const char *pattern, const char *address);
aspeteRakete 1:18009e3041ea 28 static int osc_match_star_r(const char *pattern, const char *address);
aspeteRakete 1:18009e3041ea 29 static int osc_match_single_char(const char *pattern, const char *address);
aspeteRakete 1:18009e3041ea 30 static int osc_match_bracket(const char *pattern, const char *address);
aspeteRakete 1:18009e3041ea 31 static int osc_match_curly_brace(const char *pattern, const char *address);
aspeteRakete 1:18009e3041ea 32
aspeteRakete 1:18009e3041ea 33 int osc_match(const char *pattern, const char *address, int *pattern_offset, int *address_offset)
aspeteRakete 1:18009e3041ea 34 {
aspeteRakete 1:18009e3041ea 35 if(!strcmp(pattern, address)){
aspeteRakete 1:18009e3041ea 36 *pattern_offset = strlen(pattern);
aspeteRakete 1:18009e3041ea 37 *address_offset = strlen(address);
aspeteRakete 1:18009e3041ea 38 return OSC_MATCH_ADDRESS_COMPLETE | OSC_MATCH_PATTERN_COMPLETE;
aspeteRakete 1:18009e3041ea 39 }
aspeteRakete 1:18009e3041ea 40
aspeteRakete 1:18009e3041ea 41 const char *pattern_start;
aspeteRakete 1:18009e3041ea 42 const char *address_start;
aspeteRakete 1:18009e3041ea 43
aspeteRakete 1:18009e3041ea 44 pattern_start = pattern;
aspeteRakete 1:18009e3041ea 45 address_start = address;
aspeteRakete 1:18009e3041ea 46
aspeteRakete 1:18009e3041ea 47 *pattern_offset = 0;
aspeteRakete 1:18009e3041ea 48 *address_offset = 0;
aspeteRakete 1:18009e3041ea 49
aspeteRakete 1:18009e3041ea 50 while(*address != '\0' && *pattern != '\0'){
aspeteRakete 1:18009e3041ea 51 if(*pattern == '*'){
aspeteRakete 1:18009e3041ea 52 if(!osc_match_star(pattern, address)){
aspeteRakete 1:18009e3041ea 53 return 0;
aspeteRakete 1:18009e3041ea 54 }
aspeteRakete 1:18009e3041ea 55 while(*pattern != '/' && *pattern != '\0'){
aspeteRakete 1:18009e3041ea 56 pattern++;
aspeteRakete 1:18009e3041ea 57 }
aspeteRakete 1:18009e3041ea 58 while(*address != '/' && *address != '\0'){
aspeteRakete 1:18009e3041ea 59 address++;
aspeteRakete 1:18009e3041ea 60 }
aspeteRakete 1:18009e3041ea 61 }else if(*address == '*'){
aspeteRakete 1:18009e3041ea 62 while(*pattern != '/' && *pattern != '\0'){
aspeteRakete 1:18009e3041ea 63 pattern++;
aspeteRakete 1:18009e3041ea 64 }
aspeteRakete 1:18009e3041ea 65 while(*address != '/' && *address != '\0'){
aspeteRakete 1:18009e3041ea 66 address++;
aspeteRakete 1:18009e3041ea 67 }
aspeteRakete 1:18009e3041ea 68 }else{
aspeteRakete 1:18009e3041ea 69 int n = 0;
aspeteRakete 1:18009e3041ea 70 if(!(n = osc_match_single_char(pattern, address))){
aspeteRakete 1:18009e3041ea 71 return 0;
aspeteRakete 1:18009e3041ea 72 }
aspeteRakete 1:18009e3041ea 73 if(*pattern == '['){
aspeteRakete 1:18009e3041ea 74 while(*pattern != ']'){
aspeteRakete 1:18009e3041ea 75 pattern++;
aspeteRakete 1:18009e3041ea 76 }
aspeteRakete 1:18009e3041ea 77 pattern++;
aspeteRakete 1:18009e3041ea 78 address++;
aspeteRakete 1:18009e3041ea 79 }else if(*pattern == '{'){
aspeteRakete 1:18009e3041ea 80 while(*pattern != '}'){
aspeteRakete 1:18009e3041ea 81 pattern++;
aspeteRakete 1:18009e3041ea 82 }
aspeteRakete 1:18009e3041ea 83 pattern++;
aspeteRakete 1:18009e3041ea 84 address += n;
aspeteRakete 1:18009e3041ea 85 }else{
aspeteRakete 1:18009e3041ea 86 pattern++;
aspeteRakete 1:18009e3041ea 87 address++;
aspeteRakete 1:18009e3041ea 88 }
aspeteRakete 1:18009e3041ea 89 }
aspeteRakete 1:18009e3041ea 90 }
aspeteRakete 1:18009e3041ea 91
aspeteRakete 1:18009e3041ea 92 *pattern_offset = pattern - pattern_start;
aspeteRakete 1:18009e3041ea 93 *address_offset = address - address_start;
aspeteRakete 1:18009e3041ea 94
aspeteRakete 1:18009e3041ea 95 int r = 0;
aspeteRakete 1:18009e3041ea 96
aspeteRakete 1:18009e3041ea 97 if(*address == '\0') {
aspeteRakete 1:18009e3041ea 98 r |= OSC_MATCH_ADDRESS_COMPLETE;
aspeteRakete 1:18009e3041ea 99 }
aspeteRakete 1:18009e3041ea 100
aspeteRakete 1:18009e3041ea 101 if(*pattern == '\0') {
aspeteRakete 1:18009e3041ea 102 r |= OSC_MATCH_PATTERN_COMPLETE;
aspeteRakete 1:18009e3041ea 103 }
aspeteRakete 1:18009e3041ea 104
aspeteRakete 1:18009e3041ea 105 return r;
aspeteRakete 1:18009e3041ea 106 }
aspeteRakete 1:18009e3041ea 107
aspeteRakete 1:18009e3041ea 108 static int osc_match_star(const char *pattern, const char *address)
aspeteRakete 1:18009e3041ea 109 {
aspeteRakete 1:18009e3041ea 110 const char *address_start = address;
aspeteRakete 1:18009e3041ea 111 const char *pattern_start = pattern;
aspeteRakete 1:18009e3041ea 112 int num_stars = 0;
aspeteRakete 1:18009e3041ea 113 if(*address == '\0') { return 0; }
aspeteRakete 1:18009e3041ea 114 while(*address != '/' && *address != '\0'){
aspeteRakete 1:18009e3041ea 115 address++;
aspeteRakete 1:18009e3041ea 116 }
aspeteRakete 1:18009e3041ea 117 while(*pattern != '/' && *pattern != '\0'){
aspeteRakete 1:18009e3041ea 118 if(*pattern == '*'){
aspeteRakete 1:18009e3041ea 119 num_stars++;
aspeteRakete 1:18009e3041ea 120 }
aspeteRakete 1:18009e3041ea 121 pattern++;
aspeteRakete 1:18009e3041ea 122 }
aspeteRakete 1:18009e3041ea 123 pattern--;
aspeteRakete 1:18009e3041ea 124 address--;
aspeteRakete 1:18009e3041ea 125 switch(num_stars){
aspeteRakete 1:18009e3041ea 126 case 1:
aspeteRakete 1:18009e3041ea 127 {
aspeteRakete 1:18009e3041ea 128 const char *pp = pattern, *aa = address;
aspeteRakete 1:18009e3041ea 129 while(*pp != '*'){
aspeteRakete 1:18009e3041ea 130 if(!(osc_match_single_char(pp, aa))){
aspeteRakete 1:18009e3041ea 131 return 0;
aspeteRakete 1:18009e3041ea 132 }
aspeteRakete 1:18009e3041ea 133 if(*pp == ']' || *pp == '}'){
aspeteRakete 1:18009e3041ea 134 while(*pp != '[' && *pp != '{'){
aspeteRakete 1:18009e3041ea 135 pp--;
aspeteRakete 1:18009e3041ea 136 }
aspeteRakete 1:18009e3041ea 137 }
aspeteRakete 1:18009e3041ea 138 pp--;
aspeteRakete 1:18009e3041ea 139 aa--;
aspeteRakete 1:18009e3041ea 140 }
aspeteRakete 1:18009e3041ea 141 }
aspeteRakete 1:18009e3041ea 142 break;
aspeteRakete 1:18009e3041ea 143 case 2:
aspeteRakete 1:18009e3041ea 144 #if (OSC_MATCH_ENABLE_2STARS == 1)
aspeteRakete 1:18009e3041ea 145 {
aspeteRakete 1:18009e3041ea 146 const char *pp = pattern, *aa = address;
aspeteRakete 1:18009e3041ea 147 while(*pp != '*'){
aspeteRakete 1:18009e3041ea 148 if(!(osc_match_single_char(pp, aa))){
aspeteRakete 1:18009e3041ea 149 return 0;
aspeteRakete 1:18009e3041ea 150 }
aspeteRakete 1:18009e3041ea 151 if(*pp == ']' || *pp == '}'){
aspeteRakete 1:18009e3041ea 152 while(*pp != '[' && *pp != '{'){
aspeteRakete 1:18009e3041ea 153 pp--;
aspeteRakete 1:18009e3041ea 154 }
aspeteRakete 1:18009e3041ea 155 }
aspeteRakete 1:18009e3041ea 156 pp--;
aspeteRakete 1:18009e3041ea 157 aa--;
aspeteRakete 1:18009e3041ea 158 }
aspeteRakete 1:18009e3041ea 159 aa++; // we want to start one character forward to allow the star to match nothing
aspeteRakete 1:18009e3041ea 160 const char *star2 = pp;
aspeteRakete 1:18009e3041ea 161 const char *test = aa;
aspeteRakete 1:18009e3041ea 162 int i = 0;
aspeteRakete 1:18009e3041ea 163 while(test > address_start){
aspeteRakete 1:18009e3041ea 164 pp = star2 - 1;
aspeteRakete 1:18009e3041ea 165 aa = test - 1;
aspeteRakete 1:18009e3041ea 166 i++;
aspeteRakete 1:18009e3041ea 167 while(*pp != '*'){
aspeteRakete 1:18009e3041ea 168 if(!osc_match_single_char(pp, aa)){
aspeteRakete 1:18009e3041ea 169 break;
aspeteRakete 1:18009e3041ea 170 }
aspeteRakete 1:18009e3041ea 171 if(*pp == ']' || *pp == '}'){
aspeteRakete 1:18009e3041ea 172 while(*pp != '[' && *pp != '{'){
aspeteRakete 1:18009e3041ea 173 pp--;
aspeteRakete 1:18009e3041ea 174 }
aspeteRakete 1:18009e3041ea 175 }
aspeteRakete 1:18009e3041ea 176 pp--;
aspeteRakete 1:18009e3041ea 177 aa--;
aspeteRakete 1:18009e3041ea 178 }
aspeteRakete 1:18009e3041ea 179 if(pp == pattern_start){
aspeteRakete 1:18009e3041ea 180 return 1;
aspeteRakete 1:18009e3041ea 181 }
aspeteRakete 1:18009e3041ea 182 test--;
aspeteRakete 1:18009e3041ea 183 }
aspeteRakete 1:18009e3041ea 184 return 0;
aspeteRakete 1:18009e3041ea 185 }
aspeteRakete 1:18009e3041ea 186 break;
aspeteRakete 1:18009e3041ea 187 #else
aspeteRakete 1:18009e3041ea 188 return 0;
aspeteRakete 1:18009e3041ea 189 #endif
aspeteRakete 1:18009e3041ea 190 default:
aspeteRakete 1:18009e3041ea 191 #if (OSC_MATCH_ENABLE_NSTARS == 1)
aspeteRakete 1:18009e3041ea 192 return osc_match_star_r(pattern_start, address_start);
aspeteRakete 1:18009e3041ea 193 break;
aspeteRakete 1:18009e3041ea 194 #else
aspeteRakete 1:18009e3041ea 195 return 0;
aspeteRakete 1:18009e3041ea 196 #endif
aspeteRakete 1:18009e3041ea 197 }
aspeteRakete 1:18009e3041ea 198 return 1;
aspeteRakete 1:18009e3041ea 199 }
aspeteRakete 1:18009e3041ea 200
aspeteRakete 1:18009e3041ea 201 #if (OSC_MATCH_ENABLE_NSTARS == 1)
aspeteRakete 1:18009e3041ea 202 static int osc_match_star_r(const char *pattern, const char *address)
aspeteRakete 1:18009e3041ea 203 {
aspeteRakete 1:18009e3041ea 204 if(*address == '/' || *address == '\0'){
aspeteRakete 1:18009e3041ea 205 if(*pattern == '/' || *pattern == '\0' || (*pattern == '*' && ((*(pattern + 1) == '/') || *(pattern + 1) == '\0'))){
aspeteRakete 1:18009e3041ea 206 return 1;
aspeteRakete 1:18009e3041ea 207 }else{
aspeteRakete 1:18009e3041ea 208 return 0;
aspeteRakete 1:18009e3041ea 209 }
aspeteRakete 1:18009e3041ea 210 }
aspeteRakete 1:18009e3041ea 211 if(*pattern == '*'){
aspeteRakete 1:18009e3041ea 212 if(osc_match_star_r(pattern + 1, address)){
aspeteRakete 1:18009e3041ea 213 return 1;
aspeteRakete 1:18009e3041ea 214 }else{
aspeteRakete 1:18009e3041ea 215 return osc_match_star_r(pattern, address + 1);
aspeteRakete 1:18009e3041ea 216 }
aspeteRakete 1:18009e3041ea 217 }else{
aspeteRakete 1:18009e3041ea 218 if(!osc_match_single_char(pattern, address)){
aspeteRakete 1:18009e3041ea 219 return 0;
aspeteRakete 1:18009e3041ea 220 }
aspeteRakete 1:18009e3041ea 221 if(*pattern == '[' || *pattern == '{'){
aspeteRakete 1:18009e3041ea 222 while(*pattern != ']' && *pattern != '}'){
aspeteRakete 1:18009e3041ea 223 pattern++;
aspeteRakete 1:18009e3041ea 224 }
aspeteRakete 1:18009e3041ea 225 }
aspeteRakete 1:18009e3041ea 226 return osc_match_star_r(pattern + 1, address + 1);
aspeteRakete 1:18009e3041ea 227 }
aspeteRakete 1:18009e3041ea 228 }
aspeteRakete 1:18009e3041ea 229 #endif
aspeteRakete 1:18009e3041ea 230
aspeteRakete 1:18009e3041ea 231 static int osc_match_single_char(const char *pattern, const char *address)
aspeteRakete 1:18009e3041ea 232 {
aspeteRakete 1:18009e3041ea 233 switch(*pattern){
aspeteRakete 1:18009e3041ea 234 case '[':
aspeteRakete 1:18009e3041ea 235 return osc_match_bracket(pattern, address);
aspeteRakete 1:18009e3041ea 236 case ']':
aspeteRakete 1:18009e3041ea 237 while(*pattern != '['){
aspeteRakete 1:18009e3041ea 238 pattern--;
aspeteRakete 1:18009e3041ea 239 }
aspeteRakete 1:18009e3041ea 240 return osc_match_bracket(pattern, address);
aspeteRakete 1:18009e3041ea 241 case '{':
aspeteRakete 1:18009e3041ea 242 return osc_match_curly_brace(pattern, address);
aspeteRakete 1:18009e3041ea 243 case '}':
aspeteRakete 1:18009e3041ea 244 while(*pattern != '{'){
aspeteRakete 1:18009e3041ea 245 pattern--;
aspeteRakete 1:18009e3041ea 246 }
aspeteRakete 1:18009e3041ea 247 return osc_match_curly_brace(pattern, address);
aspeteRakete 1:18009e3041ea 248 case '?':
aspeteRakete 1:18009e3041ea 249 return 1;
aspeteRakete 1:18009e3041ea 250 default:
aspeteRakete 1:18009e3041ea 251 if(*pattern == *address){
aspeteRakete 1:18009e3041ea 252 return 1;
aspeteRakete 1:18009e3041ea 253 }else{
aspeteRakete 1:18009e3041ea 254 return 0;
aspeteRakete 1:18009e3041ea 255 }
aspeteRakete 1:18009e3041ea 256 }
aspeteRakete 1:18009e3041ea 257 return 0;
aspeteRakete 1:18009e3041ea 258 }
aspeteRakete 1:18009e3041ea 259
aspeteRakete 1:18009e3041ea 260 static int osc_match_bracket(const char *pattern, const char *address)
aspeteRakete 1:18009e3041ea 261 {
aspeteRakete 1:18009e3041ea 262 pattern++;
aspeteRakete 1:18009e3041ea 263 int val = 1;
aspeteRakete 1:18009e3041ea 264 if(*pattern == '!'){
aspeteRakete 1:18009e3041ea 265 pattern++;
aspeteRakete 1:18009e3041ea 266 val = 0;
aspeteRakete 1:18009e3041ea 267 }
aspeteRakete 1:18009e3041ea 268 int matched = !val;
aspeteRakete 1:18009e3041ea 269 while(*pattern != ']' && *pattern != '\0'){
aspeteRakete 1:18009e3041ea 270 // the character we're on now is the beginning of a range
aspeteRakete 1:18009e3041ea 271 if(*(pattern + 1) == '-'){
aspeteRakete 1:18009e3041ea 272 if(*address >= *pattern && *address <= *(pattern + 2)){
aspeteRakete 1:18009e3041ea 273 matched = val;
aspeteRakete 1:18009e3041ea 274 break;
aspeteRakete 1:18009e3041ea 275 }else{
aspeteRakete 1:18009e3041ea 276 pattern += 3;
aspeteRakete 1:18009e3041ea 277 }
aspeteRakete 1:18009e3041ea 278 }else{
aspeteRakete 1:18009e3041ea 279 // just test the character
aspeteRakete 1:18009e3041ea 280 if(*pattern == *address){
aspeteRakete 1:18009e3041ea 281 matched = val;
aspeteRakete 1:18009e3041ea 282 break;
aspeteRakete 1:18009e3041ea 283 }
aspeteRakete 1:18009e3041ea 284 pattern++;
aspeteRakete 1:18009e3041ea 285 }
aspeteRakete 1:18009e3041ea 286 }
aspeteRakete 1:18009e3041ea 287 return matched;
aspeteRakete 1:18009e3041ea 288 }
aspeteRakete 1:18009e3041ea 289
aspeteRakete 1:18009e3041ea 290 static int osc_match_curly_brace(const char *pattern, const char *address)
aspeteRakete 1:18009e3041ea 291 {
aspeteRakete 1:18009e3041ea 292 pattern++;
aspeteRakete 1:18009e3041ea 293 const char *ptr = pattern;
aspeteRakete 1:18009e3041ea 294 while(*ptr != '}' && *ptr != '\0' && *ptr != '/'){
aspeteRakete 1:18009e3041ea 295 while(*ptr != '}' && *ptr != '\0' && *ptr != '/' && *ptr != ','){
aspeteRakete 1:18009e3041ea 296 ptr++;
aspeteRakete 1:18009e3041ea 297 }
aspeteRakete 1:18009e3041ea 298 int n = ptr - pattern;
aspeteRakete 1:18009e3041ea 299 if(!strncmp(pattern, address, n)){
aspeteRakete 1:18009e3041ea 300 return n;
aspeteRakete 1:18009e3041ea 301 }else{
aspeteRakete 1:18009e3041ea 302 ptr++;
aspeteRakete 1:18009e3041ea 303 pattern = ptr;
aspeteRakete 1:18009e3041ea 304 }
aspeteRakete 1:18009e3041ea 305 }
aspeteRakete 1:18009e3041ea 306 return 0;
aspeteRakete 1:18009e3041ea 307 }