Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 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/Dictionary.h>
24 #include <lsp-plug.in/i18n/JsonDictionary.h>
25
26 namespace lsp
27 {
28 namespace i18n
29 {
30 Dictionary::Dictionary(resource::ILoader *loader)
31 {
32 pLoader = loader;
33 }
34
35 Dictionary::~Dictionary()
36 {
37 clear();
38 }
39
40 status_t Dictionary::load_json(IDictionary **dict, const io::Path *path)
41 {
42 JsonDictionary *d = new JsonDictionary();
43 status_t res;
44
45 if (pLoader != NULL)
46 {
47 io::IInStream *is = pLoader->read_stream(path);
48 if (is != NULL)
49 {
50 res = d->init(is);
51 is->close();
52 delete is;
53 }
54 else
55 res = pLoader->last_error();
56 }
57 else
58 res = d->init(path);
59
60 if (res == STATUS_OK)
61 *dict = d;
62 else
63 delete d;
64
65 return res;
66 }
67
68 status_t Dictionary::create_child(IDictionary **dict, const LSPString *path)
69 {
70 LSPString xp;
71 if (!xp.append(&sPath))
72 return STATUS_NO_MEM;
73 if (!xp.append('/'))
74 return STATUS_NO_MEM;
75 if (!xp.append(path))
76 return STATUS_NO_MEM;
77
78 Dictionary *d = new Dictionary(pLoader);
79 status_t res = d->init(&xp);
80 if (res != STATUS_OK)
81 delete d;
82 else
83 *dict = d;
84 return res;
85 }
86
87 status_t Dictionary::load_dictionary(const LSPString *id, IDictionary **dict)
88 {
89 status_t res;
90 io::Path path;
91
92 if ((res = path.set(&sPath)) != STATUS_OK)
93 return res;
94 if ((res = path.append_child(id)) != STATUS_OK)
95 return res;
96 if ((res = path.append(".json")) != STATUS_OK)
97 return res;
98
99 // Prefer builtin over external
100 lsp_debug("Trying to load file %s...", path.as_native());
101 res = load_json(dict, &path);
102 lsp_debug("Overall status = %d", int(res));
103
104 return res;
105 }
106
107 status_t Dictionary::lookup(const LSPString *key, LSPString *value)
108 {
109 if (key == NULL)
110 return STATUS_INVALID_VALUE;
111
112 LSPString id, subkey;
113 ssize_t idx = key->index_of('.');
114 if (idx < 0)
115 {
116 if (!id.set(key))
117 return STATUS_NO_MEM;
118 }
119 else
120 {
121 if (!id.set(key, 0, idx))
122 return STATUS_NO_MEM;
123 if (!subkey.set(key, idx+1))
124 return STATUS_NO_MEM;
125 }
126
127 // Perform binary search of the item
128 ssize_t first = 0, last = vNodes.size()-1;
129 while (first <= last)
130 {
131 ssize_t curr = (first + last) >> 1;
132 node_t *node = vNodes.uget(curr);
133 int cmp = node->sKey.compare_to(&id);
134
135 if (cmp > 0)
136 last = curr - 1;
137 else if (cmp < 0)
138 first = curr + 1;
139 else
140 {
141 if (id.is_empty())
142 return STATUS_NOT_FOUND;
143 return (node->pDict != NULL) ? node->pDict->lookup(&subkey, value) : STATUS_NOT_FOUND;
144 }
145 }
146
147 // Dictionary identifier was specified?
148 if (id.is_empty())
149 return STATUS_NOT_FOUND;
150
151 // Dictionary not found, try to create new one
152 IDictionary *dict = NULL;
153 status_t res = load_dictionary(&id, &dict);
154 if (res == STATUS_NOT_FOUND)
155 res = create_child(&dict, &id);
156
157 // Add node to list of nodes
158 if (res != STATUS_OK)
159 return res;
160
161 node_t *child = new node_t;
162 if ((child == NULL) || (!vNodes.insert(first, child)))
163 {
164 delete dict;
165 return STATUS_NO_MEM;
166 }
167
168 child->sKey.swap(&id);
169 child->pDict = dict;
170
171 return dict->lookup(&subkey, value);
172 }
173
174 status_t Dictionary::lookup(const LSPString *key, IDictionary **value)
175 {
176 if (key == NULL)
177 return STATUS_INVALID_VALUE;
178
179 ssize_t idx = key->index_of('.');
180 LSPString id, subkey;
181
182 if (idx > 0)
183 {
184 if (!id.set(key, 0, idx))
185 return STATUS_NO_MEM;
186 if (!subkey.set(key, idx+1))
187 return STATUS_NO_MEM;
188 }
189 else if (!id.set(key))
190 return STATUS_NO_MEM;
191
192 // Perform binary search of the dictionary
193 IDictionary *dict = NULL;
194 ssize_t first = 0, last = vNodes.size()-1;
195 while (first <= last)
196 {
197 ssize_t curr = (first + last) >> 1;
198 node_t *node = vNodes.uget(curr);
199 int cmp = node->sKey.compare_to(&id);
200
201 if (cmp > 0)
202 last = curr - 1;
203 else if (cmp < 0)
204 first = curr + 1;
205 else
206 {
207 if (node->pDict == NULL)
208 return STATUS_NOT_FOUND;
209 dict = node->pDict;
210 break;
211 }
212 }
213
214 // Dictionary object not found?
215 if (dict == NULL)
216 {
217 // Try to load/create node
218 bool root = false;
219 status_t res = load_dictionary(&id, &dict);
220 if (res == STATUS_NOT_FOUND)
221 {
222 res = create_child(&dict, &id);
223 root = true;
224 }
225 if (res != STATUS_OK)
226 return res;
227
228 // Add node to list of nodes
229 node_t *child = new node_t;
230 if ((child == NULL) || (!vNodes.insert(first, child)))
231 {
232 delete dict;
233 return STATUS_NO_MEM;
234 }
235
236 child->sKey.swap(&id);
237 child->pDict = dict;
238 child->bRoot = root;
239
240 // There is no path element defined more?
241 if ((child->bRoot) && (idx < 0))
242 return STATUS_NOT_FOUND;
243 }
244
245 if (idx > 0)
246 return dict->lookup(&subkey, value);
247
248 *value = dict;
249 return STATUS_OK;
250 }
251
252 status_t Dictionary::get_value(size_t index, LSPString *key, LSPString *value)
253 {
254 node_t *node = vNodes.get(index);
255 return (node == NULL) ? STATUS_NOT_FOUND : STATUS_BAD_TYPE;
256 }
257
258 status_t Dictionary::get_child(size_t index, LSPString *key, IDictionary **dict)
259 {
260 node_t *node = vNodes.get(index);
261 if ((node == NULL) || (node->pDict == NULL))
262 return STATUS_NOT_FOUND;
263
264 if ((key != NULL) && (!key->set(&node->sKey)))
265 return STATUS_NO_MEM;
266
267 if (dict != NULL)
268 *dict = node->pDict;
269
270 return STATUS_OK;
271 }
272
273 size_t Dictionary::size()
274 {
275 return vNodes.size();
276 }
277
278 status_t Dictionary::Dictionary::init(const LSPString *path)
279 {
280 lsp_trace("Init dictionary path: %s" , path->get_utf8());
281 return (sPath.set(path)) ? STATUS_OK : STATUS_NO_MEM;
282 }
283
284 void Dictionary::Dictionary::clear()
285 {
286 for (size_t i=0, n=vNodes.size(); i<n; ++i)
287 {
288 node_t *node = vNodes.uget(i);
289 if (node == NULL)
290 continue;
291
292 if (node->pDict != NULL)
293 delete node->pDict;
294 delete node;
295 }
296
297 vNodes.flush();
298 }
299
300 } /* namespace i18n */
301 } /* namespace lsp */