From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>,
Luiz Capitulino <lcapitulino@redhat.com>
Subject: [Qemu-devel] [PATCH 03/11] Allow strings to grow in size
Date: Wed, 11 Nov 2009 11:28:55 -0600 [thread overview]
Message-ID: <1257960543-26373-3-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1257960543-26373-1-git-send-email-aliguori@us.ibm.com>
This lets us use QString for building larger strings
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
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 */
--
1.6.2.5
next prev parent reply other threads:[~2009-11-11 17:29 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-11 17:28 [Qemu-devel] [PATCH 01/11] Properly escape QDECREF macro arguments Anthony Liguori
2009-11-11 17:28 ` [Qemu-devel] [PATCH 02/11] Add operations to qlist to allow it to be used as a stack Anthony Liguori
2009-11-12 15:33 ` Kevin Wolf
2009-11-12 16:46 ` Anthony Liguori
2009-11-12 16:56 ` Kevin Wolf
2009-11-12 17:13 ` Anthony Liguori
2009-11-12 17:20 ` Luiz Capitulino
2009-11-13 8:51 ` Kevin Wolf
2009-11-12 17:17 ` Ian Molton
2009-11-11 17:28 ` Anthony Liguori [this message]
2009-11-11 17:28 ` [Qemu-devel] [PATCH 04/11] Add a QFloat datatype Anthony Liguori
2009-11-11 17:28 ` [Qemu-devel] [PATCH 05/11] Add unit test for QFloat Anthony Liguori
2009-11-11 17:28 ` [Qemu-devel] [PATCH 06/11] Add a QBool type Anthony Liguori
2009-11-11 17:28 ` [Qemu-devel] [PATCH 07/11] Add a lexer for JSON Anthony Liguori
2009-11-11 17:29 ` [Qemu-devel] [PATCH 08/11] Add a JSON message boundary identifier Anthony Liguori
2009-11-11 17:29 ` [Qemu-devel] [PATCH 09/11] Add a JSON parser Anthony Liguori
2009-11-11 17:29 ` [Qemu-devel] [PATCH 10/11] Add a QObject JSON wrapper Anthony Liguori
2009-11-11 18:12 ` [Qemu-devel] " Luiz Capitulino
2009-11-11 19:39 ` Anthony Liguori
2009-11-11 19:58 ` Luiz Capitulino
2009-11-11 17:29 ` [Qemu-devel] [PATCH 11/11] Add a unit test for JSON support Anthony Liguori
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=1257960543-26373-3-git-send-email-aliguori@us.ibm.com \
--to=aliguori@us.ibm.com \
--cc=lcapitulino@redhat.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).