PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 line
6.9KB

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