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. */
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. */
13
t_float atom_getfloat(t_atom *a)
15
if (a->a_type == A_FLOAT) return (a->a_w.w_float);
19
t_int atom_getint(t_atom *a)
21
return (atom_getfloat(a));
24
t_symbol *atom_getsymbol(t_atom *a) /* LATER think about this more carefully */
27
if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
28
else return (&s_float);
31
t_symbol *atom_gensym(t_atom *a) /* this works better for graph labels */
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, "???");
41
t_float atom_getfloatarg(int which, int argc, t_atom *argv)
43
if (argc <= which) return (0);
45
if (argv->a_type == A_FLOAT) return (argv->a_w.w_float);
49
t_int atom_getintarg(int which, int argc, t_atom *argv)
51
return (atom_getfloatarg(which, argc, argv));
54
t_symbol *atom_getsymbolarg(int which, int argc, t_atom *argv)
56
if (argc <= which) return (&s_);
58
if (argv->a_type == A_SYMBOL) return (argv->a_w.w_symbol);
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.
68
void atom_string(t_atom *a, char *buf, unsigned int bufsize)
73
case A_SEMI: strcpy(buf, ";"); break;
74
case A_COMMA: strcpy(buf, ","); break;
76
strcpy(buf, "(pointer)");
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, "+");
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[1] >= '0' && sp[1] <= '9'))
95
char *bp = buf, *ep = buf + (bufsize-2);
96
sp = a->a_w.w_symbol->s_name;
97
while (bp < ep && *sp)
99
if (*sp == ';' || *sp == ',' || *sp == '\\' ||
100
(*sp == '$' && sp[1] >= '0' && sp[1] <= '9'))
104
if (*sp) *bp++ = '*';
106
/* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
110
if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
113
strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
114
strcpy(buf + (bufsize - 2), "*");
120
sprintf(buf, "$%d", a->a_w.w_index);
123
strncpy(buf, a->a_w.w_symbol->s_name, bufsize);