[PD-cvs] externals/zexy/src matchbox.c,1.1,1.2

IOhannes m zmölnig zmoelnig at users.sourceforge.net
Thu Mar 8 14:06:16 CET 2007


Update of /cvsroot/pure-data/externals/zexy/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12048

Modified Files:
	matchbox.c 
Log Message:
added regex support


Index: matchbox.c
===================================================================
RCS file: /cvsroot/pure-data/externals/zexy/src/matchbox.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** matchbox.c	7 Mar 2007 15:16:35 -0000	1.1
--- matchbox.c	8 Mar 2007 13:06:14 -0000	1.2
***************
*** 16,50 ****
  
  
  
! /*
! OSC pattern matching code was written by Matt Wright, 
! The Center for New Music and Audio Technologies,
! University of California, Berkeley.  Copyright (c) 1998,99,2000,01,02,03,04
! 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.
! 
! The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl
! */
  
! #include "zexy.h"
  
  #include <string.h>
  
  /*
   * matchbox    : see whether a regular expression matches the given symbol
--- 16,35 ----
  
  
+ #include "zexy.h"
  
! #define MATCHBOX_EXACT 0
! #define MATCHBOX_OSC 1
  
! #ifdef HAVE_REGEX_H
! # include <sys/types.h>
! # include <regex.h>
! #define MATCHBOX_REGEX 2
! #endif
  
  #include <string.h>
  
+ #define FALSE 0
+ #define TRUE  1
+ 
  /*
   * matchbox    : see whether a regular expression matches the given symbol
***************
*** 57,63 ****
  static t_class *matchbox_class;
  
- #define MATCHBOX_EXACT 0
- #define MATCHBOX_OSC 1
- 
  
  typedef struct _listlist {
--- 42,45 ----
***************
*** 132,147 ****
      }
    } else {
!     post("types don't match!");
!     return 0;
    }
  
!   return 0;
  }
  
  #ifdef MATCHBOX_OSC /* OSC */
  
  #define OSCWarning post
- #define FALSE 0
- #define TRUE  1
  static int OSC_MatchBrackets (const char *pattern, const char *test, const char*theWholePattern);
  static int OSC_MatchList (const char *pattern, const char *test, const char*theWholePattern);
--- 114,153 ----
      }
    } else {
!     //post("types don't match!");
!     return FALSE;
    }
  
!   return TRUE;
  }
  
  #ifdef MATCHBOX_OSC /* OSC */
  
+ /*
+ OSC pattern matching code was written by Matt Wright, 
+ The Center for New Music and Audio Technologies,
+ University of California, Berkeley.  Copyright (c) 1998,99,2000,01,02,03,04
+ 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.
+ 
+ The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl
+ */
+ 
  #define OSCWarning post
  static int OSC_MatchBrackets (const char *pattern, const char *test, const char*theWholePattern);
  static int OSC_MatchList (const char *pattern, const char *test, const char*theWholePattern);
***************
*** 170,174 ****
    case ']'    :
    case '}'    :
!     OSCWarning("Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
      return FALSE;
    case '['    :
--- 176,180 ----
    case ']'    :
    case '}'    :
!     z_verbose(1, "Spurious %c in pattern \".../%s/...\"",pattern[0], theWholePattern);
      return FALSE;
    case '['    :
***************
*** 193,197 ****
  }
  
- 
  /* we know that pattern[0] == '[' and test[0] != 0 */
  
--- 199,202 ----
***************
*** 202,206 ****
  
    if (pattern[1] == 0) {
!     OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
      return FALSE;
    }
--- 207,211 ----
  
    if (pattern[1] == 0) {
!     z_verbose(1, "Unterminated [ in pattern \".../%s/...\"", theWholePattern);
      return FALSE;
    }
