From 17bbb7ac85846bcc2c29ec382013711c13dc6fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Tue, 24 Jul 2007 13:30:29 +0000 Subject: added "delete" functionality svn path=/trunk/externals/zexy/; revision=8230 --- reference/matchbox-help.pd | 97 ++++++++++++++++++++++++++++---------- src/matchbox.c | 115 ++++++++++++++++++++++++++++++++------------- 2 files changed, 156 insertions(+), 56 deletions(-) diff --git a/reference/matchbox-help.pd b/reference/matchbox-help.pd index 5812348..1a1556d 100644 --- a/reference/matchbox-help.pd +++ b/reference/matchbox-help.pd @@ -1,4 +1,4 @@ -#N canvas 80 59 728 623 10; +#N canvas 80 59 640 602 10; #X text 68 47 you can store a number of lists by sending them to its right inlet.; #X text 50 23 [matchbox] - retrieve matching lists; @@ -13,12 +13,12 @@ at object creation.; #X text 90 204 '==': only matches if lists are exactly equal; #X text 90 216 'OSC': list-atoms are compared using OSC-pattern matching ; -#X obj 205 384 matchbox; -#X floatatom 248 405 5 0 0 1 num_results - -; -#X obj 205 439 print results; +#X obj 205 424 matchbox; +#X floatatom 248 445 5 0 0 1 num_results - -; +#X obj 205 479 print results; #X msg 248 351 list this is my string \, list this is your string \, list this was my string; -#X text 278 385 default is exact matching; +#X text 268 426 default is exact matching; #X msg 205 256 list this is my string; #X msg 218 275 list this is no string; #X msg 233 295 list this is * string; @@ -28,15 +28,6 @@ list this was my string; #X msg 111 450 dump; #X text 37 395 delete all stored lists; #X text 35 438 output all stored lists; -#X floatatom 240 595 5 0 0 1 num_results - -; -#X msg 240 551 list this is my string \, list this is your string \, -list this was my string; -#X msg 173 480 list this is my string; -#X obj 173 578 matchbox OSC; -#X obj 173 613 print OSCresults; -#X msg 201 513 list this *s * string; -#X msg 186 496 list this is * string; -#X obj 240 535 loadbang; #X obj 248 335 loadbang; #X text 301 336 add lists to the pool; #X text 52 294 change matching mode; @@ -46,6 +37,71 @@ pattern-matching; #X msg 247 313 list this .+ my string; #X text 394 295 an OSC pattern; #X text 394 314 a regular expression; +#N canvas 0 0 556 395 default 0; +#X floatatom 100 295 5 0 0 1 num_results - -; +#X msg 100 161 list this is my string \, list this is your string \, +list this was my string; +#X msg 33 90 list this is my string; +#X obj 33 238 matchbox OSC; +#X obj 33 313 print OSCresults; +#X msg 61 123 list this *s * string; +#X msg 46 106 list this is * string; +#X obj 100 145 loadbang; +#X text 57 38 you can give the matching mode as 1st argument to matchbox] +; +#X text 118 239 <-- this object does OSC-pattern matching; +#X connect 1 0 3 1; +#X connect 2 0 3 0; +#X connect 3 0 4 0; +#X connect 3 1 0 0; +#X connect 5 0 3 0; +#X connect 6 0 3 0; +#X connect 7 0 1 0; +#X restore 206 527 pd default mode; +#N canvas 220 74 737 499 deleting 0; +#X obj 205 284 matchbox; +#X obj 205 369 print results; +#X msg 248 241 list this is my string \, list this is your string \, +list this was my string; +#X text 268 286 default is exact matching; +#X msg 119 172 mode ==; +#X msg 120 190 mode OSC; +#X msg 110 267 clear; +#X msg 351 342 dump; +#X text 37 255 delete all stored lists; +#X obj 248 225 loadbang; +#X text 301 226 add lists to the pool; +#X text 52 154 change matching mode; +#X msg 120 210 mode regex; +#X text 400 156 an OSC pattern; +#X text 398 174 a regular expression; +#X msg 205 116 delete this is my string; +#X msg 217 135 delete this is no string; +#X msg 234 155 delete this is * string; +#X msg 247 173 delete this .+ my string; +#X text 80 51 you can either "clear" all stored lists \, or "delete" +them based pattern matching; +#X text 327 316 after you have deleted something \, click "dump" to +see the currently stored lists.; +#X text 70 426 when "delete"ing \, [matchbox] will output the deleted +list(s) with a "deleted" prepended; +#X text 73 456 you can use [route delete] to distinguish between 'deleted' +and 'found' results; +#X connect 0 0 1 0; +#X connect 2 0 0 1; +#X connect 4 0 0 0; +#X connect 5 0 0 0; +#X connect 6 0 0 0; +#X connect 7 0 0 0; +#X connect 9 0 2 0; +#X connect 12 0 0 0; +#X connect 15 0 0 0; +#X connect 16 0 0 0; +#X connect 17 0 0 0; +#X connect 18 0 0 0; +#X restore 206 547 pd deleting; +#X text 114 539 more info; +#X text 475 15 part of zexy (2.1); #X connect 7 0 9 0; #X connect 7 1 8 0; #X connect 10 0 7 1; @@ -56,13 +112,6 @@ pattern-matching; #X connect 16 0 7 0; #X connect 17 0 7 0; #X connect 18 0 7 0; -#X connect 22 0 24 1; -#X connect 23 0 24 0; -#X connect 24 0 25 0; -#X connect 24 1 21 0; -#X connect 26 0 24 0; -#X connect 27 0 24 0; -#X connect 28 0 22 0; -#X connect 29 0 10 0; -#X connect 33 0 7 0; -#X connect 34 0 7 0; +#X connect 21 0 10 0; +#X connect 25 0 7 0; +#X connect 26 0 7 0; diff --git a/src/matchbox.c b/src/matchbox.c index b17dfdd..253d6c6 100644 --- a/src/matchbox.c +++ b/src/matchbox.c @@ -92,21 +92,27 @@ static t_listlist* addlistlist(t_listlist*list, int argc, t_atom*argv) { return list; } +/* delete the _next_ element from the list */ +static t_listlist* deletelistnext(t_listlist*list) { + if(!list || !list->next)return list; /* nothing to delete */ + t_listlist*ll=list->next; + list->next=ll->next; + if(ll->argv)freebytes(ll->argv, ll->argc*sizeof(t_atom)); + ll->argv=0; + ll->argc=0; + ll->next=0; + freebytes(ll, sizeof(t_listlist)); + return list; +} + +/* delete the entire list of lists */ static void clearlistlist(t_listlist*list) { - while(list){ - t_listlist*ll=list; - list=list->next; - - if(ll->argv)freebytes(ll->argv, ll->argc*sizeof(t_atom)); - ll->argv=0; - ll->argc=0; - ll->next=0; - freebytes(ll, sizeof(t_listlist)); + if(!list)return; /* nothing to delete */ + while(list->next){ + list=deletelistnext(list); } } - - /* -------------- here comes the matching algorithms ----------- */ @@ -387,7 +393,7 @@ static int listmatch_regex(int p_argc, regex_t**pattern, int t_argc, t_atom*test return TRUE; } -static t_listlist*matchlistlist_regex(unsigned int*numresults, t_listlist*searchlist, int p_argc, t_atom*p_argv, int flags) { +static t_listlist*matchlistlist_regex(unsigned int*numresults, t_listlist*searchlist, int p_argc, t_atom*p_argv, int flags, int delete_results) { regex_t**regexpressions=0; t_listlist*matchinglist=0, *sl; int i=0; @@ -421,10 +427,25 @@ static t_listlist*matchlistlist_regex(unsigned int*numresults, t_listlist*search } /* 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++; + if(FALSE==delete_results) { + 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++; + } + } + } else if (TRUE==delete_results) { + /* yummy: delete matching lists! */ + t_listlist*lastgood=searchlist; + 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++; + + sl=deletelistnext(lastgood); + } else { + lastgood=sl; + } } } @@ -474,24 +495,39 @@ static int matchlist(int argc_pattern, t_atom*argv_pattern, return TRUE; } -static t_listlist*matchlistlist(unsigned int*numresults, t_listlist*searchlist, int p_argc, t_atom*p_argv, int mode) { +static t_listlist*matchlistlist(unsigned int*numresults, t_listlist*searchlist, int p_argc, t_atom*p_argv, int mode, int delete_results) { unsigned int num=0; 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); + matchinglist=matchlistlist_regex(&num, searchlist, p_argc, p_argv, 0, delete_results); } 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)) { - matchinglist=addlistlist(matchinglist, sl->argc, sl->argv); - num++; + if(FALSE==delete_results) { + for(sl=searchlist->next; 0!=sl; sl=sl->next) { + if(matchlist(p_argc, p_argv, sl->argc, sl->argv, mode)) { + matchinglist=addlistlist(matchinglist, sl->argc, sl->argv); + num++; + } + } + } else if (TRUE==delete_results) { + /* yummy: delete matching lists! */ + t_listlist*lastgood=searchlist; + for(sl=searchlist->next; 0!=sl; sl=sl->next) { + if(matchlist(p_argc, p_argv, sl->argc, sl->argv, mode)) { + matchinglist=addlistlist(matchinglist, sl->argc, sl->argv); + num++; + + sl=deletelistnext(lastgood); + } else { + lastgood=sl; + } } } - + if(numresults!=0) *numresults=num; return matchinglist; @@ -501,20 +537,18 @@ static t_listlist*matchlistlist(unsigned int*numresults, t_listlist*searchlist, static void matchbox_list(t_matchbox*x, t_symbol*s, int argc, t_atom*argv) { int results=0; int mode=x->x_mode; - t_listlist*resultlist=matchlistlist(&results, x->x_lists, argc, argv, mode); + t_listlist*resultlist=matchlistlist(&results, x->x_lists, argc, argv, mode, FALSE); t_listlist*dummylist; outlet_float(x->x_outNumResults, results); for(dummylist=resultlist; 0!=dummylist; dummylist=dummylist->next) outlet_list(x->x_outResult, &s_list, dummylist->argc, dummylist->argv); - - } static void matchbox_add(t_matchbox*x, t_symbol*s, int argc, t_atom*argv) { // 1st match, whether we already have this entry - if(matchlistlist(0, x->x_lists, argc, argv, MATCHBOX_EXACT)) { + if(matchlistlist(0, x->x_lists, argc, argv, MATCHBOX_EXACT, FALSE)) { // already there, skip the rest z_verbose(1, "this list is already in the buffer!, skipping..."); return; @@ -525,19 +559,32 @@ static void matchbox_add(t_matchbox*x, t_symbol*s, int argc, t_atom*argv) { x->x_numlists++; } +static void matchbox_delete(t_matchbox*x, t_symbol*s, int argc, t_atom*argv) { + int results=0; + int mode=x->x_mode; + t_listlist*resultlist=matchlistlist(&results, x->x_lists, argc, argv, mode, TRUE); + t_listlist*dummylist; + t_symbol*delsym=gensym("deleted"); + x->x_numlists-=results; + + outlet_float(x->x_outNumResults, results); + + for(dummylist=resultlist; 0!=dummylist; dummylist=dummylist->next) + outlet_anything(x->x_outResult, delsym, dummylist->argc, dummylist->argv); +} static void matchbox_dump(t_matchbox*x) { t_listlist*lp=0; - if(0==x->x_lists){ + if(0==x->x_lists || 0==x->x_lists->next){ outlet_float(x->x_outNumResults, 0); return; } outlet_float(x->x_outNumResults, x->x_numlists); - for(lp=x->x_lists; 0!=lp; lp=lp->next) + for(lp=x->x_lists->next; 0!=lp; lp=lp->next) { outlet_list(x->x_outResult, &s_list, lp->argc, lp->argv); } @@ -546,8 +593,6 @@ static void matchbox_dump(t_matchbox*x) { static void matchbox_clear(t_matchbox*x) { clearlistlist(x->x_lists); - - x->x_lists=0; x->x_numlists=0; } @@ -583,7 +628,10 @@ static void *matchbox_new(t_symbol *s, int argc, t_atom*argv) x->x_outNumResults=outlet_new(&x->x_obj, gensym("float")); - x->x_lists=0; + x->x_lists=(t_listlist*)getbytes(sizeof(t_listlist)); + x->x_lists->next=0; + x->x_lists->argc=0; + x->x_lists->argv=0; x->x_numlists=0; x->x_mode = MATCHBOX_EXACT; @@ -599,6 +647,8 @@ static void *matchbox_new(t_symbol *s, int argc, t_atom*argv) static void matchbox_free(t_matchbox *x) { matchbox_clear(x); + freebytes(x->x_lists, sizeof(t_listlist)); + x->x_lists=0; } static void matchbox_help(t_matchbox*x) @@ -619,6 +669,7 @@ void matchbox_setup(void) class_addlist (matchbox_class, matchbox_list); class_addmethod(matchbox_class, (t_method)matchbox_add, gensym("add"), A_GIMME, 0); + class_addmethod(matchbox_class, (t_method)matchbox_delete, gensym("delete"), A_GIMME, 0); class_addmethod(matchbox_class, (t_method)matchbox_clear, gensym("clear"), A_NULL, 0); class_addmethod(matchbox_class, (t_method)matchbox_dump, gensym("dump"), A_NULL); -- cgit v1.2.1