aboutsummaryrefslogtreecommitdiff
path: root/src/unpack.c
blob: 33f2414e7f8f0d3abb9bba68e7c187afb2755204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* 
 * unpack: a type-agnostic version of [unpack]
 *
 * (c) 2007-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
 *
 * 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, see <http://www.gnu.org/licenses/>.
/*

/*
 * this version of [unpack] does not care about types, so the arguments are merely there for documentation reasons
 * (and to determine the number of outlets)
 * e.g. sending [symbol foo( to [zexy/unpack 0] will output [symbol foo(
 *
 * for know this object is named [zexy/unpack], as there might be some issues with compatibility with the original [unpack]
 */


#include "zexy.h"

/* ------------------------- zunpack ------------------------------- */

/* like pack, but does no type-checking */

static t_class *zunpack_class;

typedef struct _zunpack
{
  t_object x_obj;
  t_outlet**x_out;
  t_int x_numouts;
} t_zunpack;

static void zunpack_any(t_zunpack *x, t_symbol *s, int argc, t_atom *argv)
{
  int offset=(s!=NULL)?1:0;
  int count=((argc+offset)<(x->x_numouts))?(argc+offset):x->x_numouts;

  while(count-->offset) {
    outlet_list(x->x_out[count], gensym("list"), 1, argv+count-offset);
  }

  if(s!=NULL) {
    outlet_symbol(x->x_out[0], s);
  }
}

static void zunpack_list(t_zunpack *x, t_symbol *s, int argc, t_atom *argv)
{
  zunpack_any(x, 0, argc, argv);
}

static void zunpack_bang(t_zunpack *x)
{
  outlet_bang(x->x_out[0]);
}

static void zunpack_free(t_zunpack *x)
{
  int i=0;
  for(i=0; i<x->x_numouts; i++) {
    outlet_free(x->x_out[i]);
  }
  freebytes(x->x_out, x->x_numouts*sizeof(t_outlet*));

  x->x_numouts=0;
  x->x_out=0;  
}

static void *zunpack_new(t_symbol*s, int argc, t_atom*argv)
{
  t_zunpack *x = (t_zunpack *)pd_new(zunpack_class);
  int count=(argc>0)?argc:2;
  int i=0;
  
  x->x_numouts=count;
  x->x_out=(t_outlet**)getbytes(count*sizeof(t_outlet*));

  for(i=0; i<count; i++) {
    x->x_out[i]  =outlet_new(&x->x_obj, 0);
  } 

  return (x);
}

void zunpack_setup(void)
{
  
  zunpack_class = class_new(gensym("zexy/unpack"), 
                            (t_newmethod)zunpack_new, (t_method)zunpack_free, sizeof(t_zunpack), 
                            0,  A_GIMME, 0);
#if 0
  /* oops Pd-0.42 allows us to override built-ins
   * this is bad as long as the 2 objects are not compatible */
  class_addcreator((t_newmethod)zunpack_new, gensym("unpack"), A_GIMME, 0);
#endif

  class_addbang(zunpack_class, zunpack_bang);
  class_addlist(zunpack_class, zunpack_list);
  class_addanything(zunpack_class, zunpack_any);

  zexy_register("unpack");
}

void unpack_setup(void)
{
  zunpack_setup();
}