| 1 | /* |
| 2 | * Copyright (C) 2014 Steve Harris et al. (see AUTHORS) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as |
| 6 | * published by the Free Software Foundation; either version 2.1 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * $Id$ |
| 15 | */ |
| 16 | |
| 17 | /* This code was originally forked from: */ |
| 18 | |
| 19 | /* Open SoundControl kit in C++ */ |
| 20 | /* Copyright (C) 2002-2004 libOSC++ contributors. See AUTHORS */ |
| 21 | /* */ |
| 22 | /* This library is free software; you can redistribute it and/or */ |
| 23 | /* modify it under the terms of the GNU Lesser General Public */ |
| 24 | /* License as published by the Free Software Foundation; either */ |
| 25 | /* version 2.1 of the License, or (at your option) any later version. */ |
| 26 | /* */ |
| 27 | /* This library is distributed in the hope that it will be useful, */ |
| 28 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 29 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ |
| 30 | /* Lesser General Public License for more details. */ |
| 31 | /* */ |
| 32 | /* You should have received a copy of the GNU Lesser General Public */ |
| 33 | /* License along with this library; if not, write to the Free Software */ |
| 34 | /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 35 | /* */ |
| 36 | /* For questions regarding this program contact */ |
| 37 | /* Daniel Holth <[email protected]> or visit */ |
| 38 | /* http://wiretap.stetson.edu/ */ |
| 39 | |
| 40 | /* In the sprit of the public domain, my modifications to this file are also */ |
| 41 | /* dedicated to the public domain. Daniel Holth, Oct. 2004 */ |
| 42 | |
| 43 | /* ChangeLog: |
| 44 | * |
| 45 | * 2004-10-29 Import, convert to C++, begin OSC syntax changes. -dwh |
| 46 | * OSC syntax changes are now working, needs more testing. |
| 47 | * |
| 48 | */ |
| 49 | |
| 50 | // Original header and syntax: |
| 51 | |
| 52 | /* |
| 53 | * robust glob pattern matcher |
| 54 | * ozan s. yigit/dec 1994 |
| 55 | * public domain |
| 56 | * |
| 57 | * glob patterns: |
| 58 | * * matches zero or more characters |
| 59 | * ? matches any single character |
| 60 | * [set] matches any character in the set |
| 61 | * [^set] matches any character NOT in the set |
| 62 | * where a set is a group of characters or ranges. a range |
| 63 | * is written as two characters seperated with a hyphen: a-z denotes |
| 64 | * all characters between a to z inclusive. |
| 65 | * [-set] set matches a literal hypen and any character in the set |
| 66 | * []set] matches a literal close bracket and any character in the set |
| 67 | * |
| 68 | * char matches itself except where char is '*' or '?' or '[' |
| 69 | * \char matches char, including any pattern character |
| 70 | * |
| 71 | * examples: |
| 72 | * a*c ac abc abbc ... |
| 73 | * a?c acc abc aXc ... |
| 74 | * a[a-z]c aac abc acc ... |
| 75 | * a[-a-z]c a-c aac abc ... |
| 76 | * |
| 77 | * $Log$ |
| 78 | * Revision 1.1 2004/11/19 23:00:57 theno23 |
| 79 | * Added lo_send_timestamped |
| 80 | * |
| 81 | * Revision 1.3 1995/09/14 23:24:23 oz |
| 82 | * removed boring test/main code. |
| 83 | * |
| 84 | * Revision 1.2 94/12/11 10:38:15 oz |
| 85 | * cset code fixed. it is now robust and interprets all |
| 86 | * variations of cset [i think] correctly, including [z-a] etc. |
| 87 | * |
| 88 | * Revision 1.1 94/12/08 12:45:23 oz |
| 89 | * Initial revision |
| 90 | */ |
| 91 | |
| 92 | #include <string.h> |
| 93 | |
| 94 | #ifndef NEGATE |
| 95 | #define NEGATE '!' |
| 96 | #endif |
| 97 | |
| 98 | #ifndef true |
| 99 | #define true 1 |
| 100 | #endif |
| 101 | #ifndef false |
| 102 | #define false 0 |
| 103 | #endif |
| 104 | |
| 105 | int lo_pattern_match(const char *str, const char *p) |
| 106 | { |
| 107 | int negate; |
| 108 | int match; |
| 109 | char c; |
| 110 | |
| 111 | while (*p) { |
| 112 | if (!*str && *p != '*') |
| 113 | return false; |
| 114 | |
| 115 | switch (c = *p++) { |
| 116 | |
| 117 | case '*': |
| 118 | while (*p == '*' && *p != '/') |
| 119 | p++; |
| 120 | |
| 121 | if (!*p) |
| 122 | return true; |
| 123 | |
| 124 | // if (*p != '?' && *p != '[' && *p != '\\') |
| 125 | if (*p != '?' && *p != '[' && *p != '{') |
| 126 | while (*str && *p != *str) |
| 127 | str++; |
| 128 | |
| 129 | while (*str) { |
| 130 | if (lo_pattern_match(str, p)) |
| 131 | return true; |
| 132 | str++; |
| 133 | } |
| 134 | return false; |
| 135 | |
| 136 | case '?': |
| 137 | if (*str) |
| 138 | break; |
| 139 | return false; |
| 140 | /* |
| 141 | * set specification is inclusive, that is [a-z] is a, z and |
| 142 | * everything in between. this means [z-a] may be interpreted |
| 143 | * as a set that contains z, a and nothing in between. |
| 144 | */ |
| 145 | case '[': |
| 146 | if (*p != NEGATE) |
| 147 | negate = false; |
| 148 | else { |
| 149 | negate = true; |
| 150 | p++; |
| 151 | } |
| 152 | |
| 153 | match = false; |
| 154 | |
| 155 | while (!match && (c = *p++)) { |
| 156 | if (!*p) |
| 157 | return false; |
| 158 | if (*p == '-') { /* c-c */ |
| 159 | if (!*++p) |
| 160 | return false; |
| 161 | if (*p != ']') { |
| 162 | if (*str == c || *str == *p || |
| 163 | (*str > c && *str < *p)) |
| 164 | match = true; |
| 165 | } else { /* c-] */ |
| 166 | // spec 1.0: "no special meaning" so it should match '-' |
| 167 | if (*str == '-') |
| 168 | match = true; |
| 169 | break; |
| 170 | } |
| 171 | } else { /* cc or c] */ |
| 172 | if (c == *str) |
| 173 | match = true; |
| 174 | if (*p != ']') { |
| 175 | if (*p == *str) |
| 176 | match = true; |
| 177 | } else |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (negate == match) |
| 183 | return false; |
| 184 | /* |
| 185 | * if there is a match, skip past the cset and continue on |
| 186 | */ |
| 187 | while (*p && *p != ']') |
| 188 | p++; |
| 189 | if (!*p++) /* oops! */ |
| 190 | return false; |
| 191 | break; |
| 192 | |
| 193 | /* |
| 194 | * {astring,bstring,cstring} |
| 195 | */ |
| 196 | case '{': |
| 197 | { |
| 198 | // *p is now first character in the {brace list} |
| 199 | const char *place = str; // to backtrack |
| 200 | const char *remainder = p; // to forwardtrack |
| 201 | |
| 202 | // find the end of the brace list |
| 203 | while (*remainder && *remainder != '}') |
| 204 | remainder++; |
| 205 | if (!*remainder++) /* oops! */ |
| 206 | return false; |
| 207 | |
| 208 | c = *p++; |
| 209 | |
| 210 | while (c) { |
| 211 | if (c == ',') { |
| 212 | if (lo_pattern_match(str, remainder)) { |
| 213 | return true; |
| 214 | } else { |
| 215 | // backtrack on test string |
| 216 | str = place; |
| 217 | // continue testing, |
| 218 | } |
| 219 | } else if (c == '}') { |
| 220 | // continue normal pattern matching |
| 221 | if (!*p && !*str) |
| 222 | return true; |
| 223 | str--; // str is incremented again below |
| 224 | break; |
| 225 | } else if (c == *str) { |
| 226 | str++; |
| 227 | if (!*str && *remainder) |
| 228 | return false; |
| 229 | } else { // skip to next comma |
| 230 | str = place; |
| 231 | while (*p != ',' && *p != '}' && *p) |
| 232 | p++; |
| 233 | if (*p == ',') |
| 234 | p++; |
| 235 | else if (*p == '}') { |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | c = *p++; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | break; |
| 244 | |
| 245 | /* Not part of OSC pattern matching |
| 246 | case '\\': |
| 247 | if (*p) |
| 248 | c = *p++; |
| 249 | */ |
| 250 | |
| 251 | case '/': |
| 252 | if (*p == '/') { |
| 253 | // spec 1.1 '//' xpath-insired pattern, find first |
| 254 | // matching subpath |
| 255 | while (*p && *str) { |
| 256 | if (lo_pattern_match(str, p)) |
| 257 | return true; |
| 258 | else { |
| 259 | while (*++str && *str != '/') |
| 260 | ; |
| 261 | if (!*str) |
| 262 | return false; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | default: |
| 268 | if (c != *str) |
| 269 | return false; |
| 270 | break; |
| 271 | |
| 272 | } |
| 273 | str++; |
| 274 | } |
| 275 | |
| 276 | return !*str; |
| 277 | } |
| 278 | |
| 279 | int lo_string_contains_pattern(const char *str) |
| 280 | { |
| 281 | if (!str) return 0; |
| 282 | return strpbrk(str, " #*,?[]{}") != NULL; |
| 283 | } |