Vuo 2.4.4
Loading...
Searching...
No Matches
VuoOscMessage.c
Go to the documentation of this file.
1
10#include "VuoOscMessage.h"
11
13#ifdef VUO_COMPILER
15 "title" : "OSC Message",
16 "description" : "An OSC message.",
17 "keywords" : [ ],
18 "version" : "1.0.0",
19 "dependencies" : [
20 "VuoOscType",
21 "VuoText"
22 ]
23 });
24#endif
26
27
34void VuoOscMessage_free(void *message)
35{
36 VuoOscMessage o = (VuoOscMessage)message;
37 VuoRelease(o->address);
38
39 for (unsigned int i = 0; i < o->dataCount; ++i)
40 json_object_put(o->data[i]);
41
42 free(o);
43}
44
53VuoOscMessage VuoOscMessage_make(VuoText address, unsigned int dataCount, struct json_object **data, VuoOscType *dataTypes)
54{
55 VuoOscMessage o = (VuoOscMessage)malloc(sizeof(struct _VuoOscMessage));
57
58 o->address = address;
59 VuoRetain(o->address);
60
61 if (dataCount > VUOOSC_MAX_MESSAGE_ARGUMENTS)
62 VUserLog("Warning: OSC message has more than %d arguments; ignoring extras.", VUOOSC_MAX_MESSAGE_ARGUMENTS);
63
64 o->dataCount = MIN(dataCount, VUOOSC_MAX_MESSAGE_ARGUMENTS);
65 if (dataCount > 0)
66 {
67 memcpy(o->data, data, sizeof(struct json_object *) * o->dataCount);
68 memcpy(o->dataTypes, dataTypes, sizeof(VuoOscType) * o->dataCount);
69 }
70
71 return o;
72}
73
86{
87 if (!js)
88 return NULL;
89
90 json_object *o = NULL;
91
92 VuoText address = NULL;
93 if (json_object_object_get_ex(js, "address", &o))
94 address = VuoText_makeFromJson(o);
95
96 unsigned int dataCount = 0;
97 json_object *data[VUOOSC_MAX_MESSAGE_ARGUMENTS];
99 if (json_object_object_get_ex(js, "data", &o))
100 {
101 dataCount = json_object_array_length(o);
102 for (unsigned int i = 0; i < dataCount; ++i)
103 {
104 json_object *di = json_object_array_get_idx(o, i);
105 json_object *v;
106 if (json_object_object_get_ex(di, "type", &v))
107 dataTypes[i] = VuoOscType_makeFromJson(v);
108 if (json_object_object_get_ex(di, "data", &v))
109 data[i] = json_object_get(v);
110 }
111 }
112
113 return VuoOscMessage_make(address, dataCount, data, dataTypes);
114}
115
120json_object * VuoOscMessage_getJson(const VuoOscMessage value)
121{
122 json_object *js = json_object_new_object();
123
124 if (!value)
125 return NULL;
126
127 if (value->address)
128 json_object_object_add(js, "address", json_object_new_string(value->address));
129
130 if (value->dataCount)
131 {
132 struct json_object *data = json_object_new_array();
133 for (unsigned int i = 0; i < value->dataCount; ++i)
134 {
135 struct json_object *v = json_object_new_object();
136 json_object_object_add(v, "type", VuoOscType_getJson(value->dataTypes[i]));
137 json_object_object_add(v, "data", json_object_get(value->data[i]));
138 json_object_array_add(data, v);
139 }
140 json_object_object_add(js, "data", data);
141 }
142
143 return js;
144}
145
151{
152 if (!value)
153 return strdup("No message");
154
155 int dataCount = value->dataCount;
156 char *data[dataCount];
157 if (dataCount)
158 {
159 for (int i = 0; i < dataCount; ++i)
160 {
161 json_object *o = value->data[i];
162 switch (json_object_get_type(o))
163 {
164 case json_type_null:
165 data[i] = strdup("null");
166 break;
167 case json_type_boolean:
168 data[i] = strdup(json_object_get_boolean(o) ? "true" : "false");
169 break;
170 case json_type_double:
171 {
172 double v = json_object_get_double(o);
173 data[i] = VuoText_format("%g", v);
174 break;
175 }
176 case json_type_int:
177 {
178 long long v = json_object_get_int64(o);
179 data[i] = VuoText_format("%lld", v);
180 break;
181 }
182 case json_type_string:
183 data[i] = strdup(json_object_get_string(o));
184 break;
185 default:
186 data[i] = strdup("?");
187 }
188 }
189 }
190
191 int dataSize = 0;
192 for (int i = 0; i < dataCount; ++i)
193 dataSize += strlen(data[i]);
194 if (dataCount > 1)
195 dataSize += (dataCount - 1) * strlen(", ");
196 dataSize += 1; // Null terminator
197
198 char *compositeData = (char *)malloc(dataSize);
199 compositeData[0] = 0;
200 for (int i = 0; i < dataCount; ++i)
201 {
202 if (i>0)
203 strlcat(compositeData, ", ", dataSize);
204 strlcat(compositeData, data[i], dataSize);
205 }
206
207 char *valueAsString = VuoText_format("%s<br>[ %s ]", value->address, compositeData);
208
209 free(compositeData);
210 for (int i = 0; i < dataCount; ++i)
211 free(data[i]);
212
213 return valueAsString;
214}