fdc: Make errp the last parameter of fdctrl_connect_drives
[qemu/ar7.git] / qobject / qdict.c
blob291eef1a199d8757b5a521b12cb7fb39cd9a80e9
1 /*
2 * QDict Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qint.h"
15 #include "qapi/qmp/qfloat.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qbool.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qmp/qobject.h"
20 #include "qapi/error.h"
21 #include "qemu/queue.h"
22 #include "qemu-common.h"
23 #include "qemu/cutils.h"
25 /**
26 * qdict_new(): Create a new QDict
28 * Return strong reference.
30 QDict *qdict_new(void)
32 QDict *qdict;
34 qdict = g_malloc0(sizeof(*qdict));
35 qobject_init(QOBJECT(qdict), QTYPE_QDICT);
37 return qdict;
40 /**
41 * qobject_to_qdict(): Convert a QObject into a QDict
43 QDict *qobject_to_qdict(const QObject *obj)
45 if (!obj || qobject_type(obj) != QTYPE_QDICT) {
46 return NULL;
48 return container_of(obj, QDict, base);
51 /**
52 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
53 * (from module-init-tools)
55 static unsigned int tdb_hash(const char *name)
57 unsigned value; /* Used to compute the hash value. */
58 unsigned i; /* Used to cycle through random values. */
60 /* Set the initial value from the key size. */
61 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
62 value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
64 return (1103515243 * value + 12345);
67 /**
68 * alloc_entry(): allocate a new QDictEntry
70 static QDictEntry *alloc_entry(const char *key, QObject *value)
72 QDictEntry *entry;
74 entry = g_malloc0(sizeof(*entry));
75 entry->key = g_strdup(key);
76 entry->value = value;
78 return entry;
81 /**
82 * qdict_entry_value(): Return qdict entry value
84 * Return weak reference.
86 QObject *qdict_entry_value(const QDictEntry *entry)
88 return entry->value;
91 /**
92 * qdict_entry_key(): Return qdict entry key
94 * Return a *pointer* to the string, it has to be duplicated before being
95 * stored.
97 const char *qdict_entry_key(const QDictEntry *entry)
99 return entry->key;
103 * qdict_find(): List lookup function
105 static QDictEntry *qdict_find(const QDict *qdict,
106 const char *key, unsigned int bucket)
108 QDictEntry *entry;
110 QLIST_FOREACH(entry, &qdict->table[bucket], next)
111 if (!strcmp(entry->key, key))
112 return entry;
114 return NULL;
118 * qdict_put_obj(): Put a new QObject into the dictionary
120 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
121 * its 'value' will be replaced.
123 * This is done by freeing the reference to the stored QObject and
124 * storing the new one in the same entry.
126 * NOTE: ownership of 'value' is transferred to the QDict
128 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
130 unsigned int bucket;
131 QDictEntry *entry;
133 bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
134 entry = qdict_find(qdict, key, bucket);
135 if (entry) {
136 /* replace key's value */
137 qobject_decref(entry->value);
138 entry->value = value;
139 } else {
140 /* allocate a new entry */
141 entry = alloc_entry(key, value);
142 QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next);
143 qdict->size++;
148 * qdict_get(): Lookup for a given 'key'
150 * Return a weak reference to the QObject associated with 'key' if
151 * 'key' is present in the dictionary, NULL otherwise.
153 QObject *qdict_get(const QDict *qdict, const char *key)
155 QDictEntry *entry;
157 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
158 return (entry == NULL ? NULL : entry->value);
162 * qdict_haskey(): Check if 'key' exists
164 * Return 1 if 'key' exists in the dict, 0 otherwise
166 int qdict_haskey(const QDict *qdict, const char *key)
168 unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
169 return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1);
173 * qdict_size(): Return the size of the dictionary
175 size_t qdict_size(const QDict *qdict)
177 return qdict->size;
181 * qdict_get_double(): Get an number mapped by 'key'
183 * This function assumes that 'key' exists and it stores a
184 * QFloat or QInt object.
186 * Return number mapped by 'key'.
188 double qdict_get_double(const QDict *qdict, const char *key)
190 QObject *obj = qdict_get(qdict, key);
192 assert(obj);
193 switch (qobject_type(obj)) {
194 case QTYPE_QFLOAT:
195 return qfloat_get_double(qobject_to_qfloat(obj));
196 case QTYPE_QINT:
197 return qint_get_int(qobject_to_qint(obj));
198 default:
199 abort();
204 * qdict_get_int(): Get an integer mapped by 'key'
206 * This function assumes that 'key' exists and it stores a
207 * QInt object.
209 * Return integer mapped by 'key'.
211 int64_t qdict_get_int(const QDict *qdict, const char *key)
213 return qint_get_int(qobject_to_qint(qdict_get(qdict, key)));
217 * qdict_get_bool(): Get a bool mapped by 'key'
219 * This function assumes that 'key' exists and it stores a
220 * QBool object.
222 * Return bool mapped by 'key'.
224 bool qdict_get_bool(const QDict *qdict, const char *key)
226 return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key)));
230 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
232 QList *qdict_get_qlist(const QDict *qdict, const char *key)
234 return qobject_to_qlist(qdict_get(qdict, key));
238 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
240 QDict *qdict_get_qdict(const QDict *qdict, const char *key)
242 return qobject_to_qdict(qdict_get(qdict, key));
246 * qdict_get_str(): Get a pointer to the stored string mapped
247 * by 'key'
249 * This function assumes that 'key' exists and it stores a
250 * QString object.
252 * Return pointer to the string mapped by 'key'.
254 const char *qdict_get_str(const QDict *qdict, const char *key)
256 return qstring_get_str(qobject_to_qstring(qdict_get(qdict, key)));
260 * qdict_get_try_int(): Try to get integer mapped by 'key'
262 * Return integer mapped by 'key', if it is not present in
263 * the dictionary or if the stored object is not of QInt type
264 * 'def_value' will be returned.
266 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
267 int64_t def_value)
269 QInt *qint = qobject_to_qint(qdict_get(qdict, key));
271 return qint ? qint_get_int(qint) : def_value;
275 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
277 * Return bool mapped by 'key', if it is not present in the
278 * dictionary or if the stored object is not of QBool type
279 * 'def_value' will be returned.
281 bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
283 QBool *qbool = qobject_to_qbool(qdict_get(qdict, key));
285 return qbool ? qbool_get_bool(qbool) : def_value;
289 * qdict_get_try_str(): Try to get a pointer to the stored string
290 * mapped by 'key'
292 * Return a pointer to the string mapped by 'key', if it is not present
293 * in the dictionary or if the stored object is not of QString type
294 * NULL will be returned.
296 const char *qdict_get_try_str(const QDict *qdict, const char *key)
298 QString *qstr = qobject_to_qstring(qdict_get(qdict, key));
300 return qstr ? qstring_get_str(qstr) : NULL;
304 * qdict_iter(): Iterate over all the dictionary's stored values.
306 * This function allows the user to provide an iterator, which will be
307 * called for each stored value in the dictionary.
309 void qdict_iter(const QDict *qdict,
310 void (*iter)(const char *key, QObject *obj, void *opaque),
311 void *opaque)
313 int i;
314 QDictEntry *entry;
316 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
317 QLIST_FOREACH(entry, &qdict->table[i], next)
318 iter(entry->key, entry->value, opaque);
322 static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
324 int i;
326 for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) {
327 if (!QLIST_EMPTY(&qdict->table[i])) {
328 return QLIST_FIRST(&qdict->table[i]);
332 return NULL;
336 * qdict_first(): Return first qdict entry for iteration.
338 const QDictEntry *qdict_first(const QDict *qdict)
340 return qdict_next_entry(qdict, 0);
344 * qdict_next(): Return next qdict entry in an iteration.
346 const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
348 QDictEntry *ret;
350 ret = QLIST_NEXT(entry, next);
351 if (!ret) {
352 unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX;
353 ret = qdict_next_entry(qdict, bucket + 1);
356 return ret;
360 * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
361 * another reference is added.
363 QDict *qdict_clone_shallow(const QDict *src)
365 QDict *dest;
366 QDictEntry *entry;
367 int i;
369 dest = qdict_new();
371 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
372 QLIST_FOREACH(entry, &src->table[i], next) {
373 qobject_incref(entry->value);
374 qdict_put_obj(dest, entry->key, entry->value);
378 return dest;
382 * qentry_destroy(): Free all the memory allocated by a QDictEntry
384 static void qentry_destroy(QDictEntry *e)
386 assert(e != NULL);
387 assert(e->key != NULL);
388 assert(e->value != NULL);
390 qobject_decref(e->value);
391 g_free(e->key);
392 g_free(e);
396 * qdict_del(): Delete a 'key:value' pair from the dictionary
398 * This will destroy all data allocated by this entry.
400 void qdict_del(QDict *qdict, const char *key)
402 QDictEntry *entry;
404 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
405 if (entry) {
406 QLIST_REMOVE(entry, next);
407 qentry_destroy(entry);
408 qdict->size--;
413 * qdict_destroy_obj(): Free all the memory allocated by a QDict
415 void qdict_destroy_obj(QObject *obj)
417 int i;
418 QDict *qdict;
420 assert(obj != NULL);
421 qdict = qobject_to_qdict(obj);
423 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
424 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
425 while (entry) {
426 QDictEntry *tmp = QLIST_NEXT(entry, next);
427 QLIST_REMOVE(entry, next);
428 qentry_destroy(entry);
429 entry = tmp;
433 g_free(qdict);
437 * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
438 * value of 'key' in 'src' is copied there (and the refcount increased
439 * accordingly).
441 void qdict_copy_default(QDict *dst, QDict *src, const char *key)
443 QObject *val;
445 if (qdict_haskey(dst, key)) {
446 return;
449 val = qdict_get(src, key);
450 if (val) {
451 qobject_incref(val);
452 qdict_put_obj(dst, key, val);
457 * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
458 * new QString initialised by 'val' is put there.
460 void qdict_set_default_str(QDict *dst, const char *key, const char *val)
462 if (qdict_haskey(dst, key)) {
463 return;
466 qdict_put(dst, key, qstring_from_str(val));
469 static void qdict_flatten_qdict(QDict *qdict, QDict *target,
470 const char *prefix);
472 static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
474 QObject *value;
475 const QListEntry *entry;
476 char *new_key;
477 int i;
479 /* This function is never called with prefix == NULL, i.e., it is always
480 * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
481 * need to remove list entries during the iteration (the whole list will be
482 * deleted eventually anyway from qdict_flatten_qdict()). */
483 assert(prefix);
485 entry = qlist_first(qlist);
487 for (i = 0; entry; entry = qlist_next(entry), i++) {
488 value = qlist_entry_obj(entry);
489 new_key = g_strdup_printf("%s.%i", prefix, i);
491 if (qobject_type(value) == QTYPE_QDICT) {
492 qdict_flatten_qdict(qobject_to_qdict(value), target, new_key);
493 } else if (qobject_type(value) == QTYPE_QLIST) {
494 qdict_flatten_qlist(qobject_to_qlist(value), target, new_key);
495 } else {
496 /* All other types are moved to the target unchanged. */
497 qobject_incref(value);
498 qdict_put_obj(target, new_key, value);
501 g_free(new_key);
505 static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
507 QObject *value;
508 const QDictEntry *entry, *next;
509 char *new_key;
510 bool delete;
512 entry = qdict_first(qdict);
514 while (entry != NULL) {
516 next = qdict_next(qdict, entry);
517 value = qdict_entry_value(entry);
518 new_key = NULL;
519 delete = false;
521 if (prefix) {
522 new_key = g_strdup_printf("%s.%s", prefix, entry->key);
525 if (qobject_type(value) == QTYPE_QDICT) {
526 /* Entries of QDicts are processed recursively, the QDict object
527 * itself disappears. */
528 qdict_flatten_qdict(qobject_to_qdict(value), target,
529 new_key ? new_key : entry->key);
530 delete = true;
531 } else if (qobject_type(value) == QTYPE_QLIST) {
532 qdict_flatten_qlist(qobject_to_qlist(value), target,
533 new_key ? new_key : entry->key);
534 delete = true;
535 } else if (prefix) {
536 /* All other objects are moved to the target unchanged. */
537 qobject_incref(value);
538 qdict_put_obj(target, new_key, value);
539 delete = true;
542 g_free(new_key);
544 if (delete) {
545 qdict_del(qdict, entry->key);
547 /* Restart loop after modifying the iterated QDict */
548 entry = qdict_first(qdict);
549 continue;
552 entry = next;
557 * qdict_flatten(): For each nested QDict with key x, all fields with key y
558 * are moved to this QDict and their key is renamed to "x.y". For each nested
559 * QList with key x, the field at index y is moved to this QDict with the key
560 * "x.y" (i.e., the reverse of what qdict_array_split() does).
561 * This operation is applied recursively for nested QDicts and QLists.
563 void qdict_flatten(QDict *qdict)
565 qdict_flatten_qdict(qdict, qdict, NULL);
568 /* extract all the src QDict entries starting by start into dst */
569 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
572 const QDictEntry *entry, *next;
573 const char *p;
575 *dst = qdict_new();
576 entry = qdict_first(src);
578 while (entry != NULL) {
579 next = qdict_next(src, entry);
580 if (strstart(entry->key, start, &p)) {
581 qobject_incref(entry->value);
582 qdict_put_obj(*dst, p, entry->value);
583 qdict_del(src, entry->key);
585 entry = next;
589 static int qdict_count_prefixed_entries(const QDict *src, const char *start)
591 const QDictEntry *entry;
592 int count = 0;
594 for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
595 if (strstart(entry->key, start, NULL)) {
596 if (count == INT_MAX) {
597 return -ERANGE;
599 count++;
603 return count;
607 * qdict_array_split(): This function moves array-like elements of a QDict into
608 * a new QList. Every entry in the original QDict with a key "%u" or one
609 * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
610 * incrementally counting up, will be moved to a new QDict at index %u in the
611 * output QList with the key prefix removed, if that prefix is "%u.". If the
612 * whole key is just "%u", the whole QObject will be moved unchanged without
613 * creating a new QDict. The function terminates when there is no entry in the
614 * QDict with a prefix directly (incrementally) following the last one; it also
615 * returns if there are both entries with "%u" and "%u." for the same index %u.
616 * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
617 * (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
618 * => [{"a": 42, "b": 23}, {"x": 0}, 66]
619 * and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
621 void qdict_array_split(QDict *src, QList **dst)
623 unsigned i;
625 *dst = qlist_new();
627 for (i = 0; i < UINT_MAX; i++) {
628 QObject *subqobj;
629 bool is_subqdict;
630 QDict *subqdict;
631 char indexstr[32], prefix[32];
632 size_t snprintf_ret;
634 snprintf_ret = snprintf(indexstr, 32, "%u", i);
635 assert(snprintf_ret < 32);
637 subqobj = qdict_get(src, indexstr);
639 snprintf_ret = snprintf(prefix, 32, "%u.", i);
640 assert(snprintf_ret < 32);
642 /* Overflow is the same as positive non-zero results */
643 is_subqdict = qdict_count_prefixed_entries(src, prefix);
645 // There may be either a single subordinate object (named "%u") or
646 // multiple objects (each with a key prefixed "%u."), but not both.
647 if (!subqobj == !is_subqdict) {
648 break;
651 if (is_subqdict) {
652 qdict_extract_subqdict(src, &subqdict, prefix);
653 assert(qdict_size(subqdict) > 0);
654 } else {
655 qobject_incref(subqobj);
656 qdict_del(src, indexstr);
659 qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
664 * qdict_split_flat_key:
665 * @key: the key string to split
666 * @prefix: non-NULL pointer to hold extracted prefix
667 * @suffix: non-NULL pointer to remaining suffix
669 * Given a flattened key such as 'foo.0.bar', split it into two parts
670 * at the first '.' separator. Allows double dot ('..') to escape the
671 * normal separator.
673 * e.g.
674 * 'foo.0.bar' -> prefix='foo' and suffix='0.bar'
675 * 'foo..0.bar' -> prefix='foo.0' and suffix='bar'
677 * The '..' sequence will be unescaped in the returned 'prefix'
678 * string. The 'suffix' string will be left in escaped format, so it
679 * can be fed back into the qdict_split_flat_key() key as the input
680 * later.
682 * The caller is responsible for freeing the string returned in @prefix
683 * using g_free().
685 static void qdict_split_flat_key(const char *key, char **prefix,
686 const char **suffix)
688 const char *separator;
689 size_t i, j;
691 /* Find first '.' separator, but if there is a pair '..'
692 * that acts as an escape, so skip over '..' */
693 separator = NULL;
694 do {
695 if (separator) {
696 separator += 2;
697 } else {
698 separator = key;
700 separator = strchr(separator, '.');
701 } while (separator && separator[1] == '.');
703 if (separator) {
704 *prefix = g_strndup(key, separator - key);
705 *suffix = separator + 1;
706 } else {
707 *prefix = g_strdup(key);
708 *suffix = NULL;
711 /* Unescape the '..' sequence into '.' */
712 for (i = 0, j = 0; (*prefix)[i] != '\0'; i++, j++) {
713 if ((*prefix)[i] == '.') {
714 assert((*prefix)[i + 1] == '.');
715 i++;
717 (*prefix)[j] = (*prefix)[i];
719 (*prefix)[j] = '\0';
723 * qdict_is_list:
724 * @maybe_list: dict to check if keys represent list elements.
726 * Determine whether all keys in @maybe_list are valid list elements.
727 * If @maybe_list is non-zero in length and all the keys look like
728 * valid list indexes, this will return 1. If @maybe_list is zero
729 * length or all keys are non-numeric then it will return 0 to indicate
730 * it is a normal qdict. If there is a mix of numeric and non-numeric
731 * keys, or the list indexes are non-contiguous, an error is reported.
733 * Returns: 1 if a valid list, 0 if a dict, -1 on error
735 static int qdict_is_list(QDict *maybe_list, Error **errp)
737 const QDictEntry *ent;
738 ssize_t len = 0;
739 ssize_t max = -1;
740 int is_list = -1;
741 int64_t val;
743 for (ent = qdict_first(maybe_list); ent != NULL;
744 ent = qdict_next(maybe_list, ent)) {
746 if (qemu_strtoi64(ent->key, NULL, 10, &val) == 0) {
747 if (is_list == -1) {
748 is_list = 1;
749 } else if (!is_list) {
750 error_setg(errp,
751 "Cannot mix list and non-list keys");
752 return -1;
754 len++;
755 if (val > max) {
756 max = val;
758 } else {
759 if (is_list == -1) {
760 is_list = 0;
761 } else if (is_list) {
762 error_setg(errp,
763 "Cannot mix list and non-list keys");
764 return -1;
769 if (is_list == -1) {
770 assert(!qdict_size(maybe_list));
771 is_list = 0;
774 /* NB this isn't a perfect check - e.g. it won't catch
775 * a list containing '1', '+1', '01', '3', but that
776 * does not matter - we've still proved that the
777 * input is a list. It is up the caller to do a
778 * stricter check if desired */
779 if (len != (max + 1)) {
780 error_setg(errp, "List indices are not contiguous, "
781 "saw %zd elements but %zd largest index",
782 len, max);
783 return -1;
786 return is_list;
790 * qdict_crumple:
791 * @src: the original flat dictionary (only scalar values) to crumple
793 * Takes a flat dictionary whose keys use '.' separator to indicate
794 * nesting, and values are scalars, and crumples it into a nested
795 * structure.
797 * To include a literal '.' in a key name, it must be escaped as '..'
799 * For example, an input of:
801 * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
802 * 'foo.1.bar': 'two', 'foo.1.wizz': '2' }
804 * will result in an output of:
807 * 'foo': [
808 * { 'bar': 'one', 'wizz': '1' },
809 * { 'bar': 'two', 'wizz': '2' }
810 * ],
813 * The following scenarios in the input dict will result in an
814 * error being returned:
816 * - Any values in @src are non-scalar types
817 * - If keys in @src imply that a particular level is both a
818 * list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
819 * - If keys in @src imply that a particular level is a list,
820 * but the indices are non-contiguous. e.g. "foo.0.bar" and
821 * "foo.2.bar" without any "foo.1.bar" present.
822 * - If keys in @src represent list indexes, but are not in
823 * the "%zu" format. e.g. "foo.+0.bar"
825 * Returns: either a QDict or QList for the nested data structure, or NULL
826 * on error
828 QObject *qdict_crumple(const QDict *src, Error **errp)
830 const QDictEntry *ent;
831 QDict *two_level, *multi_level = NULL;
832 QObject *dst = NULL, *child;
833 size_t i;
834 char *prefix = NULL;
835 const char *suffix = NULL;
836 int is_list;
838 two_level = qdict_new();
840 /* Step 1: split our totally flat dict into a two level dict */
841 for (ent = qdict_first(src); ent != NULL; ent = qdict_next(src, ent)) {
842 if (qobject_type(ent->value) == QTYPE_QDICT ||
843 qobject_type(ent->value) == QTYPE_QLIST) {
844 error_setg(errp, "Value %s is not a scalar",
845 ent->key);
846 goto error;
849 qdict_split_flat_key(ent->key, &prefix, &suffix);
851 child = qdict_get(two_level, prefix);
852 if (suffix) {
853 if (child) {
854 if (qobject_type(child) != QTYPE_QDICT) {
855 error_setg(errp, "Key %s prefix is already set as a scalar",
856 prefix);
857 goto error;
859 } else {
860 child = QOBJECT(qdict_new());
861 qdict_put_obj(two_level, prefix, child);
863 qobject_incref(ent->value);
864 qdict_put_obj(qobject_to_qdict(child), suffix, ent->value);
865 } else {
866 if (child) {
867 error_setg(errp, "Key %s prefix is already set as a dict",
868 prefix);
869 goto error;
871 qobject_incref(ent->value);
872 qdict_put_obj(two_level, prefix, ent->value);
875 g_free(prefix);
876 prefix = NULL;
879 /* Step 2: optionally process the two level dict recursively
880 * into a multi-level dict */
881 multi_level = qdict_new();
882 for (ent = qdict_first(two_level); ent != NULL;
883 ent = qdict_next(two_level, ent)) {
885 if (qobject_type(ent->value) == QTYPE_QDICT) {
886 child = qdict_crumple(qobject_to_qdict(ent->value), errp);
887 if (!child) {
888 goto error;
891 qdict_put_obj(multi_level, ent->key, child);
892 } else {
893 qobject_incref(ent->value);
894 qdict_put_obj(multi_level, ent->key, ent->value);
897 QDECREF(two_level);
898 two_level = NULL;
900 /* Step 3: detect if we need to turn our dict into list */
901 is_list = qdict_is_list(multi_level, errp);
902 if (is_list < 0) {
903 goto error;
906 if (is_list) {
907 dst = QOBJECT(qlist_new());
909 for (i = 0; i < qdict_size(multi_level); i++) {
910 char *key = g_strdup_printf("%zu", i);
912 child = qdict_get(multi_level, key);
913 g_free(key);
915 if (!child) {
916 error_setg(errp, "Missing list index %zu", i);
917 goto error;
920 qobject_incref(child);
921 qlist_append_obj(qobject_to_qlist(dst), child);
923 QDECREF(multi_level);
924 multi_level = NULL;
925 } else {
926 dst = QOBJECT(multi_level);
929 return dst;
931 error:
932 g_free(prefix);
933 QDECREF(multi_level);
934 QDECREF(two_level);
935 qobject_decref(dst);
936 return NULL;
940 * qdict_array_entries(): Returns the number of direct array entries if the
941 * sub-QDict of src specified by the prefix in subqdict (or src itself for
942 * prefix == "") is valid as an array, i.e. the length of the created list if
943 * the sub-QDict would become empty after calling qdict_array_split() on it. If
944 * the array is not valid, -EINVAL is returned.
946 int qdict_array_entries(QDict *src, const char *subqdict)
948 const QDictEntry *entry;
949 unsigned i;
950 unsigned entries = 0;
951 size_t subqdict_len = strlen(subqdict);
953 assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
955 /* qdict_array_split() loops until UINT_MAX, but as we want to return
956 * negative errors, we only have a signed return value here. Any additional
957 * entries will lead to -EINVAL. */
958 for (i = 0; i < INT_MAX; i++) {
959 QObject *subqobj;
960 int subqdict_entries;
961 char *prefix = g_strdup_printf("%s%u.", subqdict, i);
963 subqdict_entries = qdict_count_prefixed_entries(src, prefix);
965 /* Remove ending "." */
966 prefix[strlen(prefix) - 1] = 0;
967 subqobj = qdict_get(src, prefix);
969 g_free(prefix);
971 if (subqdict_entries < 0) {
972 return subqdict_entries;
975 /* There may be either a single subordinate object (named "%u") or
976 * multiple objects (each with a key prefixed "%u."), but not both. */
977 if (subqobj && subqdict_entries) {
978 return -EINVAL;
979 } else if (!subqobj && !subqdict_entries) {
980 break;
983 entries += subqdict_entries ? subqdict_entries : 1;
986 /* Consider everything handled that isn't part of the given sub-QDict */
987 for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
988 if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
989 entries++;
993 /* Anything left in the sub-QDict that wasn't handled? */
994 if (qdict_size(src) != entries) {
995 return -EINVAL;
998 return i;
1002 * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
1003 * elements from src to dest.
1005 * If an element from src has a key already present in dest, it will not be
1006 * moved unless overwrite is true.
1008 * If overwrite is true, the conflicting values in dest will be discarded and
1009 * replaced by the corresponding values from src.
1011 * Therefore, with overwrite being true, the src QDict will always be empty when
1012 * this function returns. If overwrite is false, the src QDict will be empty
1013 * iff there were no conflicts.
1015 void qdict_join(QDict *dest, QDict *src, bool overwrite)
1017 const QDictEntry *entry, *next;
1019 entry = qdict_first(src);
1020 while (entry) {
1021 next = qdict_next(src, entry);
1023 if (overwrite || !qdict_haskey(dest, entry->key)) {
1024 qobject_incref(entry->value);
1025 qdict_put_obj(dest, entry->key, entry->value);
1026 qdict_del(src, entry->key);
1029 entry = next;