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