From 444eb5d156832f29a3f0be1c7b7b91975825665f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= Date: Sun, 19 Feb 2012 18:28:20 +0000 Subject: added reject-outlet for unparsable symbols svn path=/trunk/externals/zexy/; revision=16000 --- reference/atof-help.pd | 31 +++++++++++++++++++++++++++++++ src/atof.c | 47 ++++++++++++++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 reference/atof-help.pd diff --git a/reference/atof-help.pd b/reference/atof-help.pd new file mode 100644 index 0000000..ab8d93a --- /dev/null +++ b/reference/atof-help.pd @@ -0,0 +1,31 @@ +#N canvas 533 119 615 317 10; +#X floatatom 42 231 4 0 0 0 - - -; +#X symbolatom 42 155 10 0 0 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.; +#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 65 INLET_0 symbol; +#X text 12 85 OUTLET_0 float; +#X restore 565 297 pd META; +#X obj 42 102 makefilename %g; +#X obj 42 178 atof; +#X symbolatom 79 201 10 0 0 1 reject - -; +#X text 146 19 convert ascii to floats; +#X obj 94 21 atof; +#X text 328 286 updated for zexy-2.2.7; +#X obj 45 64 hsl 128 15 0 2 0 0 empty empty empty -2 -8 0 10 -262144 +-1 -1 8900 1; +#X floatatom 42 84 5 0 0 0 - - -; +#X msg 76 129 symbol foo; +#X connect 1 0 5 0; +#X connect 4 0 1 0; +#X connect 5 0 0 0; +#X connect 5 1 6 0; +#X connect 10 0 11 0; +#X connect 11 0 4 0; +#X connect 12 0 1 0; diff --git a/src/atof.c b/src/atof.c index e49d8a8..e978e0f 100644 --- a/src/atof.c +++ b/src/atof.c @@ -19,56 +19,69 @@ #include "zexy.h" #include +#include static t_class *atof_class; typedef struct _atof { t_object x_obj; - t_float f; + t_float x_f; + t_outlet*x_reject; } t_atof; static void atof_bang(t_atof *x) { - outlet_float(x->x_obj.ob_outlet, (t_float)x->f); + outlet_float(x->x_obj.ob_outlet, (t_float)x->x_f); } static void atof_float(t_atof *x, t_floatarg f) { - x->f = f; - outlet_float(x->x_obj.ob_outlet, (t_float)x->f); + x->x_f = f; + atof_bang(x); } -static void atof_symbol(t_atof *x, t_symbol *s) +static void atof_symbol(t_atof *x, t_symbol *sym) { - const char* c = s->s_name; - x->f=strtod(c, 0); - outlet_float(x->x_obj.ob_outlet, (t_float)x->f); + const char* s = sym->s_name; + char*endptr=NULL; + double d=strtod(s, &endptr); + size_t len=strlen(s); + if(endptr && ((s+len)==endptr)) { + atof_float(x, d); + } else { + outlet_symbol(x->x_reject, sym); + } } static void atof_list(t_atof *x, t_symbol *s, int argc, t_atom *argv) { const char* c; ZEXY_USEVAR(s); - if (argv->a_type==A_FLOAT){ - x->f=atom_getfloat(argv); - outlet_float(x->x_obj.ob_outlet, (t_float)x->f); + if(!argc){ + atof_bang(x); return; } - c=atom_getsymbol(argv)->s_name; - x->f=strtod(c, 0); - outlet_float(x->x_obj.ob_outlet, (t_float)x->f); + if (argv->a_type==A_FLOAT){ + atof_float(x, atom_getfloat(argv)); + return; + } + atof_symbol(x, atom_getsymbol(argv)); +} +static void *atof_free(t_atof*x) { + outlet_free(x->x_reject); + x->x_reject=NULL; } - static void *atof_new(void) { t_atof *x = (t_atof *)pd_new(atof_class); - x->f = 0.; outlet_new(&x->x_obj, gensym("float")); + x->x_reject=outlet_new(&x->x_obj, gensym("symbol")); + x->x_f = 0.; return (x); } void atof_setup(void) { - atof_class = class_new(gensym("atof"), (t_newmethod)atof_new, 0, + atof_class = class_new(gensym("atof"), (t_newmethod)atof_new, (t_method)atof_free, sizeof(t_atof), 0, A_DEFFLOAT, 0); class_addbang(atof_class, (t_method)atof_bang); -- cgit v1.2.1