forked from my-unikernel/qemu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlit.c
125 lines (107 loc) · 3.16 KB
/
qlit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* QLit literal qobject
*
* Copyright IBM, Corp. 2009
* Copyright (c) 2013, 2015, 2017 Red Hat Inc.
*
* Authors:
* Anthony Liguori <[email protected]>
* Markus Armbruster <[email protected]>
* Marc-André Lureau <[email protected]>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qapi/qmp/qlit.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qnum.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qstring.h"
#include "qapi/qmp/qnull.h"
static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
{
int i;
for (i = 0; lhs->value.qdict[i].key; i++) {
QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
return false;
}
}
/* Note: the literal qdict must not contain duplicates, this is
* considered a programming error and it isn't checked here. */
if (qdict_size(qdict) != i) {
return false;
}
return true;
}
static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
{
QListEntry *e;
int i = 0;
QLIST_FOREACH_ENTRY(qlist, e) {
QObject *obj = qlist_entry_obj(e);
if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
return false;
}
i++;
}
return !e && lhs->value.qlist[i].type == QTYPE_NONE;
}
bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
{
if (!rhs || lhs->type != qobject_type(rhs)) {
return false;
}
switch (lhs->type) {
case QTYPE_QBOOL:
return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs));
case QTYPE_QNUM:
return lhs->value.qnum == qnum_get_int(qobject_to(QNum, rhs));
case QTYPE_QSTRING:
return (strcmp(lhs->value.qstr,
qstring_get_str(qobject_to(QString, rhs))) == 0);
case QTYPE_QDICT:
return qlit_equal_qdict(lhs, qobject_to(QDict, rhs));
case QTYPE_QLIST:
return qlit_equal_qlist(lhs, qobject_to(QList, rhs));
case QTYPE_QNULL:
return true;
default:
break;
}
return false;
}
QObject *qobject_from_qlit(const QLitObject *qlit)
{
switch (qlit->type) {
case QTYPE_QNULL:
return QOBJECT(qnull());
case QTYPE_QNUM:
return QOBJECT(qnum_from_int(qlit->value.qnum));
case QTYPE_QSTRING:
return QOBJECT(qstring_from_str(qlit->value.qstr));
case QTYPE_QDICT: {
QDict *qdict = qdict_new();
QLitDictEntry *e;
for (e = qlit->value.qdict; e->key; e++) {
qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
}
return QOBJECT(qdict);
}
case QTYPE_QLIST: {
QList *qlist = qlist_new();
QLitObject *e;
for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) {
qlist_append_obj(qlist, qobject_from_qlit(e));
}
return QOBJECT(qlist);
}
case QTYPE_QBOOL:
return QOBJECT(qbool_from_bool(qlit->value.qbool));
default:
assert(0);
}
return NULL;
}