Vuo  2.4.0
VuoData.c
Go to the documentation of this file.
1
10#include "type.h"
11#include "VuoData.h"
12#include "VuoBase64.h"
13
15#ifdef VUO_COMPILER
17 "title" : "Binary Data",
18 "description" : "A blob of 8-bit binary data.",
19 "keywords" : [ ],
20 "version" : "1.0.0",
21 "dependencies" : [
22 "VuoBase64",
23 "VuoText"
24 ]
25 });
26#endif
28
35{
36 VuoData value = {0, NULL};
37
38 if (json_object_get_type(js) != json_type_string)
39 return value;
40
41 value.data = VuoBase64_decode(json_object_get_string(js), &value.size);
42 VuoRegister(value.data, free);
43 return value;
44}
45
50{
51 if (!value.data)
52 return NULL;
53
54 char *encoded = VuoBase64_encode(value.size, value.data);
55 json_object *js = json_object_new_string(encoded);
56 free(encoded);
57 return js;
58}
59
63bool VuoData_areEqual(const VuoData valueA, const VuoData valueB)
64{
65 if (valueA.size != valueB.size)
66 return false;
67
68 if (!valueA.data || !valueB.data)
69 return (!valueA.data && !valueB.data);
70
71 return memcmp(valueA.data, valueB.data, valueA.size) == 0;
72}
73
77bool VuoData_isLessThan(const VuoData valueA, const VuoData valueB)
78{
79 // Treat null data as greater than non-null data,
80 // so the more useful non-null data sorts to the beginning of the list.
81 if (!valueA.data || !valueB.data)
82 return valueA.data && !valueB.data;
83
84 int prefixCmp = memcmp(valueA.data, valueB.data, MIN(valueA.size, valueB.size));
85 if (!prefixCmp)
86 return (valueA.size < valueB.size);
87 else
88 return (prefixCmp < 0);
89}
90
94char *VuoData_getSummary(const VuoData value)
95{
96 if (value.data)
97 return VuoText_format("%lld byte%s of data", value.size, value.size == 1 ? "" : "s");
98 else
99 return strdup("no data");
100}
101
109VuoData VuoData_make(VuoInteger size, unsigned char *data)
110{
111 VuoData value;
112 value.size = size;
113 value.data = (char *)data;
114
115 if (data)
116 VuoRegister(value.data, free);
117
118 return value;
119}
120
127{
128 VuoData value;
129 value.size = strlen(text);
130 value.data = (char *)malloc(value.size);
131 memcpy(value.data, text, value.size);
132 VuoRegister(value.data, free);
133 return value;
134}
135
142{
143 char *dataAsString = (char *)malloc(data.size + 1);
144 dataAsString[data.size] = 0;
145 memcpy(dataAsString, data.data, data.size);
146 return dataAsString;
147}