]>
git.proxmox.com Git - mirror_qemu.git/blob - qobject/qdict.c
4 * Copyright (C) 2009 Red Hat Inc.
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 "block/qdict.h"
15 #include "qapi/qmp/qnum.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qbool.h"
18 #include "qapi/qmp/qlist.h"
19 #include "qapi/qmp/qnull.h"
20 #include "qapi/qmp/qstring.h"
21 #include "qapi/error.h"
22 #include "qemu/queue.h"
23 #include "qemu-common.h"
24 #include "qemu/cutils.h"
27 * qdict_new(): Create a new QDict
29 * Return strong reference.
31 QDict
*qdict_new(void)
35 qdict
= g_malloc0(sizeof(*qdict
));
36 qobject_init(QOBJECT(qdict
), QTYPE_QDICT
);
42 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
43 * (from module-init-tools)
45 static unsigned int tdb_hash(const char *name
)
47 unsigned value
; /* Used to compute the hash value. */
48 unsigned i
; /* Used to cycle through random values. */
50 /* Set the initial value from the key size. */
51 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
52 value
= (value
+ (((const unsigned char *)name
)[i
] << (i
*5 % 24)));
54 return (1103515243 * value
+ 12345);
58 * alloc_entry(): allocate a new QDictEntry
60 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
64 entry
= g_malloc0(sizeof(*entry
));
65 entry
->key
= g_strdup(key
);
72 * qdict_entry_value(): Return qdict entry value
74 * Return weak reference.
76 QObject
*qdict_entry_value(const QDictEntry
*entry
)
82 * qdict_entry_key(): Return qdict entry key
84 * Return a *pointer* to the string, it has to be duplicated before being
87 const char *qdict_entry_key(const QDictEntry
*entry
)
93 * qdict_find(): List lookup function
95 static QDictEntry
*qdict_find(const QDict
*qdict
,
96 const char *key
, unsigned int bucket
)
100 QLIST_FOREACH(entry
, &qdict
->table
[bucket
], next
)
101 if (!strcmp(entry
->key
, key
))
108 * qdict_put_obj(): Put a new QObject into the dictionary
110 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
111 * its 'value' will be replaced.
113 * This is done by freeing the reference to the stored QObject and
114 * storing the new one in the same entry.
116 * NOTE: ownership of 'value' is transferred to the QDict
118 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
123 bucket
= tdb_hash(key
) % QDICT_BUCKET_MAX
;
124 entry
= qdict_find(qdict
, key
, bucket
);
126 /* replace key's value */
127 qobject_unref(entry
->value
);
128 entry
->value
= value
;
130 /* allocate a new entry */
131 entry
= alloc_entry(key
, value
);
132 QLIST_INSERT_HEAD(&qdict
->table
[bucket
], entry
, next
);
137 void qdict_put_int(QDict
*qdict
, const char *key
, int64_t value
)
139 qdict_put(qdict
, key
, qnum_from_int(value
));
142 void qdict_put_bool(QDict
*qdict
, const char *key
, bool value
)
144 qdict_put(qdict
, key
, qbool_from_bool(value
));
147 void qdict_put_str(QDict
*qdict
, const char *key
, const char *value
)
149 qdict_put(qdict
, key
, qstring_from_str(value
));
152 void qdict_put_null(QDict
*qdict
, const char *key
)
154 qdict_put(qdict
, key
, qnull());
158 * qdict_get(): Lookup for a given 'key'
160 * Return a weak reference to the QObject associated with 'key' if
161 * 'key' is present in the dictionary, NULL otherwise.
163 QObject
*qdict_get(const QDict
*qdict
, const char *key
)
167 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_BUCKET_MAX
);
168 return (entry
== NULL
? NULL
: entry
->value
);
172 * qdict_haskey(): Check if 'key' exists
174 * Return 1 if 'key' exists in the dict, 0 otherwise
176 int qdict_haskey(const QDict
*qdict
, const char *key
)
178 unsigned int bucket
= tdb_hash(key
) % QDICT_BUCKET_MAX
;
179 return (qdict_find(qdict
, key
, bucket
) == NULL
? 0 : 1);
183 * qdict_size(): Return the size of the dictionary
185 size_t qdict_size(const QDict
*qdict
)
191 * qdict_get_double(): Get an number mapped by 'key'
193 * This function assumes that 'key' exists and it stores a QNum.
195 * Return number mapped by 'key'.
197 double qdict_get_double(const QDict
*qdict
, const char *key
)
199 return qnum_get_double(qobject_to(QNum
, qdict_get(qdict
, key
)));
203 * qdict_get_int(): Get an integer mapped by 'key'
205 * This function assumes that 'key' exists and it stores a
206 * QNum representable as int.
208 * Return integer mapped by 'key'.
210 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
212 return qnum_get_int(qobject_to(QNum
, qdict_get(qdict
, key
)));
216 * qdict_get_bool(): Get a bool mapped by 'key'
218 * This function assumes that 'key' exists and it stores a
221 * Return bool mapped by 'key'.
223 bool qdict_get_bool(const QDict
*qdict
, const char *key
)
225 return qbool_get_bool(qobject_to(QBool
, qdict_get(qdict
, key
)));
229 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
231 QList
*qdict_get_qlist(const QDict
*qdict
, const char *key
)
233 return qobject_to(QList
, qdict_get(qdict
, key
));
237 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
239 QDict
*qdict_get_qdict(const QDict
*qdict
, const char *key
)
241 return qobject_to(QDict
, qdict_get(qdict
, key
));
245 * qdict_get_str(): Get a pointer to the stored string mapped
248 * This function assumes that 'key' exists and it stores a
251 * Return pointer to the string mapped by 'key'.
253 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
255 return qstring_get_str(qobject_to(QString
, qdict_get(qdict
, key
)));
259 * qdict_get_try_int(): Try to get integer mapped by 'key'
261 * Return integer mapped by 'key', if it is not present in the
262 * dictionary or if the stored object is not a QNum representing an
263 * integer, 'def_value' will be returned.
265 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
268 QNum
*qnum
= qobject_to(QNum
, qdict_get(qdict
, key
));
271 if (!qnum
|| !qnum_get_try_int(qnum
, &val
)) {
279 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
281 * Return bool mapped by 'key', if it is not present in the
282 * dictionary or if the stored object is not of QBool type
283 * 'def_value' will be returned.
285 bool qdict_get_try_bool(const QDict
*qdict
, const char *key
, bool def_value
)
287 QBool
*qbool
= qobject_to(QBool
, qdict_get(qdict
, key
));
289 return qbool
? qbool_get_bool(qbool
) : def_value
;
293 * qdict_get_try_str(): Try to get a pointer to the stored string
296 * Return a pointer to the string mapped by 'key', if it is not present
297 * in the dictionary or if the stored object is not of QString type
298 * NULL will be returned.
300 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
302 QString
*qstr
= qobject_to(QString
, qdict_get(qdict
, key
));
304 return qstr
? qstring_get_str(qstr
) : NULL
;
308 * qdict_iter(): Iterate over all the dictionary's stored values.
310 * This function allows the user to provide an iterator, which will be
311 * called for each stored value in the dictionary.
313 void qdict_iter(const QDict
*qdict
,
314 void (*iter
)(const char *key
, QObject
*obj
, void *opaque
),
320 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
321 QLIST_FOREACH(entry
, &qdict
->table
[i
], next
)
322 iter(entry
->key
, entry
->value
, opaque
);
326 static QDictEntry
*qdict_next_entry(const QDict
*qdict
, int first_bucket
)
330 for (i
= first_bucket
; i
< QDICT_BUCKET_MAX
; i
++) {
331 if (!QLIST_EMPTY(&qdict
->table
[i
])) {
332 return QLIST_FIRST(&qdict
->table
[i
]);
340 * qdict_first(): Return first qdict entry for iteration.
342 const QDictEntry
*qdict_first(const QDict
*qdict
)
344 return qdict_next_entry(qdict
, 0);
348 * qdict_next(): Return next qdict entry in an iteration.
350 const QDictEntry
*qdict_next(const QDict
*qdict
, const QDictEntry
*entry
)
354 ret
= QLIST_NEXT(entry
, next
);
356 unsigned int bucket
= tdb_hash(entry
->key
) % QDICT_BUCKET_MAX
;
357 ret
= qdict_next_entry(qdict
, bucket
+ 1);
364 * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
365 * another reference is added.
367 QDict
*qdict_clone_shallow(const QDict
*src
)
375 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
376 QLIST_FOREACH(entry
, &src
->table
[i
], next
) {
377 qdict_put_obj(dest
, entry
->key
, qobject_ref(entry
->value
));
385 * qentry_destroy(): Free all the memory allocated by a QDictEntry
387 static void qentry_destroy(QDictEntry
*e
)
390 assert(e
->key
!= NULL
);
391 assert(e
->value
!= NULL
);
393 qobject_unref(e
->value
);
399 * qdict_del(): Delete a 'key:value' pair from the dictionary
401 * This will destroy all data allocated by this entry.
403 void qdict_del(QDict
*qdict
, const char *key
)
407 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_BUCKET_MAX
);
409 QLIST_REMOVE(entry
, next
);
410 qentry_destroy(entry
);
416 * qdict_is_equal(): Test whether the two QDicts are equal
418 * Here, equality means whether they contain the same keys and whether
419 * the respective values are in turn equal (i.e. invoking
420 * qobject_is_equal() on them yields true).
422 bool qdict_is_equal(const QObject
*x
, const QObject
*y
)
424 const QDict
*dict_x
= qobject_to(QDict
, x
);
425 const QDict
*dict_y
= qobject_to(QDict
, y
);
428 if (qdict_size(dict_x
) != qdict_size(dict_y
)) {
432 for (e
= qdict_first(dict_x
); e
; e
= qdict_next(dict_x
, e
)) {
433 const QObject
*obj_x
= qdict_entry_value(e
);
434 const QObject
*obj_y
= qdict_get(dict_y
, qdict_entry_key(e
));
436 if (!qobject_is_equal(obj_x
, obj_y
)) {
445 * qdict_destroy_obj(): Free all the memory allocated by a QDict
447 void qdict_destroy_obj(QObject
*obj
)
453 qdict
= qobject_to(QDict
, obj
);
455 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
456 QDictEntry
*entry
= QLIST_FIRST(&qdict
->table
[i
]);
458 QDictEntry
*tmp
= QLIST_NEXT(entry
, next
);
459 QLIST_REMOVE(entry
, next
);
460 qentry_destroy(entry
);
469 * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
470 * value of 'key' in 'src' is copied there (and the refcount increased
473 void qdict_copy_default(QDict
*dst
, QDict
*src
, const char *key
)
477 if (qdict_haskey(dst
, key
)) {
481 val
= qdict_get(src
, key
);
483 qdict_put_obj(dst
, key
, qobject_ref(val
));
488 * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
489 * new QString initialised by 'val' is put there.
491 void qdict_set_default_str(QDict
*dst
, const char *key
, const char *val
)
493 if (qdict_haskey(dst
, key
)) {
497 qdict_put_str(dst
, key
, val
);
500 static void qdict_flatten_qdict(QDict
*qdict
, QDict
*target
,
503 static void qdict_flatten_qlist(QList
*qlist
, QDict
*target
, const char *prefix
)
506 const QListEntry
*entry
;
510 /* This function is never called with prefix == NULL, i.e., it is always
511 * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
512 * need to remove list entries during the iteration (the whole list will be
513 * deleted eventually anyway from qdict_flatten_qdict()). */
516 entry
= qlist_first(qlist
);
518 for (i
= 0; entry
; entry
= qlist_next(entry
), i
++) {
519 value
= qlist_entry_obj(entry
);
520 new_key
= g_strdup_printf("%s.%i", prefix
, i
);
522 if (qobject_type(value
) == QTYPE_QDICT
) {
523 qdict_flatten_qdict(qobject_to(QDict
, value
), target
, new_key
);
524 } else if (qobject_type(value
) == QTYPE_QLIST
) {
525 qdict_flatten_qlist(qobject_to(QList
, value
), target
, new_key
);
527 /* All other types are moved to the target unchanged. */
528 qdict_put_obj(target
, new_key
, qobject_ref(value
));
535 static void qdict_flatten_qdict(QDict
*qdict
, QDict
*target
, const char *prefix
)
538 const QDictEntry
*entry
, *next
;
542 entry
= qdict_first(qdict
);
544 while (entry
!= NULL
) {
546 next
= qdict_next(qdict
, entry
);
547 value
= qdict_entry_value(entry
);
552 new_key
= g_strdup_printf("%s.%s", prefix
, entry
->key
);
555 if (qobject_type(value
) == QTYPE_QDICT
) {
556 /* Entries of QDicts are processed recursively, the QDict object
557 * itself disappears. */
558 qdict_flatten_qdict(qobject_to(QDict
, value
), target
,
559 new_key
? new_key
: entry
->key
);
561 } else if (qobject_type(value
) == QTYPE_QLIST
) {
562 qdict_flatten_qlist(qobject_to(QList
, value
), target
,
563 new_key
? new_key
: entry
->key
);
566 /* All other objects are moved to the target unchanged. */
567 qdict_put_obj(target
, new_key
, qobject_ref(value
));
574 qdict_del(qdict
, entry
->key
);
576 /* Restart loop after modifying the iterated QDict */
577 entry
= qdict_first(qdict
);
586 * qdict_flatten(): For each nested QDict with key x, all fields with key y
587 * are moved to this QDict and their key is renamed to "x.y". For each nested
588 * QList with key x, the field at index y is moved to this QDict with the key
589 * "x.y" (i.e., the reverse of what qdict_array_split() does).
590 * This operation is applied recursively for nested QDicts and QLists.
592 void qdict_flatten(QDict
*qdict
)
594 qdict_flatten_qdict(qdict
, qdict
, NULL
);
597 /* extract all the src QDict entries starting by start into dst */
598 void qdict_extract_subqdict(QDict
*src
, QDict
**dst
, const char *start
)
601 const QDictEntry
*entry
, *next
;
605 entry
= qdict_first(src
);
607 while (entry
!= NULL
) {
608 next
= qdict_next(src
, entry
);
609 if (strstart(entry
->key
, start
, &p
)) {
610 qdict_put_obj(*dst
, p
, qobject_ref(entry
->value
));
611 qdict_del(src
, entry
->key
);
617 static int qdict_count_prefixed_entries(const QDict
*src
, const char *start
)
619 const QDictEntry
*entry
;
622 for (entry
= qdict_first(src
); entry
; entry
= qdict_next(src
, entry
)) {
623 if (strstart(entry
->key
, start
, NULL
)) {
624 if (count
== INT_MAX
) {
635 * qdict_array_split(): This function moves array-like elements of a QDict into
636 * a new QList. Every entry in the original QDict with a key "%u" or one
637 * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
638 * incrementally counting up, will be moved to a new QDict at index %u in the
639 * output QList with the key prefix removed, if that prefix is "%u.". If the
640 * whole key is just "%u", the whole QObject will be moved unchanged without
641 * creating a new QDict. The function terminates when there is no entry in the
642 * QDict with a prefix directly (incrementally) following the last one; it also
643 * returns if there are both entries with "%u" and "%u." for the same index %u.
644 * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
645 * (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
646 * => [{"a": 42, "b": 23}, {"x": 0}, 66]
647 * and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
649 void qdict_array_split(QDict
*src
, QList
**dst
)
655 for (i
= 0; i
< UINT_MAX
; i
++) {
659 char indexstr
[32], prefix
[32];
662 snprintf_ret
= snprintf(indexstr
, 32, "%u", i
);
663 assert(snprintf_ret
< 32);
665 subqobj
= qdict_get(src
, indexstr
);
667 snprintf_ret
= snprintf(prefix
, 32, "%u.", i
);
668 assert(snprintf_ret
< 32);
670 /* Overflow is the same as positive non-zero results */
671 is_subqdict
= qdict_count_prefixed_entries(src
, prefix
);
673 // There may be either a single subordinate object (named "%u") or
674 // multiple objects (each with a key prefixed "%u."), but not both.
675 if (!subqobj
== !is_subqdict
) {
680 qdict_extract_subqdict(src
, &subqdict
, prefix
);
681 assert(qdict_size(subqdict
) > 0);
683 qobject_ref(subqobj
);
684 qdict_del(src
, indexstr
);
687 qlist_append_obj(*dst
, subqobj
?: QOBJECT(subqdict
));
692 * qdict_split_flat_key:
693 * @key: the key string to split
694 * @prefix: non-NULL pointer to hold extracted prefix
695 * @suffix: non-NULL pointer to remaining suffix
697 * Given a flattened key such as 'foo.0.bar', split it into two parts
698 * at the first '.' separator. Allows double dot ('..') to escape the
702 * 'foo.0.bar' -> prefix='foo' and suffix='0.bar'
703 * 'foo..0.bar' -> prefix='foo.0' and suffix='bar'
705 * The '..' sequence will be unescaped in the returned 'prefix'
706 * string. The 'suffix' string will be left in escaped format, so it
707 * can be fed back into the qdict_split_flat_key() key as the input
710 * The caller is responsible for freeing the string returned in @prefix
713 static void qdict_split_flat_key(const char *key
, char **prefix
,
716 const char *separator
;
719 /* Find first '.' separator, but if there is a pair '..'
720 * that acts as an escape, so skip over '..' */
728 separator
= strchr(separator
, '.');
729 } while (separator
&& separator
[1] == '.');
732 *prefix
= g_strndup(key
, separator
- key
);
733 *suffix
= separator
+ 1;
735 *prefix
= g_strdup(key
);
739 /* Unescape the '..' sequence into '.' */
740 for (i
= 0, j
= 0; (*prefix
)[i
] != '\0'; i
++, j
++) {
741 if ((*prefix
)[i
] == '.') {
742 assert((*prefix
)[i
+ 1] == '.');
745 (*prefix
)[j
] = (*prefix
)[i
];
752 * @maybe_list: dict to check if keys represent list elements.
754 * Determine whether all keys in @maybe_list are valid list elements.
755 * If @maybe_list is non-zero in length and all the keys look like
756 * valid list indexes, this will return 1. If @maybe_list is zero
757 * length or all keys are non-numeric then it will return 0 to indicate
758 * it is a normal qdict. If there is a mix of numeric and non-numeric
759 * keys, or the list indexes are non-contiguous, an error is reported.
761 * Returns: 1 if a valid list, 0 if a dict, -1 on error
763 static int qdict_is_list(QDict
*maybe_list
, Error
**errp
)
765 const QDictEntry
*ent
;
771 for (ent
= qdict_first(maybe_list
); ent
!= NULL
;
772 ent
= qdict_next(maybe_list
, ent
)) {
774 if (qemu_strtoi64(ent
->key
, NULL
, 10, &val
) == 0) {
777 } else if (!is_list
) {
779 "Cannot mix list and non-list keys");
789 } else if (is_list
) {
791 "Cannot mix list and non-list keys");
798 assert(!qdict_size(maybe_list
));
802 /* NB this isn't a perfect check - e.g. it won't catch
803 * a list containing '1', '+1', '01', '3', but that
804 * does not matter - we've still proved that the
805 * input is a list. It is up the caller to do a
806 * stricter check if desired */
807 if (len
!= (max
+ 1)) {
808 error_setg(errp
, "List indices are not contiguous, "
809 "saw %zd elements but %zd largest index",
819 * @src: the original flat dictionary (only scalar values) to crumple
821 * Takes a flat dictionary whose keys use '.' separator to indicate
822 * nesting, and values are scalars, and crumples it into a nested
825 * To include a literal '.' in a key name, it must be escaped as '..'
827 * For example, an input of:
829 * { 'foo.0.bar': 'one', 'foo.0.wizz': '1',
830 * 'foo.1.bar': 'two', 'foo.1.wizz': '2' }
832 * will result in an output of:
836 * { 'bar': 'one', 'wizz': '1' },
837 * { 'bar': 'two', 'wizz': '2' }
841 * The following scenarios in the input dict will result in an
842 * error being returned:
844 * - Any values in @src are non-scalar types
845 * - If keys in @src imply that a particular level is both a
846 * list and a dict. e.g., "foo.0.bar" and "foo.eek.bar".
847 * - If keys in @src imply that a particular level is a list,
848 * but the indices are non-contiguous. e.g. "foo.0.bar" and
849 * "foo.2.bar" without any "foo.1.bar" present.
850 * - If keys in @src represent list indexes, but are not in
851 * the "%zu" format. e.g. "foo.+0.bar"
853 * Returns: either a QDict or QList for the nested data structure, or NULL
856 QObject
*qdict_crumple(const QDict
*src
, Error
**errp
)
858 const QDictEntry
*ent
;
859 QDict
*two_level
, *multi_level
= NULL
;
860 QObject
*dst
= NULL
, *child
;
863 const char *suffix
= NULL
;
866 two_level
= qdict_new();
868 /* Step 1: split our totally flat dict into a two level dict */
869 for (ent
= qdict_first(src
); ent
!= NULL
; ent
= qdict_next(src
, ent
)) {
870 if (qobject_type(ent
->value
) == QTYPE_QDICT
||
871 qobject_type(ent
->value
) == QTYPE_QLIST
) {
872 error_setg(errp
, "Value %s is not a scalar",
877 qdict_split_flat_key(ent
->key
, &prefix
, &suffix
);
879 child
= qdict_get(two_level
, prefix
);
881 QDict
*child_dict
= qobject_to(QDict
, child
);
884 error_setg(errp
, "Key %s prefix is already set as a scalar",
889 child_dict
= qdict_new();
890 qdict_put_obj(two_level
, prefix
, QOBJECT(child_dict
));
893 qdict_put_obj(child_dict
, suffix
, qobject_ref(ent
->value
));
896 error_setg(errp
, "Key %s prefix is already set as a dict",
900 qdict_put_obj(two_level
, prefix
, qobject_ref(ent
->value
));
907 /* Step 2: optionally process the two level dict recursively
908 * into a multi-level dict */
909 multi_level
= qdict_new();
910 for (ent
= qdict_first(two_level
); ent
!= NULL
;
911 ent
= qdict_next(two_level
, ent
)) {
912 QDict
*dict
= qobject_to(QDict
, ent
->value
);
914 child
= qdict_crumple(dict
, errp
);
919 qdict_put_obj(multi_level
, ent
->key
, child
);
921 qdict_put_obj(multi_level
, ent
->key
, qobject_ref(ent
->value
));
924 qobject_unref(two_level
);
927 /* Step 3: detect if we need to turn our dict into list */
928 is_list
= qdict_is_list(multi_level
, errp
);
934 dst
= QOBJECT(qlist_new());
936 for (i
= 0; i
< qdict_size(multi_level
); i
++) {
937 char *key
= g_strdup_printf("%zu", i
);
939 child
= qdict_get(multi_level
, key
);
943 error_setg(errp
, "Missing list index %zu", i
);
947 qlist_append_obj(qobject_to(QList
, dst
), qobject_ref(child
));
949 qobject_unref(multi_level
);
952 dst
= QOBJECT(multi_level
);
959 qobject_unref(multi_level
);
960 qobject_unref(two_level
);
966 * qdict_array_entries(): Returns the number of direct array entries if the
967 * sub-QDict of src specified by the prefix in subqdict (or src itself for
968 * prefix == "") is valid as an array, i.e. the length of the created list if
969 * the sub-QDict would become empty after calling qdict_array_split() on it. If
970 * the array is not valid, -EINVAL is returned.
972 int qdict_array_entries(QDict
*src
, const char *subqdict
)
974 const QDictEntry
*entry
;
976 unsigned entries
= 0;
977 size_t subqdict_len
= strlen(subqdict
);
979 assert(!subqdict_len
|| subqdict
[subqdict_len
- 1] == '.');
981 /* qdict_array_split() loops until UINT_MAX, but as we want to return
982 * negative errors, we only have a signed return value here. Any additional
983 * entries will lead to -EINVAL. */
984 for (i
= 0; i
< INT_MAX
; i
++) {
986 int subqdict_entries
;
987 char *prefix
= g_strdup_printf("%s%u.", subqdict
, i
);
989 subqdict_entries
= qdict_count_prefixed_entries(src
, prefix
);
991 /* Remove ending "." */
992 prefix
[strlen(prefix
) - 1] = 0;
993 subqobj
= qdict_get(src
, prefix
);
997 if (subqdict_entries
< 0) {
998 return subqdict_entries
;
1001 /* There may be either a single subordinate object (named "%u") or
1002 * multiple objects (each with a key prefixed "%u."), but not both. */
1003 if (subqobj
&& subqdict_entries
) {
1005 } else if (!subqobj
&& !subqdict_entries
) {
1009 entries
+= subqdict_entries
? subqdict_entries
: 1;
1012 /* Consider everything handled that isn't part of the given sub-QDict */
1013 for (entry
= qdict_first(src
); entry
; entry
= qdict_next(src
, entry
)) {
1014 if (!strstart(qdict_entry_key(entry
), subqdict
, NULL
)) {
1019 /* Anything left in the sub-QDict that wasn't handled? */
1020 if (qdict_size(src
) != entries
) {
1028 * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
1029 * elements from src to dest.
1031 * If an element from src has a key already present in dest, it will not be
1032 * moved unless overwrite is true.
1034 * If overwrite is true, the conflicting values in dest will be discarded and
1035 * replaced by the corresponding values from src.
1037 * Therefore, with overwrite being true, the src QDict will always be empty when
1038 * this function returns. If overwrite is false, the src QDict will be empty
1039 * iff there were no conflicts.
1041 void qdict_join(QDict
*dest
, QDict
*src
, bool overwrite
)
1043 const QDictEntry
*entry
, *next
;
1045 entry
= qdict_first(src
);
1047 next
= qdict_next(src
, entry
);
1049 if (overwrite
|| !qdict_haskey(dest
, entry
->key
)) {
1050 qdict_put_obj(dest
, entry
->key
, qobject_ref(entry
->value
));
1051 qdict_del(src
, entry
->key
);
1059 * qdict_rename_keys(): Rename keys in qdict according to the replacements
1060 * specified in the array renames. The array must be terminated by an entry
1063 * The renames are performed individually in the order of the array, so entries
1064 * may be renamed multiple times and may or may not conflict depending on the
1065 * order of the renames array.
1067 * Returns true for success, false in error cases.
1069 bool qdict_rename_keys(QDict
*qdict
, const QDictRenames
*renames
, Error
**errp
)
1073 while (renames
->from
) {
1074 if (qdict_haskey(qdict
, renames
->from
)) {
1075 if (qdict_haskey(qdict
, renames
->to
)) {
1076 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
1077 "same time", renames
->to
, renames
->from
);
1081 qobj
= qdict_get(qdict
, renames
->from
);
1082 qdict_put_obj(qdict
, renames
->to
, qobject_ref(qobj
));
1083 qdict_del(qdict
, renames
->from
);