]>
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 LC |
33 | /** |
34 | * qstring_from_str(): Create a new QString from a regular C string | |
35 | * | |
36 | * Return strong reference. | |
37 | */ | |
38 | QString *qstring_from_str(const char *str) | |
39 | { | |
40 | QString *qstring; | |
41 | ||
42 | qstring = qemu_malloc(sizeof(*qstring)); | |
d30ec846 AL |
43 | |
44 | qstring->length = strlen(str); | |
45 | qstring->capacity = qstring->length; | |
46 | ||
47 | qstring->string = qemu_malloc(qstring->capacity + 1); | |
48 | memcpy(qstring->string, str, qstring->length); | |
49 | qstring->string[qstring->length] = 0; | |
50 | ||
66f70487 LC |
51 | QOBJECT_INIT(qstring, &qstring_type); |
52 | ||
53 | return qstring; | |
54 | } | |
55 | ||
d30ec846 AL |
56 | /* qstring_append(): Append a C string to a QString |
57 | */ | |
58 | void qstring_append(QString *qstring, const char *str) | |
59 | { | |
60 | size_t len = strlen(str); | |
61 | ||
62 | if (qstring->capacity < (qstring->length + len)) { | |
63 | qstring->capacity += len; | |
64 | qstring->capacity *= 2; /* use exponential growth */ | |
65 | ||
66 | qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1); | |
67 | } | |
68 | ||
69 | memcpy(qstring->string + qstring->length, str, len); | |
70 | qstring->length += len; | |
71 | qstring->string[qstring->length] = 0; | |
72 | } | |
73 | ||
66f70487 LC |
74 | /** |
75 | * qobject_to_qstring(): Convert a QObject to a QString | |
76 | */ | |
77 | QString *qobject_to_qstring(const QObject *obj) | |
78 | { | |
79 | if (qobject_type(obj) != QTYPE_QSTRING) | |
80 | return NULL; | |
81 | ||
82 | return container_of(obj, QString, base); | |
83 | } | |
84 | ||
85 | /** | |
86 | * qstring_get_str(): Return a pointer to the stored string | |
87 | * | |
88 | * NOTE: Should be used with caution, if the object is deallocated | |
89 | * this pointer becomes invalid. | |
90 | */ | |
91 | const char *qstring_get_str(const QString *qstring) | |
92 | { | |
93 | return qstring->string; | |
94 | } | |
95 | ||
96 | /** | |
97 | * qstring_destroy_obj(): Free all memory allocated by a QString | |
98 | * object | |
99 | */ | |
100 | static void qstring_destroy_obj(QObject *obj) | |
101 | { | |
102 | QString *qs; | |
103 | ||
104 | assert(obj != NULL); | |
105 | qs = qobject_to_qstring(obj); | |
106 | qemu_free(qs->string); | |
107 | qemu_free(qs); | |
108 | } |