Vuo  2.4.0
VuoDictionary_VuoText_VuoReal.cc
Go to the documentation of this file.
1
10#include <sstream>
11
12#include "type.h"
13
14extern "C"
15{
16
17#include <string.h>
18
20
22#ifdef VUO_COMPILER
24 "title" : "Dictionary of Text keys and Real values",
25 "keywords" : [ ],
26 "version" : "1.0.0",
27 "dependencies" : [
28 "VuoReal",
29 "VuoText",
30 "VuoList_VuoReal",
31 "VuoList_VuoText"
32 ]
33 });
34#endif
36}
37
50{
52 json_object *o = NULL;
53
54 bool hasKeys = json_object_object_get_ex(js, "keys", &o);
55 d.keys = VuoList_VuoText_makeFromJson(hasKeys ? o : NULL);
56
57 bool hasValues = json_object_object_get_ex(js, "values", &o);
58 d.values = VuoList_VuoReal_makeFromJson(hasValues ? o : NULL);
59
60 return d;
61}
62
68{
69 json_object *js = json_object_new_object();
70
71 json_object_object_add(js, "keys", VuoList_VuoText_getJson(d.keys));
72 json_object_object_add(js, "values", VuoList_VuoReal_getJson(d.values));
73
74 return js;
75}
76
82{
83 unsigned long keyCount = VuoListGetCount_VuoText(d.keys);
84 if (keyCount == 0)
85 return strdup("Empty dictionary");
86
87 const int maxItems = 20;
88 size_t maxCharacters = 400;
89 size_t characterCount = 0;
90
91 std::ostringstream summary;
92 summary << "Dictionary containing " << keyCount << " item" << (keyCount == 1 ? "" : "s") << ": <table>";
93
94 unsigned long i;
95 for (i = 1; i <= keyCount && i <= maxItems && characterCount <= maxCharacters; ++i)
96 {
97 VuoText key = VuoListGetValue_VuoText(d.keys, i);
98 VuoReal value = VuoListGetValue_VuoReal(d.values, i);
99 char *keySummary = VuoText_getSummary(key);
100 char *valueSummary = VuoReal_getSummary(value);
101 summary << "<tr><td>" << keySummary << "</td><td> → " << valueSummary << "</td></tr>";
102 if (key)
103 characterCount += strlen(key);
104 characterCount += strlen(valueSummary);
105 free(keySummary);
106 free(valueSummary);
107 }
108
109 if (i <= keyCount)
110 summary << "<tr><td colspan=\"2\">…</td></tr>";
111
112 summary << "</table>";
113
114 return strdup(summary.str().c_str());
115}
116
122{
124}
125
130{
131 return (VuoDictionary_VuoText_VuoReal){keys, values};
132}
133
141{
142 unsigned long count = VuoListGetCount_VuoText(d.keys);
143 for (unsigned long i = 1; i <= count; ++i)
144 if (strcmp(key, VuoListGetValue_VuoText(d.keys, i)) == 0)
145 return VuoListGetValue_VuoReal(d.values, i);
146
147 return 0;
148}
149
158{
159 VuoListAppendValue_VuoText(d.keys, key);
160 VuoListAppendValue_VuoReal(d.values, value);
161}