]>
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 | |
7b1b5d19 PB |
13 | #include "qapi/qmp/qobject.h" |
14 | #include "qapi/qmp/qstring.h" | |
66f70487 LC |
15 | #include "qemu-common.h" |
16 | ||
aa43d9cc BS |
17 | static void qstring_destroy_obj(QObject *obj); |
18 | ||
19 | static const QType qstring_type = { | |
20 | .code = QTYPE_QSTRING, | |
21 | .destroy = qstring_destroy_obj, | |
22 | }; | |
66f70487 | 23 | |
d30ec846 AL |
24 | /** |
25 | * qstring_new(): Create a new empty QString | |
26 | * | |
27 | * Return strong reference. | |
28 | */ | |
29 | QString *qstring_new(void) | |
30 | { | |
31 | return qstring_from_str(""); | |
32 | } | |
33 | ||
54d49ac9 LC |
34 | /** |
35 | * qstring_get_length(): Get the length of a QString | |
36 | */ | |
37 | size_t qstring_get_length(const QString *qstring) | |
38 | { | |
39 | return qstring->length; | |
40 | } | |
41 | ||
66f70487 | 42 | /** |
4b5c5766 | 43 | * qstring_from_substr(): Create a new QString from a C string substring |
66f70487 | 44 | * |
4b5c5766 | 45 | * Return string reference |
66f70487 | 46 | */ |
4b5c5766 | 47 | QString *qstring_from_substr(const char *str, int start, int end) |
66f70487 LC |
48 | { |
49 | QString *qstring; | |
50 | ||
7267c094 | 51 | qstring = g_malloc(sizeof(*qstring)); |
d30ec846 | 52 | |
4b5c5766 | 53 | qstring->length = end - start + 1; |
d30ec846 AL |
54 | qstring->capacity = qstring->length; |
55 | ||
7267c094 | 56 | qstring->string = g_malloc(qstring->capacity + 1); |
4b5c5766 | 57 | memcpy(qstring->string, str + start, qstring->length); |
d30ec846 AL |
58 | qstring->string[qstring->length] = 0; |
59 | ||
66f70487 LC |
60 | QOBJECT_INIT(qstring, &qstring_type); |
61 | ||
62 | return qstring; | |
63 | } | |
64 | ||
4b5c5766 LC |
65 | /** |
66 | * qstring_from_str(): Create a new QString from a regular C string | |
67 | * | |
68 | * Return strong reference. | |
69 | */ | |
70 | QString *qstring_from_str(const char *str) | |
71 | { | |
72 | return qstring_from_substr(str, 0, strlen(str) - 1); | |
73 | } | |
74 | ||
6fe9565c | 75 | static void capacity_increase(QString *qstring, size_t len) |
d30ec846 | 76 | { |
d30ec846 AL |
77 | if (qstring->capacity < (qstring->length + len)) { |
78 | qstring->capacity += len; | |
79 | qstring->capacity *= 2; /* use exponential growth */ | |
80 | ||
7267c094 | 81 | qstring->string = g_realloc(qstring->string, qstring->capacity + 1); |
d30ec846 | 82 | } |
6fe9565c LC |
83 | } |
84 | ||
85 | /* qstring_append(): Append a C string to a QString | |
86 | */ | |
87 | void qstring_append(QString *qstring, const char *str) | |
88 | { | |
89 | size_t len = strlen(str); | |
d30ec846 | 90 | |
6fe9565c | 91 | capacity_increase(qstring, len); |
d30ec846 AL |
92 | memcpy(qstring->string + qstring->length, str, len); |
93 | qstring->length += len; | |
94 | qstring->string[qstring->length] = 0; | |
95 | } | |
96 | ||
764c1cae LC |
97 | void qstring_append_int(QString *qstring, int64_t value) |
98 | { | |
99 | char num[32]; | |
100 | ||
101 | snprintf(num, sizeof(num), "%" PRId64, value); | |
102 | qstring_append(qstring, num); | |
103 | } | |
104 | ||
6fe9565c LC |
105 | /** |
106 | * qstring_append_chr(): Append a C char to a QString | |
107 | */ | |
108 | void qstring_append_chr(QString *qstring, int c) | |
109 | { | |
110 | capacity_increase(qstring, 1); | |
111 | qstring->string[qstring->length++] = c; | |
112 | qstring->string[qstring->length] = 0; | |
113 | } | |
114 | ||
66f70487 LC |
115 | /** |
116 | * qobject_to_qstring(): Convert a QObject to a QString | |
117 | */ | |
118 | QString *qobject_to_qstring(const QObject *obj) | |
119 | { | |
120 | if (qobject_type(obj) != QTYPE_QSTRING) | |
121 | return NULL; | |
122 | ||
123 | return container_of(obj, QString, base); | |
124 | } | |
125 | ||
126 | /** | |
127 | * qstring_get_str(): Return a pointer to the stored string | |
128 | * | |
129 | * NOTE: Should be used with caution, if the object is deallocated | |
130 | * this pointer becomes invalid. | |
131 | */ | |
132 | const char *qstring_get_str(const QString *qstring) | |
133 | { | |
134 | return qstring->string; | |
135 | } | |
136 | ||
137 | /** | |
138 | * qstring_destroy_obj(): Free all memory allocated by a QString | |
139 | * object | |
140 | */ | |
141 | static void qstring_destroy_obj(QObject *obj) | |
142 | { | |
143 | QString *qs; | |
144 | ||
145 | assert(obj != NULL); | |
146 | qs = qobject_to_qstring(obj); | |
7267c094 AL |
147 | g_free(qs->string); |
148 | g_free(qs); | |
66f70487 | 149 | } |