aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Jurish <mukau@users.sourceforge.net>2009-01-24 22:43:09 +0000
committerBryan Jurish <mukau@users.sourceforge.net>2009-01-24 22:43:09 +0000
commit2fca84eeb6f1079b1f8a0723f5d2b176aa1b62e3 (patch)
treec8151eeb0fb112823680cba4538cd49198594018
parentd126e78f7728e131b0617a0318fcf6a45ad40844 (diff)
+ added basic pdstringUtils.c, bytes2wchars.c
svn path=/trunk/externals/moocow/; revision=10617
-rwxr-xr-xpdstring+wchar/configure2
-rw-r--r--pdstring+wchar/configure.ac2
-rw-r--r--pdstring+wchar/src/any2bytes.c6
-rw-r--r--pdstring+wchar/src/bytes2any-help.pd32
-rw-r--r--pdstring+wchar/src/bytes2any.c54
-rw-r--r--pdstring+wchar/src/bytes2wchars-test.pd40
-rw-r--r--pdstring+wchar/src/bytes2wchars.c171
-rw-r--r--pdstring+wchar/src/pdstring.c2
-rw-r--r--pdstring+wchar/src/pdstringUtils.c307
9 files changed, 558 insertions, 58 deletions
diff --git a/pdstring+wchar/configure b/pdstring+wchar/configure
index 3a822ed..d4be0f4 100755
--- a/pdstring+wchar/configure
+++ b/pdstring+wchar/configure
@@ -3543,7 +3543,7 @@ esac
##------- PD externals: hack EXEEXT?
PD_LIB_EXTERNALS="pdstring"
-PD_OBJ_EXTERNALS="any2bytes bytes2any"
+PD_OBJ_EXTERNALS="any2bytes bytes2any bytes2wchars"
# Check whether --enable-object-externals was given.
if test "${enable_object_externals+set}" = set; then
diff --git a/pdstring+wchar/configure.ac b/pdstring+wchar/configure.ac
index 30ca59b..cca35f6 100644
--- a/pdstring+wchar/configure.ac
+++ b/pdstring+wchar/configure.ac
@@ -88,7 +88,7 @@ esac
##------- PD externals: hack EXEEXT?
PD_LIB_EXTERNALS="pdstring"
-PD_OBJ_EXTERNALS="any2bytes bytes2any"
+PD_OBJ_EXTERNALS="any2bytes bytes2any bytes2wchars"
AC_ARG_ENABLE(object-externals,
AC_HELP_STRING([--enable-object-externals], [Whether to build single-object externals (default=no)]),
diff --git a/pdstring+wchar/src/any2bytes.c b/pdstring+wchar/src/any2bytes.c
index f765f6f..4500ee3 100644
--- a/pdstring+wchar/src/any2bytes.c
+++ b/pdstring+wchar/src/any2bytes.c
@@ -26,12 +26,14 @@
#include <string.h>
#include <m_pd.h>
-#include "mooPdUtils.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include "mooPdUtils.h"
+#include "pdstringUtils.c"
+
/* black magic */
#ifdef NT
#pragma warning( disable : 4244 )
@@ -49,7 +51,7 @@
# define A2SDEBUG(x)
#endif
-#define ANY2BYTES_DEFAULT_BUFLEN 256
+#define ANY2BYTES_DEFAULT_BUFLEN PDSTRING_DEFAULT_BUFLEN
/*=====================================================================
diff --git a/pdstring+wchar/src/bytes2any-help.pd b/pdstring+wchar/src/bytes2any-help.pd
index 0a8e138..0663ec3 100644
--- a/pdstring+wchar/src/bytes2any-help.pd
+++ b/pdstring+wchar/src/bytes2any-help.pd
@@ -1,16 +1,13 @@
-#N canvas 179 42 612 604 10;
+#N canvas 507 20 612 604 10;
#X text 13 52 INLETS:;
#X text 309 54 OUTLETS:;
#X text 325 68 1 - pd messages;
#X text 325 82 2 - bang on done;
-#X obj 24 350 string2any 128 0;
-#X obj 24 529 print string2any-help;
-#X obj 131 454 print string2any-help-done;
#X msg 24 113 97 32 98 32 99;
#X msg 39 135 97 0 98 0 99;
-#X msg 99 206 0;
-#X msg 67 206 -1;
-#X msg 131 206 32;
+#X msg 92 206 0;
+#X msg 60 206 -1;
+#X msg 124 206 32;
#X text 166 202 The second inlet sets the message-separating EOS ("end-of-string")
character. If this character is encountered in an input list \, the
list is "split" at that point \, and two separate messages are output
@@ -29,7 +26,6 @@ has been processed.;
#X text 306 580 Bryan Jurish <moocow@ling.uni-potsdam.de>;
#X text 27 83 2 - EOS character (float);
#X text 161 121 lists of floats are converted to pd messages.;
-#X msg 54 161 StringThing 97;
#X text 168 161 ... selector is ignored ...;
#X text 21 571 SEE ALSO:;
#X obj 102 570 pdstring;
@@ -37,11 +33,15 @@ has been processed.;
#X text 59 7 bytes2any : convert lists of byte values to pd messages
;
#X text 13 30 SYNTAX: bytes2any [INITIAL_BUFSIZE [EOS_CHAR]];
-#X connect 4 0 5 0;
-#X connect 4 1 6 0;
-#X connect 7 0 4 0;
-#X connect 8 0 4 0;
-#X connect 9 0 4 1;
-#X connect 10 0 4 1;
-#X connect 11 0 4 1;
-#X connect 20 0 4 0;
+#X obj 124 454 print bytes2any-help-done;
+#X obj 24 529 print bytes2any-help;
+#X msg 54 161 StringThing 97;
+#X obj 24 350 bytes2any 128 0;
+#X connect 4 0 26 0;
+#X connect 5 0 26 0;
+#X connect 6 0 26 1;
+#X connect 7 0 26 1;
+#X connect 8 0 26 1;
+#X connect 25 0 26 0;
+#X connect 26 0 24 0;
+#X connect 26 1 23 0;
diff --git a/pdstring+wchar/src/bytes2any.c b/pdstring+wchar/src/bytes2any.c
index f4cb9d1..81f28bd 100644
--- a/pdstring+wchar/src/bytes2any.c
+++ b/pdstring+wchar/src/bytes2any.c
@@ -26,12 +26,14 @@
#include <string.h>
#include <m_pd.h>
-#include "mooPdUtils.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include "mooPdUtils.h"
+#include "pdstringUtils.c"
+
/* black magic */
#ifdef NT
#pragma warning( disable : 4244 )
@@ -50,8 +52,7 @@
# define S2ADEBUG(x)
#endif
-#define BYTES2ANY_DEFAULT_BUFLEN 256
-
+#define BYTES2ANY_DEFAULT_BUFLEN PDSTRING_DEFAULT_BUFLEN
/*=====================================================================
* Constants
@@ -67,9 +68,8 @@ static t_class *bytes2any_class;
typedef struct _bytes2any
{
t_object x_obj;
- size_t x_size;
- t_float x_eos; //-- eos character
- char *x_text;
+ t_pdstring_bytes x_bytes; //-- byte buffer: {b_buf~x_text,b_len~?,b_alloc~x_size}
+ t_float x_eos; //-- eos byte value
t_binbuf *x_binbuf;
t_inlet *x_eos_in;
t_outlet *x_outlet;
@@ -86,32 +86,12 @@ typedef struct _bytes2any
*/
static void bytes2any_atoms(t_bytes2any *x, int argc, t_atom *argv)
{
- char *s;
- int x_argc, a_argc=0;
+ t_pdstring_atoms src = {argv,argc,argc};
+ pdstring_atoms2bytes(&(x->x_bytes), &src, x->x_eos);
+ pdstring_bytes2any(NULL, &(x->x_bytes), x->x_binbuf);
+ int x_argc;
t_atom *x_argv;
- /*-- allocate --*/
- if ( ((int)x->x_size) <= (argc+1) ) {
- freebytes(x->x_text, x->x_size*sizeof(char));
- x->x_size = argc+1;
- x->x_text = (char *)getbytes(x->x_size*sizeof(char));
- }
-
- /*-- get text --*/
- for (s=x->x_text; argc > 0; argc--, a_argc++, argv++, s++) {
- *s = atom_getfloat(argv);
- S2ADEBUG(post("bytes2any[%p]: a_argc=%d,*s=%d", x, a_argc, *s));
- if ((x->x_eos<0 && !*s) || (*s==x->x_eos)) { break; } /*-- hack: look for eos char --*/
- }
- *s = 0;
- S2ADEBUG(post("bytes2any[%p]: text: \"%s\", strlen=%d, argc=%d", x, x->x_text, strlen(x->x_text), a_argc));
-
- /*-- clear and fill binbuf --*/
- binbuf_clear(x->x_binbuf);
- binbuf_text(x->x_binbuf, x->x_text, a_argc); //-- handle NULs if binbuf will (but it won't) ?
- S2ADEBUG(post("bytes2any[%p]: binbuf_print: ", x));
- S2ADEBUG(binbuf_print(x->x_binbuf));
-
/*-- output --*/
x_argc = binbuf_getnatom(x->x_binbuf);
x_argv = binbuf_getvec(x->x_binbuf);
@@ -165,16 +145,16 @@ static void bytes2any_anything(t_bytes2any *x, MOO_UNUSED t_symbol *sel, int arg
static void *bytes2any_new(MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
{
t_bytes2any *x = (t_bytes2any *)pd_new(bytes2any_class);
+ int bufsize = BYTES2ANY_DEFAULT_BUFLEN;
//-- defaults
x->x_binbuf = binbuf_new();
- x->x_size = BYTES2ANY_DEFAULT_BUFLEN;
x->x_eos = -1;
//-- args: 0: bufsize
if (argc > 0) {
int initial_bufsize = atom_getintarg(0,argc,argv);
- if (initial_bufsize > 0) x->x_size = initial_bufsize;
+ if (initial_bufsize > 0) bufsize = initial_bufsize;
x->x_eos = -1; //-- backwards-compatibility hack: no default eos character if only bufsize is specified
}
//-- args: 1: separator
@@ -182,8 +162,8 @@ static void *bytes2any_new(MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
x->x_eos = atom_getfloatarg(1,argc,argv);
}
- //-- allocate
- x->x_text = (char *)getbytes(x->x_size*sizeof(char));
+ //-- allocate x_bytes
+ pdstring_bytes_init(&x->x_bytes, bufsize);
//-- inlets
x->x_eos_in = floatinlet_new(&x->x_obj, &x->x_eos);
@@ -203,10 +183,8 @@ static void *bytes2any_new(MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
*/
static void bytes2any_free(t_bytes2any *x)
{
- if (x->x_text) {
- freebytes(x->x_text, x->x_size*sizeof(char));
- x->x_text = NULL;
- }
+ if (x->x_bytes.b_buf)
+ freebytes(x->x_bytes.b_buf, x->x_bytes.b_alloc*sizeof(unsigned char));
binbuf_free(x->x_binbuf);
inlet_free(x->x_eos_in);
outlet_free(x->x_outlet_done);
diff --git a/pdstring+wchar/src/bytes2wchars-test.pd b/pdstring+wchar/src/bytes2wchars-test.pd
new file mode 100644
index 0000000..b5d3706
--- /dev/null
+++ b/pdstring+wchar/src/bytes2wchars-test.pd
@@ -0,0 +1,40 @@
+#N canvas 96 10 578 300 10;
+#X obj 114 136 bytes2wchars;
+#X obj 138 168 print wchars;
+#X obj 285 238 locale;
+#X msg 285 159 get;
+#X msg 321 215 set LC_CTYPE \$1;
+#X obj 321 194 symbol;
+#X msg 321 163 C;
+#X msg 355 163 en_US.ISO-8859-1;
+#X msg 477 163 en_US.UTF-8;
+#X obj 284 262 print locale;
+#X msg 41 41 246;
+#X floatatom 116 204 5 0 0 0 - - -;
+#X msg 41 61 195 182;
+#X text 39 23 &ouml \;;
+#X text 95 41 latin1;
+#X text 99 59 utf8;
+#X msg 159 61 197 147;
+#X text 155 23 &oelig \;;
+#X msg 159 39 339;
+#X msg 245 37 960;
+#X msg 245 59 207 128;
+#X text 245 21 &pi \;;
+#X msg 337 63 207 128 197 147 195 182 0;
+#X connect 0 0 1 0;
+#X connect 0 0 11 0;
+#X connect 2 0 9 0;
+#X connect 3 0 2 0;
+#X connect 4 0 2 0;
+#X connect 5 0 4 0;
+#X connect 6 0 5 0;
+#X connect 7 0 5 0;
+#X connect 8 0 5 0;
+#X connect 10 0 0 0;
+#X connect 12 0 0 0;
+#X connect 16 0 0 0;
+#X connect 18 0 0 0;
+#X connect 19 0 0 0;
+#X connect 20 0 0 0;
+#X connect 22 0 0 0;
diff --git a/pdstring+wchar/src/bytes2wchars.c b/pdstring+wchar/src/bytes2wchars.c
new file mode 100644
index 0000000..185a3cb
--- /dev/null
+++ b/pdstring+wchar/src/bytes2wchars.c
@@ -0,0 +1,171 @@
+/* -*- Mode: C -*- */
+/*=============================================================================*\
+ * File: bytes2wchars.c
+ * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
+ * Description: convert byte-valued atom lists to wchar_t-valued atom lists
+ *
+ * Copyright (c) 2009 Bryan Jurish.
+ *
+ * For information on usage and redistribution, and for a DISCLAIMER OF ALL
+ * WARRANTIES, see the file "COPYING", in this distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *=============================================================================*/
+
+#include <string.h>
+#include <wchar.h>
+#include <stdlib.h>
+#include <m_pd.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "mooPdUtils.h"
+#include "pdstringUtils.c"
+
+/* black magic */
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+/*--------------------------------------------------------------------
+ * DEBUG
+ *--------------------------------------------------------------------*/
+
+/*=====================================================================
+ * Constants
+ *=====================================================================*/
+static char *bytes2wchars_banner = "bytes2wchars: pdstring version " PACKAGE_VERSION " by Bryan Jurish";
+
+#define BYTES2WCHARS_DEFAULT_BUFLEN PDSTRING_DEFAULT_BUFLEN
+
+/*=====================================================================
+ * Structures and Types: any2string
+ *=====================================================================*/
+
+static t_class *bytes2wchars_class;
+
+typedef struct _bytes2wchars
+{
+ t_object x_obj;
+ t_pdstring_bytes x_bytes; //-- byte buffer
+ t_pdstring_wchars x_wchars; //-- wide character buffer
+ t_pdstring_atoms x_atoms; //-- atoms to output
+ t_outlet *x_outlet;
+} t_bytes2wchars;
+
+
+/*=====================================================================
+ * Utilities
+ *=====================================================================*/
+
+/*--------------------------------------------------------------------
+ * bytes2wchars_atoms()
+ */
+static void bytes2wchars_atoms(t_bytes2wchars *x, int argc, t_atom *argv)
+{
+ t_pdstring_atoms src = {argv,argc,0};
+
+ /*-- convert atoms -> bytes -> wchars -> atoms --*/
+ pdstring_atoms2bytes(&x->x_bytes, &src, PDSTRING_EOS_NONE);
+ pdstring_bytes2wchars(&x->x_wchars, &x->x_bytes);
+ pdstring_wchars2atoms(&x->x_atoms, &x->x_wchars); //-- TODO
+}
+
+
+/*=====================================================================
+ * Methods
+ *=====================================================================*/
+
+/*--------------------------------------------------------------------
+ * anything
+ */
+static void bytes2wchars_anything(t_bytes2wchars *x, MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
+{
+ bytes2wchars_atoms(x, argc, argv);
+
+ /*-- output --*/
+ outlet_anything(x->x_outlet, &s_list, x->x_atoms.a_len, x->x_atoms.a_buf);
+}
+
+
+/*--------------------------------------------------------------------
+ * new
+ */
+static void *bytes2wchars_new(MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
+{
+ t_bytes2wchars *x = (t_bytes2wchars *)pd_new(bytes2wchars_class);
+ int bufsize = PDSTRING_DEFAULT_BUFLEN;
+
+ //-- args: 0: bufsize
+ if (argc > 0) {
+ int initial_bufsize = atom_getintarg(0,argc,argv);
+ if (initial_bufsize > 0) bufsize = initial_bufsize;
+ }
+
+ //-- allocate
+ pdstring_bytes_init(&x->x_bytes, bufsize);
+ pdstring_wchars_init(&x->x_wchars, bufsize);
+ pdstring_atoms_init(&x->x_atoms, bufsize);
+
+ //-- outlets
+ x->x_outlet = outlet_new(&x->x_obj, &s_list);
+
+ return (void *)x;
+}
+
+/*--------------------------------------------------------------------
+ * free
+ */
+static void bytes2wchars_free(t_bytes2wchars *x)
+{
+ pdstring_bytes_clear(&x->x_bytes);
+ pdstring_wchars_clear(&x->x_wchars);
+ pdstring_atoms_clear(&x->x_atoms);
+ outlet_free(x->x_outlet);
+ return;
+}
+
+/*--------------------------------------------------------------------
+ * setup: guts
+ */
+void bytes2wchars_setup_guts(void)
+{
+ //-- class
+ bytes2wchars_class = class_new(gensym("bytes2wchars"),
+ (t_newmethod)bytes2wchars_new,
+ (t_method)bytes2wchars_free,
+ sizeof(t_bytes2wchars),
+ CLASS_DEFAULT,
+ A_GIMME, //-- initial_bufsize
+ 0);
+
+ //-- methods
+ class_addanything(bytes2wchars_class, (t_method)bytes2wchars_anything);
+
+ //-- help symbol
+ //class_sethelpsymbol(bytes2wchars_class, gensym("bytes2wchars-help.pd")); //-- breaks pd-extended help lookup
+}
+
+/*--------------------------------------------------------------------
+ * setup
+ */
+void bytes2wchars_setup(void)
+{
+ post(bytes2wchars_banner);
+ bytes2wchars_setup_guts();
+}
diff --git a/pdstring+wchar/src/pdstring.c b/pdstring+wchar/src/pdstring.c
index d68cbdd..bea27b1 100644
--- a/pdstring+wchar/src/pdstring.c
+++ b/pdstring+wchar/src/pdstring.c
@@ -57,6 +57,7 @@ typedef struct _pdstring
#ifndef PDSTRING_OBJECT_EXTERNALS
# include "any2bytes.c"
# include "bytes2any.c"
+# include "bytes2wchars.c"
#endif
/*--------------------------------------------------------------------
@@ -88,6 +89,7 @@ void pdstring_setup(void)
#ifndef PDSTRING_OBJECT_EXTERNALS
any2bytes_setup_guts();
bytes2any_setup_guts();
+ bytes2wchars_setup_guts();
#endif
pdstring_class = class_new(gensym("pdstring"),
diff --git a/pdstring+wchar/src/pdstringUtils.c b/pdstring+wchar/src/pdstringUtils.c
new file mode 100644
index 0000000..b07f760
--- /dev/null
+++ b/pdstring+wchar/src/pdstringUtils.c
@@ -0,0 +1,307 @@
+/* -*- Mode: C -*- */
+/*=============================================================================*\
+ * File: pdstringUtils.c
+ * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
+ * Description: pdstring: common utilities
+ *
+ * Copyright (c) 2009 Bryan Jurish.
+ *
+ * For information on usage and redistribution, and for a DISCLAIMER OF ALL
+ * WARRANTIES, see the file "COPYING", in this distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *=============================================================================*/
+
+#ifndef PDSTRING_UTILS_C
+#define PDSTRING_UTILS_C
+
+#include <string.h>
+#include <m_pd.h>
+#include <stdlib.h>
+#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
+ *=====================================================================*/
+
+/* PDSTRING_EOS_NONE
+ * + "safe" float value to use as x_eos if no truncation is desired
+ */
+#define PDSTRING_EOS_NONE 1e38f
+
+/* PDSTRING_DEFAULT_BUFLEN
+ * + common default buffer length
+ */
+#define PDSTRING_DEFAULT_BUFLEN 256
+
+/* PDSTRING_DEFAULT_GET
+ * + common default buffer grow length
+ */
+#define PDSTRING_DEFAULT_GET 256
+
+/* PDSTRING_BYSTES_GET
+ * + number of extra bytes to get when buffer must grow
+ */
+#define PDSTRING_BYTES_GET PDSTRING_DEFAULT_GET
+#define PDSTRING_WCHARS_GET PDSTRING_DEFAULT_GET
+#define PDSTRING_ATOMS_GET PDSTRING_DEFAULT_GET
+
+/*=====================================================================
+ * Structures & Types
+ *=====================================================================*/
+
+/* t_pdstring_bytes
+ * + a byte-string buffer
+ */
+typedef struct _pdstring_bytes {
+ unsigned char *b_buf; //-- byte-string buffer
+ int b_len; //-- current length of b_buf
+ size_t b_alloc; //-- allocated size of b_buf
+} t_pdstring_bytes;
+
+/* t_pdstring_wchars
+ * + a wide character buffer
+ */
+typedef struct _pdstring_wchars {
+ wchar_t *w_buf; //-- wide character buffer
+ int w_len; //-- current length of w_buf
+ size_t w_alloc; //-- allocated size of w_buf
+} t_pdstring_wchars;
+
+/* t_pdstring_atoms
+ * + an atom-list buffer
+ */
+typedef struct _pdstring_atoms {
+ t_atom *a_buf; //-- t_atom buffer (aka argv)
+ int a_len; //-- current length of a_buf (aka argc)
+ size_t a_alloc; //-- allocated size of a_buf
+} t_pdstring_atoms;
+
+/*=====================================================================
+ * Initialization
+ *=====================================================================*/
+
+//-- 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);
+}
+
+//-- 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);
+}
+
+//-- 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);
+}
+
+
+/*=====================================================================
+ * Utilities
+ *=====================================================================*/
+
+/*--------------------------------------------------------------------
+ * 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(t_pdstring_bytes *dst, //-- destination byte buffer
+ t_pdstring_atoms *src, //-- source t_atom float list
+ t_float x_eos) //-- EOS byte value: stop if reached (negative ~ 0)
+{
+ 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_bytes2any()
+ * + uses x_binbuf for conversion
+ * + 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()
+ */
+static void pdstring_bytes2any(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_bytes2wchars()
+ */
+static int pdstring_bytes2wchars(t_pdstring_wchars *dst, t_pdstring_bytes *src)
+{
+ /*
+ //-- get required length
+ int nwc=0, size;
+ unsigned char *s;
+ for (s=src->b_buf, size=src->b_len, nwc=0; size>0; nwc++) {
+ int csize = mblen(s,size);
+ if (csize < 0) {
+ error("pdstring_bytes2wchars(): could not compute length for byte-string \"%s\" - skipping a byte!");
+ s++;
+ continue;
+ }
+ s += csize;
+ }
+ */
+
+ //-- re-allocate?
+ if ( dst->w_alloc < src->b_len )
+ pdstring_wchars_realloc(dst, src->b_len + PDSTRING_WCHARS_GET);
+
+ //-- convert
+ size_t newlen = mbstowcs(dst->w_buf, (char*)src->b_buf, src->b_len);
+ if (newlen==((size_t)-1)) {
+ error("pdstring_bytes2wchars(): could not convert multibyte string \"%s\"", src->b_buf);
+ }
+ dst->w_len = newlen;
+
+ return (newlen < src->b_len ? newlen : newlen+1);
+}
+
+/*--------------------------------------------------------------------
+ * pdstring_wchars2atoms()
+ */
+static void pdstring_wchars2atoms(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;
+}
+
+
+#endif /* PDSTRING_UTILS_C */