From e76e2af1752600028f40e26484e427963e3f0570 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 11 Mar 2011 04:08:45 +0000 Subject: first sketch of a logpost object svn path=/trunk/externals/log/; revision=15023 --- logpost.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 logpost.c (limited to 'logpost.c') diff --git a/logpost.c b/logpost.c new file mode 100644 index 0000000..e4f31d9 --- /dev/null +++ b/logpost.c @@ -0,0 +1,100 @@ + +#include "m_pd.h" +#include + +/* -------------------------- logpost ------------------------------ */ +static t_class *logpost_class; + +typedef struct _logpost +{ + t_object x_obj; + t_float level; + t_symbol* tag; +} t_logpost; + +static void logpost_bang(t_logpost *x) +{ + logpost(x, (const int)x->level, "%s%sbang", + x->tag->s_name, (*x->tag->s_name ? ": " : "")); +} + +static void logpost_pointer(t_logpost *x, t_gpointer *gp) +{ + logpost(x, (const int)x->level, "%s%s(pointer %lx)", + x->tag->s_name, (*x->tag->s_name ? ": " : ""), gp); +} + +static void logpost_float(t_logpost *x, t_float f) +{ + logpost(x, (const int)x->level, "%s%s%g", + x->tag->s_name, (*x->tag->s_name ? ": " : ""), f); +} + +static void logpost_list(t_logpost *x, t_symbol *s, int argc, t_atom *argv) +{ + int i; + if (argc && argv->a_type != A_SYMBOL) startpost("%s:", x->tag->s_name); + else startpost("%s%s%s", x->tag->s_name, + (*x->tag->s_name ? ": " : ""), + (argc > 1 ? s_list.s_name : (argc == 1 ? s_symbol.s_name : + s_bang.s_name))); + postatom(argc, argv); + endpost(); +} + +static void logpost_anything(t_logpost *x, t_symbol *s, int argc, t_atom *argv) +{ + int i; + startpost("%s%s%s", x->tag->s_name, (*x->tag->s_name ? ": " : ""), + s->s_name); + postatom(argc, argv); + endpost(); +} + +static void *logpost_new(t_symbol *sel, int argc, t_atom *argv) +{ + t_logpost *x = (t_logpost *)pd_new(logpost_class); + + if (argc == 0) + x->tag = &s_; + else if (argc == 1) + { + t_symbol *s = atom_getsymbolarg(0, argc, argv); + if (s != &s_) // we have a symbol + x->tag = s; + else // we have a float + { + x->level = atom_getfloatarg(0, argc, argv); + } + } + else + { + int bufsize; + char *buf; + t_binbuf *bb = binbuf_new(); + binbuf_add(bb, argc, argv); + binbuf_gettext(bb, &buf, &bufsize); + buf = resizebytes(buf, bufsize, bufsize+1); + buf[bufsize] = 0; + x->tag = gensym(buf); + freebytes(buf, bufsize+1); + binbuf_free(bb); + } + floatinlet_new(&x->x_obj, &x->level); + return (x); +} + +void logpost_setup(void) +{ + logpost_class = class_new(gensym("logpost"), + (t_newmethod)logpost_new, + 0, + sizeof(t_logpost), + CLASS_DEFAULT, + A_GIMME, 0); + class_addbang(logpost_class, logpost_bang); + class_addfloat(logpost_class, logpost_float); + class_addpointer(logpost_class, logpost_pointer); + class_addlist(logpost_class, logpost_list); + class_addanything(logpost_class, logpost_anything); +} -- cgit v1.2.1