aboutsummaryrefslogtreecommitdiff
path: root/slist.c
blob: 6af28c69fb96183e0fa758aae4837764d243b584 (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
/*
Copyright (C) 2003 Antoine Rousseau 

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  

*/

/* a shared symbol list, ala "value" .*/

#include <m_pd.h>
#include <stdlib.h>
#include <string.h>

static t_class *slist_class, *scommon_class;

typedef struct _sitem t_sitem;

struct _sitem
{
	t_sitem *next;
	t_symbol *s;
};

typedef struct scommon
{
    t_pd c_pd;
    t_symbol *c_sym;
    int c_refcount;
	t_sitem *first;
} t_scommon;

typedef struct _slist
{
    t_object x_obj;
    t_symbol *x_sym;
    t_scommon *x_c;
	t_outlet *x_symout;
	t_outlet *x_lenout;
} t_slist;


static void sitem_delete(t_sitem **x)
{
	t_sitem *next=(*x)->next;
	
	//freebytes((*x)->name,strlen((*x)->name)+1);
	freebytes(*x,sizeof(t_sitem));
	(*x)=next;
}

static void sitem_add(t_sitem **x,t_symbol *s)
{
	t_int l;
	t_sitem *newone=getbytes(sizeof(t_sitem));
	
	//newone->name=getbytes(l=(strlen(s->s_name)+1));
	//strncpy(newone->name,s->s_name,l);
	newone->s=s;
	newone->next=0;
	
	while(*x) x=&((*x)->next);
	
	*x=newone;
}

static void *scommon_new(t_symbol *s)
{
    t_scommon *x = (t_scommon *)pd_new(scommon_class);

	x->c_refcount = 0;
	x->c_sym=s;
	pd_bind((t_pd*)x, s);
	
    x->first=0;

    return (x);
}

static int scommon_find(t_scommon *x, t_symbol *s)
{
	t_sitem *si=x->first;
	t_int i=1;
	
	while(si) {
		if(!strcmp(si->s->s_name,s->s_name)) return i; 
		si=si->next;
		i++;
	}
	return 0;
}
 
static void scommon_add(t_scommon *x, t_symbol *s)
{
	sitem_add(&x->first,s);
}

static void scommon_reset(t_scommon *x)
{
 	while(x->first) sitem_delete(&x->first);
}

static void scommon_ff(t_scommon *x)
{
 	scommon_reset(x);
	pd_unbind((t_pd*)x, x->c_sym);
}


    /* get a pointer to a named symbol list (a "scommon" object), 
	which is created if necessary. */
t_scommon *slist_get(t_symbol *s)
{
    t_scommon *c = (t_scommon *)pd_findbyclass(s, scommon_class);

    if (!c) c = (t_scommon *)scommon_new(s);
    c->c_refcount++;
    return (c);
}

    /* release a variable.  This only frees the "scommon" resource when the
    last interested party releases it. */
void slist_release(t_scommon *c)
{
	if (!--c->c_refcount) scommon_ff(c);
}

 
static void *slist_new(t_symbol *s)
{
    t_slist *x = (t_slist *)pd_new(slist_class);
    x->x_sym = s;
    x->x_c = slist_get(s);
    outlet_new(&x->x_obj, &s_float);
    x->x_symout=outlet_new(&x->x_obj, &s_symbol);
    x->x_lenout=outlet_new(&x->x_obj, &s_float);
    return (x);
}

static void slist_ff(t_slist *x)
{
    slist_release(x->x_c);
}

static void slist_print(t_slist *x)
{
	t_sitem *t=x->x_c->first;
	int i=0;
	
	while(t){
		post("item %d: %s",++i,t->s->s_name);
		t=t->next;
	}
}

static void slist_reset(t_slist *x)
{
	scommon_reset(x->x_c);
}

static void slist_add(t_slist *x,t_symbol *s)
{
	scommon_add(x->x_c,s);
}

static void slist_find(t_slist *x,t_symbol *s)
{
	outlet_float(x->x_obj.ob_outlet,scommon_find(x->x_c,s));
}

static void slist_setlist(t_slist *x,t_symbol *s)
{
	slist_release(x->x_c);
	x->x_c = slist_get(s);
}

static void slist_float(t_slist *x, t_float f)
{
	t_sitem *t=x->x_c->first;
	int i=0;

	if(!f) return;
		
	while(t&&((++i)!=f)){
		t=t->next;
	}

	if(t) outlet_symbol(x->x_symout,t->s);
}

static void slist_len(t_slist *x)
{
	t_sitem *t=x->x_c->first;
	int i=0;

	while(t){
		t=t->next;
		i++;
	}

	outlet_float(x->x_lenout,i);
}


void slist_setup(void)
{
    slist_class = class_new(gensym("slist"), (t_newmethod)slist_new,
    	(t_method)slist_ff,
    	sizeof(t_slist), 0, A_DEFSYM, 0);

    //class_addbang(slist_class, slist_bang);
    class_addfloat(slist_class, slist_float);
    class_addmethod(slist_class,(t_method)slist_add, gensym("add"),A_SYMBOL,0);
    class_addmethod(slist_class,(t_method)slist_find, gensym("find"),A_SYMBOL,0);
    class_addmethod(slist_class,(t_method)slist_setlist, gensym("setlist"),A_SYMBOL,0);
    class_addmethod(slist_class,(t_method)slist_reset, gensym("reset"),0);
    class_addmethod(slist_class,(t_method)slist_print, gensym("print"),0);
    class_addmethod(slist_class,(t_method)slist_len, gensym("len"),0);
   scommon_class = class_new(gensym("slist"), 0, 0,
    	sizeof(t_scommon), CLASS_PD, 0);
}