-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol2.c
107 lines (94 loc) · 2.62 KB
/
symbol2.c
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
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/* connective objects */
#include "m_pd.h"
#include <string.h>
#include <stdio.h>
static t_class *symbol2_class;
static t_symbol* symbol2_sym;
typedef struct _symbol2
{
t_object x_obj;
t_symbol *x_s;
} t_symbol2;
static int istypeselector(t_symbol* s)
{
if(s == &s_pointer || s == &s_float || s == &s_symbol ||
s == &s_bang || s == &s_list || s == symbol2_sym)
return 1;
else
return 0;
}
static t_symbol* setassymbol(t_symbol *sel, int argc, t_atom *argv)
{
t_symbol* ret = &s_;
if (argc == 0)
{
if(! istypeselector(sel))
ret = sel;
}
else if (argc == 1)
{
if (argv->a_type == A_FLOAT)
{
char buf[MAXPDSTRING];
t_float f = atom_getfloatarg(0, argc, argv);
snprintf(buf, MAXPDSTRING, "%g", f);
ret = gensym(buf);
}
else
ret = atom_getsymbolarg(0, argc, argv);
}
else
{
int bufsize;
char *buf;
t_atom a;
t_binbuf *bb = binbuf_new();
/* if starts with a float, leave out the 'list' selector */
if(! istypeselector(sel))
{
SETSYMBOL(&a, sel);
binbuf_add(bb, 1, &a);
}
binbuf_add(bb, argc, argv);
binbuf_gettext(bb, &buf, &bufsize);
buf = resizebytes(buf, bufsize, bufsize+1);
buf[bufsize] = 0;
ret = gensym(buf);
freebytes(buf, bufsize+1);
binbuf_free(bb);
}
return ret;
}
static void *symbol2_new(t_symbol *sel, int argc, t_atom *argv)
{
t_symbol2 *x = (t_symbol2 *)pd_new(symbol2_class);
outlet_new(&x->x_obj, &s_symbol);
symbolinlet_new(&x->x_obj, &x->x_s);
x->x_s = setassymbol(sel, argc, argv);
return (x);
}
static void symbol2_bang(t_symbol2 *x)
{
outlet_symbol(x->x_obj.ob_outlet, x->x_s);
}
static void symbol2_symbol(t_symbol2 *x, t_symbol *s)
{
outlet_symbol(x->x_obj.ob_outlet, x->x_s = s);
}
static void symbol2_anything(t_symbol2 *x, t_symbol *s, int argc, t_atom *argv)
{
x->x_s = setassymbol(s, argc, argv);
outlet_symbol(x->x_obj.ob_outlet, x->x_s);
}
void symbol2_setup(void)
{
symbol2_sym = gensym("symbol2");
symbol2_class = class_new(symbol2_sym, (t_newmethod)symbol2_new, 0,
sizeof(t_symbol2), 0, A_GIMME, 0);
class_addbang(symbol2_class, symbol2_bang);
class_addsymbol(symbol2_class, symbol2_symbol);
class_addanything(symbol2_class, symbol2_anything);
}