From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MZ3OZ-0008Dc-17 for qemu-devel@nongnu.org; Thu, 06 Aug 2009 09:52:39 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MZ3OU-00087A-Dp for qemu-devel@nongnu.org; Thu, 06 Aug 2009 09:52:38 -0400 Received: from [199.232.76.173] (port=42900 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MZ3OU-000871-BW for qemu-devel@nongnu.org; Thu, 06 Aug 2009 09:52:34 -0400 Received: from mx2.redhat.com ([66.187.237.31]:50407) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MZ3OT-0003sE-Rg for qemu-devel@nongnu.org; Thu, 06 Aug 2009 09:52:34 -0400 From: Luiz Capitulino Date: Thu, 6 Aug 2009 10:52:15 -0300 Message-Id: <1249566736-5020-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1249566736-5020-1-git-send-email-lcapitulino@redhat.com> References: <1249566736-5020-1-git-send-email-lcapitulino@redhat.com> Subject: [Qemu-devel] [PATCH 2/3] Introduce QString data type List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, filip.navara@gmail.com, avi@redhat.com Signed-off-by: Luiz Capitulino --- Makefile | 2 +- qobject.h | 1 + qstring.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qstring.h | 17 +++++++++++ 4 files changed, 108 insertions(+), 1 deletions(-) create mode 100644 qstring.c create mode 100644 qstring.h diff --git a/Makefile b/Makefile index 2ce539f..9e3faec 100644 --- a/Makefile +++ b/Makefile @@ -104,7 +104,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 += qdict.o +obj-y += qdict.o qstring.o obj-$(CONFIG_BRLAPI) += baum.o diff --git a/qobject.h b/qobject.h index b70e669..d3378b1 100644 --- a/qobject.h +++ b/qobject.h @@ -16,6 +16,7 @@ typedef enum { QTYPE_NONE, + QTYPE_QSTRING, } qtype_t; struct QObject; diff --git a/qstring.c b/qstring.c new file mode 100644 index 0000000..b1ddf7b --- /dev/null +++ b/qstring.c @@ -0,0 +1,89 @@ +/* + * QString data type. + * + * Copyright (C) 2009 Red Hat Inc. + * + * Authors: + * Luiz Capitulino + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + */ +#include "qobject.h" +#include "qstring.h" +#include "qemu-common.h" + +static QType qstring_type; + +/** + * qstring_from_str(): Create a new QString from a regular C string + * + * return new QString. + */ +QString *qstring_from_str(const char *str) +{ + QString *qstring; + + qstring = qemu_mallocz(sizeof(*qstring)); + qstring->string = qemu_strdup(str); + qstring->base.type = &qstring_type; + + return qstring; +} + +/** + * qstring_to_str(): Export QString to regular C string + * + * Return a pointer to a *copy* of the string. + */ +char *qstring_to_str(const QString *qstring) +{ + return qemu_strdup(qstring->string); +} + +/** + * qstring_size(): Return QString string size + */ +size_t qstring_size(const QString *qstring) +{ + return strlen(qstring->string); +} + +/** + * qstring_destroy(): Free all memory allocated by a QString + * object + */ +void qstring_destroy(QString *qstring) +{ + qemu_free(qstring->string); + qemu_free(qstring); +} + +/** + * qstring_clone_obj(): Clone a QString object + * + * return a copy of the provided QString object. + */ +static QObject *qstring_clone_obj(const QObject *obj) +{ + QString *old, *new; + + old = container_of(obj, QString, base); + new = qstring_from_str(old->string); + return &new->base; +} + +/** + * qstring_destroy_obj(): Destroy a QString object + */ +static void qstring_destroy_obj(QObject *obj) +{ + QString *qstring = container_of(obj, QString, base); + qstring_destroy(qstring); +} + +static QType qstring_type = { + .code = QTYPE_QSTRING, + .clone = qstring_clone_obj, + .destroy = qstring_destroy_obj, +}; diff --git a/qstring.h b/qstring.h new file mode 100644 index 0000000..1856a12 --- /dev/null +++ b/qstring.h @@ -0,0 +1,17 @@ +#ifndef QSTRING_H +#define QSTRING_H + +#include +#include "qobject.h" + +typedef struct QString { + QObject base; + char *string; +} QString; + +QString *qstring_from_str(const char *str); +char *qstring_to_str(const QString *qstring); +size_t qstring_size(const QString *qstring); +void qstring_destroy(QString *qstring); + +#endif /* QSTRING_H */ -- 1.6.4.rc3.12.gdf73a