aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/plugin_interface/sc_msg_iter.h
blob: 240a30f6399f2bd5c108c2d8930448f268dca9b7 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#ifndef _sc_msg_iter_
#define _sc_msg_iter_

#include "SC_Endian.h"
#include "SC_Types.h"
#include <string.h>

// return the ptr to the byte after the OSC string.
inline char* OSCstrskip(char *str)
{
//	while (str[3]) { str += 4; }
//	return str + 4;
	do { str += 4; } while (str[-1]);
	return str;
}

// returns the number of bytes (including padding) for an OSC string.
inline int OSCstrlen(char *strin)
{
	return OSCstrskip(strin) - strin;
}

// returns a float, converting an int if necessary
inline float32 OSCfloat(char* inData)
{
	elem32* elem = (elem32*)inData;
	elem->u = ntohl(elem->u);
	return elem->f;
}

inline int32 OSCint(char* inData)
{
	return (int32)ntohl(*(uint32*)inData);
}

inline int64 OSCtime(char* inData)
{
	return ((int64)ntohl(*(uint32*)inData) << 32) + (ntohl(*(uint32*)(inData + 4)));
}

inline float64 OSCdouble(char* inData)
{
	elem64 slot;
	slot.i = ((int64)ntohl(*(uint32*)inData) << 32) + (ntohl(*(uint32*)(inData + 4)));
	return slot.f;
}

struct sc_msg_iter 
{
	char *data, *rdpos, *endpos, *tags;
	int size, count;
	
	sc_msg_iter();
	sc_msg_iter(int inSize, char* inData);
	void init(int inSize, char* inData);
	int32 geti(int32 defaultValue = 0);
	float32 getf(float32 defaultValue = 0.f);
	float64 getd(float64 defaultValue = 0.f);
	char *gets(char* defaultValue = 0);
	int32 *gets4(char* defaultValue = 0);
	size_t getbsize();
	void getb(char* outData, size_t inSize);
	void skipb();
	int remain() { return endpos - rdpos; }
        
    char nextTag(char defaultTag = 'f') { return tags ? tags[count] : defaultTag; }
};

inline sc_msg_iter::sc_msg_iter()
{
}

inline sc_msg_iter::sc_msg_iter(int inSize, char* inData)
{
	init(inSize, inData);
}

inline void sc_msg_iter::init(int inSize, char* inData)
{
	data = inData;
	size = inSize;
	endpos = data + size;
	count = 0;
	if (data[0] == ',') {
		tags = data+1;
		rdpos = OSCstrskip(data);
	} else {
		tags = 0;
		rdpos = data;
	}
}

inline int32 sc_msg_iter::geti(int32 defaultValue)
{
	int value;
	if (remain() <= 0) return defaultValue;
	if (tags) {
		if (tags[count] == 'i') {
			value = OSCint(rdpos);
			rdpos += sizeof(int32);
		} else if (tags[count] == 'f') {
			value = (int32)OSCfloat(rdpos);
			rdpos += sizeof(float32);
/*		} else if (tags[count] == 's') {
			value = atoi(rdpos);
			rdpos = OSCstrskip(rdpos);
*/
		} else {
			value = defaultValue;
		}
	} else {
		value = (int)OSCint(rdpos);
		rdpos += sizeof(int32);
	}
	count ++;
	return value;
}

inline float32 sc_msg_iter::getf(float32 defaultValue)
{
	float32 value;
	if (remain() <= 0) return defaultValue;
	if (tags) {
		if (tags[count] == 'f') {
			value = OSCfloat(rdpos);
			rdpos += sizeof(float32);
		} else if (tags[count] == 'd') {
			value = (float64)OSCdouble(rdpos);
			rdpos += sizeof(float64);
		} else if (tags[count] == 'i') {
			value = (float32)OSCint(rdpos);
			rdpos += sizeof(int32);
/*		} else if (tags[count] == 's') {
			value = atof(rdpos);
			rdpos = OSCstrskip(rdpos);
*/
		} else {
			value = defaultValue;
		}
	} else {
		value = OSCfloat(rdpos);
		rdpos += sizeof(float32);
	}
	count ++;
	return value;
}

inline float64 sc_msg_iter::getd(float64 defaultValue)
{
	float64 value;
	if (remain() <= 0) return defaultValue;
	if (tags) {
		if (tags[count] == 'f') {
			value = (float64)OSCfloat(rdpos);
			rdpos += sizeof(float32);
		} else if (tags[count] == 'd') {
			value = OSCdouble(rdpos);
			rdpos += sizeof(float64);
		} else if (tags[count] == 'i') {
			value = (float64)OSCint(rdpos);
			rdpos += sizeof(int32);
/*		} else if (tags[count] == 's') {
			value = atof(rdpos);
			rdpos = OSCstrskip(rdpos);
*/
		} else {
			value = defaultValue;
		}
	} else {
		value = OSCdouble(rdpos);
		rdpos += sizeof(float64);
	}
	count ++;
	return value;
}


inline char* sc_msg_iter::gets(char* defaultValue)
{
	char* value;
	if (remain() <= 0) return 0;
	if (tags) {
		if (tags[count] == 's') {
			value = rdpos;
			rdpos = OSCstrskip(rdpos);
		} else {
			value = defaultValue;
		}
	} else {
		value = rdpos;
		rdpos = OSCstrskip(rdpos);
	}
	count ++;
	return value;
}

inline int32* sc_msg_iter::gets4(char* defaultValue)
{
	int32* value;
	if (remain() <= 0) return 0;
	if (tags) {
		if (tags[count] == 's') {
			value = (int32*)rdpos;
			rdpos = OSCstrskip(rdpos);
		} else {
			value = (int32*)defaultValue;
		}
	} else {
		value = (int32*)rdpos;
		rdpos = OSCstrskip(rdpos);
	}
	count ++;
	return value;
}

inline size_t sc_msg_iter::getbsize()
{
	if (remain() <= 0) return 0;
	if (tags && tags[count] != 'b') return 0;
	return (size_t)OSCint(rdpos);
}

inline void sc_msg_iter::getb(char* outArray, size_t size)
{
	size_t len = OSCint(rdpos);
	if (size < len) return;
	rdpos += sizeof(int32);
	size_t len4 = (len + 3) & -4;
	memcpy(outArray, rdpos, size);
	rdpos += len4;
	count ++;
}

inline void sc_msg_iter::skipb()
{
	size_t len = OSCint(rdpos);
	rdpos += sizeof(int32);
	size_t len4 = (len + 3) & -4;
	rdpos += len4;
	count ++;
}

#endif