summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/src/m_atom.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/src/m_atom.c')
-rw-r--r--apps/plugins/pdbox/PDa/src/m_atom.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/apps/plugins/pdbox/PDa/src/m_atom.c b/apps/plugins/pdbox/PDa/src/m_atom.c
new file mode 100644
index 0000000000..a4b08ff2cb
--- /dev/null
+++ b/apps/plugins/pdbox/PDa/src/m_atom.c
@@ -0,0 +1,258 @@
1/* Copyright (c) 1997-1999 Miller Puckette.
2* For information on usage and redistribution, and for a DISCLAIMER OF ALL
3* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
4
5#include "m_pd.h"
6#include <stdio.h>
7#include <string.h>
8
9 /* convenience routines for checking and getting values of
10 atoms. There's no "pointer" version since there's nothing
11 safe to return if there's an error. */
12
13t_float atom_getfloat(t_atom *a)
14{
15 if (a->a_type == A_FLOAT) return (a->a_w.w_float);
16 else return (0);
17}
18
19t_int atom_getint(t_atom *a)
20{
21 return (atom_getfloat(a));
22}
23
24t_symbol *atom_getsymbol(t_atom *a) /* LATER think about this more carefully */
25{
26 char buf[30];
27 if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
28 else return (&s_float);
29}
30
31t_symbol *atom_gensym(t_atom *a) /* this works better for graph labels */
32{
33 char buf[30];
34 if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
35 else if (a->a_type == A_FLOAT)
36 sprintf(buf, "%g", a->a_w.w_float);
37 else strcpy(buf, "???");
38 return (gensym(buf));
39}
40
41t_float atom_getfloatarg(int which, int argc, t_atom *argv)
42{
43 if (argc <= which) return (0);
44 argv += which;
45 if (argv->a_type == A_FLOAT) return (argv->a_w.w_float);
46 else return (0);
47}
48
49t_int atom_getintarg(int which, int argc, t_atom *argv)
50{
51 return (atom_getfloatarg(which, argc, argv));
52}
53
54t_symbol *atom_getsymbolarg(int which, int argc, t_atom *argv)
55{
56 if (argc <= which) return (&s_);
57 argv += which;
58 if (argv->a_type == A_SYMBOL) return (argv->a_w.w_symbol);
59 else return (&s_);
60}
61
62/* convert an atom into a string, in the reverse sense of binbuf_text (q.v.)
63* special attention is paid to symbols containing the special characters
64* ';', ',', '$', and '\'; these are quoted with a preceding '\', except that
65* the '$' only gets quoted at the beginning of the string.
66*/
67
68void atom_string(t_atom *a, char *buf, unsigned int bufsize)
69{
70 char tbuf[30];
71 switch(a->a_type)
72 {
73 case A_SEMI: strcpy(buf, ";"); break;
74 case A_COMMA: strcpy(buf, ","); break;
75 case A_POINTER:
76 strcpy(buf, "(pointer)");
77 break;
78 case A_FLOAT:
79 sprintf(tbuf, "%g", a->a_w.w_float);
80 if (strlen(tbuf) < bufsize-1) strcpy(buf, tbuf);
81 else if (a->a_w.w_float < 0) strcpy(buf, "-");
82 else strcat(buf, "+");
83 break;
84 case A_SYMBOL:
85 {
86 char *sp;
87 unsigned int len;
88 int quote;
89 for (sp = a->a_w.w_symbol->s_name, len = 0, quote = 0; *sp; sp++, len++)
90 if (*sp == ';' || *sp == ',' || *sp == '\\' ||
91 (*sp == '$' && sp == a->a_w.w_symbol->s_name && sp[1] >= '0'
92 && sp[1] <= '9'))
93 quote = 1;
94 if (quote)
95 {
96 char *bp = buf, *ep = buf + (bufsize-2);
97 sp = a->a_w.w_symbol->s_name;
98 while (bp < ep && *sp)
99 {
100 if (*sp == ';' || *sp == ',' || *sp == '\\' ||
101 (*sp == '$' && bp == buf && sp[1] >= '0' && sp[1] <= '9'))
102 *bp++ = '\\';
103 *bp++ = *sp++;
104 }
105 if (*sp) *bp++ = '*';
106 *bp = 0;
107 /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
108 }
109 else
110 {
111 if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
112 else
113 {
114 strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
115 strcpy(buf + (bufsize - 2), "*");
116 }
117 }
118 }
119 break;
120 case A_DOLLAR:
121 sprintf(buf, "$%d", a->a_w.w_index);
122 break;
123 case A_DOLLSYM:
124 sprintf(buf, "$%s", a->a_w.w_symbol->s_name);
125 break;
126 default:
127 bug("atom_string");
128 }
129}
130/* Copyright (c) 1997-1999 Miller Puckette.
131* For information on usage and redistribution, and for a DISCLAIMER OF ALL
132* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
133
134#include "m_pd.h"
135#include <stdio.h>
136#include <string.h>
137
138 /* convenience routines for checking and getting values of
139 atoms. There's no "pointer" version since there's nothing
140 safe to return if there's an error. */
141
142t_float atom_getfloat(t_atom *a)
143{
144 if (a->a_type == A_FLOAT) return (a->a_w.w_float);
145 else return (0);
146}
147
148t_int atom_getint(t_atom *a)
149{
150 return (atom_getfloat(a));
151}
152
153t_symbol *atom_getsymbol(t_atom *a) /* LATER think about this more carefully */
154{
155 char buf[30];
156 if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
157 else return (&s_float);
158}
159
160t_symbol *atom_gensym(t_atom *a) /* this works better for graph labels */
161{
162 char buf[30];
163 if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
164 else if (a->a_type == A_FLOAT)
165 sprintf(buf, "%g", a->a_w.w_float);
166 else strcpy(buf, "???");
167 return (gensym(buf));
168}
169
170t_float atom_getfloatarg(int which, int argc, t_atom *argv)
171{
172 if (argc <= which) return (0);
173 argv += which;
174 if (argv->a_type == A_FLOAT) return (argv->a_w.w_float);
175 else return (0);
176}
177
178t_int atom_getintarg(int which, int argc, t_atom *argv)
179{
180 return (atom_getfloatarg(which, argc, argv));
181}
182
183t_symbol *atom_getsymbolarg(int which, int argc, t_atom *argv)
184{
185 if (argc <= which) return (&s_);
186 argv += which;
187 if (argv->a_type == A_SYMBOL) return (argv->a_w.w_symbol);
188 else return (&s_);
189}
190
191/* convert an atom into a string, in the reverse sense of binbuf_text (q.v.)
192* special attention is paid to symbols containing the special characters
193* ';', ',', '$', and '\'; these are quoted with a preceding '\', except that
194* the '$' only gets quoted at the beginning of the string.
195*/
196
197void atom_string(t_atom *a, char *buf, unsigned int bufsize)
198{
199 char tbuf[30];
200 switch(a->a_type)
201 {
202 case A_SEMI: strcpy(buf, ";"); break;
203 case A_COMMA: strcpy(buf, ","); break;
204 case A_POINTER:
205 strcpy(buf, "(pointer)");
206 break;
207 case A_FLOAT:
208 sprintf(tbuf, "%g", a->a_w.w_float);
209 if (strlen(tbuf) < bufsize-1) strcpy(buf, tbuf);
210 else if (a->a_w.w_float < 0) strcpy(buf, "-");
211 else strcat(buf, "+");
212 break;
213 case A_SYMBOL:
214 {
215 char *sp;
216 unsigned int len;
217 int quote;
218 for (sp = a->a_w.w_symbol->s_name, len = 0, quote = 0; *sp; sp++, len++)
219 if (*sp == ';' || *sp == ',' || *sp == '\\' ||
220 (*sp == '$' && sp == a->a_w.w_symbol->s_name && sp[1] >= '0'
221 && sp[1] <= '9'))
222 quote = 1;
223 if (quote)
224 {
225 char *bp = buf, *ep = buf + (bufsize-2);
226 sp = a->a_w.w_symbol->s_name;
227 while (bp < ep && *sp)
228 {
229 if (*sp == ';' || *sp == ',' || *sp == '\\' ||
230 (*sp == '$' && bp == buf && sp[1] >= '0' && sp[1] <= '9'))
231 *bp++ = '\\';
232 *bp++ = *sp++;
233 }
234 if (*sp) *bp++ = '*';
235 *bp = 0;
236 /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
237 }
238 else
239 {
240 if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
241 else
242 {
243 strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
244 strcpy(buf + (bufsize - 2), "*");
245 }
246 }
247 }
248 break;
249 case A_DOLLAR:
250 sprintf(buf, "$%d", a->a_w.w_index);
251 break;
252 case A_DOLLSYM:
253 sprintf(buf, "$%s", a->a_w.w_symbol->s_name);
254 break;
255 default:
256 bug("atom_string");
257 }
258}