]>
git.proxmox.com Git - mirror_qemu.git/blob - qobject/qstring.c
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 "qemu/osdep.h"
14 #include "qapi/qmp/qstring.h"
17 * qstring_new(): Create a new empty QString
19 * Return strong reference.
21 QString
*qstring_new(void)
23 return qstring_from_str("");
27 * qstring_get_length(): Get the length of a QString
29 size_t qstring_get_length(const QString
*qstring
)
31 return qstring
->length
;
35 * qstring_from_substr(): Create a new QString from a C string substring
37 * Return string reference
39 QString
*qstring_from_substr(const char *str
, size_t start
, size_t end
)
45 qstring
= g_malloc(sizeof(*qstring
));
46 qobject_init(QOBJECT(qstring
), QTYPE_QSTRING
);
48 qstring
->length
= end
- start
;
49 qstring
->capacity
= qstring
->length
;
51 assert(qstring
->capacity
< SIZE_MAX
);
52 qstring
->string
= g_malloc(qstring
->capacity
+ 1);
53 memcpy(qstring
->string
, str
+ start
, qstring
->length
);
54 qstring
->string
[qstring
->length
] = 0;
60 * qstring_from_str(): Create a new QString from a regular C string
62 * Return strong reference.
64 QString
*qstring_from_str(const char *str
)
66 return qstring_from_substr(str
, 0, strlen(str
));
70 * qstring_from_gstring(): Convert a GString to a QString
72 * Return strong reference.
75 QString
*qstring_from_gstring(GString
*gstr
)
79 qstring
= g_malloc(sizeof(*qstring
));
80 qobject_init(QOBJECT(qstring
), QTYPE_QSTRING
);
81 qstring
->length
= gstr
->len
;
82 qstring
->capacity
= gstr
->allocated_len
;
83 qstring
->string
= g_string_free(gstr
, false);
88 static void capacity_increase(QString
*qstring
, size_t len
)
90 if (qstring
->capacity
< (qstring
->length
+ len
)) {
91 assert(len
<= SIZE_MAX
- qstring
->capacity
);
92 qstring
->capacity
+= len
;
93 assert(qstring
->capacity
<= SIZE_MAX
/ 2);
94 qstring
->capacity
*= 2; /* use exponential growth */
96 qstring
->string
= g_realloc(qstring
->string
, qstring
->capacity
+ 1);
100 /* qstring_append(): Append a C string to a QString
102 void qstring_append(QString
*qstring
, const char *str
)
104 size_t len
= strlen(str
);
106 capacity_increase(qstring
, len
);
107 memcpy(qstring
->string
+ qstring
->length
, str
, len
);
108 qstring
->length
+= len
;
109 qstring
->string
[qstring
->length
] = 0;
112 void qstring_append_int(QString
*qstring
, int64_t value
)
116 snprintf(num
, sizeof(num
), "%" PRId64
, value
);
117 qstring_append(qstring
, num
);
121 * qstring_append_chr(): Append a C char to a QString
123 void qstring_append_chr(QString
*qstring
, int c
)
125 capacity_increase(qstring
, 1);
126 qstring
->string
[qstring
->length
++] = c
;
127 qstring
->string
[qstring
->length
] = 0;
131 * qstring_get_str(): Return a pointer to the stored string
133 * NOTE: Should be used with caution, if the object is deallocated
134 * this pointer becomes invalid.
136 const char *qstring_get_str(const QString
*qstring
)
138 return qstring
->string
;
142 * qstring_get_try_str(): Return a pointer to the stored string
144 * NOTE: will return NULL if qstring is not provided.
146 const char *qstring_get_try_str(const QString
*qstring
)
148 return qstring
? qstring_get_str(qstring
) : NULL
;
152 * qobject_get_try_str(): Return a pointer to the corresponding string
154 * NOTE: the string will only be returned if the object is valid, and
155 * its type is QString, otherwise NULL is returned.
157 const char *qobject_get_try_str(const QObject
*qstring
)
159 return qstring_get_try_str(qobject_to(QString
, qstring
));
163 * qstring_is_equal(): Test whether the two QStrings are equal
165 bool qstring_is_equal(const QObject
*x
, const QObject
*y
)
167 return !strcmp(qobject_to(QString
, x
)->string
,
168 qobject_to(QString
, y
)->string
);
172 * qstring_destroy_obj(): Free all memory allocated by a QString
175 void qstring_destroy_obj(QObject
*obj
)
180 qs
= qobject_to(QString
, obj
);