From patchwork Sat Oct 17 13:36:01 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 36302 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1A09AB7B69 for ; Sun, 18 Oct 2009 00:38:05 +1100 (EST) Received: from localhost ([127.0.0.1]:47940 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mz9Tt-0006ag-PY for incoming@patchwork.ozlabs.org; Sat, 17 Oct 2009 09:38:01 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mz9SJ-0005mj-Kr for qemu-devel@nongnu.org; Sat, 17 Oct 2009 09:36:23 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mz9SE-0005jc-J1 for qemu-devel@nongnu.org; Sat, 17 Oct 2009 09:36:22 -0400 Received: from [199.232.76.173] (port=58959 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mz9SE-0005jT-5h for qemu-devel@nongnu.org; Sat, 17 Oct 2009 09:36:18 -0400 Received: from e37.co.us.ibm.com ([32.97.110.158]:49394) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Mz9SD-0000O8-OG for qemu-devel@nongnu.org; Sat, 17 Oct 2009 09:36:17 -0400 Received: from d03relay02.boulder.ibm.com (d03relay02.boulder.ibm.com [9.17.195.227]) by e37.co.us.ibm.com (8.14.3/8.13.1) with ESMTP id n9HDZAkf023720 for ; Sat, 17 Oct 2009 07:35:10 -0600 Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d03relay02.boulder.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id n9HDaCku248578 for ; Sat, 17 Oct 2009 07:36:12 -0600 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n9HDaC2o021429 for ; Sat, 17 Oct 2009 07:36:12 -0600 Received: from localhost.localdomain (sig-9-65-54-101.mts.ibm.com [9.65.54.101]) by d03av04.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n9HDaBJB021369; Sat, 17 Oct 2009 07:36:11 -0600 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Sat, 17 Oct 2009 08:36:01 -0500 Message-Id: <1255786571-3528-2-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.6.2.5 In-Reply-To: <1255786571-3528-1-git-send-email-aliguori@us.ibm.com> References: <1255786571-3528-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Anthony Liguori Subject: [Qemu-devel] [PATCH 01/11] Add append method to qstring and empty constructor X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This allows qstring to be used for dynamic string construction. Signed-off-by: Anthony Liguori --- qstring.c | 37 ++++++++++++++++++++++++++++++++++++- qstring.h | 4 ++++ 2 files changed, 40 insertions(+), 1 deletions(-) diff --git a/qstring.c b/qstring.c index 6d411da..441a9e6 100644 --- a/qstring.c +++ b/qstring.c @@ -21,6 +21,16 @@ static const QType qstring_type = { }; /** + * qstring_new(): Create a new empty QString + * + * Return strong reference. + */ +QString *qstring_new(void) +{ + return qstring_from_str(""); +} + +/** * qstring_from_str(): Create a new QString from a regular C string * * Return strong reference. @@ -30,12 +40,37 @@ QString *qstring_from_str(const char *str) QString *qstring; qstring = qemu_malloc(sizeof(*qstring)); - qstring->string = qemu_strdup(str); + + qstring->length = strlen(str); + qstring->capacity = qstring->length; + + qstring->string = qemu_malloc(qstring->capacity + 1); + memcpy(qstring->string, str, qstring->length); + qstring->string[qstring->length] = 0; + QOBJECT_INIT(qstring, &qstring_type); return qstring; } +/* qstring_append(): Append a C string to a QString + */ +void qstring_append(QString *qstring, const char *str) +{ + size_t len = strlen(str); + + if (qstring->capacity < (qstring->length + len)) { + qstring->capacity += len; + qstring->capacity *= 2; /* use exponential growth */ + + qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1); + } + + memcpy(qstring->string + qstring->length, str, len); + qstring->length += len; + qstring->string[qstring->length] = 0; +} + /** * qobject_to_qstring(): Convert a QObject to a QString */ diff --git a/qstring.h b/qstring.h index e012cb7..65905d4 100644 --- a/qstring.h +++ b/qstring.h @@ -6,10 +6,14 @@ typedef struct QString { QObject_HEAD; char *string; + size_t length; + size_t capacity; } QString; +QString *qstring_new(void); QString *qstring_from_str(const char *str); const char *qstring_get_str(const QString *qstring); +void qstring_append(QString *qstring, const char *str); QString *qobject_to_qstring(const QObject *obj); #endif /* QSTRING_H */