1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/qmp/qobject.h"
15#include "qapi/qmp/qstring.h"
16#include "qemu-common.h"
17
18
19
20
21
22
23QString *qstring_new(void)
24{
25 return qstring_from_str("");
26}
27
28
29
30
31size_t qstring_get_length(const QString *qstring)
32{
33 return qstring->length;
34}
35
36
37
38
39
40
41QString *qstring_from_substr(const char *str, int start, int end)
42{
43 QString *qstring;
44
45 qstring = g_malloc(sizeof(*qstring));
46 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
47
48 qstring->length = end - start + 1;
49 qstring->capacity = qstring->length;
50
51 qstring->string = g_malloc(qstring->capacity + 1);
52 memcpy(qstring->string, str + start, qstring->length);
53 qstring->string[qstring->length] = 0;
54
55
56 return qstring;
57}
58
59
60
61
62
63
64QString *qstring_from_str(const char *str)
65{
66 return qstring_from_substr(str, 0, strlen(str) - 1);
67}
68
69static void capacity_increase(QString *qstring, size_t len)
70{
71 if (qstring->capacity < (qstring->length + len)) {
72 qstring->capacity += len;
73 qstring->capacity *= 2;
74
75 qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
76 }
77}
78
79
80
81void qstring_append(QString *qstring, const char *str)
82{
83 size_t len = strlen(str);
84
85 capacity_increase(qstring, len);
86 memcpy(qstring->string + qstring->length, str, len);
87 qstring->length += len;
88 qstring->string[qstring->length] = 0;
89}
90
91void qstring_append_int(QString *qstring, int64_t value)
92{
93 char num[32];
94
95 snprintf(num, sizeof(num), "%" PRId64, value);
96 qstring_append(qstring, num);
97}
98
99
100
101
102void qstring_append_chr(QString *qstring, int c)
103{
104 capacity_increase(qstring, 1);
105 qstring->string[qstring->length++] = c;
106 qstring->string[qstring->length] = 0;
107}
108
109
110
111
112QString *qobject_to_qstring(const QObject *obj)
113{
114 if (!obj || qobject_type(obj) != QTYPE_QSTRING) {
115 return NULL;
116 }
117 return container_of(obj, QString, base);
118}
119
120
121
122
123
124
125
126const char *qstring_get_str(const QString *qstring)
127{
128 return qstring->string;
129}
130
131
132
133
134
135void qstring_destroy_obj(QObject *obj)
136{
137 QString *qs;
138
139 assert(obj != NULL);
140 qs = qobject_to_qstring(obj);
141 g_free(qs->string);
142 g_free(qs);
143}
144