]>
Commit | Line | Data |
---|---|---|
66f70487 LC |
1 | /* |
2 | * QString data type. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
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 | ||
6fe9565c | 56 | static void capacity_increase(QString *qstring, size_t len) |
d30ec846 | 57 | { |
d30ec846 AL |
58 | if (qstring->capacity < (qstring->length + len)) { |
59 | qstring->capacity += len; | |
60 | qstring->capacity *= 2; /* use exponential growth */ | |
61 | ||
62 | qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1); | |
63 | } | |
6fe9565c LC |
64 | } |
65 | ||
66 | /* qstring_append(): Append a C string to a QString | |
67 | */ | |
68 | void qstring_append(QString *qstring, const char *str) | |
69 | { | |
70 | size_t len = strlen(str); | |
d30ec846 | 71 | |
6fe9565c | 72 | capacity_increase(qstring, len); |
d30ec846 AL |
73 | memcpy(qstring->string + qstring->length, str, len); |
74 | qstring->length += len; | |
75 | qstring->string[qstring->length] = 0; | |
76 | } | |
77 | ||
764c1cae LC |
78 | void qstring_append_int(QString *qstring, int64_t value) |
79 | { | |
80 | char num[32]; | |
81 | ||
82 | snprintf(num, sizeof(num), "%" PRId64, value); | |
83 | qstring_append(qstring, num); | |
84 | } | |
85 | ||
6fe9565c LC |
86 | /** |
87 | * qstring_append_chr(): Append a C char to a QString | |
88 | */ | |
89 | void qstring_append_chr(QString *qstring, int c) | |
90 | { | |
91 | capacity_increase(qstring, 1); | |
92 | qstring->string[qstring->length++] = c; | |
93 | qstring->string[qstring->length] = 0; | |
94 | } | |
95 | ||
66f70487 LC |
96 | /** |
97 | * qobject_to_qstring(): Convert a QObject to a QString | |
98 | */ | |
99 | QString *qobject_to_qstring(const QObject *obj) | |
100 | { | |
101 | if (qobject_type(obj) != QTYPE_QSTRING) | |
102 | return NULL; | |
103 | ||
104 | return container_of(obj, QString, base); | |
105 | } | |
106 | ||
107 | /** | |
108 | * qstring_get_str(): Return a pointer to the stored string | |
109 | * | |
110 | * NOTE: Should be used with caution, if the object is deallocated | |
111 | * this pointer becomes invalid. | |
112 | */ | |
113 | const char *qstring_get_str(const QString *qstring) | |
114 | { | |
115 | return qstring->string; | |
116 | } | |
117 | ||
118 | /** | |
119 | * qstring_destroy_obj(): Free all memory allocated by a QString | |
120 | * object | |
121 | */ | |
122 | static void qstring_destroy_obj(QObject *obj) | |
123 | { | |
124 | QString *qs; | |
125 | ||
126 | assert(obj != NULL); | |
127 | qs = qobject_to_qstring(obj); | |
128 | qemu_free(qs->string); | |
129 | qemu_free(qs); | |
130 | } |