]>
Commit | Line | Data |
---|---|---|
66f70487 | 1 | /* |
41836a9f | 2 | * QString Module |
66f70487 LC |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * | |
41836a9f LC |
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. | |
66f70487 | 11 | */ |
41836a9f | 12 | |
f2ad72b3 | 13 | #include "qemu/osdep.h" |
7b1b5d19 | 14 | #include "qapi/qmp/qstring.h" |
66f70487 LC |
15 | #include "qemu-common.h" |
16 | ||
d30ec846 AL |
17 | /** |
18 | * qstring_new(): Create a new empty QString | |
19 | * | |
20 | * Return strong reference. | |
21 | */ | |
22 | QString *qstring_new(void) | |
23 | { | |
24 | return qstring_from_str(""); | |
25 | } | |
26 | ||
54d49ac9 LC |
27 | /** |
28 | * qstring_get_length(): Get the length of a QString | |
29 | */ | |
30 | size_t qstring_get_length(const QString *qstring) | |
31 | { | |
32 | return qstring->length; | |
33 | } | |
34 | ||
66f70487 | 35 | /** |
4b5c5766 | 36 | * qstring_from_substr(): Create a new QString from a C string substring |
66f70487 | 37 | * |
4b5c5766 | 38 | * Return string reference |
66f70487 | 39 | */ |
ad63c549 | 40 | QString *qstring_from_substr(const char *str, size_t start, size_t end) |
66f70487 LC |
41 | { |
42 | QString *qstring; | |
43 | ||
b65ab77b MA |
44 | assert(start <= end + 1); |
45 | ||
7267c094 | 46 | qstring = g_malloc(sizeof(*qstring)); |
55e1819c | 47 | qobject_init(QOBJECT(qstring), QTYPE_QSTRING); |
d30ec846 | 48 | |
4b5c5766 | 49 | qstring->length = end - start + 1; |
d30ec846 AL |
50 | qstring->capacity = qstring->length; |
51 | ||
b65ab77b | 52 | assert(qstring->capacity < SIZE_MAX); |
7267c094 | 53 | qstring->string = g_malloc(qstring->capacity + 1); |
4b5c5766 | 54 | memcpy(qstring->string, str + start, qstring->length); |
d30ec846 AL |
55 | qstring->string[qstring->length] = 0; |
56 | ||
66f70487 LC |
57 | return qstring; |
58 | } | |
59 | ||
4b5c5766 LC |
60 | /** |
61 | * qstring_from_str(): Create a new QString from a regular C string | |
62 | * | |
63 | * Return strong reference. | |
64 | */ | |
65 | QString *qstring_from_str(const char *str) | |
66 | { | |
67 | return qstring_from_substr(str, 0, strlen(str) - 1); | |
68 | } | |
69 | ||
6fe9565c | 70 | static void capacity_increase(QString *qstring, size_t len) |
d30ec846 | 71 | { |
d30ec846 | 72 | if (qstring->capacity < (qstring->length + len)) { |
b65ab77b | 73 | assert(len <= SIZE_MAX - qstring->capacity); |
d30ec846 | 74 | qstring->capacity += len; |
b65ab77b | 75 | assert(qstring->capacity <= SIZE_MAX / 2); |
d30ec846 AL |
76 | qstring->capacity *= 2; /* use exponential growth */ |
77 | ||
7267c094 | 78 | qstring->string = g_realloc(qstring->string, qstring->capacity + 1); |
d30ec846 | 79 | } |
6fe9565c LC |
80 | } |
81 | ||
82 | /* qstring_append(): Append a C string to a QString | |
83 | */ | |
84 | void qstring_append(QString *qstring, const char *str) | |
85 | { | |
86 | size_t len = strlen(str); | |
d30ec846 | 87 | |
6fe9565c | 88 | capacity_increase(qstring, len); |
d30ec846 AL |
89 | memcpy(qstring->string + qstring->length, str, len); |
90 | qstring->length += len; | |
91 | qstring->string[qstring->length] = 0; | |
92 | } | |
93 | ||
764c1cae LC |
94 | void qstring_append_int(QString *qstring, int64_t value) |
95 | { | |
96 | char num[32]; | |
97 | ||
98 | snprintf(num, sizeof(num), "%" PRId64, value); | |
99 | qstring_append(qstring, num); | |
100 | } | |
101 | ||
6fe9565c LC |
102 | /** |
103 | * qstring_append_chr(): Append a C char to a QString | |
104 | */ | |
105 | void qstring_append_chr(QString *qstring, int c) | |
106 | { | |
107 | capacity_increase(qstring, 1); | |
108 | qstring->string[qstring->length++] = c; | |
109 | qstring->string[qstring->length] = 0; | |
110 | } | |
111 | ||
66f70487 LC |
112 | /** |
113 | * qstring_get_str(): Return a pointer to the stored string | |
114 | * | |
115 | * NOTE: Should be used with caution, if the object is deallocated | |
116 | * this pointer becomes invalid. | |
117 | */ | |
118 | const char *qstring_get_str(const QString *qstring) | |
119 | { | |
120 | return qstring->string; | |
121 | } | |
122 | ||
77593202 PX |
123 | /** |
124 | * qstring_get_try_str(): Return a pointer to the stored string | |
125 | * | |
126 | * NOTE: will return NULL if qstring is not provided. | |
127 | */ | |
128 | const char *qstring_get_try_str(const QString *qstring) | |
129 | { | |
130 | return qstring ? qstring_get_str(qstring) : NULL; | |
131 | } | |
132 | ||
b26ae1cb PX |
133 | /** |
134 | * qobject_get_try_str(): Return a pointer to the corresponding string | |
135 | * | |
136 | * NOTE: the string will only be returned if the object is valid, and | |
137 | * its type is QString, otherwise NULL is returned. | |
138 | */ | |
139 | const char *qobject_get_try_str(const QObject *qstring) | |
140 | { | |
141 | return qstring_get_try_str(qobject_to(QString, qstring)); | |
142 | } | |
143 | ||
b38dd678 HR |
144 | /** |
145 | * qstring_is_equal(): Test whether the two QStrings are equal | |
146 | */ | |
147 | bool qstring_is_equal(const QObject *x, const QObject *y) | |
148 | { | |
7dc847eb HR |
149 | return !strcmp(qobject_to(QString, x)->string, |
150 | qobject_to(QString, y)->string); | |
b38dd678 HR |
151 | } |
152 | ||
66f70487 LC |
153 | /** |
154 | * qstring_destroy_obj(): Free all memory allocated by a QString | |
155 | * object | |
156 | */ | |
55e1819c | 157 | void qstring_destroy_obj(QObject *obj) |
66f70487 LC |
158 | { |
159 | QString *qs; | |
160 | ||
161 | assert(obj != NULL); | |
7dc847eb | 162 | qs = qobject_to(QString, obj); |
7267c094 AL |
163 | g_free(qs->string); |
164 | g_free(qs); | |
66f70487 | 165 | } |