~ubuntu-branches/ubuntu/precise/puredata/precise

« back to all changes in this revision

Viewing changes to src/m_atom.c

  • Committer: Bazaar Package Importer
  • Author(s): Paul Brossier
  • Date: 2009-12-22 21:29:31 UTC
  • mfrom: (1.2.6 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091222212931-nhwkzapjwsmjao1l
Tags: 0.42.5-3
* debian/control:
  - add community site to homepage field
  - improve long description
  - remove Replaces and Conflicts fields
  - add Suggests on pd-csound, pd-pdp, pd-zexy, pd-aubio
* debian/rules: add per-arch configuration flags
* debian/patches/02_kfreebsd.diff:
  - also define pd_tilde_dllextent on FreeBSD
  - fix typo (really closing #414414 this time)
  - also add hurd glue
* debian/patches/04_hurd.diff:
  - add hurd glue and s_midi_dummy.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
t_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
 
 
19
t_int atom_getint(t_atom *a)
 
20
{
 
21
    return (atom_getfloat(a));
 
22
}
 
23
 
 
24
t_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
 
 
31
t_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
 
 
41
t_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
 
 
49
t_int atom_getintarg(int which, int argc, t_atom *argv)
 
50
{
 
51
    return (atom_getfloatarg(which, argc, argv));
 
52
}
 
53
 
 
54
t_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
 
 
68
void 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[1] >= '0' && sp[1] <= '9'))
 
92
                quote = 1;
 
93
        if (quote)
 
94
        {
 
95
            char *bp = buf, *ep = buf + (bufsize-2);
 
96
            sp = a->a_w.w_symbol->s_name;
 
97
            while (bp < ep && *sp)
 
98
            {
 
99
                if (*sp == ';' || *sp == ',' || *sp == '\\' ||
 
100
                    (*sp == '$' && sp[1] >= '0' && sp[1] <= '9'))
 
101
                        *bp++ = '\\';
 
102
                *bp++ = *sp++;
 
103
            }
 
104
            if (*sp) *bp++ = '*';
 
105
            *bp = 0;
 
106
            /* post("quote %s -> %s", a->a_w.w_symbol->s_name, buf); */
 
107
        }
 
108
        else
 
109
        {
 
110
            if (len < bufsize-1) strcpy(buf, a->a_w.w_symbol->s_name);
 
111
            else
 
112
            {
 
113
                strncpy(buf, a->a_w.w_symbol->s_name, bufsize - 2);
 
114
                strcpy(buf + (bufsize - 2), "*");
 
115
            }
 
116
        }
 
117
    }
 
118
        break;
 
119
    case A_DOLLAR:
 
120
        sprintf(buf, "$%d", a->a_w.w_index);
 
121
        break;
 
122
    case A_DOLLSYM:
 
123
        strncpy(buf, a->a_w.w_symbol->s_name, bufsize);
 
124
        buf[bufsize-1] = 0;
 
125
        break;
 
126
    default:
 
127
        bug("atom_string");
 
128
    }
 
129
}