flatom_pr.cpp 2.74 KB
Newer Older
thomas's avatar
thomas committed
1
2
3
4
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

5
Copyright (c) 2001-2006 Thomas Grill (gr@grrrr.org)
thomas's avatar
thomas committed
6
7
8
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

thomas's avatar
thomas committed
9
10
11
$LastChangedRevision$
$LastChangedDate$
$LastChangedBy$
thomas's avatar
thomas committed
12
13
14
15
16
17
18
19
20
21
22
23
*/

/*! \file flatom_pr.cpp
    \brief Definitions for printing and scanning the t_atom type.
*/
 
#include "flext.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

thomas's avatar
thomas committed
24
25
26
27
#ifdef _MSC_VER
#define snprintf _snprintf
#endif

thomas's avatar
thomas committed
28
// \TODO take bufsz into account!
thomas's avatar
thomas committed
29
bool flext::PrintAtom(const t_atom &a,char *buf,size_t bufsz)
thomas's avatar
thomas committed
30
{
thomas's avatar
thomas committed
31
32
    bool ok = true;
    if(IsFloat(a)) {
33
        ok = STD::snprintf(buf,bufsz,"%g",GetFloat(a)) > 0;
thomas's avatar
thomas committed
34
35
    }
    else if(IsInt(a)) {
36
        ok = STD::snprintf(buf,bufsz,"%i",GetInt(a)) > 0;
thomas's avatar
thomas committed
37
38
    }
    else if(IsSymbol(a)) {
thomas's avatar
thomas committed
39
40
41
42
43
44
45
46
		const char *c = GetString(a);
		size_t len = strlen(c);
		if(len < bufsz) {
			memcpy(buf,c,len); buf[len] = 0;
			ok = true;
		}
		else 
			ok = false;
thomas's avatar
thomas committed
47
    }
48
49
#if FLEXT_SYS == FLEXT_SYS_PD
#ifndef FLEXT_COMPATIBLE
thomas's avatar
thomas committed
50
    else if(IsPointer(a)) {
51
        ok = STD::snprintf(buf,bufsz,"%p",GetPointer(a)) > 0;
thomas's avatar
thomas committed
52
    }
53
#endif
thomas's avatar
thomas committed
54
    else if(a.a_type == A_DOLLAR) {
55
        ok = STD::snprintf(buf,bufsz,"$%d",a.a_w.w_index) > 0;
thomas's avatar
thomas committed
56
57
    }
    else if(a.a_type == A_DOLLSYM) {
58
        ok = STD::snprintf(buf,bufsz,"$%s",GetString(a)) > 0;
thomas's avatar
thomas committed
59
    }
thomas's avatar
thomas committed
60
#elif FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
61
    else if(a.a_type == A_DOLLAR) {
62
        ok = STD::snprintf(buf,bufsz,"$%d",a.a_w.w_long) > 0;
thomas's avatar
thomas committed
63
    }
thomas's avatar
thomas committed
64
65
#else
//#pragma message("Not implemented")
thomas's avatar
thomas committed
66
#endif
thomas's avatar
thomas committed
67
    else {
thomas's avatar
thomas committed
68
        error("flext: atom type unknown");
thomas's avatar
thomas committed
69
70
71
        ok = false;
    }
    return ok;
thomas's avatar
thomas committed
72
73
}

thomas's avatar
thomas committed
74
bool flext::PrintList(int argc,const t_atom *argv,char *buf,size_t bufsz)
thomas's avatar
thomas committed
75
{
thomas's avatar
thomas committed
76
    bool ok = true;
thomas's avatar
thomas committed
77
    for(int i = 0; ok && i < argc && bufsz > 0; ++i) {
thomas's avatar
thomas committed
78
79
80
        if(i) { *(buf++) = ' '; --bufsz; } // prepend space

        if(PrintAtom(argv[i],buf,bufsz)) {
thomas's avatar
thomas committed
81
            size_t len = strlen(buf);
thomas's avatar
thomas committed
82
83
84
85
            buf += len,bufsz -= len;
        }
        else
            ok = false;
thomas's avatar
thomas committed
86
    }
thomas's avatar
thomas committed
87
    *buf = 0;
thomas's avatar
thomas committed
88
89
90
    return ok;
}

thomas's avatar
thomas committed
91

thomas's avatar
thomas committed
92
const char *flext::ScanAtom(t_atom &a,const char *c)
thomas's avatar
thomas committed
93
{
thomas's avatar
thomas committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	// skip leading whitespace
	while(*c && isspace(*c)) ++c;
	if(!*c) return NULL;

    // go to next space and save character
    char *end = const_cast<char *>(c);
    while(*end && !isspace(*end)) ++end;
    char sv = *end;

    float fres;
    // first try float
    char *endp;
    // see if it's a float - thanks to Frank Barknecht
    fres = (float)strtod(c,&endp);   
    if(!*c && endp != c) { 
        int ires = (int)fres; // try a cast
        if(fres == ires)
            SetInt(a,ires);
        else
            SetFloat(a,fres);
thomas's avatar
thomas committed
114
    }
thomas's avatar
thomas committed
115
116
117
    // no, it's a symbol
    else
        SetString(a,c);
thomas's avatar
thomas committed
118

thomas's avatar
thomas committed
119
    *end = sv;
thomas's avatar
thomas committed
120

thomas's avatar
thomas committed
121
	return c;
thomas's avatar
thomas committed
122
}