Vuo 2.4.4
Loading...
Searching...
No Matches
VuoDictionary_VuoText_VuoText.cc
Go to the documentation of this file.
1
10#include <sstream>
11
12extern "C"
13{
14
16
18#ifdef VUO_COMPILER
20 "title" : "Dictionary of Text keys and Text values",
21 "keywords" : [ ],
22 "version" : "1.0.0",
23 "dependencies" : [
24 "VuoText",
25 "VuoText",
26 "VuoList_VuoText",
27 "VuoList_VuoText"
28 ]
29 });
30#endif
32}
33
46{
48 json_object *o = NULL;
49
50 bool hasKeys = json_object_object_get_ex(js, "keys", &o);
51 d.keys = VuoList_VuoText_makeFromJson(hasKeys ? o : NULL);
52
53 bool hasValues = json_object_object_get_ex(js, "values", &o);
54 d.values = VuoList_VuoText_makeFromJson(hasValues ? o : NULL);
55
56 return d;
57}
58
64{
65 json_object *js = json_object_new_object();
66
67 json_object_object_add(js, "keys", VuoList_VuoText_getJson(d.keys));
68 json_object_object_add(js, "values", VuoList_VuoText_getJson(d.values));
69
70 return js;
71}
72
78{
79 unsigned long keyCount = VuoListGetCount_VuoText(d.keys);
80 if (keyCount == 0)
81 return strdup("Empty dictionary");
82
83 const int maxItems = 20;
84 size_t maxCharacters = 400;
85 size_t characterCount = 0;
86
87 std::ostringstream summary;
88 summary << "Dictionary containing " << keyCount << " item" << (keyCount == 1 ? "" : "s") << ": <table>";
89
90 unsigned long i;
91 for (i = 1; i <= keyCount && i <= maxItems && characterCount <= maxCharacters; ++i)
92 {
93 VuoText key = VuoListGetValue_VuoText(d.keys, i);
94 VuoText value = VuoListGetValue_VuoText(d.values, i);
95 char *keySummary = VuoText_getSummary(key);
96 char *valueSummary = VuoText_getSummary(value);
97 summary << "<tr><td>" << keySummary << "</td><td> → " << valueSummary << "</td></tr>";
98 if (key)
99 characterCount += strlen(key);
100 if (value)
101 characterCount += strlen(value);
102 free(keySummary);
103 free(valueSummary);
104 }
105
106 if (i <= keyCount)
107 summary << "<tr><td colspan=\"2\">…</td></tr>";
108
109 summary << "</table>";
110
111 return strdup(summary.str().c_str());
112}
113
122
130
138{
139 unsigned long count = VuoListGetCount_VuoText(d.keys);
140 for (unsigned long i = 1; i <= count; ++i)
141 if (strcmp(key, VuoListGetValue_VuoText(d.keys, i)) == 0)
142 return VuoListGetValue_VuoText(d.values, i);
143
144 return VuoText_make("");
145}
146
159
165{
166 VuoRetain(value.keys);
167 VuoRetain(value.values);
168}
169
175{
176 VuoRelease(value.keys);
177 VuoRelease(value.values);
178}