aboutsummaryrefslogtreecommitdiff
path: root/cyclone/hammer/substitute.c
blob: 48a9b9d830a07d6417947da85ab5aec352aee364 (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <string.h>
#include "m_pd.h"
#include "common/grow.h"

#define SUBSTITUTE_INISIZE   32  /* LATER rethink */
#define SUBSTITUTE_MAXSIZE  256

typedef struct _substitute
{
    t_object   x_ob;
    t_pd      *x_proxy;
    t_atom     x_match;
    t_atom     x_repl;
    int        x_size;    /* as allocated */
    t_atom    *x_message;
    t_atom     x_messini[SUBSTITUTE_INISIZE];
    int        x_entered;
    t_atom     x_auxmatch;
    t_atom     x_auxrepl;
    t_outlet  *x_passout;
} t_substitute;

typedef struct _substitute_proxy
{
    t_object       p_ob;
    t_atom        *p_match;  /* pointing to parent's (aux)match */
    t_atom        *p_repl;
} t_substitute_proxy;

static t_class *substitute_class;
static t_class *substitute_proxy_class;

/* LATER rethink */
static void substitute_dooutput(t_substitute *x,
				t_symbol *s, int ac, t_atom *av, int pass)
{
    t_outlet *out = (pass ? x->x_passout : ((t_object *)x)->ob_outlet);
    if (s == &s_float)
    {
	if (ac > 1)
	    outlet_list(out, &s_list, ac, av);
	else
	    outlet_float(out, av->a_w.w_float);
    }
    else if (s == &s_bang && !ac)  /* CHECKED */
	outlet_bang(out);
    else if (s == &s_symbol && ac == 1 && av->a_type == A_SYMBOL)
	outlet_symbol(out, av->a_w.w_symbol);
    else if (s)
	outlet_anything(out, s, ac, av);
    else if (ac)
	outlet_list(out, &s_list, ac, av);
}

static int substitute_check(t_substitute *x, t_symbol *s, int ac, t_atom *av)
{
    if (x->x_repl.a_type == A_NULL)
	return (-2);
    /* see substitute_proxy_validate() for possible types and values */
    if (x->x_match.a_type == A_FLOAT)
    {
	t_float f = x->x_match.a_w.w_float;
	int i;
	for (i = 0; i < ac; i++, av++)
	    if (av->a_type == A_FLOAT && av->a_w.w_float == f)
		return (i);
    }
    else if (x->x_match.a_type == A_SYMBOL)
    {
	/* match symbol is validated -- never null */
	t_symbol *match = x->x_match.a_w.w_symbol;
	int i;
	if (s == match)
	    return (-1);
	for (i = 0; i < ac; i++, av++)
	    if (av->a_type == A_SYMBOL && av->a_w.w_symbol == match)
		return (i);
    }
    return (-2);
}

static void substitute_doit(t_substitute *x,
			    t_symbol *s, int ac, t_atom *av, int startndx)
{
    int cnt = ac - startndx;
    if (cnt > 0)
    {
	t_atom *ap = av + startndx;
	if (x->x_match.a_type == A_FLOAT)
	{
	    t_float f = x->x_match.a_w.w_float;
	    while (cnt--)
	    {
		if (ap->a_type == A_FLOAT && ap->a_w.w_float == f)
		    *ap = x->x_repl;
		ap++;
	    }
	}
	else if (x->x_match.a_type == A_SYMBOL)
	{
	    t_symbol *match = x->x_match.a_w.w_symbol;
	    while (cnt--)
	    {
		if (ap->a_type == A_SYMBOL && ap->a_w.w_symbol == match)
		    *ap = x->x_repl;
		ap++;
	    }
	}
    }
    substitute_dooutput(x, s, ac, av, 0);
}

static void substitute_anything(t_substitute *x,
				t_symbol *s, int ac, t_atom *av)
{
    int matchndx = substitute_check(x, s, ac, av);
    if (matchndx < -1)
	substitute_dooutput(x, s, ac, av, 1);
    else
    {
	int reentered = x->x_entered;
	int prealloc = !reentered;
	int ntotal = ac;
	t_atom *buf;
	t_substitute_proxy *proxy = (t_substitute_proxy *)x->x_proxy;
	x->x_entered = 1;
	proxy->p_match = &x->x_auxmatch;
	proxy->p_repl = &x->x_auxrepl;
	if (s == &s_) s = 0;
	if (matchndx == -1)
	{
	    if (x->x_repl.a_type == A_FLOAT)
	    {
		ntotal++;
		if (ac) s = &s_list;
		else s = &s_float;
	    }
	    else if (x->x_repl.a_type == A_SYMBOL)
	    {
		s = x->x_repl.a_w.w_symbol;
		matchndx = 0;
	    }
	}
	else if (matchndx == 0
		 && (!s || s == &s_list || s == &s_float)
		 && av->a_type == A_FLOAT
		 && x->x_repl.a_type == A_SYMBOL)
	{
	    s = x->x_repl.a_w.w_symbol;
	    ac--;
	    av++;
	    ntotal = ac;
	}
	if (prealloc && ac > x->x_size)
	{
	    if (ntotal > SUBSTITUTE_MAXSIZE)
		prealloc = 0;
	    else
		x->x_message = grow_nodata(&ntotal, &x->x_size, x->x_message,
					   SUBSTITUTE_INISIZE, x->x_messini,
					   sizeof(*x->x_message));
	}
	if (prealloc) buf = x->x_message;
	else
	    /* LATER consider using the stack if ntotal <= MAXSTACK */
	    buf = getbytes(ntotal * sizeof(*buf));
	if (buf)
	{
	    int ncopy = ntotal;
	    t_atom *bp = buf;
	    if (matchndx == -1)
	    {
		SETFLOAT(bp++, x->x_repl.a_w.w_float);
		ncopy--;
	    }
	    if (ncopy)
		memcpy(bp, av, ncopy * sizeof(*buf));
	    substitute_doit(x, s, ntotal, buf, matchndx);
	    if (buf != x->x_message)
		freebytes(buf, ntotal * sizeof(*buf));
	}
	if (!reentered)
	{
	    x->x_entered = 0;
	    if (x->x_auxmatch.a_type != A_NULL)
	    {
		x->x_match = x->x_auxmatch;
		x->x_auxmatch.a_type = A_NULL;
	    }
	    if (x->x_auxrepl.a_type != A_NULL)
	    {
		x->x_repl = x->x_auxrepl;
		x->x_auxrepl.a_type = A_NULL;
	    }
	    proxy->p_match = &x->x_match;
	    proxy->p_repl = &x->x_repl;
	}
    }
}

static void substitute_bang(t_substitute *x)
{
    substitute_anything(x, &s_bang, 0, 0);
}

static void substitute_float(t_substitute *x, t_float f)
{
    t_atom at;
    SETFLOAT(&at, f);
    substitute_anything(x, 0, 1, &at);
}

/* CHECKED (but LATER rethink) */
static void substitute_symbol(t_substitute *x, t_symbol *s)
{
    t_atom at;
    SETSYMBOL(&at, s);
    substitute_anything(x, &s_symbol, 1, &at);
}

/* LATER gpointer */

static void substitute_list(t_substitute *x, t_symbol *s, int ac, t_atom *av)
{
    substitute_anything(x, 0, ac, av);
}

static int substitute_atomvalidate(t_atom *ap)
{
    return (ap->a_type == A_FLOAT
	    || (ap->a_type == A_SYMBOL
		&& ap->a_w.w_symbol && ap->a_w.w_symbol != &s_));
}

/* CHECKED: 'set' is ignored, single '<atom>' does not modify a replacement */
static void substitute_proxy_anything(t_substitute_proxy *x,
				      t_symbol *s, int ac, t_atom *av)
{
    if (s == &s_) s = 0;
    if (s)
    {
	SETSYMBOL(x->p_match, s);
	if (ac && substitute_atomvalidate(av))
	    *x->p_repl = *av;
    }
    else if (ac && substitute_atomvalidate(av))
    {
	*x->p_match = *av++;
	if (ac > 1 && substitute_atomvalidate(av))
	    *x->p_repl = *av;
    }
}

static void substitute_proxy_bang(t_substitute_proxy *x)
{
    SETSYMBOL(x->p_match, &s_bang);
}

static void substitute_proxy_float(t_substitute_proxy *x, t_float f)
{
    SETFLOAT(x->p_match, f);
}

/* CHECKED (but LATER rethink) */
static void substitute_proxy_symbol(t_substitute_proxy *x, t_symbol *s)
{
    SETSYMBOL(x->p_match, &s_symbol);
    SETSYMBOL(x->p_repl, s);
}

/* LATER gpointer */

static void substitute_proxy_list(t_substitute_proxy *x,
				  t_symbol *s, int ac, t_atom *av)
{
    substitute_proxy_anything(x, 0, ac, av);
}

static void substitute_free(t_substitute *x)
{
    if (x->x_proxy) pd_free(x->x_proxy);
    if (x->x_message != x->x_messini)
	freebytes(x->x_message, x->x_size * sizeof(*x->x_message));
}

static void *substitute_new(t_symbol *s, int ac, t_atom *av)
{
    t_substitute *x = 0;
    t_substitute_proxy *proxy =
	(t_substitute_proxy *)pd_new(substitute_proxy_class);
    if (proxy)
    {
	x = (t_substitute *)pd_new(substitute_class);
	proxy->p_match = &x->x_match;
	proxy->p_repl = &x->x_repl;
	x->x_proxy = (t_pd *)proxy;
	x->x_size = SUBSTITUTE_INISIZE;
	x->x_message = x->x_messini;
	x->x_entered = 0;
	/* CHECKED: everything is to be passed unchanged, until both are set */
	/* CHECKED: max crashes if a match has been set, but not a replacement,
	   and there is a match */
	x->x_match.a_type = x->x_repl.a_type = A_NULL;
	x->x_auxmatch.a_type = x->x_auxrepl.a_type = A_NULL;
	inlet_new((t_object *)x, (t_pd *)proxy, 0, 0);
	outlet_new((t_object *)x, &s_anything);
	/* CHECKED (refman error: 'a bang is sent') */
	x->x_passout = outlet_new((t_object *)x, &s_anything);
	substitute_proxy_anything(proxy, 0, ac, av);
    }
    return (x);
}

void substitute_setup(void)
{
    substitute_class = class_new(gensym("substitute"),
			      (t_newmethod)substitute_new,
			      (t_method)substitute_free,
			      sizeof(t_substitute), 0,
			      A_GIMME, 0);
    class_addbang(substitute_class, substitute_bang);
    class_addfloat(substitute_class, substitute_float);
    class_addsymbol(substitute_class, substitute_symbol);
    class_addlist(substitute_class, substitute_list);
    class_addanything(substitute_class, substitute_anything);
    substitute_proxy_class = class_new(gensym("_substitute_proxy"), 0, 0,
				       sizeof(t_substitute_proxy),
				       CLASS_PD | CLASS_NOINLET, 0);
    class_addbang(substitute_proxy_class, substitute_proxy_bang);
    class_addfloat(substitute_proxy_class, substitute_proxy_float);
    class_addsymbol(substitute_proxy_class, substitute_proxy_symbol);
    class_addlist(substitute_proxy_class, substitute_proxy_list);
    class_addanything(substitute_proxy_class, substitute_proxy_anything);
    class_addmethod(substitute_proxy_class, (t_method)substitute_proxy_list,
		    gensym("set"), A_GIMME, 0);
}