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