A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
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
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 */
14
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. */
18
19t_float atom_getfloat(t_atom *a)
20{
21 if (a->a_type == A_FLOAT) return (a->a_w.w_float);
22 else return (0);
23}
24
25t_int atom_getint(t_atom *a)
26{
27 return (atom_getfloat(a));
28}
29
30t_symbol *atom_getsymbol(t_atom *a) /* LATER think about this more carefully */
31{
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);
37}
38
39t_symbol *atom_gensym(t_atom *a) /* this works better for graph labels */
40{
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));
51}
52
53t_float atom_getfloatarg(int which, int argc, t_atom *argv)
54{
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);
59}
60
61t_int atom_getintarg(int which, int argc, t_atom *argv)
62{
63 return (atom_getfloatarg(which, argc, argv));
64}
65
66t_symbol *atom_getsymbolarg(int which, int argc, t_atom *argv)
67{
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_);
72}
73
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.
78*/
79
80void atom_string(t_atom *a, char *buf, unsigned int bufsize)
81{
82 char tbuf[30];
83 switch(a->a_type)
84 {
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:
101 {
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)
111 {
112 char *bp = buf, *ep = buf + (bufsize-2);
113 sp = a->a_w.w_symbol->s_name;
114 while (bp < ep && *sp)
115 {
116 if (*sp == ';' || *sp == ',' || *sp == '\\' ||
117 (*sp == '$' && bp == buf && sp[1] >= '0' && sp[1] <= '9'))
118 *bp++ = '\\';
119 *bp++ = *sp++;
120 }
121 if (*sp) *bp++ = '*';
122 *bp = 0;
123 /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
124 }
125 else
126 {
127 if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
128 else
129 {
130 strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
131 strcpy(buf + (bufsize - 2), "*");
132 }
133 }
134 }
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");
144 }
145}
146