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
|
/******************************************************
*
* 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>
/*
* symbol2list: convert a symbol into a list (with given delimiters)
*/
/* ------------------------- symbol2list ------------------------------- */
static t_class *symbol2list_class;
typedef struct _symbol2list
{
t_object x_obj;
t_symbol *s, *delimiter;
t_atom *argv;
int argc, argnum; /* "argnum" is the number of reserved atoms (might be >argc) */
} t_symbol2list;
static void symbol2list_delimiter(t_symbol2list *x, t_symbol *s){
x->delimiter = s;
}
STATIC_INLINE void string2atom(t_atom *ap, char* cp, int clen){
char *buffer=getbytes(sizeof(char)*(clen+1));
char *endptr[1];
t_float ftest;
strncpy(buffer, cp, clen);
buffer[clen]=0;
ftest=strtod(buffer, endptr);
/* what should we do with the special cases of hexadecimal values, "INF" and "NAN" ???
* strtod() parses them to numeric values:
* symbol "hallo 0x12" will become "list hallo 18"
* do we want this ??
*/
if (buffer+clen!=*endptr){
/* strtof() failed, we have a symbol */
SETSYMBOL(ap, gensym(buffer));
} else {
/* it is a number. */
SETFLOAT(ap,ftest);
}
freebytes(buffer, sizeof(char)*(clen+1));
}
static void symbol2list_process(t_symbol2list *x)
{
char *cc;
char *deli;
int dell;
char *cp, *d;
int i=1;
if (x->s==NULL){
x->argc=0;
return;
}
cc=x->s->s_name;
cp=cc;
if (x->delimiter==NULL || x->delimiter==&s_){
i=strlen(cc);
if(x->argnum<i){
freebytes(x->argv, x->argnum*sizeof(t_atom));
x->argnum=i+10;
x->argv=getbytes(x->argnum*sizeof(t_atom));
}
x->argc=i;
while(i--)string2atom(x->argv+i, cc+i, 1);
return;
}
deli=x->delimiter->s_name;
dell=strlen(deli);
/* get the number of tokens */
while((d=strstr(cp, deli))){
if (d!=NULL && d!=cp){
i++;
}
cp=d+dell;
}
/* resize the list-buffer if necessary */
if(x->argnum<i){
freebytes(x->argv, x->argnum*sizeof(t_atom));
x->argnum=i+10;
x->argv=getbytes(x->argnum*sizeof(t_atom));
}
x->argc=i;
/* parse the tokens into the list-buffer */
i=0;
/* find the first token */
cp=cc;
while(cp==(d=strstr(cp,deli))){cp+=dell;}
while((d=strstr(cp, deli))){
if(d!=cp){
string2atom(x->argv+i, cp, d-cp);
i++;
}
cp=d+dell;
}
if(cp)string2atom(x->argv+i, cp, strlen(cp));
}
static void symbol2list_bang(t_symbol2list *x){
if(!(x->s) || x->s==&s_){
outlet_bang(x->x_obj.ob_outlet);
return;
}
symbol2list_process(x);
if(x->argc)outlet_list(x->x_obj.ob_outlet, 0, x->argc, x->argv);
}
static void symbol2list_symbol(t_symbol2list *x, t_symbol *s){
x->s = s;
symbol2list_bang(x);
}
static void *symbol2list_new(t_symbol *s, int argc, t_atom *argv)
{
t_symbol2list *x = (t_symbol2list *)pd_new(symbol2list_class);
ZEXY_USEVAR(s);
outlet_new(&x->x_obj, 0);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("symbol"), gensym(""));
x->argc=0;
x->argnum=16;
x->argv=getbytes(x->argnum*sizeof(t_atom));
symbol2list_delimiter(x, (argc)?atom_getsymbol(argv):gensym(" "));
return (x);
}
static void symbol2list_free(t_symbol2list *x)
{}
static void symbol2list_help(t_symbol2list*x)
{
post("\n%c symbol2list\t:: split a symbol into a list of atoms", HEARTSYMBOL);
}
void symbol2list_setup(void)
{
symbol2list_class = class_new(gensym("symbol2list"), (t_newmethod)symbol2list_new,
(t_method)symbol2list_free, sizeof(t_symbol2list), 0, A_GIMME, 0);
class_addcreator((t_newmethod)symbol2list_new, gensym("s2l"), A_GIMME, 0);
class_addsymbol (symbol2list_class, symbol2list_symbol);
class_addbang (symbol2list_class, symbol2list_bang);
class_addmethod (symbol2list_class, (t_method)symbol2list_delimiter, gensym(""), A_SYMBOL, 0);
class_addmethod(symbol2list_class, (t_method)symbol2list_help, gensym("help"), A_NULL);
class_sethelpsymbol(symbol2list_class, gensym("zexy/symbol2list"));
zexy_register("symbol2list");
}
void s2l_setup(void)
{
symbol2list_setup();
}
|