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