int qdict_haskey(const QDict *qdict, const char *key);
QObject *qdict_get(const QDict *qdict, const char *key);
bool qdict_is_equal(const QObject *x, const QObject *y);
-void qdict_iter(const QDict *qdict,
- void (*iter)(const char *key, QObject *obj, void *opaque),
- void *opaque);
const QDictEntry *qdict_first(const QDict *qdict);
const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry);
void qdict_destroy_obj(QObject *obj);
return qstring_get_str(qstr);
}
-static void qdict_add_key(const char *key, QObject *obj, void *opaque)
-{
- GHashTable *h = opaque;
- g_hash_table_insert(h, (gpointer) key, NULL);
-}
-
static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
const char *name,
QObject *obj, void *qapi)
{
GHashTable *h;
StackObject *tos = g_new0(StackObject, 1);
+ QDict *qdict = qobject_to(QDict, obj);
+ QList *qlist = qobject_to(QList, obj);
+ const QDictEntry *entry;
assert(obj);
tos->name = name;
tos->obj = obj;
tos->qapi = qapi;
- if (qobject_type(obj) == QTYPE_QDICT) {
+ if (qdict) {
h = g_hash_table_new(g_str_hash, g_str_equal);
- qdict_iter(qobject_to(QDict, obj), qdict_add_key, h);
+ for (entry = qdict_first(qdict);
+ entry;
+ entry = qdict_next(qdict, entry)) {
+ g_hash_table_insert(h, (void *)qdict_entry_key(entry), NULL);
+ }
tos->h = h;
} else {
- assert(qobject_type(obj) == QTYPE_QLIST);
- tos->entry = qlist_first(qobject_to(QList, obj));
+ assert(qlist);
+ tos->entry = qlist_first(qlist);
tos->index = -1;
}
return qstr ? qstring_get_str(qstr) : NULL;
}
-/**
- * qdict_iter(): Iterate over all the dictionary's stored values.
- *
- * This function allows the user to provide an iterator, which will be
- * called for each stored value in the dictionary.
- */
-void qdict_iter(const QDict *qdict,
- void (*iter)(const char *key, QObject *obj, void *opaque),
- void *opaque)
-{
- int i;
- QDictEntry *entry;
-
- for (i = 0; i < QDICT_BUCKET_MAX; i++) {
- QLIST_FOREACH(entry, &qdict->table[i], next)
- iter(entry->key, entry->value, opaque);
- }
-}
-
static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
{
int i;
return qdict;
}
-typedef struct ToJsonIterState
-{
- int indent;
- int pretty;
- int count;
- QString *str;
-} ToJsonIterState;
-
static void to_json(const QObject *obj, QString *str, int pretty, int indent);
static void json_pretty_newline(QString *str, bool pretty, int indent)
}
}
-static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
-{
- ToJsonIterState *s = opaque;
- QString *qkey;
-
- if (s->count) {
- qstring_append(s->str, s->pretty ? "," : ", ");
- }
-
- json_pretty_newline(s->str, s->pretty, s->indent);
-
- qkey = qstring_from_str(key);
- to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
- qobject_unref(qkey);
-
- qstring_append(s->str, ": ");
- to_json(obj, s->str, s->pretty, s->indent);
- s->count++;
-}
-
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
{
switch (qobject_type(obj)) {
break;
}
case QTYPE_QDICT: {
- ToJsonIterState s;
QDict *val = qobject_to(QDict, obj);
+ const char *comma = pretty ? "," : ", ";
+ const char *sep = "";
+ const QDictEntry *entry;
+ QString *qkey;
- s.count = 0;
- s.str = str;
- s.indent = indent + 1;
- s.pretty = pretty;
qstring_append(str, "{");
- qdict_iter(val, to_json_dict_iter, &s);
+
+ for (entry = qdict_first(val);
+ entry;
+ entry = qdict_next(val, entry)) {
+ qstring_append(str, sep);
+ json_pretty_newline(str, pretty, indent + 1);
+
+ qkey = qstring_from_str(qdict_entry_key(entry));
+ to_json(QOBJECT(qkey), str, pretty, indent + 1);
+ qobject_unref(qkey);
+
+ qstring_append(str, ": ");
+ to_json(qdict_entry_value(entry), str, pretty, indent + 1);
+ sep = comma;
+ }
+
json_pretty_newline(str, pretty, indent);
qstring_append(str, "}");
break;
OptsFromQDictState state;
Error *local_err = NULL;
QemuOpts *opts;
+ const QDictEntry *entry;
opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
&local_err);
state.errp = &local_err;
state.opts = opts;
- qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
+
+ for (entry = qdict_first(qdict);
+ entry;
+ entry = qdict_next(qdict, entry)) {
+ qemu_opts_from_qdict_1(qdict_entry_key(entry),
+ qdict_entry_value(entry),
+ &state);
+ }
if (local_err) {
error_propagate(errp, local_err);
qemu_opts_del(opts);