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