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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/******************************************************
*
* zexy - implementation file
*
* copyleft (c) IOhannes m zmölnig
*
* 1999:forum::für::umläute:2004
*
* institute of electronic music and acoustics (iem)
*
******************************************************
*
* license: GNU General Public License v.2
*
******************************************************/
#include "zexy.h"
#include <stdlib.h>
#include <string.h>
/*
* list2symbol: convert a list into a single symbol
*/
/* ------------------------- list2symbol ------------------------------- */
static t_class *list2symbol_class;
typedef struct _list2symbol
{
t_object x_obj;
int ac;
t_atom *ap;
t_symbol *s,*connector;
} t_list2symbol;
static void list2symbol_connector(t_list2symbol *x, t_symbol *s){
x->connector = s;
}
static void list2symbol_bang(t_list2symbol *x)
{
t_atom *argv=x->ap;
int argc=x->ac;
char *result = 0;
int length = 0, len=0;
int i= argc;
char *connector=0;
char connlen=0;
if(x->connector)connector=x->connector->s_name;
if(connector)connlen=strlen(connector);
/* 1st get the length of the symbol */
if(x->s)length+=strlen(x->s->s_name);
else length-=connlen;
length+=i*connlen;
while(i--){
char buffer[MAXPDSTRING];
int len=0;
if(A_SYMBOL==argv->a_type){
len=strlen(argv->a_w.w_symbol->s_name);
} else {
atom_string(argv, buffer, MAXPDSTRING);
len=strlen(buffer);
}
length+=len;
argv++;
}
if (length<=0){
outlet_symbol(x->x_obj.ob_outlet, gensym(""));
return;
}
result = (char*)getbytes((length+1)*sizeof(char));
/* 2nd create the symbol */
if (x->s){
char *buf = x->s->s_name;
strcpy(result+len, buf);
len+=strlen(buf);
if(i && connector){
strcpy(result+len, connector);
len += connlen;
}
}
i=argc;
argv=x->ap;
while(i--){
if(A_SYMBOL==argv->a_type){
strcpy(result+len, argv->a_w.w_symbol->s_name);
len+= strlen(argv->a_w.w_symbol->s_name);
} else {
char buffer[MAXPDSTRING];
atom_string(argv, buffer, MAXPDSTRING);
strcpy(result+len, buffer);
len += strlen(buffer);
}
argv++;
if(i && connector){
strcpy(result+len, connector);
len += connlen;
}
}
result[length]=0;
outlet_symbol(x->x_obj.ob_outlet, gensym(result));
freebytes(result, (length+1)*sizeof(char));
}
static void list2symbol_anything(t_list2symbol *x, t_symbol *s, int argc, t_atom *argv)
{
x->s =s;
x->ac=argc;
x->ap=argv;
list2symbol_bang(x);
}
static void list2symbol_list(t_list2symbol *x, t_symbol *s, int argc, t_atom *argv)
{
list2symbol_anything(x, 0, argc, argv);
}
static void *list2symbol_new(t_symbol *s, int argc, t_atom *argv)
{
t_list2symbol *x = (t_list2symbol *)pd_new(list2symbol_class);
outlet_new(&x->x_obj, 0);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym(""));
x->connector = gensym(" ");
list2symbol_anything(x, 0, argc, argv);
return (x);
}
static void list2symbol_free(t_list2symbol *x)
{}
void list2symbol_setup(void)
{
list2symbol_class = class_new(gensym("list2symbol"), (t_newmethod)list2symbol_new,
(t_method)list2symbol_free, sizeof(t_list2symbol), 0, A_GIMME, 0);
class_addcreator((t_newmethod)list2symbol_new, gensym("l2s"), A_GIMME, 0);
class_addbang (list2symbol_class, list2symbol_bang);
class_addlist (list2symbol_class, list2symbol_list);
class_addanything(list2symbol_class, list2symbol_anything);
class_addmethod (list2symbol_class, (t_method)list2symbol_connector, gensym(""), A_SYMBOL, 0);
class_sethelpsymbol(list2symbol_class, gensym("zexy/list2symbol"));
zexy_register("list2symbol");
}
void l2s_setup(void)
{
list2symbol_setup();
}
void z_list2symbol_setup(void)
{
list2symbol_setup();
}
|