4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qapi/qmp/qobject.h"
14 #include "qapi/qmp/qstring.h"
15 #include "qemu-common.h"
17 static void qstring_destroy_obj(QObject
*obj
);
19 static const QType qstring_type
= {
20 .code
= QTYPE_QSTRING
,
21 .destroy
= qstring_destroy_obj
,
25 * qstring_new(): Create a new empty QString
27 * Return strong reference.
29 QString
*qstring_new(void)
31 return qstring_from_str("");
35 * qstring_get_length(): Get the length of a QString
37 size_t qstring_get_length(const QString
*qstring
)
39 return qstring
->length
;
43 * qstring_from_substr(): Create a new QString from a C string substring
45 * Return string reference
47 QString
*qstring_from_substr(const char *str
, int start
, int end
)
51 qstring
= g_malloc(sizeof(*qstring
));
53 qstring
->length
= end
- start
+ 1;
54 qstring
->capacity
= qstring
->length
;
56 qstring
->string
= g_malloc(qstring
->capacity
+ 1);
57 memcpy(qstring
->string
, str
+ start
, qstring
->length
);
58 qstring
->string
[qstring
->length
] = 0;
60 QOBJECT_INIT(qstring
, &qstring_type
);
66 * qstring_from_str(): Create a new QString from a regular C string
68 * Return strong reference.
70 QString
*qstring_from_str(const char *str
)
72 return qstring_from_substr(str
, 0, strlen(str
) - 1);
75 static void capacity_increase(QString
*qstring
, size_t len
)
77 if (qstring
->capacity
< (qstring
->length
+ len
)) {
78 qstring
->capacity
+= len
;
79 qstring
->capacity
*= 2; /* use exponential growth */
81 qstring
->string
= g_realloc(qstring
->string
, qstring
->capacity
+ 1);
85 /* qstring_append(): Append a C string to a QString
87 void qstring_append(QString
*qstring
, const char *str
)
89 size_t len
= strlen(str
);
91 capacity_increase(qstring
, len
);
92 memcpy(qstring
->string
+ qstring
->length
, str
, len
);
93 qstring
->length
+= len
;
94 qstring
->string
[qstring
->length
] = 0;
97 void qstring_append_int(QString
*qstring
, int64_t value
)
101 snprintf(num
, sizeof(num
), "%" PRId64
, value
);
102 qstring_append(qstring
, num
);
106 * qstring_append_chr(): Append a C char to a QString
108 void qstring_append_chr(QString
*qstring
, int c
)
110 capacity_increase(qstring
, 1);
111 qstring
->string
[qstring
->length
++] = c
;
112 qstring
->string
[qstring
->length
] = 0;
116 * qobject_to_qstring(): Convert a QObject to a QString
118 QString
*qobject_to_qstring(const QObject
*obj
)
120 if (qobject_type(obj
) != QTYPE_QSTRING
)
123 return container_of(obj
, QString
, base
);
127 * qstring_get_str(): Return a pointer to the stored string
129 * NOTE: Should be used with caution, if the object is deallocated
130 * this pointer becomes invalid.
132 const char *qstring_get_str(const QString
*qstring
)
134 return qstring
->string
;
138 * qstring_destroy_obj(): Free all memory allocated by a QString
141 static void qstring_destroy_obj(QObject
*obj
)
146 qs
= qobject_to_qstring(obj
);