aboutsummaryrefslogtreecommitdiff
path: root/src/any2string.c
diff options
context:
space:
mode:
authorBryan Jurish <mukau@users.sourceforge.net>2007-07-26 12:20:50 +0000
committerBryan Jurish <mukau@users.sourceforge.net>2007-07-26 12:20:50 +0000
commit0d8513a9086f284cb2cd33beacfd4e8d64adcc44 (patch)
treea2712f9623790f7fecc8e0c4d52529f6e20382dc /src/any2string.c
parent4613a9c711dfd5882bc1e1ce561bcff83097e33e (diff)
pdstring v0.05
+ added persistent string buffer to any2string + added initial buffer size and eos-character arguments to any2string, string2any - defaults are backwards-compatible but ugly + added --enable-object-externals option to configure svn path=/trunk/externals/moocow/pdstring/; revision=8247
Diffstat (limited to 'src/any2string.c')
-rw-r--r--src/any2string.c171
1 files changed, 97 insertions, 74 deletions
diff --git a/src/any2string.c b/src/any2string.c
index 094907f..e005be2 100644
--- a/src/any2string.c
+++ b/src/any2string.c
@@ -1,8 +1,8 @@
/* -*- Mode: C -*- */
/*=============================================================================*\
- * File: any2string.c
+ * File: any2string_dynamic.c
* Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
- * Description: convert pd messages to strings
+ * Description: convert pd messages to strings (dynamic allocation)
*
* Copyright (c) 2004 - 2007 Bryan Jurish.
*
@@ -25,9 +25,12 @@
*=============================================================================*/
#include <string.h>
-
#include <m_pd.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
/* black magic */
#ifdef NT
#pragma warning( disable : 4244 )
@@ -45,7 +48,7 @@
# define A2SDEBUG(x)
#endif
-#define ANY2STRING_INITIAL_BUFLEN 32
+#define ANY2STRING_DEFAULT_BUFLEN 256
/*=====================================================================
@@ -58,8 +61,11 @@ typedef struct _any2string
t_object x_obj;
int x_alloc;
int x_argc;
+ t_float x_eos; //-- EOS character to add (<0 for none)
+ char *x_text;
t_atom *x_argv;
t_binbuf *x_binbuf;
+ t_inlet *x_eos_in;
t_outlet *x_outlet;
} t_any2string;
@@ -67,6 +73,7 @@ typedef struct _any2string
/*=====================================================================
* Constants
*=====================================================================*/
+static char *any2string_banner = "any2string: pdstring version " PACKAGE_VERSION " by Bryan Jurish";
/*=====================================================================
* Methods
@@ -78,75 +85,60 @@ typedef struct _any2string
static void any2string_anything(t_any2string *x, t_symbol *sel, int argc, t_atom *argv)
{
t_atom *a;
- char *text=NULL, *s, *s_max;
+ char *s, *s_max;
int len;
- A2SDEBUG(post("-------any2string_anything()---------"));
+ A2SDEBUG(post("-------any2string_anything(%p,...)---------", x));
/*-- set up binbuf --*/
- A2SDEBUG(post("any2string: binbuf_clear()"));
+ A2SDEBUG(post("any2string[%p]: binbuf_clear()", x));
binbuf_clear(x->x_binbuf);
- A2SDEBUG(post("any2string: binbuf_add()"));
+
+ /*-- binbuf_add(): selector --*/
+ if (sel != &s_float && sel != &s_list && sel != &s_) {
+ t_atom a;
+ A2SDEBUG(post("any2string[%p]: binbuf_add(): selector: '%s'", x, sel->s_name));
+ SETSYMBOL((&a), sel);
+ binbuf_add(x->x_binbuf, 1, &a);
+ }
+ A2SDEBUG(else { post("any2string[%p]: selector: '%s': IGNORED", x, sel->s_name); });
+
+ /*-- binbuf_add(): arg list --*/
+ A2SDEBUG(post("any2string[%p]: binbuf_add(): arg list", x));
binbuf_add(x->x_binbuf, argc, argv);
- A2SDEBUG(startpost("any2string: binbuf_print: "));
+ A2SDEBUG(post("any2string[%p]: binbuf_print: ", x));
A2SDEBUG(binbuf_print(x->x_binbuf));
- A2SDEBUG(post("any2string: binbuf_gettext()"));
- binbuf_gettext(x->x_binbuf, &text, &len);
- A2SDEBUG(post("any2string: binbuf_gettext() = \"%s\" ; len=%d", text, len));
- /*text[len] = 0;*/ /*-- ? avoid errors: free(): invalid next size(fast): [HEX_ADDRESS] */
+ A2SDEBUG(post("any2string[%p]: binbuf_gettext()", x));
+ binbuf_gettext(x->x_binbuf, &(x->x_text), &len);
+ A2SDEBUG(post("any2string[%p]: binbuf_gettext() = \"%s\" ; len=%d", x, x->x_text, len));
+ /*text[len] = 0;*/ /*-- ? avoid errors: "free(): invalid next size(fast): 0x..." */
- /*-- get string length --*/
- x->x_argc = len+1;
- if (sel != &s_float && sel != &s_list && sel != &s_) {
- x->x_argc += strlen(sel->s_name);
- if (argc > 0) x->x_argc++;
- }
- A2SDEBUG(post("any2string: argc=%d", x->x_argc));
+ /*-- get output atom-list length --*/
+ x->x_argc = len;
+ if (x->x_eos >= 0) { x->x_argc++; }
+ A2SDEBUG(post("any2string[%p]: argc=%d", x, x->x_argc));
- /*-- (re-)allocate --*/
+ /*-- (re-)allocate (maybe) --*/
if (x->x_alloc < x->x_argc) {
- A2SDEBUG(post("any2string: reallocate(%d->%d)", x->x_alloc, x->x_argc));
- freebytes(x->x_argv, x->x_alloc * sizeof(t_atom));
- x->x_argv = (t_atom *)getbytes(x->x_argc * sizeof(t_atom));
+ A2SDEBUG(post("any2string[%p]: reallocate(%d->%d)", x, x->x_alloc, x->x_argc));
+ freebytes(x->x_argv, x->x_alloc*sizeof(t_atom));
+ x->x_argv = (t_atom*)getbytes(x->x_argc * sizeof(t_atom));
x->x_alloc = x->x_argc;
}
- /*-- add selector (maybe) --*/
- a=x->x_argv;
- if (sel != &s_float && sel != &s_list && sel != &s_) {
- A2SDEBUG(post("any2string: for {...} //sel"));
- for (s=sel->s_name; *s; s++, a++) {
- A2SDEBUG(post("any2string: for: SETFLOAT(a,'%c'=%d) //sel", *s, *s));
- SETFLOAT(a,*s);
- }
- A2SDEBUG(post("any2string: /for {...} //sel"));
-
- if (argc > 0) {
- SETFLOAT(a,' ');
- a++;
- }
- }
-
- /*-- add binbuf text --*/
- A2SDEBUG(post("any2string: for {...}"));
- s_max = text+len;
- for (s=text; s < s_max; s++, a++) {
- A2SDEBUG(post("any2string: for: //SETFLOAT(a,'%c'=%d)", *s, *s));
+ /*-- atom buffer: binbuf text --*/
+ A2SDEBUG(post("any2string[%p]: atom buffer: for {...}", x));
+ a = x->x_argv;
+ s_max = x->x_text+len;
+ for (s=x->x_text; s < s_max; s++, a++) {
+ A2SDEBUG(post("any2string[%p]: atom buffer[%d]: SETFLOAT(a,%d='%c')", x, (a-x->x_argv), *s, *s));
SETFLOAT(a,*s);
}
- A2SDEBUG(post("any2string: /for {...}"));
-
- /*-- add terminating NUL --*/
- SETFLOAT(a,0);
-
- A2SDEBUG(post("any2string: freebytes()"));
- freebytes(text, len);
+ A2SDEBUG(post("any2string: atom buffer: DONE"));
- /*
- A2SDEBUG(post("any2string: binbuf_free()"));
- binbuf_free(x->x_binbuf);
- */
+ /*-- add EOS character (maybe) --*/
+ if (x->x_eos >= 0) { SETFLOAT(a, ((int)x->x_eos)); }
A2SDEBUG(post("any2string: outlet_list(..., %d, ...)", x->x_argc));
outlet_list(x->x_outlet, &s_list, x->x_argc, x->x_argv);
@@ -156,19 +148,39 @@ static void any2string_anything(t_any2string *x, t_symbol *sel, int argc, t_atom
/*--------------------------------------------------------------------
* new
*/
-static void *any2string_new(void)
+static void *any2string_new(t_symbol *sel, int argc, t_atom *argv)
{
t_any2string *x = (t_any2string *)pd_new(any2string_class);
//-- defaults
- x->x_alloc = ANY2STRING_INITIAL_BUFLEN;
- x->x_argc = 0;
- x->x_argv = (t_atom *)getbytes(x->x_alloc * sizeof(t_atom));
+ x->x_eos = 0;
+ x->x_alloc = ANY2STRING_DEFAULT_BUFLEN;
+
+ //-- args: 0: bufsize
+ if (argc > 0) {
+ int bufsize = atom_getintarg(0, argc, argv);
+ if (bufsize > 0) { x->x_alloc = bufsize; }
+ }
+ //-- args: 1: eos
+ if (argc > 1) {
+ x->x_eos = atom_getfloatarg(1, argc, argv);
+ }
+
+ //-- allocate
+ x->x_text = (char *)getbytes(x->x_alloc*sizeof(char));
+ x->x_argc = 0;
+ x->x_argv = (t_atom *)getbytes(x->x_alloc*sizeof(t_atom));
x->x_binbuf = binbuf_new();
+ //-- inlets
+ x->x_eos_in = floatinlet_new(&x->x_obj, &x->x_eos);
+
//-- outlets
x->x_outlet = outlet_new(&x->x_obj, &s_list);
+ //-- report
+ A2SDEBUG(post("any2string_new(): x=%p, alloc=%d, eos=%d, text=%p, argv=%p, binbuf=%p", x, x->x_alloc, x->x_eos, x->x_text, x->x_argv, x->x_binbuf));
+
return (void *)x;
}
@@ -177,36 +189,47 @@ static void *any2string_new(void)
*/
static void any2string_free(t_any2string *x)
{
+ if (x->x_text) {
+ freebytes(x->x_text, x->x_alloc*sizeof(char));
+ x->x_text = NULL;
+ }
if (x->x_argv) {
- freebytes(x->x_argv, x->x_alloc * sizeof(t_atom));
+ freebytes(x->x_argv, x->x_alloc*sizeof(t_atom));
x->x_argv = NULL;
}
binbuf_free(x->x_binbuf);
+ inlet_free(x->x_eos_in);
outlet_free(x->x_outlet);
return;
}
/*--------------------------------------------------------------------
- * setup
+ * setup (guts)
*/
-void any2string_setup(void)
+void any2string_setup_guts(void)
{
//-- class
any2string_class = class_new(gensym("any2string"),
- (t_newmethod)any2string_new,
- (t_method)any2string_free,
- sizeof(t_any2string),
- CLASS_DEFAULT,
- 0);
+ (t_newmethod)any2string_new,
+ (t_method)any2string_free,
+ sizeof(t_any2string),
+ CLASS_DEFAULT,
+ A_GIMME, //-- initial_bufsize, eos_char
+ 0);
//-- methods
- /*class_addmethod(any2string_class, (t_method)any2string_symbol, &s_symbol,
- A_DEFSYMBOL, 0);
- */
- class_addanything(any2string_class,
- (t_method)any2string_anything);
+ class_addanything(any2string_class, (t_method)any2string_anything);
//-- help symbol
- class_sethelpsymbol(any2string_class, gensym("pdstring-help.pd"));
+ class_sethelpsymbol(any2string_class, gensym("any2string-help.pd"));
+}
+
+/*--------------------------------------------------------------------
+ * setup
+ */
+void any2string_setup(void)
+{
+ post(any2string_banner);
+ any2string_setup_guts();
}