***************
*** 213,217 ****
    while (*p != ']') {
      if (*p == 0) {
!       OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
--- 218,222 ----
    while (*p != ']') {
      if (*p == 0) {
!       z_verbose(1, "Unterminated [ in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
***************
*** 238,242 ****
    while (*p != ']') {
      if (*p == 0) {
!       OSCWarning("Unterminated [ in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
--- 243,247 ----
    while (*p != ']') {
      if (*p == 0) {
!       z_verbose(1, "Unterminated [ in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
***************
*** 253,257 ****
    for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
      if (*restOfPattern == 0) {
!       OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
--- 258,262 ----
    for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
      if (*restOfPattern == 0) {
!       z_verbose(1, "Unterminated { in pattern \".../%s/...\"", theWholePattern);
        return FALSE;
      }
***************
*** 333,336 ****
--- 338,442 ----
  
  
+ #ifdef MATCHBOX_REGEX
+ static int atommatch_regex(regex_t*pattern,  t_atom*test) {
+   int result=FALSE;
+   char*s_test=0;
+   int test_size=0;
+ 
+   if(0==pattern)return FALSE;
+   if(0==test)   return FALSE;
+ 
+   if(test->a_type==A_SYMBOL) {
+     s_test=test->a_w.w_symbol->s_name;
+   } else {
+     test_size=sizeof(char)*MAXPDSTRING;
+     s_test=(char*)getbytes(test_size);
+     atom_string(test, s_test, test_size);
+   }
+ 
+   result=!(regexec(pattern, s_test, 0, 0, 0));
+ 
+   if(test_size>0) {
+     freebytes(s_test, test_size);
+     s_test=0; test_size=0;
+   } 
+   
+   return result;
+ }
+ 
+ static int listmatch_regex(int p_argc, regex_t**pattern, int t_argc, t_atom*test) {
+   /* match the patterns to the test */
+   int argc=p_argc;
+   if(p_argc!=t_argc)
+     return FALSE;
+ 
+   while(argc--) {
+     if(FALSE==atommatch_regex(*pattern++, test++)) {
+       return FALSE;
+     }
+   }
+ 
+   return TRUE;
+ }
+ 
+ static t_listlist*matchlistlist_regex(unsigned int*numresults, t_listlist*searchlist, int p_argc, t_atom*p_argv, int flags) {
+   regex_t**regexpressions=0;
+   t_listlist*matchinglist=0, *sl;
+   int i=0;
+   flags|=REG_EXTENDED;
+ 
+   int num=0;
+ 
+   /* 1st compile the patterns */
+   regexpressions=(regex_t**)getbytes(sizeof(regex_t*)*p_argc);
+   for(i=0; i<p_argc; i++) {
+     char*s_pattern=0;
+     int pattern_size=0;
+     t_atom*pattern=p_argv+i;
+     if(pattern->a_type==A_SYMBOL) {
+       s_pattern=pattern->a_w.w_symbol->s_name;
+     } else {
+       pattern_size=sizeof(char)*MAXPDSTRING;
+       s_pattern=(char*)getbytes(pattern_size);
+       atom_string(pattern, s_pattern, pattern_size);
+     }
+     regexpressions[i]=(regex_t*)getbytes(sizeof(regex_t));
+     if(regcomp(regexpressions[i], s_pattern, flags)) {
+       z_verbose(1, "[matchbox]: invalid regular expression: %s", s_pattern);
+       if(regexpressions[i])freebytes(regexpressions[i], sizeof(regex_t));
+        regexpressions[i]=0;
+     }
+     if(pattern_size>0) {
+       freebytes(s_pattern, pattern_size);
+       s_pattern=0; pattern_size=0;
+     }
+   }
+  
+   /* match the patterns to the tests */
+   for(sl=searchlist; 0!=sl; sl=sl->next) {
+     if(TRUE==listmatch_regex(p_argc, regexpressions, sl->argc, sl->argv)) {
+       matchinglist=addlistlist(matchinglist, sl->argc, sl->argv);
+       num++;
+     }
+   }
+ 
+   /* clear the patterns */
+   for(i=0; i<p_argc; i++) {
+     if(regexpressions[i]){
+       regfree(regexpressions[i]);
+       freebytes(regexpressions[i], sizeof(regex_t));
+     }
+   }
+   freebytes(regexpressions, sizeof(regex_t*)*p_argc);
+ 
+   /* return the result */  
+   if(numresults!=0)
+     *numresults=num;
+   return matchinglist;
+ }
+ #endif /* MATCHBOX_REGEX */
+ 
+ 
+ 
  
  
***************
*** 351,362 ****
  
    if(argc!=argc_pattern)
!     return 0;
  
    for(i=0; i<argc; i++) {
      if(0==matchbox_atommatch(argv_pattern+i, argv+i, mode))
!       return 0;
    }
! 
!   return 1;
  }
  
--- 457,468 ----
  
    if(argc!=argc_pattern)
!     return FALSE;
  
    for(i=0; i<argc; i++) {
      if(0==matchbox_atommatch(argv_pattern+i, argv+i, mode))
!       return FALSE;
    }
!   
!   return TRUE;
  }
  
***************
*** 365,368 ****
--- 471,481 ----
    t_listlist*matchinglist=0, *sl;
  
+   /* extra handling of regex matching (because we want to compile only once */
+ #ifdef MATCHBOX_REGEX
+   if(MATCHBOX_REGEX==mode) {
+     matchinglist=matchlistlist_regex(&num, searchlist, p_argc, p_argv, 0);
+   } else 
+ #endif /* MATCHBOX_REGEX */
+   /* normal matching */
    for(sl=searchlist; 0!=sl; sl=sl->next) {
      if(matchlist(p_argc, p_argv, sl->argc, sl->argv, mode)) {
***************
*** 433,441 ****
  
  static void matchbox_mode(t_matchbox*x, t_symbol*s) {
!   if (gensym("OSC")==s)
!     x->x_mode=MATCHBOX_OSC;
!   else if(gensym("==")==s)
      x->x_mode=MATCHBOX_EXACT;
!   else {
      pd_error(x, "mode '%s' is unknown, switching to 'exact' mode", s->s_name);
      x->x_mode=MATCHBOX_EXACT;
--- 546,564 ----
  
  static void matchbox_mode(t_matchbox*x, t_symbol*s) {
!   if(gensym("==")==s)
      x->x_mode=MATCHBOX_EXACT;
!   else if (gensym("OSC")==s) {
! #ifdef MATCHBOX_OSC
!     x->x_mode=MATCHBOX_OSC;
! #else
!      pd_error(x, "[matchbox] has been compiled without 'OSC' support; ignoring your request");
! #endif /* MATCHBOX_OSC */
!   } else if(gensym("regex")==s) {
! #ifdef MATCHBOX_REGEX
!     x->x_mode=MATCHBOX_REGEX;
! #else
!     pd_error(x, "[matchbox] has been compiled without 'regex' support; ignoring your request");
! #endif /* MATCHBOX_REGEX */
!   } else {
      pd_error(x, "mode '%s' is unknown, switching to 'exact' mode", s->s_name);
      x->x_mode=MATCHBOX_EXACT;
***************
*** 443,448 ****
  }
  
- 
- 
  static void *matchbox_new(t_symbol *s, int argc, t_atom*argv)
  {
--- 566,569 ----
***************
*** 485,489 ****
  
  
- 
    matchbox_class = class_new(gensym("matchbox"), (t_newmethod)matchbox_new, 
  			 (t_method)matchbox_free, sizeof(t_matchbox), 0, A_GIMME, 0);
--- 606,609 ----





More information about the Pd-cvs mailing list