]>
Commit | Line | Data |
---|---|---|
fb08dde0 | 1 | /* |
41836a9f | 2 | * QDict Module |
fb08dde0 LC |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * | |
41836a9f LC |
9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
10 | * See the COPYING.LIB file in the top-level directory. | |
fb08dde0 LC |
11 | */ |
12 | ||
7b1b5d19 PB |
13 | #include "qapi/qmp/qint.h" |
14 | #include "qapi/qmp/qfloat.h" | |
15 | #include "qapi/qmp/qdict.h" | |
16 | #include "qapi/qmp/qbool.h" | |
17 | #include "qapi/qmp/qstring.h" | |
18 | #include "qapi/qmp/qobject.h" | |
1de7afc9 | 19 | #include "qemu/queue.h" |
fb08dde0 LC |
20 | #include "qemu-common.h" |
21 | ||
aa43d9cc BS |
22 | static void qdict_destroy_obj(QObject *obj); |
23 | ||
24 | static const QType qdict_type = { | |
25 | .code = QTYPE_QDICT, | |
26 | .destroy = qdict_destroy_obj, | |
27 | }; | |
fb08dde0 LC |
28 | |
29 | /** | |
30 | * qdict_new(): Create a new QDict | |
31 | * | |
32 | * Return strong reference. | |
33 | */ | |
34 | QDict *qdict_new(void) | |
35 | { | |
36 | QDict *qdict; | |
37 | ||
7267c094 | 38 | qdict = g_malloc0(sizeof(*qdict)); |
fb08dde0 LC |
39 | QOBJECT_INIT(qdict, &qdict_type); |
40 | ||
41 | return qdict; | |
42 | } | |
43 | ||
44 | /** | |
45 | * qobject_to_qdict(): Convert a QObject into a QDict | |
46 | */ | |
47 | QDict *qobject_to_qdict(const QObject *obj) | |
48 | { | |
89cad9f3 | 49 | if (!obj || qobject_type(obj) != QTYPE_QDICT) { |
fb08dde0 | 50 | return NULL; |
89cad9f3 | 51 | } |
fb08dde0 LC |
52 | return container_of(obj, QDict, base); |
53 | } | |
54 | ||
55 | /** | |
56 | * tdb_hash(): based on the hash agorithm from gdbm, via tdb | |
57 | * (from module-init-tools) | |
58 | */ | |
59 | static unsigned int tdb_hash(const char *name) | |
60 | { | |
61 | unsigned value; /* Used to compute the hash value. */ | |
62 | unsigned i; /* Used to cycle through random values. */ | |
63 | ||
64 | /* Set the initial value from the key size. */ | |
65 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) | |
66 | value = (value + (((const unsigned char *)name)[i] << (i*5 % 24))); | |
67 | ||
68 | return (1103515243 * value + 12345); | |
69 | } | |
70 | ||
71 | /** | |
72 | * alloc_entry(): allocate a new QDictEntry | |
73 | */ | |
74 | static QDictEntry *alloc_entry(const char *key, QObject *value) | |
75 | { | |
76 | QDictEntry *entry; | |
77 | ||
7267c094 AL |
78 | entry = g_malloc0(sizeof(*entry)); |
79 | entry->key = g_strdup(key); | |
fb08dde0 LC |
80 | entry->value = value; |
81 | ||
82 | return entry; | |
83 | } | |
84 | ||
0d078b2a LC |
85 | /** |
86 | * qdict_entry_value(): Return qdict entry value | |
87 | * | |
88 | * Return weak reference. | |
89 | */ | |
90 | QObject *qdict_entry_value(const QDictEntry *entry) | |
91 | { | |
92 | return entry->value; | |
93 | } | |
94 | ||
95 | /** | |
96 | * qdict_entry_key(): Return qdict entry key | |
97 | * | |
98 | * Return a *pointer* to the string, it has to be duplicated before being | |
99 | * stored. | |
100 | */ | |
101 | const char *qdict_entry_key(const QDictEntry *entry) | |
102 | { | |
103 | return entry->key; | |
104 | } | |
105 | ||
fb08dde0 LC |
106 | /** |
107 | * qdict_find(): List lookup function | |
108 | */ | |
109 | static QDictEntry *qdict_find(const QDict *qdict, | |
c8bc3cd7 | 110 | const char *key, unsigned int bucket) |
fb08dde0 LC |
111 | { |
112 | QDictEntry *entry; | |
113 | ||
c8bc3cd7 | 114 | QLIST_FOREACH(entry, &qdict->table[bucket], next) |
fb08dde0 LC |
115 | if (!strcmp(entry->key, key)) |
116 | return entry; | |
117 | ||
118 | return NULL; | |
119 | } | |
120 | ||
121 | /** | |
122 | * qdict_put_obj(): Put a new QObject into the dictionary | |
123 | * | |
124 | * Insert the pair 'key:value' into 'qdict', if 'key' already exists | |
125 | * its 'value' will be replaced. | |
126 | * | |
127 | * This is done by freeing the reference to the stored QObject and | |
128 | * storing the new one in the same entry. | |
129 | * | |
130 | * NOTE: ownership of 'value' is transferred to the QDict | |
131 | */ | |
132 | void qdict_put_obj(QDict *qdict, const char *key, QObject *value) | |
133 | { | |
c8bc3cd7 | 134 | unsigned int bucket; |
fb08dde0 LC |
135 | QDictEntry *entry; |
136 | ||
c8bc3cd7 LC |
137 | bucket = tdb_hash(key) % QDICT_BUCKET_MAX; |
138 | entry = qdict_find(qdict, key, bucket); | |
fb08dde0 LC |
139 | if (entry) { |
140 | /* replace key's value */ | |
141 | qobject_decref(entry->value); | |
142 | entry->value = value; | |
143 | } else { | |
144 | /* allocate a new entry */ | |
145 | entry = alloc_entry(key, value); | |
c8bc3cd7 | 146 | QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next); |
29ec3156 | 147 | qdict->size++; |
fb08dde0 | 148 | } |
fb08dde0 LC |
149 | } |
150 | ||
151 | /** | |
152 | * qdict_get(): Lookup for a given 'key' | |
153 | * | |
154 | * Return a weak reference to the QObject associated with 'key' if | |
155 | * 'key' is present in the dictionary, NULL otherwise. | |
156 | */ | |
157 | QObject *qdict_get(const QDict *qdict, const char *key) | |
158 | { | |
159 | QDictEntry *entry; | |
160 | ||
c8bc3cd7 | 161 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX); |
fb08dde0 LC |
162 | return (entry == NULL ? NULL : entry->value); |
163 | } | |
164 | ||
165 | /** | |
166 | * qdict_haskey(): Check if 'key' exists | |
167 | * | |
168 | * Return 1 if 'key' exists in the dict, 0 otherwise | |
169 | */ | |
170 | int qdict_haskey(const QDict *qdict, const char *key) | |
171 | { | |
c8bc3cd7 LC |
172 | unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX; |
173 | return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1); | |
fb08dde0 LC |
174 | } |
175 | ||
176 | /** | |
177 | * qdict_size(): Return the size of the dictionary | |
178 | */ | |
179 | size_t qdict_size(const QDict *qdict) | |
180 | { | |
181 | return qdict->size; | |
182 | } | |
183 | ||
184 | /** | |
185 | * qdict_get_obj(): Get a QObject of a specific type | |
186 | */ | |
187 | static QObject *qdict_get_obj(const QDict *qdict, const char *key, | |
188 | qtype_code type) | |
189 | { | |
190 | QObject *obj; | |
191 | ||
192 | obj = qdict_get(qdict, key); | |
193 | assert(obj != NULL); | |
194 | assert(qobject_type(obj) == type); | |
195 | ||
196 | return obj; | |
197 | } | |
198 | ||
acc3b033 MA |
199 | /** |
200 | * qdict_get_double(): Get an number mapped by 'key' | |
201 | * | |
202 | * This function assumes that 'key' exists and it stores a | |
203 | * QFloat or QInt object. | |
204 | * | |
205 | * Return number mapped by 'key'. | |
206 | */ | |
207 | double qdict_get_double(const QDict *qdict, const char *key) | |
208 | { | |
209 | QObject *obj = qdict_get(qdict, key); | |
210 | ||
211 | assert(obj); | |
212 | switch (qobject_type(obj)) { | |
213 | case QTYPE_QFLOAT: | |
214 | return qfloat_get_double(qobject_to_qfloat(obj)); | |
215 | case QTYPE_QINT: | |
216 | return qint_get_int(qobject_to_qint(obj)); | |
217 | default: | |
43dc2a64 | 218 | abort(); |
acc3b033 MA |
219 | } |
220 | } | |
221 | ||
fb08dde0 LC |
222 | /** |
223 | * qdict_get_int(): Get an integer mapped by 'key' | |
224 | * | |
225 | * This function assumes that 'key' exists and it stores a | |
226 | * QInt object. | |
227 | * | |
228 | * Return integer mapped by 'key'. | |
229 | */ | |
230 | int64_t qdict_get_int(const QDict *qdict, const char *key) | |
231 | { | |
fcf73f66 | 232 | return qint_get_int(qobject_to_qint(qdict_get(qdict, key))); |
fb08dde0 LC |
233 | } |
234 | ||
cd4dde36 LC |
235 | /** |
236 | * qdict_get_bool(): Get a bool mapped by 'key' | |
237 | * | |
238 | * This function assumes that 'key' exists and it stores a | |
239 | * QBool object. | |
240 | * | |
241 | * Return bool mapped by 'key'. | |
242 | */ | |
34acbc95 | 243 | bool qdict_get_bool(const QDict *qdict, const char *key) |
cd4dde36 | 244 | { |
14b61600 | 245 | return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key))); |
cd4dde36 LC |
246 | } |
247 | ||
f2e17508 LC |
248 | /** |
249 | * qdict_get_qlist(): Get the QList mapped by 'key' | |
250 | * | |
251 | * This function assumes that 'key' exists and it stores a | |
252 | * QList object. | |
253 | * | |
254 | * Return QList mapped by 'key'. | |
255 | */ | |
256 | QList *qdict_get_qlist(const QDict *qdict, const char *key) | |
257 | { | |
258 | return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST)); | |
259 | } | |
260 | ||
df10ce6a LC |
261 | /** |
262 | * qdict_get_qdict(): Get the QDict mapped by 'key' | |
263 | * | |
264 | * This function assumes that 'key' exists and it stores a | |
265 | * QDict object. | |
266 | * | |
267 | * Return QDict mapped by 'key'. | |
268 | */ | |
269 | QDict *qdict_get_qdict(const QDict *qdict, const char *key) | |
270 | { | |
89cad9f3 | 271 | return qobject_to_qdict(qdict_get(qdict, key)); |
df10ce6a LC |
272 | } |
273 | ||
fb08dde0 LC |
274 | /** |
275 | * qdict_get_str(): Get a pointer to the stored string mapped | |
276 | * by 'key' | |
277 | * | |
278 | * This function assumes that 'key' exists and it stores a | |
279 | * QString object. | |
280 | * | |
281 | * Return pointer to the string mapped by 'key'. | |
282 | */ | |
283 | const char *qdict_get_str(const QDict *qdict, const char *key) | |
284 | { | |
285 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING); | |
286 | return qstring_get_str(qobject_to_qstring(obj)); | |
287 | } | |
288 | ||
289 | /** | |
290 | * qdict_get_try_int(): Try to get integer mapped by 'key' | |
291 | * | |
292 | * Return integer mapped by 'key', if it is not present in | |
293 | * the dictionary or if the stored object is not of QInt type | |
83aba69e | 294 | * 'def_value' will be returned. |
fb08dde0 LC |
295 | */ |
296 | int64_t qdict_get_try_int(const QDict *qdict, const char *key, | |
83aba69e | 297 | int64_t def_value) |
fb08dde0 | 298 | { |
fcf73f66 | 299 | QInt *qint = qobject_to_qint(qdict_get(qdict, key)); |
fb08dde0 | 300 | |
fcf73f66 | 301 | return qint ? qint_get_int(qint) : def_value; |
fb08dde0 LC |
302 | } |
303 | ||
35006ac8 LC |
304 | /** |
305 | * qdict_get_try_bool(): Try to get a bool mapped by 'key' | |
306 | * | |
307 | * Return bool mapped by 'key', if it is not present in the | |
308 | * dictionary or if the stored object is not of QBool type | |
309 | * 'def_value' will be returned. | |
310 | */ | |
34acbc95 | 311 | bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value) |
35006ac8 | 312 | { |
14b61600 | 313 | QBool *qbool = qobject_to_qbool(qdict_get(qdict, key)); |
35006ac8 | 314 | |
14b61600 | 315 | return qbool ? qbool_get_bool(qbool) : def_value; |
35006ac8 LC |
316 | } |
317 | ||
fb08dde0 LC |
318 | /** |
319 | * qdict_get_try_str(): Try to get a pointer to the stored string | |
320 | * mapped by 'key' | |
321 | * | |
322 | * Return a pointer to the string mapped by 'key', if it is not present | |
323 | * in the dictionary or if the stored object is not of QString type | |
324 | * NULL will be returned. | |
325 | */ | |
326 | const char *qdict_get_try_str(const QDict *qdict, const char *key) | |
327 | { | |
328 | QObject *obj; | |
329 | ||
330 | obj = qdict_get(qdict, key); | |
331 | if (!obj || qobject_type(obj) != QTYPE_QSTRING) | |
332 | return NULL; | |
333 | ||
334 | return qstring_get_str(qobject_to_qstring(obj)); | |
335 | } | |
336 | ||
21f800d3 LC |
337 | /** |
338 | * qdict_iter(): Iterate over all the dictionary's stored values. | |
339 | * | |
340 | * This function allows the user to provide an iterator, which will be | |
341 | * called for each stored value in the dictionary. | |
342 | */ | |
343 | void qdict_iter(const QDict *qdict, | |
344 | void (*iter)(const char *key, QObject *obj, void *opaque), | |
345 | void *opaque) | |
346 | { | |
347 | int i; | |
348 | QDictEntry *entry; | |
349 | ||
c8bc3cd7 | 350 | for (i = 0; i < QDICT_BUCKET_MAX; i++) { |
21f800d3 LC |
351 | QLIST_FOREACH(entry, &qdict->table[i], next) |
352 | iter(entry->key, entry->value, opaque); | |
353 | } | |
354 | } | |
355 | ||
f2b07f35 LC |
356 | static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket) |
357 | { | |
358 | int i; | |
359 | ||
360 | for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) { | |
361 | if (!QLIST_EMPTY(&qdict->table[i])) { | |
362 | return QLIST_FIRST(&qdict->table[i]); | |
363 | } | |
364 | } | |
365 | ||
366 | return NULL; | |
367 | } | |
368 | ||
369 | /** | |
370 | * qdict_first(): Return first qdict entry for iteration. | |
371 | */ | |
372 | const QDictEntry *qdict_first(const QDict *qdict) | |
373 | { | |
374 | return qdict_next_entry(qdict, 0); | |
375 | } | |
376 | ||
377 | /** | |
378 | * qdict_next(): Return next qdict entry in an iteration. | |
379 | */ | |
380 | const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry) | |
381 | { | |
382 | QDictEntry *ret; | |
383 | ||
384 | ret = QLIST_NEXT(entry, next); | |
385 | if (!ret) { | |
386 | unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX; | |
387 | ret = qdict_next_entry(qdict, bucket + 1); | |
388 | } | |
389 | ||
390 | return ret; | |
391 | } | |
392 | ||
b382bc9a KW |
393 | /** |
394 | * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but | |
395 | * another reference is added. | |
396 | */ | |
397 | QDict *qdict_clone_shallow(const QDict *src) | |
398 | { | |
399 | QDict *dest; | |
400 | QDictEntry *entry; | |
401 | int i; | |
402 | ||
403 | dest = qdict_new(); | |
404 | ||
405 | for (i = 0; i < QDICT_BUCKET_MAX; i++) { | |
406 | QLIST_FOREACH(entry, &src->table[i], next) { | |
407 | qobject_incref(entry->value); | |
408 | qdict_put_obj(dest, entry->key, entry->value); | |
409 | } | |
410 | } | |
411 | ||
412 | return dest; | |
413 | } | |
414 | ||
fb08dde0 LC |
415 | /** |
416 | * qentry_destroy(): Free all the memory allocated by a QDictEntry | |
417 | */ | |
418 | static void qentry_destroy(QDictEntry *e) | |
419 | { | |
420 | assert(e != NULL); | |
421 | assert(e->key != NULL); | |
422 | assert(e->value != NULL); | |
423 | ||
424 | qobject_decref(e->value); | |
7267c094 AL |
425 | g_free(e->key); |
426 | g_free(e); | |
fb08dde0 LC |
427 | } |
428 | ||
429 | /** | |
430 | * qdict_del(): Delete a 'key:value' pair from the dictionary | |
431 | * | |
432 | * This will destroy all data allocated by this entry. | |
433 | */ | |
434 | void qdict_del(QDict *qdict, const char *key) | |
435 | { | |
436 | QDictEntry *entry; | |
437 | ||
c8bc3cd7 | 438 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX); |
fb08dde0 | 439 | if (entry) { |
72cf2d4f | 440 | QLIST_REMOVE(entry, next); |
fb08dde0 LC |
441 | qentry_destroy(entry); |
442 | qdict->size--; | |
443 | } | |
444 | } | |
445 | ||
446 | /** | |
447 | * qdict_destroy_obj(): Free all the memory allocated by a QDict | |
448 | */ | |
449 | static void qdict_destroy_obj(QObject *obj) | |
450 | { | |
451 | int i; | |
452 | QDict *qdict; | |
453 | ||
454 | assert(obj != NULL); | |
455 | qdict = qobject_to_qdict(obj); | |
456 | ||
c8bc3cd7 | 457 | for (i = 0; i < QDICT_BUCKET_MAX; i++) { |
72cf2d4f | 458 | QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
fb08dde0 | 459 | while (entry) { |
72cf2d4f BS |
460 | QDictEntry *tmp = QLIST_NEXT(entry, next); |
461 | QLIST_REMOVE(entry, next); | |
fb08dde0 LC |
462 | qentry_destroy(entry); |
463 | entry = tmp; | |
464 | } | |
465 | } | |
466 | ||
7267c094 | 467 | g_free(qdict); |
fb08dde0 | 468 | } |
f660dc6a | 469 | |
7990d2c9 KW |
470 | /** |
471 | * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the | |
472 | * value of 'key' in 'src' is copied there (and the refcount increased | |
473 | * accordingly). | |
474 | */ | |
475 | void qdict_copy_default(QDict *dst, QDict *src, const char *key) | |
476 | { | |
477 | QObject *val; | |
478 | ||
479 | if (qdict_haskey(dst, key)) { | |
480 | return; | |
481 | } | |
482 | ||
483 | val = qdict_get(src, key); | |
484 | if (val) { | |
485 | qobject_incref(val); | |
486 | qdict_put_obj(dst, key, val); | |
487 | } | |
488 | } | |
489 | ||
490 | /** | |
491 | * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a | |
492 | * new QString initialised by 'val' is put there. | |
493 | */ | |
494 | void qdict_set_default_str(QDict *dst, const char *key, const char *val) | |
495 | { | |
496 | if (qdict_haskey(dst, key)) { | |
497 | return; | |
498 | } | |
499 | ||
500 | qdict_put(dst, key, qstring_from_str(val)); | |
501 | } | |
502 | ||
9f23fc0c HR |
503 | static void qdict_flatten_qdict(QDict *qdict, QDict *target, |
504 | const char *prefix); | |
505 | ||
506 | static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix) | |
507 | { | |
508 | QObject *value; | |
509 | const QListEntry *entry; | |
510 | char *new_key; | |
511 | int i; | |
512 | ||
513 | /* This function is never called with prefix == NULL, i.e., it is always | |
514 | * called from within qdict_flatten_q(list|dict)(). Therefore, it does not | |
515 | * need to remove list entries during the iteration (the whole list will be | |
516 | * deleted eventually anyway from qdict_flatten_qdict()). */ | |
517 | assert(prefix); | |
518 | ||
519 | entry = qlist_first(qlist); | |
520 | ||
521 | for (i = 0; entry; entry = qlist_next(entry), i++) { | |
522 | value = qlist_entry_obj(entry); | |
523 | new_key = g_strdup_printf("%s.%i", prefix, i); | |
524 | ||
525 | if (qobject_type(value) == QTYPE_QDICT) { | |
526 | qdict_flatten_qdict(qobject_to_qdict(value), target, new_key); | |
527 | } else if (qobject_type(value) == QTYPE_QLIST) { | |
528 | qdict_flatten_qlist(qobject_to_qlist(value), target, new_key); | |
529 | } else { | |
530 | /* All other types are moved to the target unchanged. */ | |
531 | qobject_incref(value); | |
532 | qdict_put_obj(target, new_key, value); | |
533 | } | |
534 | ||
535 | g_free(new_key); | |
536 | } | |
537 | } | |
538 | ||
539 | static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix) | |
f660dc6a KW |
540 | { |
541 | QObject *value; | |
542 | const QDictEntry *entry, *next; | |
6273d113 | 543 | char *new_key; |
f660dc6a KW |
544 | bool delete; |
545 | ||
546 | entry = qdict_first(qdict); | |
547 | ||
548 | while (entry != NULL) { | |
549 | ||
550 | next = qdict_next(qdict, entry); | |
551 | value = qdict_entry_value(entry); | |
552 | new_key = NULL; | |
553 | delete = false; | |
554 | ||
555 | if (prefix) { | |
f660dc6a | 556 | new_key = g_strdup_printf("%s.%s", prefix, entry->key); |
f660dc6a KW |
557 | } |
558 | ||
559 | if (qobject_type(value) == QTYPE_QDICT) { | |
4d5977ea KW |
560 | /* Entries of QDicts are processed recursively, the QDict object |
561 | * itself disappears. */ | |
9f23fc0c HR |
562 | qdict_flatten_qdict(qobject_to_qdict(value), target, |
563 | new_key ? new_key : entry->key); | |
564 | delete = true; | |
565 | } else if (qobject_type(value) == QTYPE_QLIST) { | |
566 | qdict_flatten_qlist(qobject_to_qlist(value), target, | |
567 | new_key ? new_key : entry->key); | |
f660dc6a | 568 | delete = true; |
4d5977ea KW |
569 | } else if (prefix) { |
570 | /* All other objects are moved to the target unchanged. */ | |
571 | qobject_incref(value); | |
572 | qdict_put_obj(target, new_key, value); | |
573 | delete = true; | |
f660dc6a KW |
574 | } |
575 | ||
6273d113 KW |
576 | g_free(new_key); |
577 | ||
f660dc6a KW |
578 | if (delete) { |
579 | qdict_del(qdict, entry->key); | |
580 | ||
581 | /* Restart loop after modifying the iterated QDict */ | |
582 | entry = qdict_first(qdict); | |
583 | continue; | |
584 | } | |
585 | ||
586 | entry = next; | |
587 | } | |
588 | } | |
589 | ||
590 | /** | |
591 | * qdict_flatten(): For each nested QDict with key x, all fields with key y | |
9f23fc0c HR |
592 | * are moved to this QDict and their key is renamed to "x.y". For each nested |
593 | * QList with key x, the field at index y is moved to this QDict with the key | |
594 | * "x.y" (i.e., the reverse of what qdict_array_split() does). | |
595 | * This operation is applied recursively for nested QDicts and QLists. | |
f660dc6a KW |
596 | */ |
597 | void qdict_flatten(QDict *qdict) | |
598 | { | |
9f23fc0c | 599 | qdict_flatten_qdict(qdict, qdict, NULL); |
f660dc6a | 600 | } |
5726d872 BC |
601 | |
602 | /* extract all the src QDict entries starting by start into dst */ | |
603 | void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start) | |
604 | ||
605 | { | |
606 | const QDictEntry *entry, *next; | |
607 | const char *p; | |
608 | ||
609 | *dst = qdict_new(); | |
610 | entry = qdict_first(src); | |
611 | ||
612 | while (entry != NULL) { | |
613 | next = qdict_next(src, entry); | |
614 | if (strstart(entry->key, start, &p)) { | |
615 | qobject_incref(entry->value); | |
616 | qdict_put_obj(*dst, p, entry->value); | |
617 | qdict_del(src, entry->key); | |
618 | } | |
619 | entry = next; | |
620 | } | |
621 | } | |
05a8c222 | 622 | |
bd50530a | 623 | static int qdict_count_prefixed_entries(const QDict *src, const char *start) |
bae3f92a HR |
624 | { |
625 | const QDictEntry *entry; | |
bd50530a | 626 | int count = 0; |
bae3f92a HR |
627 | |
628 | for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) { | |
629 | if (strstart(entry->key, start, NULL)) { | |
bd50530a KW |
630 | if (count == INT_MAX) { |
631 | return -ERANGE; | |
632 | } | |
633 | count++; | |
bae3f92a HR |
634 | } |
635 | } | |
636 | ||
bd50530a | 637 | return count; |
bae3f92a HR |
638 | } |
639 | ||
05a8c222 HR |
640 | /** |
641 | * qdict_array_split(): This function moves array-like elements of a QDict into | |
bae3f92a HR |
642 | * a new QList. Every entry in the original QDict with a key "%u" or one |
643 | * prefixed "%u.", where %u designates an unsigned integer starting at 0 and | |
05a8c222 | 644 | * incrementally counting up, will be moved to a new QDict at index %u in the |
bae3f92a HR |
645 | * output QList with the key prefix removed, if that prefix is "%u.". If the |
646 | * whole key is just "%u", the whole QObject will be moved unchanged without | |
647 | * creating a new QDict. The function terminates when there is no entry in the | |
648 | * QDict with a prefix directly (incrementally) following the last one; it also | |
649 | * returns if there are both entries with "%u" and "%u." for the same index %u. | |
650 | * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66} | |
651 | * (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66}) | |
652 | * => [{"a": 42, "b": 23}, {"x": 0}, 66] | |
653 | * and {"4.y": 1, "o.o": 7} (remainder of the old QDict) | |
05a8c222 HR |
654 | */ |
655 | void qdict_array_split(QDict *src, QList **dst) | |
656 | { | |
657 | unsigned i; | |
658 | ||
659 | *dst = qlist_new(); | |
660 | ||
661 | for (i = 0; i < UINT_MAX; i++) { | |
bae3f92a HR |
662 | QObject *subqobj; |
663 | bool is_subqdict; | |
05a8c222 | 664 | QDict *subqdict; |
bae3f92a | 665 | char indexstr[32], prefix[32]; |
05a8c222 HR |
666 | size_t snprintf_ret; |
667 | ||
bae3f92a HR |
668 | snprintf_ret = snprintf(indexstr, 32, "%u", i); |
669 | assert(snprintf_ret < 32); | |
670 | ||
671 | subqobj = qdict_get(src, indexstr); | |
672 | ||
05a8c222 HR |
673 | snprintf_ret = snprintf(prefix, 32, "%u.", i); |
674 | assert(snprintf_ret < 32); | |
675 | ||
bd50530a KW |
676 | /* Overflow is the same as positive non-zero results */ |
677 | is_subqdict = qdict_count_prefixed_entries(src, prefix); | |
bae3f92a HR |
678 | |
679 | // There may be either a single subordinate object (named "%u") or | |
680 | // multiple objects (each with a key prefixed "%u."), but not both. | |
681 | if (!subqobj == !is_subqdict) { | |
05a8c222 HR |
682 | break; |
683 | } | |
684 | ||
bae3f92a HR |
685 | if (is_subqdict) { |
686 | qdict_extract_subqdict(src, &subqdict, prefix); | |
687 | assert(qdict_size(subqdict) > 0); | |
688 | } else { | |
689 | qobject_incref(subqobj); | |
690 | qdict_del(src, indexstr); | |
691 | } | |
692 | ||
693 | qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict)); | |
05a8c222 HR |
694 | } |
695 | } | |
9c526812 | 696 | |
bd50530a KW |
697 | /** |
698 | * qdict_array_entries(): Returns the number of direct array entries if the | |
699 | * sub-QDict of src specified by the prefix in subqdict (or src itself for | |
700 | * prefix == "") is valid as an array, i.e. the length of the created list if | |
701 | * the sub-QDict would become empty after calling qdict_array_split() on it. If | |
702 | * the array is not valid, -EINVAL is returned. | |
703 | */ | |
704 | int qdict_array_entries(QDict *src, const char *subqdict) | |
705 | { | |
706 | const QDictEntry *entry; | |
707 | unsigned i; | |
708 | unsigned entries = 0; | |
709 | size_t subqdict_len = strlen(subqdict); | |
710 | ||
711 | assert(!subqdict_len || subqdict[subqdict_len - 1] == '.'); | |
712 | ||
713 | /* qdict_array_split() loops until UINT_MAX, but as we want to return | |
714 | * negative errors, we only have a signed return value here. Any additional | |
715 | * entries will lead to -EINVAL. */ | |
716 | for (i = 0; i < INT_MAX; i++) { | |
717 | QObject *subqobj; | |
718 | int subqdict_entries; | |
719 | size_t slen = 32 + subqdict_len; | |
720 | char indexstr[slen], prefix[slen]; | |
721 | size_t snprintf_ret; | |
722 | ||
723 | snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i); | |
724 | assert(snprintf_ret < slen); | |
725 | ||
726 | subqobj = qdict_get(src, indexstr); | |
727 | ||
728 | snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i); | |
729 | assert(snprintf_ret < slen); | |
730 | ||
731 | subqdict_entries = qdict_count_prefixed_entries(src, prefix); | |
732 | if (subqdict_entries < 0) { | |
733 | return subqdict_entries; | |
734 | } | |
735 | ||
736 | /* There may be either a single subordinate object (named "%u") or | |
737 | * multiple objects (each with a key prefixed "%u."), but not both. */ | |
738 | if (subqobj && subqdict_entries) { | |
739 | return -EINVAL; | |
740 | } else if (!subqobj && !subqdict_entries) { | |
741 | break; | |
742 | } | |
743 | ||
744 | entries += subqdict_entries ? subqdict_entries : 1; | |
745 | } | |
746 | ||
747 | /* Consider everything handled that isn't part of the given sub-QDict */ | |
748 | for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) { | |
749 | if (!strstart(qdict_entry_key(entry), subqdict, NULL)) { | |
750 | entries++; | |
751 | } | |
752 | } | |
753 | ||
754 | /* Anything left in the sub-QDict that wasn't handled? */ | |
755 | if (qdict_size(src) != entries) { | |
756 | return -EINVAL; | |
757 | } | |
758 | ||
759 | return i; | |
760 | } | |
761 | ||
9c526812 HR |
762 | /** |
763 | * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all | |
764 | * elements from src to dest. | |
765 | * | |
766 | * If an element from src has a key already present in dest, it will not be | |
767 | * moved unless overwrite is true. | |
768 | * | |
769 | * If overwrite is true, the conflicting values in dest will be discarded and | |
770 | * replaced by the corresponding values from src. | |
771 | * | |
772 | * Therefore, with overwrite being true, the src QDict will always be empty when | |
773 | * this function returns. If overwrite is false, the src QDict will be empty | |
774 | * iff there were no conflicts. | |
775 | */ | |
776 | void qdict_join(QDict *dest, QDict *src, bool overwrite) | |
777 | { | |
778 | const QDictEntry *entry, *next; | |
779 | ||
780 | entry = qdict_first(src); | |
781 | while (entry) { | |
782 | next = qdict_next(src, entry); | |
783 | ||
784 | if (overwrite || !qdict_haskey(dest, entry->key)) { | |
785 | qobject_incref(entry->value); | |
786 | qdict_put_obj(dest, entry->key, entry->value); | |
787 | qdict_del(src, entry->key); | |
788 | } | |
789 | ||
790 | entry = next; | |
791 | } | |
792 | } |