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