aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--qstring.c37
-rw-r--r--qstring.h4
2 files changed, 40 insertions, 1 deletions
diff --git a/qstring.c b/qstring.c
index 6d411da7b7..441a9e6ef0 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 e012cb7dc5..65905d457f 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 */