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 #include "../../pdbox.h"
15 /* convenience routines for checking and getting values of
16 atoms. There's no "pointer" version since there's nothing
17 safe to return if there's an error. */
19 t_float
atom_getfloat(t_atom
*a
)
21 if (a
->a_type
== A_FLOAT
) return (a
->a_w
.w_float
);
25 t_int
atom_getint(t_atom
*a
)
27 return (atom_getfloat(a
));
30 t_symbol
*atom_getsymbol(t_atom
*a
) /* LATER think about this more carefully */
35 if (a
->a_type
== A_SYMBOL
) return (a
->a_w
.w_symbol
);
36 else return (&s_float
);
39 t_symbol
*atom_gensym(t_atom
*a
) /* this works better for graph labels */
42 if (a
->a_type
== A_SYMBOL
) return (a
->a_w
.w_symbol
);
43 else if (a
->a_type
== A_FLOAT
)
45 ftoan(a
->a_w
.w_float
, buf
, sizeof(buf
)-1);
47 sprintf(buf
, "%g", a
->a_w
.w_float
);
49 else strcpy(buf
, "???");
53 t_float
atom_getfloatarg(int which
, int argc
, t_atom
*argv
)
55 if (argc
<= which
) return (0);
57 if (argv
->a_type
== A_FLOAT
) return (argv
->a_w
.w_float
);
61 t_int
atom_getintarg(int which
, int argc
, t_atom
*argv
)
63 return (atom_getfloatarg(which
, argc
, argv
));
66 t_symbol
*atom_getsymbolarg(int which
, int argc
, t_atom
*argv
)
68 if (argc
<= which
) return (&s_
);
70 if (argv
->a_type
== A_SYMBOL
) return (argv
->a_w
.w_symbol
);
74 /* convert an atom into a string, in the reverse sense of binbuf_text (q.v.)
75 * special attention is paid to symbols containing the special characters
76 * ';', ',', '$', and '\'; these are quoted with a preceding '\', except that
77 * the '$' only gets quoted at the beginning of the string.
80 void atom_string(t_atom
*a
, char *buf
, unsigned int bufsize
)
85 case A_SEMI
: strcpy(buf
, ";"); break;
86 case A_COMMA
: strcpy(buf
, ","); break;
88 strcpy(buf
, "(pointer)");
92 ftoan(a
->a_w
.w_float
, tbuf
, sizeof(tbuf
)-1);
94 sprintf(tbuf
, "%g", a
->a_w
.w_float
);
96 if (strlen(tbuf
) < bufsize
-1) strcpy(buf
, tbuf
);
97 else if (a
->a_w
.w_float
< 0) strcpy(buf
, "-");
98 else strcat(buf
, "+");
105 for (sp
= a
->a_w
.w_symbol
->s_name
, len
= 0, quote
= 0; *sp
; sp
++, len
++)
106 if (*sp
== ';' || *sp
== ',' || *sp
== '\\' ||
107 (*sp
== '$' && sp
== a
->a_w
.w_symbol
->s_name
&& sp
[1] >= '0'
112 char *bp
= buf
, *ep
= buf
+ (bufsize
-2);
113 sp
= a
->a_w
.w_symbol
->s_name
;
114 while (bp
< ep
&& *sp
)
116 if (*sp
== ';' || *sp
== ',' || *sp
== '\\' ||
117 (*sp
== '$' && bp
== buf
&& sp
[1] >= '0' && sp
[1] <= '9'))
121 if (*sp
) *bp
++ = '*';
123 /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
127 if (len
< bufsize
-1) strcpy(buf
, a
->a_w
.w_symbol
->s_name
);
130 strncpy(buf
, a
->a_w
.w_symbol
->s_name
, bufsize
- 2);
131 strcpy(buf
+ (bufsize
- 2), "*");
137 snprintf(buf
, bufsize
, "$%d", a
->a_w
.w_index
);
140 snprintf(buf
, bufsize
, "$%s", a
->a_w
.w_symbol
->s_name
);