aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/list2symbol.pd30
-rw-r--r--src/list2symbol.c62
-rw-r--r--tests/l2s/argument.pd28
-rw-r--r--tests/l2s/delimiter.pd29
-rw-r--r--tests/runtests.txt2
5 files changed, 122 insertions, 29 deletions
diff --git a/examples/list2symbol.pd b/examples/list2symbol.pd
index 846a85a..c7f2fe3 100644
--- a/examples/list2symbol.pd
+++ b/examples/list2symbol.pd
@@ -1,13 +1,11 @@
-#N canvas 228 508 809 343 10;
+#N canvas 14 156 817 446 10;
#X obj 135 48 list2symbol;
-#X text 219 48 convert a list into a symbol;
+#X text 228 49 convert a list into a symbol;
#X obj 457 50 l2s;
#X symbolatom 75 286 0 0 0 0 - - -;
#X msg 75 97 list this was a list and is now a symbol;
#X msg 159 118 anythings work fine too;
-#X obj 507 268 l2s my bonnie is over the ocean;
#X obj 507 288 print;
-#X msg 507 247 bang;
#X symbolatom 147 223 10 0 0 0 - - -;
#X msg 147 158 symbol --;
#X text 230 157 by default \, list-elements are separated by spaces.
@@ -16,11 +14,19 @@ You can set the separator to any symbol;
#X msg 156 201 symbol;
#X obj 75 258 list2symbol;
#X text 222 203 even to a zero-length symbol!;
-#X connect 4 0 14 0;
-#X connect 5 0 14 0;
-#X connect 6 0 7 0;
-#X connect 8 0 6 0;
-#X connect 9 0 14 1;
-#X connect 10 0 9 0;
-#X connect 13 0 9 0;
-#X connect 14 0 3 0;
+#X msg 507 247 my bonnie is over the ovean;
+#X obj 507 268 l2s .;
+#X text 537 61 updated for zexy-2.2;
+#X text 554 270 argument: default delimiter;
+#X text 316 326 in prior versions of [list2symbol] \, the default delimiter
+was not settable via arguments \; instead the arguments denoted the
+incoming list. this has changed!!!!;
+#X text 321 302 Attention:;
+#X connect 4 0 12 0;
+#X connect 5 0 12 0;
+#X connect 7 0 12 1;
+#X connect 8 0 7 0;
+#X connect 11 0 7 0;
+#X connect 12 0 3 0;
+#X connect 14 0 15 0;
+#X connect 15 0 6 0;
diff --git a/src/list2symbol.c b/src/list2symbol.c
index dc67769..6f1a8f9 100644
--- a/src/list2symbol.c
+++ b/src/list2symbol.c
@@ -32,6 +32,8 @@ typedef struct _list2symbol
int ac;
t_atom *ap;
t_symbol *s,*connector;
+ t_inlet *x_inlet2;
+ t_outlet*x_outlet;
} t_list2symbol;
static void list2symbol_connector(t_list2symbol *x, t_symbol *s){
@@ -48,9 +50,11 @@ static void list2symbol_bang(t_list2symbol *x)
char *connector=0;
char connlen=0;
char*buffer = (char*)getbytes(MAXPDSTRING*sizeof(char));
- if(x->connector)connector=x->connector->s_name;
- if(connector)connlen=strlen(connector);
-
+ if(x->connector){
+ connector=x->connector->s_name;
+ connlen=strlen(connector);
+ }
+
/* 1st get the length of the symbol */
if(x->s)length+=strlen(x->s->s_name);
else length-=connlen;
@@ -79,10 +83,11 @@ static void list2symbol_bang(t_list2symbol *x)
/* 2nd create the symbol */
if (x->s){
char *buf = x->s->s_name;
- strcpy(result+len, buf);
- len+=strlen(buf);
+ int buflen=strlen(buf);
+ strncpy(result+len, buf, length-len);
+ len+=buflen;
if(i && connector){
- strcpy(result+len, connector);
+ strncpy(result+len, connector, length-len);
len += connlen;
}
}
@@ -90,16 +95,16 @@ static void list2symbol_bang(t_list2symbol *x)
argv=x->ap;
while(i--){
if(A_SYMBOL==argv->a_type){
- strcpy(result+len, argv->a_w.w_symbol->s_name);
+ strncpy(result+len, argv->a_w.w_symbol->s_name, length-len);
len+= strlen(argv->a_w.w_symbol->s_name);
} else {
atom_string(argv, buffer, MAXPDSTRING);
- strcpy(result+len, buffer);
+ strncpy(result+len, buffer, length-len);
len += strlen(buffer);
}
argv++;
if(i && connector){
- strcpy(result+len, connector);
+ strncpy(result+len, connector, length-len);
len += connlen;
}
}
@@ -112,41 +117,64 @@ static void list2symbol_bang(t_list2symbol *x)
static void list2symbol_anything(t_list2symbol *x, t_symbol *s, int argc, t_atom *argv)
{
+ if(x->ap){
+ freebytes(x->ap, x->ac*sizeof(t_atom));
+ x->ap=0;
+ }
+
x->s =s;
x->ac=argc;
- x->ap=argv;
-
+
+ x->ap=(t_atom*)getbytes(x->ac*sizeof(t_atom));
+ if(x->ap){
+ t_atom*ap=x->ap;
+ while(argc--){
+ *ap++=*argv++;
+ }
+ }
list2symbol_bang(x);
}
static void list2symbol_list(t_list2symbol *x, t_symbol *s, int argc, t_atom *argv)
{
- ZEXY_USEVAR(s);
list2symbol_anything(x, 0, argc, argv);
}
static void *list2symbol_new(t_symbol *s, int argc, t_atom *argv)
{
t_list2symbol *x = (t_list2symbol *)pd_new(list2symbol_class);
- ZEXY_USEVAR(s);
- outlet_new(&x->x_obj, 0);
- inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym(""));
+ x->x_outlet=outlet_new(&x->x_obj, 0);
+ x->x_inlet2=inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym(""));
+
+#if 0
+ /* old behaviour: the argument list is used as the list-to-be-converted */
x->connector = gensym(" ");
list2symbol_anything(x, 0, argc, argv);
+#else
+ /* new behaviour: set the delimiter with the argument */
+ list2symbol_connector(x, (argc)?atom_getsymbol(argv):gensym(" "));
+#endif
+
return (x);
}
static void list2symbol_free(t_list2symbol *x)
{
- ZEXY_USEVAR(x);
+ if(x->ap){
+ freebytes(x->ap, x->ac*sizeof(t_atom));
+ x->ap=0;
+ }
+ outlet_free(x->x_outlet);
+ inlet_free(x->x_inlet2);
}
void list2symbol_setup(void)
{
list2symbol_class = class_new(gensym("list2symbol"), (t_newmethod)list2symbol_new,
- (t_method)list2symbol_free, sizeof(t_list2symbol), 0, A_GIMME, 0);
+ (t_method)list2symbol_free, sizeof(t_list2symbol), 0,
+ A_GIMME, 0);
class_addcreator((t_newmethod)list2symbol_new, gensym("l2s"), A_GIMME, 0);
class_addbang (list2symbol_class, list2symbol_bang);
diff --git a/tests/l2s/argument.pd b/tests/l2s/argument.pd
new file mode 100644
index 0000000..19c689a
--- /dev/null
+++ b/tests/l2s/argument.pd
@@ -0,0 +1,28 @@
+#N canvas 349 104 581 409 10;
+#X obj 74 255 outlet;
+#X obj 74 7 inlet;
+#X obj 74 28 b;
+#X text 169 7 the argument is now the delimiter;
+#X msg 74 50 list 192 168 7 1;
+#X obj 74 89 t l l b;
+#X obj 124 126 l2s;
+#X obj 74 126 l2s .;
+#X msg 144 106 symbol .;
+#X obj 74 155 select s;
+#X msg 74 181 1;
+#X msg 133 180 0;
+#X obj 74 214 t f;
+#X connect 1 0 2 0;
+#X connect 2 0 4 0;
+#X connect 4 0 5 0;
+#X connect 5 0 7 0;
+#X connect 5 1 6 0;
+#X connect 5 2 8 0;
+#X connect 6 0 9 1;
+#X connect 7 0 9 0;
+#X connect 8 0 6 1;
+#X connect 9 0 10 0;
+#X connect 9 1 11 0;
+#X connect 10 0 12 0;
+#X connect 11 0 12 0;
+#X connect 12 0 0 0;
diff --git a/tests/l2s/delimiter.pd b/tests/l2s/delimiter.pd
new file mode 100644
index 0000000..ddde552
--- /dev/null
+++ b/tests/l2s/delimiter.pd
@@ -0,0 +1,29 @@
+#N canvas 349 104 581 409 10;
+#X obj 74 255 outlet;
+#X obj 74 7 inlet;
+#X obj 74 28 b;
+#X msg 74 50 list 192 168 7 1;
+#X obj 74 130 l2s;
+#X msg 93 110 symbol .;
+#X obj 74 155 select s;
+#X msg 74 181 1;
+#X msg 133 180 0;
+#X obj 74 214 t f;
+#X obj 74 89 t l b b;
+#X msg 133 135 symbol 192.168.7.1;
+#X text 21 -14 check whether the generated symbol is what we expect...
+;
+#X connect 1 0 2 0;
+#X connect 2 0 3 0;
+#X connect 3 0 10 0;
+#X connect 4 0 6 0;
+#X connect 5 0 4 1;
+#X connect 6 0 7 0;
+#X connect 6 1 8 0;
+#X connect 7 0 9 0;
+#X connect 8 0 9 0;
+#X connect 9 0 0 0;
+#X connect 10 0 4 0;
+#X connect 10 1 5 0;
+#X connect 10 2 11 0;
+#X connect 11 0 6 1;
diff --git a/tests/runtests.txt b/tests/runtests.txt
index 60ae970..788dcdd 100644
--- a/tests/runtests.txt
+++ b/tests/runtests.txt
@@ -68,5 +68,7 @@ help-msg/urn;
help-msg/wrap;
help-msg/zexy;
help-msg/z~;
+l2s/argument;
+l2s/delimiter;
repeat/noargs;
s2l/numsymbols;