aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Jurish <mukau@users.sourceforge.net>2009-01-26 21:29:04 +0000
committerBryan Jurish <mukau@users.sourceforge.net>2009-01-26 21:29:04 +0000
commit561c134e4ab3b0c746b79b26d305ae0c19e1ed4c (patch)
treef46906ad3c2fbdaba0a467e043be1dda30af69c6
parentbb3ac3fc500294135ddee8846a177c301e065116 (diff)
+ played with raw byte-level access to g_array: probably a bad idea
svn path=/trunk/externals/moocow/pdstring/; revision=10655
-rw-r--r--src/array2rawbytes.c187
-rw-r--r--src/pdstring.c6
-rw-r--r--src/raw-arraytest.pd67
-rw-r--r--src/rawbytes2array.c169
4 files changed, 429 insertions, 0 deletions
diff --git a/src/array2rawbytes.c b/src/array2rawbytes.c
new file mode 100644
index 0000000..ecd6de8
--- /dev/null
+++ b/src/array2rawbytes.c
@@ -0,0 +1,187 @@
+/* -*- Mode: C -*- */
+/*=============================================================================*\
+ * File: array2rawbytes.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.h"
+
+/* black magic */
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+/*--------------------------------------------------------------------
+ * DEBUG
+ *--------------------------------------------------------------------*/
+
+/*=====================================================================
+ * Constants
+ *=====================================================================*/
+static char *array2rawbytes_banner = "array2rawbytes: pdstring version " PACKAGE_VERSION " by Bryan Jurish";
+
+#define ARRAY2RAWBYTES_DEFAULT_BUFLEN PDSTRING_DEFAULT_BUFLEN
+
+/*=====================================================================
+ * Structures and Types: any2string
+ *=====================================================================*/
+
+static t_class *array2rawbytes_class;
+
+typedef struct _array2rawbytes
+{
+ t_object x_obj;
+ t_symbol *x_name; //-- name of source array
+ t_pdstring_bytes x_bytes; //-- byte "buffer", really just aliases array t_float*
+ t_pdstring_atoms x_atoms; //-- output atom buffer
+ t_outlet *x_outlet;
+} t_array2rawbytes;
+
+
+/*=====================================================================
+ * Methods
+ *=====================================================================*/
+
+/*--------------------------------------------------------------------
+ * get OFFSET LENGTH
+ */
+static void array2rawbytes_get(t_array2rawbytes *x, t_float offset_f, t_float len_f)
+{
+ int offset=offset_f, len=len_f, fvecsize, cvecsize;
+ t_garray *a;
+ t_float *fvec;
+ unsigned char *cvec;
+
+ //-- sanity check(s)
+ if (!(a = (t_garray *)pd_findbyclass(x->x_name, garray_class))) {
+ pd_error(x, "array2rawbytes_get: no such array '%s'", x->x_name->s_name);
+ return;
+ }
+
+ //-- get float* from array
+ if (!garray_getfloatarray(a, &fvecsize, &fvec))
+ pd_error(x,"array2rawbytes: bad template for write to array '%s'", x->x_name->s_name);
+
+ //-- get character offset & len
+ cvec = (unsigned char*)fvec;
+ cvecsize = fvecsize*sizeof(t_float)/sizeof(unsigned char);
+ post("array2rawbytes[x=%p]: char data: cvecsize=%d", x, cvecsize);
+
+ //-- tweak out-of-bounds values
+ post("array2rawbytes[x=%p]: pre-tweak: offset=%d, len=%d", x, offset, len);
+
+ offset %= cvecsize;
+ if (len <= 0 || offset+len >= cvecsize)
+ len = cvecsize - offset;
+
+ post("array2rawbytes[x=%p]: post-tweak: offset=%d, len=%d", x, offset, len);
+
+ //-- munge x_bytes
+ x->x_bytes.b_buf = cvec+offset;
+ x->x_bytes.b_len = len;
+
+ //-- convert bytes -> atoms
+ pdstring_bytes2atoms(x, &x->x_atoms, &x->x_bytes, PDSTRING_EOS_NONE);
+
+ //-- output
+ outlet_list(x->x_outlet, &s_list, x->x_atoms.a_len, x->x_atoms.a_buf);
+}
+
+
+/*--------------------------------------------------------------------
+ * new
+ */
+static void *array2rawbytes_new(t_symbol *arrayname)
+{
+ t_array2rawbytes *x = (t_array2rawbytes *)pd_new(array2rawbytes_class);
+
+ //-- init
+ x->x_name = arrayname;
+ pdstring_bytes_init(&x->x_bytes, 0); //-- gets clobbered by array data
+ pdstring_atoms_init(&x->x_atoms, PDSTRING_DEFAULT_BUFLEN);
+
+ //-- outlets
+ x->x_outlet = outlet_new(&x->x_obj, &s_list);
+
+ return (void *)x;
+}
+
+/*--------------------------------------------------------------------
+ * set ARRAYNAME
+ */
+static void array2rawbytes_set(t_array2rawbytes *x, t_symbol *arrayname)
+{
+ x->x_name = arrayname;
+}
+
+/*--------------------------------------------------------------------
+ * free
+ */
+static void array2rawbytes_free(t_array2rawbytes *x)
+{
+ pdstring_atoms_clear(&x->x_atoms);
+ outlet_free(x->x_outlet);
+ return;
+}
+
+/*--------------------------------------------------------------------
+ * setup: guts
+ */
+void array2rawbytes_setup_guts(void)
+{
+ //-- class
+ array2rawbytes_class = class_new(gensym("array2rawbytes"),
+ (t_newmethod)array2rawbytes_new,
+ (t_method)array2rawbytes_free,
+ sizeof(t_array2rawbytes),
+ CLASS_DEFAULT,
+ A_DEFSYM, //-- arrayname
+ 0);
+
+ //-- methods
+ class_addmethod(array2rawbytes_class, (t_method)array2rawbytes_get, gensym("get"), A_DEFFLOAT, A_DEFFLOAT, 0);
+ class_addmethod(array2rawbytes_class, (t_method)array2rawbytes_set, gensym("set"), A_DEFSYM, 0);
+
+ //-- help symbol
+ //class_sethelpsymbol(array2rawbytes_class, gensym("array2rawbytes-help.pd")); //-- breaks pd-extended help lookup
+}
+
+/*--------------------------------------------------------------------
+ * setup
+ */
+void array2rawbytes_setup(void)
+{
+ post(array2rawbytes_banner);
+ array2rawbytes_setup_guts();
+}
diff --git a/src/pdstring.c b/src/pdstring.c
index 907d16d..54e9f88 100644
--- a/src/pdstring.c
+++ b/src/pdstring.c
@@ -57,8 +57,12 @@ typedef struct _pdstring
#ifndef PDSTRING_OBJECT_EXTERNALS
# include "any2bytes.c"
# include "bytes2any.c"
+
# include "bytes2wchars.c"
# include "wchars2bytes.c"
+
+//# include "bytes2array.c"
+//# include "array2bytes.c"
#endif
/*--------------------------------------------------------------------
@@ -92,6 +96,8 @@ void pdstring_setup(void)
bytes2any_setup_guts();
bytes2wchars_setup_guts();
wchars2bytes_setup_guts();
+ //bytes2array_setup_guts();
+ //array2bytes_setup_guts();
#endif
pdstring_class = class_new(gensym("pdstring"),
diff --git a/src/raw-arraytest.pd b/src/raw-arraytest.pd
new file mode 100644
index 0000000..e0bfffe
--- /dev/null
+++ b/src/raw-arraytest.pd
@@ -0,0 +1,67 @@
+#N canvas 424 14 776 478 10;
+#N canvas 0 0 450 300 (subpatch) 0;
+#X array array1 4 float 3;
+#A 0 0 66 67 68;
+#X coords 0 255 4 0 200 140 1;
+#X restore 40 24 graph;
+#X msg 35 187 const 0;
+#X obj 100 242 s array1;
+#X msg 111 187 list 0 0 1 2 3;
+#X obj 262 186 any2bytes;
+#X msg 263 161 foo;
+#X msg 293 161 bar;
+#X msg 323 161 cat;
+#X msg 355 159 dog;
+#X obj 262 210 list prepend 0;
+#X msg 431 145 foo;
+#X msg 461 145 bar;
+#X msg 491 145 cat;
+#X msg 523 143 dog;
+#X obj 430 194 list prepend 0;
+#X obj 430 220 bytes2array array1;
+#X msg 385 159 data;
+#X msg 555 143 data;
+#X obj 274 302 array2bytes array1;
+#X msg 274 278 get 0 -1;
+#X obj 224 330 print array2bytes;
+#X obj 558 196 print any2bytes;
+#X obj 358 402 print bytes2any;
+#X msg 411 89 this_is_a_test!!;
+#X obj 52 333 arraysize array1;
+#X floatatom 52 357 5 0 0 0 - - -;
+#X msg 52 306 bang;
+#X obj 358 376 bytes2any -1 0;
+#X obj 430 170 any2bytes -1 0;
+#X msg 418 281 bang;
+#X obj 418 303 tabdump array1;
+#X obj 454 333 print tabdump;
+#X obj 612 383 tabset array1;
+#X msg 613 350 65 66 67 115;
+#X connect 1 0 2 0;
+#X connect 3 0 2 0;
+#X connect 4 0 9 0;
+#X connect 5 0 4 0;
+#X connect 6 0 4 0;
+#X connect 7 0 4 0;
+#X connect 8 0 4 0;
+#X connect 9 0 2 0;
+#X connect 10 0 28 0;
+#X connect 11 0 28 0;
+#X connect 12 0 28 0;
+#X connect 13 0 28 0;
+#X connect 14 0 15 0;
+#X connect 16 0 4 0;
+#X connect 17 0 28 0;
+#X connect 18 0 20 0;
+#X connect 18 0 27 0;
+#X connect 19 0 18 0;
+#X connect 23 0 28 0;
+#X connect 24 0 25 0;
+#X connect 26 0 24 0;
+#X connect 27 0 22 0;
+#X connect 28 0 21 0;
+#X connect 28 0 14 0;
+#X connect 29 0 30 0;
+#X connect 30 0 31 0;
+#X connect 30 0 27 0;
+#X connect 33 0 32 0;
diff --git a/src/rawbytes2array.c b/src/rawbytes2array.c
new file mode 100644
index 0000000..fefd82f
--- /dev/null
+++ b/src/rawbytes2array.c
@@ -0,0 +1,169 @@
+/* -*- Mode: C -*- */
+/*=============================================================================*\
+ * File: rawbytes2array.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.h"
+
+/* black magic */
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+/*--------------------------------------------------------------------
+ * DEBUG
+ *--------------------------------------------------------------------*/
+
+/*=====================================================================
+ * Constants
+ *=====================================================================*/
+static char *rawbytes2array_banner = "rawbytes2array: pdstring version " PACKAGE_VERSION " by Bryan Jurish";
+
+#define RAWBYTES2ARRAY_DEFAULT_BUFLEN PDSTRING_DEFAULT_BUFLEN
+
+/*=====================================================================
+ * Structures and Types: any2string
+ *=====================================================================*/
+
+static t_class *rawbytes2array_class;
+
+typedef struct _rawbytes2array
+{
+ t_object x_obj;
+ t_symbol *x_name; //-- name of output array
+ //t_pdstring_bytes x_bytes; //-- byte buffer
+ //t_outlet *x_outlet;
+} t_rawbytes2array;
+
+
+/*=====================================================================
+ * Methods
+ *=====================================================================*/
+
+/*--------------------------------------------------------------------
+ * anything
+ */
+static void rawbytes2array_anything(t_rawbytes2array *x, MOO_UNUSED t_symbol *sel, int argc, t_atom *argv)
+{
+ int i0,fvecsize;
+ t_garray *a;
+ t_float *fvec;
+ unsigned char *cvec;
+
+ //-- sanity check(s)
+ if (!(a = (t_garray *)pd_findbyclass(x->x_name, garray_class))) {
+ pd_error(x, "rawbytes2array: no such array '%s'", x->x_name->s_name);
+ return;
+ }
+
+ //-- prepare
+ i0 = atom_getfloat(argv);
+ argc--;
+ argv++;
+
+ //-- resize?
+ //garray_resize(a, argc);
+
+ //-- get pointer
+ if (!garray_getfloatarray(a, &fvecsize, &fvec))
+ pd_error(x,"rawbytes2array: bad template for write to array '%s'", x->x_name->s_name);
+
+ //-- write raw bytes
+ for (cvec=(unsigned char*)(fvec+i0); argc > 0 && cvec < (unsigned char*)(fvec+fvecsize); cvec++,argv++,argc--)
+ {
+ *cvec = atom_getfloat(argv);
+ }
+}
+
+
+/*--------------------------------------------------------------------
+ * new
+ */
+static void *rawbytes2array_new(t_symbol *arrayname)
+{
+ t_rawbytes2array *x = (t_rawbytes2array *)pd_new(rawbytes2array_class);
+
+ //-- init
+ x->x_name = arrayname;
+
+ return (void *)x;
+}
+
+/*--------------------------------------------------------------------
+ * set ARRAYNAME
+ */
+static void rawbytes2array_set(t_rawbytes2array *x, t_symbol *arrayname)
+{
+ x->x_name = arrayname;
+}
+
+/*--------------------------------------------------------------------
+ * free
+ */
+static void rawbytes2array_free(t_rawbytes2array *x)
+{
+ return;
+}
+
+/*--------------------------------------------------------------------
+ * setup: guts
+ */
+void rawbytes2array_setup_guts(void)
+{
+ //-- class
+ rawbytes2array_class = class_new(gensym("rawbytes2array"),
+ (t_newmethod)rawbytes2array_new,
+ (t_method)rawbytes2array_free,
+ sizeof(t_rawbytes2array),
+ CLASS_DEFAULT,
+ A_DEFSYM, //-- arrayname
+ 0);
+
+ //-- methods
+ class_addanything(rawbytes2array_class, (t_method)rawbytes2array_anything);
+ class_addmethod(rawbytes2array_class, (t_method)rawbytes2array_set, gensym("set"), A_DEFSYM, 0);
+
+ //-- help symbol
+ //class_sethelpsymbol(rawbytes2array_class, gensym("rawbytes2array-help.pd")); //-- breaks pd-extended help lookup
+}
+
+/*--------------------------------------------------------------------
+ * setup
+ */
+void rawbytes2array_setup(void)
+{
+ post(rawbytes2array_banner);
+ rawbytes2array_setup_guts();
+}