]> git.proxmox.com Git - mirror_qemu.git/blob - qobject/qlit.c
qlit: make qlit_equal_qobject return a bool
[mirror_qemu.git] / qobject / qlit.c
1 /*
2 * QLit literal qobject
3 *
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
10 * Marc-André Lureau <marcandre.lureau@redhat.com>
11 *
12 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
13 * See the COPYING.LIB file in the top-level directory.
14 */
15
16 #include "qemu/osdep.h"
17
18 #include "qapi/qmp/qlit.h"
19 #include "qapi/qmp/types.h"
20
21 typedef struct QListCompareHelper {
22 int index;
23 QLitObject *objs;
24 bool result;
25 } QListCompareHelper;
26
27 static void compare_helper(QObject *obj, void *opaque)
28 {
29 QListCompareHelper *helper = opaque;
30
31 if (!helper->result) {
32 return;
33 }
34
35 if (helper->objs[helper->index].type == QTYPE_NONE) {
36 helper->result = false;
37 return;
38 }
39
40 helper->result =
41 qlit_equal_qobject(&helper->objs[helper->index++], obj);
42 }
43
44 bool qlit_equal_qobject(QLitObject *lhs, QObject *rhs)
45 {
46 int64_t val;
47
48 if (!rhs || lhs->type != qobject_type(rhs)) {
49 return false;
50 }
51
52 switch (lhs->type) {
53 case QTYPE_QNUM:
54 g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
55 return lhs->value.qnum == val;
56 case QTYPE_QSTRING:
57 return (strcmp(lhs->value.qstr,
58 qstring_get_str(qobject_to_qstring(rhs))) == 0);
59 case QTYPE_QDICT: {
60 int i;
61
62 for (i = 0; lhs->value.qdict[i].key; i++) {
63 QObject *obj = qdict_get(qobject_to_qdict(rhs),
64 lhs->value.qdict[i].key);
65
66 if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
67 return false;
68 }
69 }
70
71 return true;
72 }
73 case QTYPE_QLIST: {
74 QListCompareHelper helper;
75
76 helper.index = 0;
77 helper.objs = lhs->value.qlist;
78 helper.result = true;
79
80 qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
81
82 return helper.result;
83 }
84 default:
85 break;
86 }
87
88 return false;
89 }