diff options
author | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2012-02-19 18:27:48 +0000 |
---|---|---|
committer | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2012-02-19 18:27:48 +0000 |
commit | 4e1acff82dbbc34dbace803eadfd899b08eefc6d (patch) | |
tree | 8d172e0f65c8c32788805da7a75e58746e8901cc | |
parent | 85d0912d11556a293503f3e9fc97390147a5965f (diff) |
added reject-outlet for unparsable symbols
svn path=/trunk/externals/zexy/; revision=15999
-rw-r--r-- | reference/atoi-help.pd | 18 | ||||
-rw-r--r-- | src/atoi.c | 59 |
2 files changed, 49 insertions, 28 deletions
diff --git a/reference/atoi-help.pd b/reference/atoi-help.pd index 3fb07db..75c02ba 100644 --- a/reference/atoi-help.pd +++ b/reference/atoi-help.pd @@ -1,4 +1,4 @@ -#N canvas 0 31 615 317 10; +#N canvas 4 49 615 317 10; #X obj 95 21 atoi; #X text 144 19 convert ascii to integer; #X obj 42 178 atoi; @@ -26,20 +26,24 @@ ; #X text 211 223 symbols starting with numbers but "0" are converted as decimal numbers; -#X text 330 285 updated for zexy-2.0; -#X text 330 285 updated for zexy-2.0; #X text 509 17 part of zexy; -#N canvas 515 193 494 344 META 0; -#X text 12 125 HELP_PATCH_AUTHORS "pd meta" information added by Jonathan -Wilkes for Pd version 0.42.; +#N canvas 517 185 494 344 META 0; #X text 12 25 LICENSE GPL v2; #X text 12 5 KEYWORDS control conversion; #X text 12 45 DESCRIPTION convert ascii to integer; -#X text 12 105 AUTHOR IOhannes m zmoelnig; +#X text 12 135 AUTHOR IOhannes m zmoelnig; #X text 12 65 INLET_0 symbol; #X text 12 85 OUTLET_0 float; +#X text 12 102 OUTLET_1 symbol; +#X text 12 155 HELP_PATCH_AUTHORS "pd meta" information added by Jonathan +Wilkes and updated by IOhannes m zmölnig (for zexy-2.2.7); #X restore 565 297 pd META; +#X symbolatom 63 228 10 0 0 3 reject - -; +#X text 330 285 updated for zexy-2.2.7; +#X text 213 254 if the symbol cannot be converted into a number \, +it will be sent to the 2nd ("reject") outlet.; #X connect 2 0 3 0; +#X connect 2 1 26 0; #X connect 4 0 5 0; #X connect 5 0 2 0; #X connect 6 0 4 0; @@ -19,46 +19,59 @@ #include "zexy.h" #include <stdlib.h> +#include <string.h> static t_class *atoi_class; typedef struct _atoi { t_object x_obj; - int i; + int x_i; + t_outlet*x_reject; } t_atoi; + static void atoi_bang(t_atoi *x) { - outlet_float(x->x_obj.ob_outlet, (t_float)x->i); + outlet_float(x->x_obj.ob_outlet, (t_float)x->x_i); } -static void atoi_float(t_atoi *x, t_floatarg f) +static void atoi_float(t_atoi *x, t_float f) { - x->i = f; - outlet_float(x->x_obj.ob_outlet, (t_float)x->i); + x->x_i = f; + atoi_bang(x); +} +static void atoi_atoi(t_atoi *x, t_symbol*s, int base) { + char*endptr=NULL; + const char*str=s->s_name; + long l=strtol(str, &endptr, base); + size_t len=strlen(str); + if(str+len == endptr) + atoi_float(x, l); + else + outlet_symbol(x->x_reject, s); } -static void atoi_symbol(t_atoi *x, t_symbol *s) + +static void atoi_symbol(t_atoi *x, t_symbol *sym) { + long l=0; int base=10; - const char* c = s->s_name; - if(c[0]=='0'){ + const char* s = sym->s_name; + if(s[0]=='0'){ base=8; - if (c[1]=='x')base=16; + if (s[1]=='x')base=16; } - x->i=strtol(c, 0, base); - outlet_float(x->x_obj.ob_outlet, (t_float)x->i); + atoi_atoi(x, sym, base); } -static void atoi_list(t_atoi *x, t_symbol *s, int argc, t_atom *argv) +static void atoi_list(t_atoi *x, t_symbol *sym, int argc, t_atom *argv) { int base=10; - const char* c; - ZEXY_USEVAR(s); + const char* s; + ZEXY_USEVAR(sym); if (argv->a_type==A_FLOAT){ - x->i=atom_getfloat(argv); - outlet_float(x->x_obj.ob_outlet, (t_float)x->i); + atoi_float(x, atom_getfloat(argv)); return; } - + if (argc>1){ base=atom_getfloat(argv+1); if (base<2) { @@ -66,21 +79,25 @@ static void atoi_list(t_atoi *x, t_symbol *s, int argc, t_atom *argv) base=10; } } - c=atom_getsymbol(argv)->s_name; - x->i=strtol(c, 0, base); - outlet_float(x->x_obj.ob_outlet, (t_float)x->i); + atoi_atoi(x, sym, base); } +static void atoi_free(t_atoi *x){ + outlet_free(x->x_reject); + x->x_reject=NULL; +} static void *atoi_new(void) { t_atoi *x = (t_atoi *)pd_new(atoi_class); outlet_new(&x->x_obj, gensym("float")); + x->x_reject=outlet_new(&x->x_obj, gensym("symbol")); + x->x_i=0; return (x); } void atoi_setup(void) { - atoi_class = class_new(gensym("atoi"), (t_newmethod)atoi_new, 0, + atoi_class = class_new(gensym("atoi"), (t_newmethod)atoi_new, (t_method)atoi_free, sizeof(t_atoi), 0, A_DEFFLOAT, 0); class_addbang(atoi_class, (t_method)atoi_bang); |