Debian Codegraph @

modules/lsp-runtime-lib/src/main/i18n/JsonDictionary.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 26 февр. 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/debug.h>
23 #include <lsp-plug.in/i18n/JsonDictionary.h>
24
25 namespace lsp
26 {
27 namespace i18n
28 {
29 JsonDictionary::JsonDictionary()
30 {
31 }
32
33 JsonDictionary::~JsonDictionary()
34 {
35 // Recursively drop the dictionary tree
36 for (size_t i=0, n = vNodes.size(); i<n; ++i)
37 {
38 node_t *node = vNodes.get(i);
39 if (node == NULL)
40 continue;
41 if (node->pChild != NULL)
42 delete node->pChild;
43 delete node;
44 }
45 vNodes.flush();
46 }
47
48 status_t JsonDictionary::init(const LSPString *path)
49 {
50 json::Parser p;
51 JsonDictionary tmp;
52
53 status_t res = p.open(path, json::JSON_VERSION5);
54 if (res == STATUS_OK)
55 res = tmp.parse_json(&p);
56
57 if (res != STATUS_OK)
58 p.close();
59 else
60 res = p.close();
61
62 if (res == STATUS_OK)
63 vNodes.swap(&tmp.vNodes);
64
65 return res;
66 }
67
68 status_t JsonDictionary::init(const char *path)
69 {
70 LSPString spath;
71 if (!spath.set_utf8(path))
72 return STATUS_NO_MEM;
73 return init(&spath);
74 }
75
76 status_t JsonDictionary::init(io::IInSequence *is)
77 {
78 json::Parser p;
79 JsonDictionary tmp;
80
81 status_t res = p.wrap(is, json::JSON_VERSION5, WRAP_NONE);
82 if (res == STATUS_OK)
83 res = tmp.parse_json(&p);
84
85 if (res != STATUS_OK)
86 p.close();
87 else
88 res = p.close();
89
90 if (res == STATUS_OK)
91 vNodes.swap(&tmp.vNodes);
92
93 return res;
94 }
95
96 status_t JsonDictionary::init(io::IInStream *is)
97 {
98 json::Parser p;
99 JsonDictionary tmp;
100
101 status_t res = p.wrap(is, json::JSON_VERSION5, WRAP_NONE, "UTF-8");
102 if (res == STATUS_OK)
103 res = tmp.parse_json(&p);
104
105 if (res != STATUS_OK)
106 p.close();
107 else
108 res = p.close();
109
110 if (res == STATUS_OK)
111 vNodes.swap(&tmp.vNodes);
112
113 return res;
114 }
115
116 status_t JsonDictionary::add_node(const node_t *src)
117 {
118 // Perform binary search, the item should not exist
119 ssize_t first = 0, last = vNodes.size()-1;
120 while (first <= last)
121 {
122 ssize_t curr = (first + last) >> 1;
123 node_t *node = vNodes.uget(curr);
124 int cmp = node->sKey.compare_to(&src->sKey);
125
126 if (cmp > 0)
127 last = curr - 1;
128 else if (cmp < 0)
129 first = curr + 1;
130 else
131 return STATUS_BAD_FORMAT;
132 }
133
134 // Create new item and insert into list
135 node_t *x = new node_t;
136 if (x == NULL)
137 return STATUS_NO_MEM;
138
139 // Initialize key
140 if (!x->sKey.set(&src->sKey))
141 {
142 delete x;
143 return STATUS_NO_MEM;
144 }
145
146 // Initialize value
147 if (src->pChild != NULL)
148 x->pChild = src->pChild;
149 else if (x->sValue.set(&src->sValue))
150 x->pChild = NULL;
151 else
152 {
153 delete x;
154 return STATUS_NO_MEM;
155 }
156
157 // Add node
158 if (!vNodes.insert(first, x))
159 {
160 delete x;
161 return STATUS_NO_MEM;
162 }
163
164 return STATUS_OK;
165 }
166
167 status_t JsonDictionary::parse_json(json::Parser *p)
168 {
169 status_t res;
170 JsonDictionary *curr = NULL;
171 json::event_t ev;
172 lltl::parray<JsonDictionary> stack;
173
174 node_t node;
175
176 while ((res = p->read_next(&ev)) == STATUS_OK)
177 {
178 switch (ev.type)
179 {
180 // Start of new object
181 case json::JE_OBJECT_START:
182 if (curr == NULL)
183 {
184 curr = this;
185 break;
186 }
187
188 if (!stack.push(curr))
189 return STATUS_NO_MEM;
190
191 // Add current dictionary to stack
192 if ((node.pChild = new JsonDictionary()) == NULL)
193 return STATUS_NO_MEM;
194
195 if ((res = curr->add_node(&node)) != STATUS_OK)
196 {
197 delete node.pChild;
198 return res;
199 }
200
201 curr = node.pChild;
202 node.pChild = NULL;
203 break;
204
205 // End of current object
206 case json::JE_OBJECT_END:
207 if (!stack.pop(&curr))
208 curr = NULL;
209 else if (curr == NULL)
210 return STATUS_BAD_STATE;
211 break;
212
213 // JSON Property key
214 case json::JE_PROPERTY:
215 if (curr == NULL)
216 return STATUS_BAD_STATE;
217 if (!node.sKey.set(&ev.sValue))
218 return STATUS_NO_MEM;
219 break;
220
221 // JSON string value
222 case json::JE_STRING:
223 if (curr == NULL)
224 return STATUS_BAD_STATE;
225 if (!node.sValue.set(&ev.sValue))
226 return STATUS_NO_MEM;
227
228 node.pChild = NULL;
229 if ((res = curr->add_node(&node)) != STATUS_OK)
230 {
231 lsp_trace("Added node: '%s' = '%s'", node.sKey.get_utf8(), node.sValue.get_utf8());
232 return res;
233 }
234 break;
235
236 // Other values are invalid
237 default:
238 return STATUS_BAD_FORMAT;
239 }
240 }
241
242 // Check final state
243 if (res != STATUS_EOF)
244 return res;
245 else if ((stack.size() > 0) || (curr != NULL))
246 return STATUS_BAD_STATE;
247
248 return STATUS_OK;
249 }
250
251 JsonDictionary::node_t *JsonDictionary::find_node(const LSPString *key)
252 {
253 // Perform binary search
254 ssize_t first = 0, last = vNodes.size()-1;
255 while (first <= last)
256 {
257 ssize_t curr = (first + last) >> 1;
258 node_t *node = vNodes.uget(curr);
259 int cmp = node->sKey.compare_to(key);
260
261 if (cmp > 0)
262 last = curr - 1;
263 else if (cmp < 0)
264 first = curr + 1;
265 else
266 return node;
267 }
268
269 return NULL;
270 }
271
272 status_t JsonDictionary::lookup(const LSPString *key, LSPString *value)
273 {
274 if (key == NULL)
275 return STATUS_INVALID_VALUE;
276
277 node_t *node;
278 JsonDictionary *curr = this;
279 size_t last = 0;
280
281 // Need to lookup sub-nodes?
282 LSPString id;
283 while (true)
284 {
285 // Is there a path element?
286 ssize_t idx = key->index_of(last, '.');
287 if (idx <= 0)
288 break;
289
290 // Get a sub-string
291 if (!id.set(key, last, idx))
292 return STATUS_NO_MEM;
293
294 // Try to find node
295 node = curr->find_node(&id);
296 if ((node == NULL) || (node->pChild == NULL))
297 return STATUS_NOT_FOUND;
298
299 last = idx+1;
300 curr = node->pChild;
301 }
302
303 // Find last element
304 if (last != 0)
305 {
306 if (!id.set(key, last))
307 return STATUS_NO_MEM;
308 node = curr->find_node(&id);
309 }
310 else
311 node = curr->find_node(key);
312
313 // Analyze node
314 if (node == NULL)
315 return STATUS_NOT_FOUND;
316 else if (node->pChild != NULL)
317 {
318 // Special case: if the last node is a sub-dictionary, then try to lookup
319 // for the empty key inside of the sub-dictionary.
320 LSPString empty;
321 node = node->pChild->find_node(&empty);
322 if ((node == NULL) || (node->pChild != NULL))
323 return STATUS_NOT_FOUND;
324 }
325
326 // Set the value
327 if ((value != NULL) && (!value->set(&node->sValue)))
328 return STATUS_NO_MEM;
329
330 return STATUS_OK;
331 }
332
333 status_t JsonDictionary::lookup(const LSPString *key, IDictionary **value)
334 {
335 if (key == NULL)
336 return STATUS_INVALID_VALUE;
337
338 node_t *node;
339 JsonDictionary *curr = this;
340 size_t last = 0;
341
342 // Need to lookup sub-nodes?
343 LSPString id;
344 while (true)
345 {
346 // Is there a path element?
347 ssize_t idx = key->index_of(last, '.');
348 if (idx <= 0)
349 break;
350
351 // Get a sub-string
352 if (!id.set(key, last, idx))
353 return STATUS_NO_MEM;
354
355 // Try to find node
356 node = curr->find_node(&id);
357 if ((node == NULL) || (node->pChild == NULL))
358 return STATUS_NOT_FOUND;
359
360 last = idx+1;
361 curr = node->pChild;
362 }
363
364 // Find last element
365 if (last != 0)
366 {
367 if (!id.set(key, last))
368 return STATUS_NO_MEM;
369 node = curr->find_node(&id);
370 }
371 else
372 node = curr->find_node(key);
373
374 // Analyze node
375 if ((node == NULL) || (node->pChild == NULL))
376 return STATUS_NOT_FOUND;
377 if (value != NULL)
378 *value = node->pChild;
379
380 return STATUS_OK;
381 }
382
383 status_t JsonDictionary::get_value(size_t index, LSPString *key, LSPString *value)
384 {
385 node_t *node = vNodes.get(index);
386 if ((node == NULL) || (node->pChild != NULL))
387 return STATUS_NOT_FOUND;
388
389 if ((key != NULL) && (!key->set(&node->sKey)))
390 return STATUS_NO_MEM;
391
392 if ((value != NULL) && (!value->set(&node->sValue)))
393 return STATUS_NO_MEM;
394
395 return STATUS_OK;
396 }
397
398 status_t JsonDictionary::get_child(size_t index, LSPString *key, IDictionary **dict)
399 {
400 node_t *node = vNodes.get(index);
401 if ((node == NULL) || (node->pChild == NULL))
402 return STATUS_NOT_FOUND;
403
404 if ((key != NULL) && (!key->set(&node->sKey)))
405 return STATUS_NO_MEM;
406
407 if (dict != NULL)
408 *dict = node->pChild;
409
410 return STATUS_OK;
411 }
412
413 size_t JsonDictionary::size()
414 {
415 return vNodes.size();
416 }
417 }
418
419 } /* namespace lsp */