forked from davidbouchard/arduino-osc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSCData.cpp
173 lines (151 loc) · 3.51 KB
/
OSCData.cpp
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
#include "OSCData.h"
/*=============================================================================
CONSTRUCTORS
overloaded methods for each of the types which will
set the type flag, the size (in bytes), and the data
=============================================================================*/
OSCData::OSCData(const char * s){
error = OSC_OK;
type = 's';
bytes = (strlen(s) + 1);
//own the data
char * mem = (char *) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
strcpy(mem, s);
data.s = mem;
}
}
OSCData::OSCData(int i){
error = OSC_OK;
type = 'i';
//cast it to an int32
bytes = sizeof(int32_t);
int32_t i32 = (int32_t) i;
data.i = i32;
}
OSCData::OSCData(int32_t i){
error = OSC_OK;
type = 'i';
bytes = sizeof(i);
data.i = i;
}
OSCData::OSCData(float f){
error = OSC_OK;
type = 'f';
bytes = sizeof(float);
data.f = f;
}
OSCData::OSCData(double d){
error = OSC_OK;
bytes = sizeof(double);
//if it's not 8 bits it's not a true double
if (bytes == 8){
type = 'd';
data.d = d;
} else {
type = 'f';
data.f = d;
}
}
OSCData::OSCData(uint8_t * b, int len){
error = OSC_OK;
type = 'b';
bytes = len + 4;
//add the size to the front of the blob
uint32_t len32 = (uint32_t) len;
//make sure the length is endian-safe
len32 = BigEndian(len32);
uint8_t * lenPtr = (uint8_t *) (& len32);
//own the data
uint8_t * mem = (uint8_t * ) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
//copy over the blob length
memcpy(mem, lenPtr, 4);
//copy over the blob data
memcpy(mem + 4, b, len);
data.b = mem;
}
}
OSCData::OSCData (OSCData * datum){
error = OSC_OK;
type = datum->type;
bytes = datum->bytes;
if (type == 'i' || type == 'f' || type == 'd'){
data = datum->data;
} else if (type == 's' || type == 'b'){
//allocate a new peice of memory
uint8_t * mem = (uint8_t * ) malloc(bytes);
if (mem == NULL){
error = ALLOCFAILED;
} else {
//copy over the blob length
memcpy(mem, datum->data.b, bytes);
data.b = mem;
}
}
}
//DESTRUCTOR
OSCData::~OSCData(){
//if there are no bytes, there is nothing to free
if (bytes>0){
//if the data is of type 's' or 'b', need to free that memory
if (type == 's'){
free(data.s);
}else if( type == 'b'){
free(data.b);
}
}
}
//sets just the type as a message placeholder
//no data
OSCData::OSCData(char t){
error = INVALID_OSC;
type = t;
bytes = 0;
}
/*=============================================================================
GETTERS
perform a safety check to make sure the data type matches the request
otherwise returns NULL
=============================================================================*/
int32_t OSCData::getInt(){
if (type == 'i'){
return data.i;
} else {
return NULL;
}
}
float OSCData::getFloat(){
if (type == 'f'){
return data.f;
} else {
return NULL;
}
}
double OSCData::getDouble(){
if (type == 'd'){
return data.d;
} else {
return NULL;
}
}
int OSCData::getString(char * strBuffer, int length){
if (type == 's' && bytes <= length){
strncpy(strBuffer, data.s, bytes);
return bytes;
} else {
return NULL;
}
}
int OSCData::getBlob(uint8_t * blobBuffer, int length){
if (type == 'b' && bytes <= length){
memcpy(blobBuffer, data.b, bytes);
return bytes;
} else {
return NULL;
}
}