-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathosc_atom_array_u.c
More file actions
235 lines (219 loc) · 6.55 KB
/
osc_atom_array_u.c
File metadata and controls
235 lines (219 loc) · 6.55 KB
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
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2009-ll, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include "osc.h"
#include "osc_mem.h"
#include "osc_byteorder.h"
#include "osc_atom_u.h"
#include "osc_atom_u.r" // need ths to get the size of the struct
#include "osc_array.h"
#include "osc_atom_array_u.h"
t_osc_atom_array_u *osc_atom_array_u_alloc(long len)
{
return (t_osc_atom_array_u *)osc_array_allocWithSize(len, sizeof(t_osc_atom_u));
}
void osc_atom_array_u_free(t_osc_atom_ar_u *ar)
{
if(ar){
int i;
for(i = 0; i < osc_atom_array_u_getLen(ar); i++){
t_osc_atom_u *a = osc_atom_array_u_get(ar, i);
if(a->alloc && a->typetag == 's'){
if(a->w.s){
osc_mem_free(a->w.s);
}
}else if(a->typetag == OSC_BUNDLE_TYPETAG){
if(a->w.bndl){
osc_bundle_u_free(a->w.bndl);
}
}
}
osc_array_free(ar);
}
}
t_osc_err osc_atom_array_u_getDoubleArray(t_osc_atom_ar_u *array, double **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(double) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getDouble(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getFloatArray(t_osc_atom_ar_u *array, float **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(float) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getFloat(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getInt32Array(t_osc_atom_ar_u *array, int32_t **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(int32_t) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getInt32(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getInt64Array(t_osc_atom_ar_u *array, int64_t **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(int64_t) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getInt64(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getUInt32Array(t_osc_atom_ar_u *array, uint32_t **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(uint32_t) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getUInt32(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getUInt64Array(t_osc_atom_ar_u *array, uint64_t **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(uint64_t) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getUInt64(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getBoolArray(t_osc_atom_ar_u *array, char **out)
{
long len = osc_atom_array_u_getLen(array);
if(!(*out)){
*out = osc_mem_alloc(sizeof(char) * len);
}
int i;
for(i = 0; i < len; i++){
(*out)[i] = osc_atom_u_getBool(osc_atom_array_u_get(array, i));
}
return OSC_ERR_NONE;
}
t_osc_err osc_atom_array_u_getStringArray(t_osc_atom_ar_u *array, long *len, char **out, const char *sep)
{
long arlen = osc_atom_array_u_getLen(array);
long buflen = *len;
if(!(*out)){
buflen = 256;
*out = osc_mem_alloc(buflen); // guess that each element will be around 8 chars
}
char *ptr = *out;
int seplen = 0;
if(sep){
seplen = strlen(sep);
}
int i;
for(i = 0; i < arlen; i++){
int n = osc_atom_u_getStringLen(osc_atom_array_u_get(array, i));
if((buflen - (ptr - *out)) < n){
long offset = ptr - *out;
int nbytes = n * (arlen - i) + 1; // assume the rest of the atoms are the same size
char *tmp = osc_mem_resize(*out, buflen + nbytes);
if(tmp){
*out = tmp;
ptr = (*out) + offset;
buflen += nbytes;
}else{
return OSC_ERR_OUTOFMEM;
}
}
ptr += osc_atom_u_getString(osc_atom_array_u_get(array, i), (buflen - (ptr - *out)), &ptr);
//*ptr++ = ' ';
if(sep){
ptr += sprintf(ptr, "%s", sep);
}
}
*ptr++ = '\0';
*len = ptr - *out;
return OSC_ERR_NONE;
}
// we can't use the copy and copyInto routines in osc_array.c because we may have an atom
// with a string that needs to be copied in which case memcpy() is not our friend.
// so unfortunately we need to iterate through every element and copy it.
t_osc_atom_array_u *osc_atom_array_u_copy(t_osc_atom_array_u *array)
{
if(!array){
return NULL;
}
long len = osc_array_getLen((t_osc_array *)array);
t_osc_ar *cp = osc_atom_array_u_alloc(len);
//memcpy(cp->ar, array->ar, cp->len * cp->membersize);
int i;
for(i = 0; i < len; i++){
t_osc_atom_u *src = osc_atom_array_u_get((t_osc_array *)array, i);
t_osc_atom_u *dest = osc_atom_array_u_get(cp, i);
osc_atom_u_copyInto(&dest, src);
}
return (t_osc_atom_array_u *)cp;
}
t_osc_err osc_atom_array_u_copyInto(t_osc_atom_array_u **dest, t_osc_atom_array_u *src, long offset)
{
if(!src){
return OSC_ERR_INVAL;
}
long srclen = osc_atom_array_u_getLen(src);
long destlen = srclen + offset;
if(!(*dest)){
*dest = osc_atom_array_u_alloc(destlen);
}else if(osc_atom_array_u_getLen(*dest) < destlen){
osc_atom_array_u_resize(*dest, destlen);
}
int i;
for(i = 0; i < srclen; i++){
t_osc_atom_u *s = osc_atom_array_u_get(src, i);
t_osc_atom_u *d = osc_atom_array_u_get(*dest, i + offset);
osc_atom_u_copyInto(&d, s);
}
return OSC_ERR_NONE;
}
void osc_atom_array_u_set(t_osc_atom_ar_u *ar, void *ptr, long len)
{
osc_array_set(ar, ptr, len, sizeof(t_osc_atom_u));
}