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