Fix pdbox makefile to actually take part in dependency generation
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / m_atom.c
blob654448abd2940aaf6e153fdcd074621769ae4e71
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. */
5 #include "m_pd.h"
7 #ifdef ROCKBOX
8 #include "plugin.h"
9 #include "../../pdbox.h"
10 #else /* ROCKBOX */
11 #include <stdio.h>
12 #include <string.h>
13 #endif /* ROCKBOX */
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);
22 else return (0);
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 */
32 #ifndef ROCKBOX
33 char buf[30];
34 #endif
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 */
41 char buf[30];
42 if (a->a_type == A_SYMBOL) return (a->a_w.w_symbol);
43 else if (a->a_type == A_FLOAT)
44 #ifdef ROCKBOX
45 ftoan(a->a_w.w_float, buf, sizeof(buf)-1);
46 #else
47 sprintf(buf, "%g", a->a_w.w_float);
48 #endif
49 else strcpy(buf, "???");
50 return (gensym(buf));
53 t_float atom_getfloatarg(int which, int argc, t_atom *argv)
55 if (argc <= which) return (0);
56 argv += which;
57 if (argv->a_type == A_FLOAT) return (argv->a_w.w_float);
58 else return (0);
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_);
69 argv += which;
70 if (argv->a_type == A_SYMBOL) return (argv->a_w.w_symbol);
71 else return (&s_);
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)
82 char tbuf[30];
83 switch(a->a_type)
85 case A_SEMI: strcpy(buf, ";"); break;
86 case A_COMMA: strcpy(buf, ","); break;
87 case A_POINTER:
88 strcpy(buf, "(pointer)");
89 break;
90 case A_FLOAT:
91 #ifdef ROCKBOX
92 ftoan(a->a_w.w_float, tbuf, sizeof(tbuf)-1);
93 #else
94 sprintf(tbuf, "%g", a->a_w.w_float);
95 #endif
96 if (strlen(tbuf) < bufsize-1) strcpy(buf, tbuf);
97 else if (a->a_w.w_float < 0) strcpy(buf, "-");
98 else strcat(buf, "+");
99 break;
100 case A_SYMBOL:
102 char *sp;
103 unsigned int len;
104 int quote;
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'
108 && sp[1] <= '9'))
109 quote = 1;
110 if (quote)
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'))
118 *bp++ = '\\';
119 *bp++ = *sp++;
121 if (*sp) *bp++ = '*';
122 *bp = 0;
123 /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
125 else
127 if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
128 else
130 strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
131 strcpy(buf + (bufsize - 2), "*");
135 break;
136 case A_DOLLAR:
137 snprintf(buf, bufsize, "$%d", a->a_w.w_index);
138 break;
139 case A_DOLLSYM:
140 snprintf(buf, bufsize, "$%s", a->a_w.w_symbol->s_name);
141 break;
142 default:
143 bug("atom_string");