2017-08-25 18:59:01 +08:00
|
|
|
/*
|
|
|
|
* QLit literal qobject
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
* Copyright (c) 2013, 2015, 2017 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
* Markus Armbruster <armbru@redhat.com>
|
|
|
|
* Marc-André Lureau <marcandre.lureau@redhat.com>
|
|
|
|
*
|
|
|
|
* 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/types.h"
|
|
|
|
|
|
|
|
typedef struct QListCompareHelper {
|
|
|
|
int index;
|
2017-08-25 18:59:02 +08:00
|
|
|
QLitObject *objs;
|
2017-08-25 18:59:05 +08:00
|
|
|
bool result;
|
2017-08-25 18:59:01 +08:00
|
|
|
} QListCompareHelper;
|
|
|
|
|
|
|
|
static void compare_helper(QObject *obj, void *opaque)
|
|
|
|
{
|
|
|
|
QListCompareHelper *helper = opaque;
|
|
|
|
|
2017-08-25 18:59:05 +08:00
|
|
|
if (!helper->result) {
|
2017-08-25 18:59:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (helper->objs[helper->index].type == QTYPE_NONE) {
|
2017-08-25 18:59:05 +08:00
|
|
|
helper->result = false;
|
2017-08-25 18:59:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
helper->result =
|
2017-08-25 18:59:04 +08:00
|
|
|
qlit_equal_qobject(&helper->objs[helper->index++], obj);
|
2017-08-25 18:59:01 +08:00
|
|
|
}
|
|
|
|
|
2017-08-25 18:59:06 +08:00
|
|
|
bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
|
2017-08-25 18:59:01 +08:00
|
|
|
{
|
|
|
|
if (!rhs || lhs->type != qobject_type(rhs)) {
|
2017-08-25 18:59:05 +08:00
|
|
|
return false;
|
2017-08-25 18:59:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (lhs->type) {
|
2017-08-25 18:59:07 +08:00
|
|
|
case QTYPE_QBOOL:
|
|
|
|
return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs));
|
2017-08-25 18:59:01 +08:00
|
|
|
case QTYPE_QNUM:
|
2017-08-25 18:59:08 +08:00
|
|
|
return lhs->value.qnum == qnum_get_int(qobject_to_qnum(rhs));
|
2017-08-25 18:59:01 +08:00
|
|
|
case QTYPE_QSTRING:
|
|
|
|
return (strcmp(lhs->value.qstr,
|
|
|
|
qstring_get_str(qobject_to_qstring(rhs))) == 0);
|
|
|
|
case QTYPE_QDICT: {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; lhs->value.qdict[i].key; i++) {
|
|
|
|
QObject *obj = qdict_get(qobject_to_qdict(rhs),
|
|
|
|
lhs->value.qdict[i].key);
|
|
|
|
|
2017-08-25 18:59:04 +08:00
|
|
|
if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
|
2017-08-25 18:59:05 +08:00
|
|
|
return false;
|
2017-08-25 18:59:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:59:05 +08:00
|
|
|
return true;
|
2017-08-25 18:59:01 +08:00
|
|
|
}
|
|
|
|
case QTYPE_QLIST: {
|
|
|
|
QListCompareHelper helper;
|
|
|
|
|
|
|
|
helper.index = 0;
|
|
|
|
helper.objs = lhs->value.qlist;
|
2017-08-25 18:59:05 +08:00
|
|
|
helper.result = true;
|
2017-08-25 18:59:01 +08:00
|
|
|
|
|
|
|
qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
|
|
|
|
|
|
|
|
return helper.result;
|
|
|
|
}
|
2017-08-25 18:59:07 +08:00
|
|
|
case QTYPE_QNULL:
|
|
|
|
return true;
|
2017-08-25 18:59:01 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:59:05 +08:00
|
|
|
return false;
|
2017-08-25 18:59:01 +08:00
|
|
|
}
|