diff options
author | Anthony Liguori <aliguori@us.ibm.com> | 2009-11-11 10:49:51 -0600 |
---|---|---|
committer | Anthony Liguori <aliguori@us.ibm.com> | 2009-11-17 08:49:38 -0600 |
commit | d30ec846ed89672426b19d50a899ae849ec27edf (patch) | |
tree | 9d3bfe599a90ebd88670bec9b5ad6411baf978e4 /qstring.c | |
parent | 033815fe14e9e6254f1483113a6dd6d5b130ef58 (diff) |
Allow strings to grow in size
This lets us use QString for building larger strings
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'qstring.c')
-rw-r--r-- | qstring.c | 37 |
1 files changed, 36 insertions, 1 deletions
@@ -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 */ |