[PD-cvs] externals/zexy/src regex.c,1.6,1.7

IOhannes m zmölnig zmoelnig at users.sourceforge.net
Tue Jun 20 15:30:27 CEST 2006


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

Modified Files:
	regex.c 
Log Message:
made additional outlets: outlet1=1/0 (whether we have at least 1 match or not); outlet2=info about the matches; outlet3=number of matches


Index: regex.c
===================================================================
RCS file: /cvsroot/pure-data/externals/zexy/src/regex.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** regex.c	19 May 2006 14:04:32 -0000	1.6
--- regex.c	20 Jun 2006 13:30:25 -0000	1.7
***************
*** 39,45 ****
--- 39,55 ----
    t_object x_obj;
  #ifdef HAVE_REGEX_H
+   char    *x_regexstring; /* the uncompiled regular expression */
+   int      x_regexstringlength; 
+ 
    regex_t *x_regexp;
    int x_matchnum;
+ 
+   int x_flags; /* flags for the regex-compiler; REG_EXTENDED is always enabled */
  #endif
+ 
+   t_outlet*x_outResult;
+   t_outlet*x_outDetails;
+   t_outlet*x_outNumDetails;
+ 
  } t_regex;
  
***************
*** 110,127 ****
    return result;
  }
- #endif
  
