aboutsummaryrefslogtreecommitdiff
path: root/src/from_ascii_code.c
blob: 31f508685f549f44eee55985b6761ba65c36d0fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "m_pd.h"

//static char* from_ascii_code_text;
// Common binbuf to all objects
static t_binbuf* from_ascii_code_binbuf;

typedef struct _from_ascii_code
{
	
	t_object 			x_ob;
	int					eof_is_set;
	int				    eof;
	t_outlet*			outlet_left;
	//t_outlet*			outlet_right;
	char*				text;
	int					text_size;
	int 				size;
} t_from_ascii_code;

static t_class *from_ascii_code_class;

// Force output even if eof has not been received
static void from_ascii_code_bang(t_from_ascii_code *x) {
	
	if ( x->size ) {
		binbuf_clear(from_ascii_code_binbuf);
		binbuf_text(from_ascii_code_binbuf, x->text, x->size);
		t_atom *argv = binbuf_getvec(from_ascii_code_binbuf);
		int     argc = binbuf_getnatom(from_ascii_code_binbuf);
		if ( argc) {
			if ( (argv->a_type == A_SYMBOL)	) {
				outlet_anything(x->outlet_left,atom_getsymbol(argv),argc-1,argv+1);
			} else {
				outlet_anything(x->outlet_left,&s_list,argc,argv);
			}
		}
		x->size = 0;
	}
}


static void from_ascii_code_float(t_from_ascii_code *x, t_float f) {
	
	if ( x->eof_is_set && f == x->eof ) {
		from_ascii_code_bang(x);
	} else if ( f > 31 && f < 127 ) {
		x->text[x->size] = (char) f;
		x->size = x->size + 1;
		if ( x->size >= x->text_size ) {
			
			x->text = resizebytes(x->text, x->text_size * sizeof(*(x->text)), 
					(x->text_size + 100) * sizeof(*(x->text)));
			x->text_size = x->text_size + 100;
		}
	}
}

static void  from_ascii_code_list(t_from_ascii_code *x, t_symbol *s, int argc, t_atom *argv) {
	int i;
	for ( i=0; i < argc; i++ ) {
		if ( ((argv+i)->a_type == A_FLOAT) ) {
			from_ascii_code_float(x,atom_getfloat(argv+i));
		}
	}
}

static void from_ascii_code_free(t_from_ascii_code *x) {
	
	freebytes(x->text,x->text_size*sizeof(*(x->text)));
}

static void *from_ascii_code_new(t_symbol *s, int ac, t_atom *av)
{
    t_from_ascii_code *x = (t_from_ascii_code *)pd_new(from_ascii_code_class);
    
    // set eof if eof is set
    if ( ac && ((av)->a_type == A_FLOAT)) {
		x->eof = (int) atom_getfloat(av);
		x->eof_is_set = 1;
    } else {
		x->eof_is_set = 0;
	}
    
    // create string
    x->text_size = 100;
	x->text = getbytes(x->text_size*sizeof(*x->text));	
    
    x->size = 0;
    
    x->outlet_left = outlet_new(&x->x_ob, &s_list);
    //x->outlet_right = outlet_new(&x->x_ob, &s_float);
    
    return (x);
}

void from_ascii_code_setup(void)
{
	// create binbuf (no need to ever free)
	from_ascii_code_binbuf = binbuf_new();
	
	
	
    from_ascii_code_class = class_new(gensym("from_ascii_code"),
			    (t_newmethod)from_ascii_code_new, (t_method)from_ascii_code_free,
			    sizeof(t_from_ascii_code), 0, A_GIMME, 0);
    
    class_addbang(from_ascii_code_class, from_ascii_code_bang);
    class_addfloat(from_ascii_code_class, from_ascii_code_float);
    class_addlist(from_ascii_code_class, from_ascii_code_list);
    
    }