From 4be9db3a7bee4bb8c254ae26253a7ed70390589b Mon Sep 17 00:00:00 2001 From: Bryan Jurish Date: Tue, 27 Jan 2009 21:39:35 +0000 Subject: + delegated optional single-object-external mish-mash to src/Makefile.am using AM_CONDITIONAL + for --enable-object-externals builds, install actual links for 'any2string', 'string2any' aliases - requires corresponding setup() routines in *bytes* + added printbytes, printwchars objects svn path=/trunk/externals/moocow/pdstring/; revision=10661 --- src/pdstringUtils.h | 334 +++++++--------------------------------------------- 1 file changed, 43 insertions(+), 291 deletions(-) (limited to 'src/pdstringUtils.h') diff --git a/src/pdstringUtils.h b/src/pdstringUtils.h index 85a72a5..a3e8f58 100644 --- a/src/pdstringUtils.h +++ b/src/pdstringUtils.h @@ -32,18 +32,6 @@ #include #include "mooPdUtils.h" -/*===================================================================== - * Debugging - *=====================================================================*/ -#define PDSTRING_UTILS_DEBUG 1 -//#undef PDSTRING_UTILS_DEBUG - -#ifdef PDSTRING_UTILS_DEBUG -# define PDSDEBUG(x) x -#else -# define PDSDEBUG(x) -#endif - /*===================================================================== * Constants *=====================================================================*/ @@ -70,6 +58,9 @@ #define PDSTRING_WCHARS_GET PDSTRING_DEFAULT_GET #define PDSTRING_ATOMS_GET PDSTRING_DEFAULT_GET +//#define PDSTRING_STATIC static +#define PDSTRING_STATIC + /*===================================================================== * Structures & Types *=====================================================================*/ @@ -101,6 +92,18 @@ typedef struct _pdstring_atoms { size_t a_alloc; //-- allocated size of a_buf } t_pdstring_atoms; +/* t_pdstring_floatarray + * + read/store strings to t_array (of t_float) + */ +typedef struct _pdstring_floatarray { + t_symbol *fa_name; //-- name bound to array + int fa_offset; //-- offset at which to begin operation + t_garray *fa_garray; //-- underlying t_garray*, populated by pdstring_floatarray_getarray() + int fa_vlen; //-- length of fa_vec (number of points), populated by pdstring_floatarray_getvec() + t_float *fa_vec; //-- underlying t_float*, populated by pdstring_floatarray_getvec() +} t_pdstring_floatarray; + + /*===================================================================== * Initialization *=====================================================================*/ @@ -108,72 +111,35 @@ typedef struct _pdstring_atoms { /*--------------------------------------------------------------------- * bytes */ -static void pdstring_bytes_clear(t_pdstring_bytes *b) -{ - if (b->b_alloc) freebytes(b->b_buf, (b->b_alloc)*sizeof(unsigned char)); - b->b_buf = NULL; - b->b_len = 0; - b->b_alloc = 0; -} -static void pdstring_bytes_realloc(t_pdstring_bytes *b, size_t n) -{ - pdstring_bytes_clear(b); - b->b_buf = n ? (unsigned char*)getbytes(n*sizeof(unsigned char)) : NULL; - b->b_alloc = n; -} -static void pdstring_bytes_init(t_pdstring_bytes *b, size_t n) -{ - pdstring_bytes_clear(b); - pdstring_bytes_realloc(b,n); -} +PDSTRING_STATIC void pdstring_bytes_clear(t_pdstring_bytes *b); +PDSTRING_STATIC void pdstring_bytes_realloc(t_pdstring_bytes *b, size_t n); +PDSTRING_STATIC void pdstring_bytes_init(t_pdstring_bytes *b, size_t n); /*--------------------------------------------------------------------- * wchars */ -static void pdstring_wchars_clear(t_pdstring_wchars *w) -{ - if (w->w_alloc) freebytes(w->w_buf, (w->w_alloc)*sizeof(wchar_t)); - w->w_buf = NULL; - w->w_len = 0; - w->w_alloc = 0; -} -static void pdstring_wchars_realloc(t_pdstring_wchars *w, size_t n) -{ - pdstring_wchars_clear(w); - w->w_buf = n ? (wchar_t*)getbytes(n*sizeof(wchar_t)) : NULL; - w->w_alloc = n; -} -static void pdstring_wchars_init(t_pdstring_wchars *w, size_t n) -{ - pdstring_wchars_clear(w); - pdstring_wchars_realloc(w,n); -} +PDSTRING_STATIC void pdstring_wchars_clear(t_pdstring_wchars *w); +PDSTRING_STATIC void pdstring_wchars_realloc(t_pdstring_wchars *w, size_t n); +PDSTRING_STATIC void pdstring_wchars_init(t_pdstring_wchars *w, size_t n); /*--------------------------------------------------------------------- * atoms */ -static void pdstring_atoms_clear(t_pdstring_atoms *a) -{ - if (a->a_alloc) freebytes(a->a_buf, (a->a_alloc)*sizeof(t_atom)); - a->a_buf = NULL; - a->a_len = 0; - a->a_alloc = 0; -} -static void pdstring_atoms_realloc(t_pdstring_atoms *a, size_t n) -{ - pdstring_atoms_clear(a); - a->a_buf = n ? (t_atom*)getbytes(n*sizeof(t_atom)) : NULL; - a->a_alloc = n; -} -static void pdstring_atoms_init(t_pdstring_atoms *a, size_t n) -{ - pdstring_atoms_clear(a); - pdstring_atoms_realloc(a,n); -} +PDSTRING_STATIC void pdstring_atoms_clear(t_pdstring_atoms *a); +PDSTRING_STATIC void pdstring_atoms_realloc(t_pdstring_atoms *a, size_t n); +PDSTRING_STATIC void pdstring_atoms_init(t_pdstring_atoms *a, size_t n); + +/*--------------------------------------------------------------------- + * floatarray + */ +PDSTRING_STATIC void pdstring_floatarray_clear(t_pdstring_floatarray *a); //-- clears all data (no free) +PDSTRING_STATIC void pdstring_floatarray_realloc(t_pdstring_floatarray *a, size_t n); //-- calls garray_resize() +PDSTRING_STATIC void pdstring_floatarray_init(t_pdstring_floatarray *a, size_t n); //-- if a->fa_name is set +PDSTRING_STATIC void pdstring_floatarray_getvec(t_object *x, t_pdstring_floatarray *a); //-- gets underlying pointers /*===================================================================== - * Utilities + * Conversions *=====================================================================*/ /*-------------------------------------------------------------------- @@ -184,58 +150,9 @@ static void pdstring_atoms_init(t_pdstring_atoms *a, size_t n) * + x_binbuf may be NULL, in which case a temporary t_binbuf is created and used * - in this case, output bytes are copied into *dst, reallocating if required * + if x_binbuf is given and non-NULL, dst may be NULL. - * - if dst is non-NULL, its values will be clobbered by those returned by - * binbuf_gettext() + * - if dst is non-NULL, its values will be clobbered by those returned by binbuf_gettext() */ -static void pdstring_any2bytes(void *x, t_pdstring_bytes *dst, t_symbol *sel, t_pdstring_atoms *src, t_binbuf *x_binbuf) -{ - int bb_is_tmp=0; - - //-- create temporary binbuf? - if (!x_binbuf) { - x_binbuf = binbuf_new(); - bb_is_tmp = 1; - } - - //-- prepare binbuf - binbuf_clear(x_binbuf); - - //-- binbuf_add(): selector - if (sel && sel != &s_float && sel != &s_list && sel != &s_) { - t_atom a; - SETSYMBOL((&a), sel); - binbuf_add(x_binbuf, 1, &a); - } - - //-- binbuf_add(): src atoms - binbuf_add(x_binbuf, src->a_len, src->a_buf); - - //-- output: get text string - if (bb_is_tmp) { - //-- temporary binbuf: copy text - char *text; - int len; - binbuf_gettext(x_binbuf, &text, &len); - - //-- reallocate? - if ( dst->b_alloc < len ) - pdstring_bytes_realloc(dst, len + PDSTRING_BYTES_GET); - - //-- copy - memcpy(dst->b_buf, text, len*sizeof(char)); - dst->b_len = len; - - //-- cleanup - binbuf_free(x_binbuf); - if (text) freebytes(text,len); - } - else if (dst) { - //-- permanent binbuf: clobber dst - pdstring_bytes_clear(dst); - binbuf_gettext(x_binbuf, ((char**)((void*)(&dst->b_buf))), &dst->b_len); - dst->b_alloc = dst->b_len; - } -} +PDSTRING_STATIC void pdstring_any2bytes(void *x, t_pdstring_bytes *dst, t_symbol *sel, t_pdstring_atoms *src, t_binbuf *x_binbuf); /*-------------------------------------------------------------------- @@ -244,78 +161,16 @@ static void pdstring_any2bytes(void *x, t_pdstring_bytes *dst, t_symbol *sel, t_ * + x_binbuf may be NULL, in which case a temporary t_binbuf is created and used * - in this case, output atoms are copied into *dst, reallocating if required * + if x_binbuf is given and non-NULL, dst may be NULL. - * - if dst is non-NULL, its values will be clobbered by those returned by - * binbuf_getnatom() and binbuf_getvec() + * - if dst is non-NULL, its values will be clobbered by those returned by binbuf_getnatom() & binbuf_getvec() */ -static void pdstring_bytes2any(void *x, t_pdstring_atoms *dst, t_pdstring_bytes *src, t_binbuf *x_binbuf) -{ - int bb_is_tmp=0; - - //-- create temporary binbuf? - if (!x_binbuf) { - x_binbuf = binbuf_new(); - bb_is_tmp = 1; - } - - //-- populate binbuf - binbuf_clear(x_binbuf); - binbuf_text(x_binbuf, (char*)src->b_buf, src->b_len); - //PDSDEBUG(post("bytes2any[dst=%p,src=%p,bb=%p]: binbuf_print: ", dst,src,x_binbuf)); - //PDSDEBUG(binbuf_print(x_binbuf)); - - //-- populate atom list - if (bb_is_tmp) { - //-- temporary binbuf: copy atoms - t_atom *argv = binbuf_getvec(x_binbuf); - int argc = binbuf_getnatom(x_binbuf); - - //-- reallocate? - if ( dst->a_alloc < argc ) - pdstring_atoms_realloc(dst, argc + PDSTRING_ATOMS_GET); - - //-- copy - memcpy(dst->a_buf, argv, argc*sizeof(t_atom)); - dst->a_len = argc; - - //-- cleanup - binbuf_free(x_binbuf); - } - else if (dst) { - //-- permanent binbuf: clobber dst - dst->a_buf = binbuf_getvec(x_binbuf); - dst->a_len = binbuf_getnatom(x_binbuf); - dst->a_alloc = 0; //-- don't try to free this - } -} - +PDSTRING_STATIC void pdstring_bytes2any(void *x, t_pdstring_atoms *dst, t_pdstring_bytes *src, t_binbuf *x_binbuf); /*-------------------------------------------------------------------- * pdstring_atoms2bytes() * + always appends a final NUL byte to *dst_buf, even if src_argv doesn't contain one * + returns number of bytes actually written to *dst_buf, __including__ implicit trailing NUL */ -static int pdstring_atoms2bytes(void *x, t_pdstring_bytes *dst, t_pdstring_atoms *src, t_float x_eos) -{ - t_atom *argv = src->a_buf; - int argc = src->a_len; - unsigned char *s; - int new_len=0; - - /*-- re-allocate? --*/ - if (dst->b_alloc <= (argc+1)) - pdstring_bytes_realloc(dst, argc + 1 + PDSTRING_BYTES_GET); - - /*-- get byte string --*/ - for (s=dst->b_buf, new_len=0; argc > 0; argc--, argv++, s++, new_len++) - { - *s = atom_getfloat(argv); - if ((x_eos<0 && !*s) || (*s==x_eos)) { break; } /*-- hack: truncate at first EOS char --*/ - } - *s = '\0'; /*-- always append terminating NUL */ - dst->b_len = new_len; - - return new_len+1; -} +PDSTRING_STATIC int pdstring_atoms2bytes(void *x, t_pdstring_bytes *dst, t_pdstring_atoms *src, t_float x_eos); /*-------------------------------------------------------------------- * pdstring_atoms2wchars() @@ -323,130 +178,27 @@ static int pdstring_atoms2bytes(void *x, t_pdstring_bytes *dst, t_pdstring_atoms * + returns number of bytes actually written to dst->w_buf, __including__ implicit trailing NUL * + but dst->w_len does NOT include implicit trailing NUL */ -static int pdstring_atoms2wchars(void *x, t_pdstring_wchars *dst, t_pdstring_atoms *src, t_float x_eos) -{ - t_atom *argv = src->a_buf; - int argc = src->a_len; - int new_len=0; - wchar_t *s; - - /*-- re-allocate? --*/ - if (dst->w_alloc <= (argc+1)) - pdstring_wchars_realloc(dst, argc + 1 + PDSTRING_WCHARS_GET); - - /*-- get wchar_t string --*/ - for (s=dst->w_buf, new_len=0; argc > 0; argc--, argv++, s++, new_len++) - { - *s = atom_getfloat(argv); - if ((x_eos<0 && !*s) || (*s==x_eos)) { break; } /*-- hack: truncate at first EOS char --*/ - } - *s = L'\0'; /*-- always append terminating NUL */ - dst->w_len = new_len; - - return new_len+1; -} - +PDSTRING_STATIC int pdstring_atoms2wchars(void *x, t_pdstring_wchars *dst, t_pdstring_atoms *src, t_float x_eos); /*-------------------------------------------------------------------- * pdstring_bytes2wchars() */ -static int pdstring_bytes2wchars(void *x, t_pdstring_wchars *dst, t_pdstring_bytes *src) -{ - size_t bi, wi; - - //-- re-allocate? - if ( dst->w_alloc < src->b_len ) - pdstring_wchars_realloc(dst, src->b_len + PDSTRING_WCHARS_GET); - - //-- convert - //PDSDEBUG(post("\nbytes2wchars[dst=%p,src=%p]: init", dst,src);) - mbtowc(NULL,NULL,0); //-- re-initialize conversion state for mbtowc() - for (bi=0,wi=0; bib_len; wi++) { - int nbytes = mbtowc(dst->w_buf+wi, (char*)(src->b_buf+bi), src->b_len-bi); - if (nbytes <= 0) { - if (nbytes < 0) { - pd_error(x,"pdstring_bytes2wchars(): malformed byte string \"%s\" at char '%c' - copying literal byte", src->b_buf, src->b_buf[bi]); - } - dst->w_buf[wi] = src->b_buf[bi]; - nbytes = 1; - } - bi += nbytes; - //PDSDEBUG(post("bytes2wchars[dst=%p,src=%p]: loop[bi=%d,wi=%d,src=%s]: nbytes=%d,wc=%u", dst,src, bi,wi,src, nbytes,dst->w_buf[wi])); - } - dst->w_len = wi; - return wi; -} +PDSTRING_STATIC int pdstring_bytes2wchars(void *x, t_pdstring_wchars *dst, t_pdstring_bytes *src); /*-------------------------------------------------------------------- * pdstring_wchars2bytes() */ -static int pdstring_wchars2bytes(void *x, t_pdstring_bytes *dst, t_pdstring_wchars *src) -{ - size_t bi, wi; - - //-- re-allocate? - if ( dst->b_alloc < src->w_len * MB_CUR_MAX ) - pdstring_bytes_realloc(dst, src->w_len * MB_CUR_MAX + PDSTRING_WCHARS_GET); - - //-- convert - for (bi=0,wi=0; wi < src->w_len; wi++) { - int nbytes = wctomb((char*)dst->b_buf+bi, src->w_buf[wi]); - if (nbytes <= 0) { - if (nbytes < 0) { - pd_error(x,"pdstring_wchars2bytes(): malformed wide character (%u) - bashing to byte", src->w_buf[wi]); - } - dst->b_buf[bi] = src->w_buf[wi]; - nbytes = 1; - } - bi += nbytes; - } - dst->b_len = bi; - return bi; -} - +PDSTRING_STATIC int pdstring_wchars2bytes(void *x, t_pdstring_bytes *dst, t_pdstring_wchars *src); /*-------------------------------------------------------------------- * pdstring_bytes2atoms() * + implicitly appends x_eos if >= 0 and != PDSTRING_EOS_NONE */ -static void pdstring_bytes2atoms(void *x, t_pdstring_atoms *dst, t_pdstring_bytes *src, t_float x_eos) -{ - int i; - - //-- re-allocate? - if ( dst->a_alloc <= src->b_len ) - pdstring_atoms_realloc(dst, src->b_len + 1 + PDSTRING_ATOMS_GET); - - //-- convert - for (i=0; i < src->b_len; i++) { - SETFLOAT((dst->a_buf+i), src->b_buf[i]); - } - dst->a_len = src->b_len; - - //-- append eos atom? - if (x_eos >= 0 && x_eos != PDSTRING_EOS_NONE) { - SETFLOAT(dst->a_buf+dst->a_len, x_eos); - dst->a_len++; - } -} +PDSTRING_STATIC void pdstring_bytes2atoms(void *x, t_pdstring_atoms *dst, t_pdstring_bytes *src, t_float x_eos); /*-------------------------------------------------------------------- * pdstring_wchars2atoms() */ -static void pdstring_wchars2atoms(void *x, t_pdstring_atoms *dst, t_pdstring_wchars *src) -{ - int i; - - //-- re-allocate? - if ( dst->a_alloc < src->w_len ) - pdstring_atoms_realloc(dst, src->w_len + PDSTRING_ATOMS_GET); - - //-- convert - for (i=0; i < src->w_len; i++) { - SETFLOAT((dst->a_buf+i), src->w_buf[i]); - } - dst->a_len = src->w_len; -} - +PDSTRING_STATIC void pdstring_wchars2atoms(void *x, t_pdstring_atoms *dst, t_pdstring_wchars *src); #endif /* PDSTRING_UTILS_H */ -- cgit v1.2.1