! static void regex_regex(t_regex *x, t_symbol*s, int argc, t_atom*argv)
  {
!   ZEXY_USEVAR(s);
! #ifdef HAVE_REGEX_H
!   char*result=0;
!   int length=0;
!   int flags =  0;
    flags |= REG_EXTENDED;
  
!   result=regex_l2s(&length, 0, argc, argv);
! 
!   if(0==result || 0==length){
      pd_error(x, "[regex]: no regular expression given");
      return;
--- 120,130 ----
    return result;
  }
  
! static void regex_compile(t_regex *x)
  {
!   int flags =  x->x_flags;
    flags |= REG_EXTENDED;
  
!   if(0==x->x_regexstring || 0==x->x_regexstringlength){
      pd_error(x, "[regex]: no regular expression given");
      return;
***************
*** 136,151 ****
    x->x_regexp=(regex_t*)getbytes(sizeof(t_regex));
  
!   if(regcomp(x->x_regexp, result, flags)) {
!     pd_error(x, "[regex]: invalid regular expression: %s", result);
      if(x->x_regexp)freebytes(x->x_regexp, sizeof(t_regex));
      x->x_regexp=0;
    }
  
!   if(result)freebytes(result, length);
  #endif
  }
  static void regex_symbol(t_regex *x, t_symbol *s, int argc, t_atom*argv)
  {
-   ZEXY_USEVAR(s);
  #ifdef HAVE_REGEX_H
    char*teststring=0;
--- 139,192 ----
    x->x_regexp=(regex_t*)getbytes(sizeof(t_regex));
  
!   if(regcomp(x->x_regexp, x->x_regexstring, flags)) {
!     pd_error(x, "[regex]: invalid regular expression: %s", x->x_regexstring);
      if(x->x_regexp)freebytes(x->x_regexp, sizeof(t_regex));
      x->x_regexp=0;
    }
+ }
+ #endif
  
! 
! static void regex_case(t_regex *x, t_float f){
! #if HAVE_REGEX_H
!   if(f>0.f)
!     x->x_flags |= REG_ICASE;
!   else
!     x->x_flags ^= REG_ICASE;
! 
!   regex_compile(x);
! #endif
! }
! 
! 
! static void regex_regex(t_regex *x, t_symbol*s, int argc, t_atom*argv)
! {
! #ifdef HAVE_REGEX_H
!   char*result=0;
!   int length=0;
! 
!   result=regex_l2s(&length, 0, argc, argv);
! 
!   if(0==result || 0==length){
!     pd_error(x, "[regex]: no regular expression given");
!     return;
!   }
! 
!   if(x->x_regexstring) {
!     freebytes(x->x_regexstring, x->x_regexstringlength);
!     x->x_regexstring=0;
!     x->x_regexstringlength=0;
!   }
!   
!   x->x_regexstring=result;
!   x->x_regexstringlength=length;
! 
!   regex_compile(x);
  #endif
  }
+ 
+ /* compare the given list as string with the precompiled regex */
  static void regex_symbol(t_regex *x, t_symbol *s, int argc, t_atom*argv)
  {
  #ifdef HAVE_REGEX_H
    char*teststring=0;
***************
*** 154,158 ****
    int num_matches=x->x_matchnum;
    regmatch_t*match=(regmatch_t*)getbytes(sizeof(regmatch_t)*num_matches);
!   t_atom*ap=(t_atom*)getbytes(sizeof(t_atom)*(1+2*num_matches));
    int ap_length=0;
  
--- 195,199 ----
    int num_matches=x->x_matchnum;
    regmatch_t*match=(regmatch_t*)getbytes(sizeof(regmatch_t)*num_matches);
!   t_atom*ap=(t_atom*)getbytes(sizeof(t_atom)*(3*num_matches));
    int ap_length=0;
  
***************
*** 169,196 ****
    }
  
    err=regexec(x->x_regexp, teststring, num_matches, match, 0);
  
!   if(err) {
!     ap_length=1;
!     SETFLOAT(ap, 0.f);
!   } else {
      int num_results=0;
      int i=0;
!     t_atom*ap2=ap+1;
      for(i=0; i<num_matches; i++){
        if(match[i].rm_so!=-1){
          if(i>0 && (match[i].rm_so==match[i-1].rm_so) && (match[i].rm_eo==match[i-1].rm_eo)){
          } else {
!           SETFLOAT(ap2+0, match[i].rm_so);
!           SETFLOAT(ap2+1, match[i].rm_eo);
!           ap2+=2;
            num_results++;
          }
        }
      }
!     ap_length=1+2*num_results;
!     SETFLOAT(ap, num_results);
    }
- 
   cleanup:
    if(teststring)freebytes(teststring, length);
--- 210,256 ----
    }
  
+   /* do the actual comparing against the regex */
    err=regexec(x->x_regexp, teststring, num_matches, match, 0);
+   if(teststring){
+     freebytes(teststring, length);
+     teststring=0;
+   }
  
!   if(err) { /* NO match */
!     if(match){
!       freebytes(match, sizeof(regmatch_t)*num_matches);
!       match=0;
!     }
! 
!     outlet_float(x->x_outResult, 0.f);
!   } else { /* match! */
      int num_results=0;
      int i=0;
!     t_atom*ap2=ap;
! 
      for(i=0; i<num_matches; i++){
        if(match[i].rm_so!=-1){
+         /* output the matches */
          if(i>0 && (match[i].rm_so==match[i-1].rm_so) && (match[i].rm_eo==match[i-1].rm_eo)){
+           // duplicate matches
          } else {
!           SETFLOAT(ap2+0, (t_float)i);
!           SETFLOAT(ap2+1, (t_float)match[i].rm_so);
!           SETFLOAT(ap2+2, (t_float)match[i].rm_eo);
!           ap2+=3;
            num_results++;
          }
        }
      }
!     if(match){
!       freebytes(match, sizeof(regmatch_t)*num_matches);
!       match=0;
!     }
!     outlet_float(x->x_outNumDetails, (t_float)num_results);
!     for(i=0; i<num_results; i++){
!       outlet_list(x->x_outDetails, gensym("list"), 3, ap+(i*3));
!     }
!     outlet_float(x->x_outResult, 1.f);
    }
   cleanup:
    if(teststring)freebytes(teststring, length);
***************
*** 198,202 ****
  
    if(ap){
-     outlet_list(x->x_obj.ob_outlet, gensym("list"), ap_length, ap);
      freebytes(ap, sizeof(t_atom)*(1+2*num_matches));
    }
--- 258,261 ----
***************
*** 207,216 ****
  {
    t_regex *x = (t_regex *)pd_new(regex_class);
-   ZEXY_USEVAR(s);
  
-   outlet_new(&x->x_obj, 0);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym("regex"));
  
  #ifdef HAVE_REGEX_H
    x->x_regexp=0;
    x->x_matchnum=NUM_REGMATCHES;
--- 266,283 ----
  {
    t_regex *x = (t_regex *)pd_new(regex_class);
  
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym("regex"));
  
+   x->x_outResult=outlet_new(&x->x_obj, 0);
+   x->x_outDetails=outlet_new(&x->x_obj, gensym("list"));
+   x->x_outNumDetails=outlet_new(&x->x_obj, gensym("float"));
+ 
+ 
  #ifdef HAVE_REGEX_H
+   x->x_flags=0;
+ 
+   x->x_regexstring=0;
+   x->x_regexstringlength=0;
+ 
    x->x_regexp=0;
    x->x_matchnum=NUM_REGMATCHES;
***************
*** 226,229 ****
--- 293,302 ----
  {
  #ifdef HAVE_REGEX_H
+   if(x->x_regexstring){
+     freebytes(x->x_regexstring, x->x_regexstringlength);
+     x->x_regexstring=0;
+     x->x_regexstringlength=0;
+   }
+ 
    if(x->x_regexp) {
      regfree(x->x_regexp);
***************
*** 245,249 ****
  
    class_addlist  (regex_class, regex_symbol);
!   class_addmethod  (regex_class, (t_method)regex_regex, gensym("regex"), A_GIMME, 0);
  
    class_addmethod(regex_class, (t_method)regex_help, gensym("help"), A_NULL);
--- 318,324 ----
  
    class_addlist  (regex_class, regex_symbol);
!   class_addmethod(regex_class, (t_method)regex_regex, gensym("regex"), A_GIMME, 0);
! 
!   class_addmethod(regex_class, (t_method)regex_case, gensym("case"), A_FLOAT, 0);
  
    class_addmethod(regex_class, (t_method)regex_help, gensym("help"), A_NULL);





More information about the Pd-cvs mailing list