aboutsummaryrefslogtreecommitdiff
path: root/src/regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex.c')
-rw-r--r--src/regex.c116
1 files changed, 68 insertions, 48 deletions
diff --git a/src/regex.c b/src/regex.c
index 83f852e..163b86f 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1,4 +1,4 @@
-/*
+/*
* regex: regular expression pattern matcher
*
* (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
@@ -7,12 +7,12 @@
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@@ -36,12 +36,11 @@
static t_class *regex_class;
-typedef struct _regex
-{
+typedef struct _regex {
t_object x_obj;
#ifdef HAVE_REGEX_H
char *x_regexstring; /* the uncompiled regular expression */
- int x_regexstringlength;
+ int x_regexstringlength;
regex_t *x_regexp;
int x_matchnum;
@@ -62,19 +61,24 @@ static char*regex_l2s(int *reslen, t_symbol*s, int argc, t_atom*argv)
int pos=0, i=0;
t_atom*ap;
int length=0;
- if(reslen)*reslen=length;
+ if(reslen) {
+ *reslen=length;
+ }
/* 1st get the length of the symbol */
- if(s)length+=strlen(s->s_name);
- else length-=1;
+ if(s) {
+ length+=strlen(s->s_name);
+ } else {
+ length-=1;
+ }
length+=argc;
i=argc;
ap=argv;
- while(i--){
+ while(i--) {
char buffer[MAXPDSTRING];
int len=0;
- if(A_SYMBOL==ap->a_type){
+ if(A_SYMBOL==ap->a_type) {
len=strlen(ap->a_w.w_symbol->s_name);
} else {
atom_string(ap, buffer, MAXPDSTRING);
@@ -84,7 +88,9 @@ static char*regex_l2s(int *reslen, t_symbol*s, int argc, t_atom*argv)
ap++;
}
- if(length<=0)return(0);
+ if(length<=0) {
+ return(0);
+ }
result = (char*)getbytes((length+1)*sizeof(char));
@@ -92,7 +98,7 @@ static char*regex_l2s(int *reslen, t_symbol*s, int argc, t_atom*argv)
char *buf = s->s_name;
strcpy(result+pos, buf);
pos+=strlen(buf);
- if(i){
+ if(i) {
strcpy(result+pos, " ");
pos += 1;
}
@@ -100,8 +106,8 @@ static char*regex_l2s(int *reslen, t_symbol*s, int argc, t_atom*argv)
ap=argv;
i=argc;
- while(i--){
- if(A_SYMBOL==ap->a_type){
+ while(i--) {
+ if(A_SYMBOL==ap->a_type) {
strcpy(result+pos, ap->a_w.w_symbol->s_name);
pos+= strlen(ap->a_w.w_symbol->s_name);
} else {
@@ -111,14 +117,16 @@ static char*regex_l2s(int *reslen, t_symbol*s, int argc, t_atom*argv)
pos += strlen(buffer);
}
ap++;
- if(i){
+ if(i) {
strcpy(result+pos, " ");
pos++;
}
}
result[length]=0;
- if(reslen)*reslen=length;
+ if(reslen) {
+ *reslen=length;
+ }
return result;
}
@@ -127,34 +135,38 @@ static void regex_compile(t_regex *x)
int flags = x->x_flags;
flags |= REG_EXTENDED;
- if(0==x->x_regexstring || 0==x->x_regexstringlength){
+ if(0==x->x_regexstring || 0==x->x_regexstringlength) {
pd_error(x, "[regex]: no regular expression given");
return;
}
- if(x->x_regexp){
- regfree(x->x_regexp);
- freebytes(x->x_regexp, sizeof(t_regex));
- x->x_regexp=0;
+ if(x->x_regexp) {
+ regfree(x->x_regexp);
+ freebytes(x->x_regexp, sizeof(t_regex));
+ x->x_regexp=0;
}
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));
+ 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){
+static void regex_case(t_regex *x, t_float f)
+{
#if HAVE_REGEX_H
- if(f>0.f)
+ if(f>0.f) {
x->x_flags |= REG_ICASE;
- else
+ } else {
x->x_flags ^= REG_ICASE;
+ }
regex_compile(x);
#endif
@@ -169,7 +181,7 @@ static void regex_regex(t_regex *x, t_symbol*s, int argc, t_atom*argv)
result=regex_l2s(&length, 0, argc, argv);
- if(0==result || 0==length){
+ if(0==result || 0==length) {
pd_error(x, "[regex]: no regular expression given");
return;
}
@@ -179,7 +191,7 @@ static void regex_regex(t_regex *x, t_symbol*s, int argc, t_atom*argv)
x->x_regexstring=0;
x->x_regexstringlength=0;
}
-
+
x->x_regexstring=result;
x->x_regexstringlength=length;
@@ -200,25 +212,25 @@ static void regex_symbol(t_regex *x, t_symbol *s, int argc, t_atom*argv)
int err=0;
- if(!x->x_regexp){
+ if(!x->x_regexp) {
pd_error(x, "[regex]: no regular expression!");
goto cleanup;
}
teststring=regex_l2s(&length, 0, argc, argv);
- if(!teststring||!length){
+ if(!teststring||!length) {
pd_error(x, "[regex]: cannot evaluate string");
goto cleanup;
}
/* do the actual comparing against the regex */
err=regexec(x->x_regexp, teststring, num_matches, match, 0);
- if(teststring){
+ if(teststring) {
freebytes(teststring, length);
teststring=0;
}
if(err) { /* NO match */
- if(match){
+ if(match) {
freebytes(match, sizeof(regmatch_t)*num_matches);
match=0;
}
@@ -229,10 +241,11 @@ static void regex_symbol(t_regex *x, t_symbol *s, int argc, t_atom*argv)
int i=0;
t_atom*ap2=ap;
- for(i=0; i<num_matches; i++){
- if(match[i].rm_so!=-1){
+ 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)){
+ 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);
@@ -243,21 +256,25 @@ static void regex_symbol(t_regex *x, t_symbol *s, int argc, t_atom*argv)
}
}
}
- if(match){
+ 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++){
+ 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);
- if(match)freebytes(match, sizeof(regmatch_t)*num_matches);
+cleanup:
+ if(teststring) {
+ freebytes(teststring, length);
+ }
+ if(match) {
+ freebytes(match, sizeof(regmatch_t)*num_matches);
+ }
- if(ap){
+ if(ap) {
freebytes(ap, sizeof(t_atom)*(1+2*num_matches));
}
#endif
@@ -282,8 +299,9 @@ static void *regex_new(t_symbol *s, int argc, t_atom*argv)
x->x_regexp=0;
x->x_matchnum=NUM_REGMATCHES;
- if(argc)regex_regex(x, gensym(""), argc, argv);
- else{
+ if(argc) {
+ regex_regex(x, gensym(""), argc, argv);
+ } else {
t_atom a;
SETSYMBOL(&a, gensym(".*"));
regex_regex(x, 0, 1, &a);
@@ -298,7 +316,7 @@ static void *regex_new(t_symbol *s, int argc, t_atom*argv)
static void regex_free(t_regex *x)
{
#ifdef HAVE_REGEX_H
- if(x->x_regexstring){
+ if(x->x_regexstring) {
freebytes(x->x_regexstring, x->x_regexstringlength);
x->x_regexstring=0;
x->x_regexstringlength=0;
@@ -319,13 +337,15 @@ static void regex_help(t_regex*x)
void regex_setup(void)
{
- regex_class = class_new(gensym("regex"), (t_newmethod)regex_new,
- (t_method)regex_free, sizeof(t_regex), 0, A_GIMME, 0);
+ regex_class = class_new(gensym("regex"), (t_newmethod)regex_new,
+ (t_method)regex_free, sizeof(t_regex), 0, A_GIMME, 0);
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_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_case, gensym("case"), A_FLOAT,
+ 0);
class_addmethod(regex_class, (t_method)regex_help, gensym("help"), A_NULL);
zexy_register("regex");