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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#ifndef TOF
#define TOF
#include "m_pd.h"
#include "g_canvas.h"
#include <string.h>
#define IS_A_SYMBOL(atom,index) ((atom+index)->a_type == A_SYMBOL)
#define IS_A_FLOAT(atom,index) ((atom+index)->a_type == A_FLOAT)
#endif
char tof_buf_temp_a[MAXPDSTRING];
char tof_buf_temp_b[MAXPDSTRING];
/*
* ALLOCATE WITH THE FOLLOWING LINE
* t_atom *at = getbytes((SIZE)*sizeof(*at));
* FREE WITH THIS ONE
* freebytes(at, (SIZE)*sizeof(*at));
*/
static void tof_copy_atoms(t_atom *src, t_atom *dst, int n)
{
while(n--)
*dst++ = *src++;
}
static t_symbol* tof_get_dollar(t_canvas* canvas, t_symbol* dollar) {
return canvas_realizedollar(canvas, dollar);
}
static t_symbol* tof_get_dollarzero(t_canvas* canvas) {
return canvas_realizedollar(canvas, gensym("$0"));
}
static int tof_canvas_is_not_subpatch(t_canvas* canvas) {
return canvas_isabstraction(canvas);
}
static t_canvas* tof_get_canvas(void)
{
return glist_getcanvas((t_glist *)canvas_getcurrent());
}
static t_symbol* tof_get_dir(t_canvas* canvas) {
return canvas_getdir(canvas);
}
static t_symbol* tof_get_canvas_name(t_canvas* canvas) {
return canvas->gl_name;
}
// Use &s_list selector if you do not want to process the selector
static int tof_anything_to_string( t_symbol* s, int ac, t_atom* av,char* buffer ) {
if ( s == &s_bang) {
buffer[0] = '\0';
return 0;
} else if ( s == &s_symbol) {
if ( ac ) {
strcpy(buffer,atom_getsymbol(av)->s_name);
} else {
buffer[0] = '\0';
}
return strlen(buffer);
} else {
char* buf;
t_binbuf* binbuf = binbuf_new();
if ( !(s == &s_list || s == &s_float ) ) {
t_atom selector;
SETSYMBOL(&selector,s);
binbuf_add(binbuf, 1, &selector);
}
binbuf_add(binbuf, ac, av);
int length;
binbuf_gettext(binbuf, &buf, &length);
int i;
for (i = 0; i < length; i++ ) {
buffer[i] = buf[i];
}
buffer[length] = '\0';
freebytes(buf, length);
binbuf_free(binbuf);
return length;
}
buffer[0] = '\0';
return 0;
}
// returns the last canvas before the root canvas
static t_canvas* tof_get_canvas_before_root(t_canvas* canvas)
{
// Find the proper parent canvas
while ( canvas->gl_owner && canvas->gl_owner->gl_owner) {
canvas = canvas->gl_owner;
}
return canvas;
}
static t_canvas* tof_get_root_canvas(t_canvas* canvas)
{
// Find the root canvas
while ( canvas->gl_owner) {
canvas = canvas->gl_owner;
}
return canvas;
}
static void tof_get_canvas_arguments(t_canvas *canvas, int *ac_p, t_atom **av_p) {
canvas_setcurrent(canvas);
canvas_getargs(ac_p , av_p);
canvas_unsetcurrent(canvas);
}
// set selector
static void tof_set_selector(t_symbol** selector_sym_p,int* ac_p, t_atom** av_p ) {
if(!(*ac_p)) {
*selector_sym_p = &s_bang;
} else if(IS_A_SYMBOL(*av_p, 0)) {
*selector_sym_p = atom_getsymbol(*av_p);
*ac_p = (*ac_p)-1,
*av_p = (*av_p)+1;
} else{
*selector_sym_p = &s_list;
}
}
/*
static int tof_get_tagged_argument(char tag, int ac, t_atom *av, int *start, int *count) {
int i;
if ( ac == 0 || *start >= ac) {
*count = 0;
return 0;
}
for ( i= *start + 1; i < ac; i++ ) {
if ( (av+i)->a_type == A_SYMBOL
&& (atom_getsymbol(av+i))->s_name[0] == tag) break;
}
*count = i - *start;
return (i-*start);
}
*/
// Returned value permits a while operation on the function
static int tof_next_tagged_argument(char tag, int ac, t_atom *av, int* ac_a, t_atom ** av_a, int* iter) {
int i;
if ( ac == 0 || *iter >= ac) {
*ac_a = 0;
*av_a = NULL;
return 0;
}
for ( i= *iter + 1; i < ac; i++ ) {
if ( (av+i)->a_type == A_SYMBOL
&& (atom_getsymbol(av+i))->s_name[0] == tag) break;
}
*ac_a = i - *iter;
*av_a = av + *iter;
*iter = i;
return (*ac_a);
}
static int tof_find_tag(char tag, t_symbol* name, int ac, t_atom* av) {
int i;
for (i=0; i<ac;i++) {
if ( IS_A_SYMBOL(av,i) && name == atom_getsymbol(av+i) ) {
return 1;
}
}
return 0;
}
// Returns 1 if the tag was found
// Fills the given int* and t_atom** with the arguments of the tag
static int tof_find_tagged_argument(char tag,t_symbol *name, int ac, t_atom *av, int *ac_r,t_atom** av_r) {
int i;
int match = 0;
int j = 0;
for (i=0;i<ac;i++) {
if ( IS_A_SYMBOL(av,i) && name == atom_getsymbol(av+i) ) {
// FOUND MATCH
match = 1;
if ( (i+1)<ac ) { // IS THERE SPACE FOR AT LEAST ONE ARGUMENT?
i=i+1;
for (j=i;j<ac;j++) { //FIND ITS END
if ( IS_A_SYMBOL(av,j) && (atom_getsymbol(av+j))->s_name[0] == tag ) {
//j = j-1;
break;
}
}
break;
}
}
}
j = j-i;
//post("i:%d j:%d",i,j);
if ( j > 0) {
*ac_r = j;
*av_r = av+i;
} else {
*ac_r = 0;
*av_r = NULL;
}
return match;
}
static void tof_outlet_anything_prepend(t_outlet* outlet, t_symbol* s, int argc, t_atom* argv, t_symbol* pp) {
if (s == &s_list || s == &s_float || s == &s_symbol) {
outlet_anything(outlet,pp,argc,argv);
} else if (s != &s_bang) {
int ac = argc + 1;
t_atom *av = (t_atom *)getbytes(ac*sizeof(t_atom));
tof_copy_atoms(argv,av+1,argc);
SETSYMBOL(av, s);
outlet_anything(outlet,pp,ac,av);
freebytes(av, ac*sizeof(t_atom));
} else {
outlet_bang(outlet);
}
}
static void tof_send_anything_prepend(t_symbol* target,t_symbol* s, int ac, t_atom* av,t_symbol* prepend) {
if (target->s_thing) {
if ( s == &s_list || s == &s_float || s == &s_symbol ) {
typedmess(target->s_thing, prepend, ac, av);
} else {
int new_ac = ac + 1;
t_atom *new_av = getbytes(new_ac*sizeof(*new_av));
tof_copy_atoms(av,new_av+1,ac);
SETSYMBOL(new_av, s);
typedmess(target->s_thing, prepend, new_ac, new_av);
freebytes(new_av, new_ac*sizeof(*new_av));
}
}
}
static t_symbol* tof_remove_extension(t_symbol* s) {
t_symbol* newsymbol = s;
int length = strlen(s->s_name) + 1;
char* newstring = getbytes(length * sizeof(*newstring));
strcpy(newstring,s->s_name);
char* last = strrchr( newstring, '.');
if (last != NULL) {
*last = '\0';
newsymbol = gensym(newstring);
}
freebytes(newstring,length * sizeof(*newstring));
return newsymbol;
}
|