From: Luiz Capitulino <lcapitulino@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com
Subject: [Qemu-devel] [PATCH 01/10] Introduce qmisc module
Date: Thu, 8 Oct 2009 18:35:38 -0300 [thread overview]
Message-ID: <1255037747-3340-2-git-send-email-lcapitulino@redhat.com> (raw)
In-Reply-To: <1255037747-3340-1-git-send-email-lcapitulino@redhat.com>
This module provides miscellania QObject functions.
Currently it exports qobject_from_fmt(), which is somewhat
based on Python's Py_BuildValue() function. It is capable of
creating QObjects from a specified string format.
For example, to create a QDict with mixed data-types one
could do:
QObject *obj = qobject_from_fmt("{ s: [ i, s ], s: i }", ... );
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
Makefile | 2 +-
qmisc.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
qmisc.h | 19 +++++
3 files changed, 242 insertions(+), 1 deletions(-)
create mode 100644 qmisc.c
create mode 100644 qmisc.h
diff --git a/Makefile b/Makefile
index d96fb4b..182f176 100644
--- a/Makefile
+++ b/Makefile
@@ -100,7 +100,7 @@ obj-y += buffered_file.o migration.o migration-tcp.o net.o qemu-sockets.o
obj-y += qemu-char.o aio.o net-checksum.o savevm.o
obj-y += msmouse.o ps2.o
obj-y += qdev.o qdev-properties.o ssi.o
-obj-y += qint.o qstring.o qdict.o qlist.o qemu-config.o
+obj-y += qint.o qstring.o qdict.o qlist.o qmisc.o qemu-config.o
obj-$(CONFIG_BRLAPI) += baum.o
obj-$(CONFIG_WIN32) += tap-win32.o
diff --git a/qmisc.c b/qmisc.c
new file mode 100644
index 0000000..42b6f22
--- /dev/null
+++ b/qmisc.c
@@ -0,0 +1,222 @@
+/*
+ * Misc QObject functions.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ * Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#include "qmisc.h"
+#include "qint.h"
+#include "qlist.h"
+#include "qdict.h"
+#include "qstring.h"
+#include "qobject.h"
+#include "qemu-common.h"
+
+/*
+ * qobject_from_fmt() and related functions are based on the Python's
+ * Py_BuildValue() and are subject to the Python Software Foundation
+ * License Version 2.
+ *
+ * Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Python
+ * Software Foundation.
+ */
+static QObject *build_qobject(const char **fmt, va_list *args);
+
+static QObject *do_mkdict(const char **fmt, va_list *args, int endchar,
+ int nr_elems)
+{
+ int i;
+ QDict *qdict;
+
+ if (nr_elems < 0)
+ return NULL;
+
+ qdict = qdict_new();
+
+ for (i = 0; i < nr_elems; i += 2) {
+ QString *qs;
+ QObject *obj, *key;
+
+ key = build_qobject(fmt, args);
+ if (!key)
+ goto err;
+
+ obj = build_qobject(fmt, args);
+ if (!obj) {
+ qobject_decref(key);
+ goto err;
+ }
+
+ qs = qobject_to_qstring(key);
+ qdict_put_obj(qdict, qstring_get_str(qs), obj);
+ QDECREF(qs);
+ }
+
+ while (**fmt == ' ')
+ (*fmt)++;
+
+ if (**fmt != endchar)
+ goto err;
+
+ if (endchar)
+ ++*fmt;
+
+ return QOBJECT(qdict);
+
+err:
+ QDECREF(qdict);
+ return NULL;
+}
+
+static QObject *do_mklist(const char **fmt, va_list *args, int endchar,
+ int nr_elems)
+{
+ int i;
+ QList *qlist;
+
+ if (nr_elems < 0)
+ return NULL;
+
+ qlist = qlist_new();
+
+ for (i = 0; i < nr_elems; i++) {
+ QObject *obj = build_qobject(fmt, args);
+ if (!obj)
+ goto err;
+ qlist_append_obj(qlist, obj);
+ }
+
+ while (**fmt == ' ')
+ (*fmt)++;
+
+ if (**fmt != endchar)
+ goto err;
+
+ if (endchar)
+ ++*fmt;
+
+ return QOBJECT(qlist);
+
+err:
+ QDECREF(qlist);
+ return NULL;
+}
+
+static int
+count_format(const char *format, int endchar)
+{
+ int count = 0;
+ int level = 0;
+
+ while (level > 0 || *format != endchar) {
+ switch (*format) {
+ case '\0':
+ /* Premature end */
+ return -1;
+ case '[':
+ case '{':
+ if (level == 0)
+ count++;
+ level++;
+ break;
+ case ']':
+ case '}':
+ level--;
+ break;
+ case ',':
+ case ':':
+ case ' ':
+ case '\t':
+ break;
+ default:
+ if (level == 0)
+ count++;
+ }
+ format++;
+ }
+ return count;
+}
+
+static QObject *build_qobject(const char **fmt, va_list *args)
+{
+ for (;;) {
+ switch (*(*fmt)++) {
+ case 'i':
+ {
+ QInt *qi = qint_from_int((int64_t) va_arg(*args, int64_t));
+ return QOBJECT(qi);
+ }
+ case 's':
+ {
+ QString *qs = qstring_from_str((char *) va_arg(*args, char *));
+ return QOBJECT(qs);
+ }
+ case '[':
+ return do_mklist(fmt, args, ']', count_format(*fmt, ']'));
+ case '{':
+ return do_mkdict(fmt, args, '}', count_format(*fmt, '}'));
+ case ',':
+ case ':':
+ case ' ':
+ case '\t':
+ break;
+ default:
+ return NULL;
+ }
+ }
+}
+
+/**
+ * qobject_from_fmt(): build QObjects from a specified format.
+ *
+ * Valid characters of the format:
+ *
+ * i integer, map to QInt
+ * s string, map to QString
+ * [] list, map to QList
+ * {} dictionary, map to QDict
+ *
+ * Examples:
+ *
+ * - Create a QInt
+ *
+ * qobject_from_fmt("i", 42);
+ *
+ * - Create a QList of QStrings
+ *
+ * qobject_from_fmt("[ i, i, i ]", 0, 1 , 2);
+ *
+ * - Create a QDict with mixed data-types
+ *
+ * qobject_from_fmt("{ s: [ i, s ], s: i }", ... );
+ *
+ * Return a strong reference to a QObject on success, NULL otherwise.
+ */
+QObject *qobject_from_fmt(const char *fmt, ...)
+{
+ va_list args;
+ QObject *obj;
+
+ va_start(args, fmt);
+ switch (*fmt) {
+ case '[':
+ fmt++;
+ obj = do_mklist(&fmt, &args, ']', count_format(fmt, ']'));
+ break;
+ case '{':
+ fmt++;
+ obj = do_mkdict(&fmt, &args, '}', count_format(fmt, '}'));
+ break;
+ default:
+ obj = build_qobject(&fmt, &args);
+ break;
+ }
+ va_end(args);
+
+ return obj;
+}
diff --git a/qmisc.h b/qmisc.h
new file mode 100644
index 0000000..ac481fe
--- /dev/null
+++ b/qmisc.h
@@ -0,0 +1,19 @@
+/*
+ * QMisc header file.
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * Authors:
+ * Luiz Capitulino <lcapitulino@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ */
+#ifndef QMISC_H
+#define QMISC_H
+
+#include "qobject.h"
+
+QObject *qobject_from_fmt(const char *fmt, ...);
+
+#endif /* QMISC_H */
--
1.6.5.rc2.17.gdbc1b
next prev parent reply other threads:[~2009-10-08 21:36 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-08 21:35 [Qemu-devel] [PATCH v0 00/10]: More QObject conversions Luiz Capitulino
2009-10-08 21:35 ` Luiz Capitulino [this message]
2009-10-15 14:02 ` [Qemu-devel] [PATCH 01/10] Introduce qmisc module Anthony Liguori
2009-10-15 15:26 ` Luiz Capitulino
2009-10-15 15:35 ` Anthony Liguori
2009-10-15 17:17 ` Luiz Capitulino
2009-10-15 18:33 ` Anthony Liguori
2009-10-15 18:45 ` Anthony Liguori
2009-10-15 16:39 ` Daniel P. Berrange
2009-10-15 16:46 ` Daniel P. Berrange
2009-10-15 17:28 ` Luiz Capitulino
2009-10-15 18:34 ` Anthony Liguori
2009-10-16 13:24 ` [Qemu-devel] " Paolo Bonzini
2009-10-16 13:45 ` Anthony Liguori
2009-10-16 17:35 ` Paolo Bonzini
2009-10-16 17:38 ` Anthony Liguori
2009-10-16 19:36 ` Paolo Bonzini
2009-10-16 21:37 ` Anthony Liguori
2009-10-17 0:32 ` Paolo Bonzini
2009-10-17 0:38 ` malc
2009-10-17 0:46 ` Paolo Bonzini
2009-10-17 1:49 ` Anthony Liguori
2009-10-17 1:50 ` Anthony Liguori
2009-10-17 7:48 ` Paolo Bonzini
2009-10-17 10:01 ` Vincent Hanquez
2009-10-18 14:06 ` Luiz Capitulino
2009-10-18 14:08 ` Paolo Bonzini
2009-10-18 14:49 ` Anthony Liguori
2009-10-18 15:18 ` Luiz Capitulino
2009-10-18 15:25 ` Paolo Bonzini
2009-10-18 16:05 ` Luiz Capitulino
2009-10-18 16:32 ` Anthony Liguori
2009-10-18 18:04 ` Paolo Bonzini
2009-10-18 22:00 ` Luiz Capitulino
2009-10-18 16:26 ` Anthony Liguori
2009-10-18 17:32 ` Vincent Hanquez
2009-10-18 21:24 ` Anthony Liguori
2009-10-18 15:06 ` Vincent Hanquez
2009-10-18 15:35 ` Luiz Capitulino
2009-10-18 15:39 ` Paolo Bonzini
2009-10-18 16:56 ` Vincent Hanquez
2009-10-18 16:29 ` Anthony Liguori
2009-10-18 16:46 ` Vincent Hanquez
2009-10-18 17:59 ` Paolo Bonzini
2009-10-08 21:35 ` [Qemu-devel] [PATCH 02/10] monitor: Convert do_memory_save() to QObject Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 03/10] monitor: Convert do_physical_memory_save() " Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 04/10] monitor: Convert do_migrate() " Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 05/10] monitor: Convert do_migrate_set_speed() " Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 06/10] monitor: Convert do_migrate_cancel() " Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 07/10] monitor: Convert do_info_migrate() " Luiz Capitulino
2009-10-10 12:11 ` Markus Armbruster
2009-10-15 14:07 ` Anthony Liguori
2009-10-08 21:35 ` [Qemu-devel] [PATCH 08/10] monitor: Convert bdrv_info() " Luiz Capitulino
2009-10-10 12:18 ` Markus Armbruster
2009-10-14 13:23 ` Luiz Capitulino
2009-10-14 14:11 ` Markus Armbruster
2009-10-15 14:13 ` Anthony Liguori
2009-10-08 21:35 ` [Qemu-devel] [PATCH 09/10] monitor: Convert pci_device_hot_add() " Luiz Capitulino
2009-10-08 21:35 ` [Qemu-devel] [PATCH 10/10] monitor: Convert do_pci_device_hot_remove() " Luiz Capitulino
2009-10-10 12:31 ` [Qemu-devel] [PATCH v0 00/10]: More QObject conversions Markus Armbruster
2009-10-11 14:48 ` Luiz Capitulino
2009-10-12 15:36 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1255037747-3340-2-git-send-email-lcapitulino@redhat.com \
--to=lcapitulino@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).