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