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