xref: /openbmc/qemu/qobject/qstring.c (revision b3119b08)
1 /*
2  * QString Module
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 LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qstring.h"
15 #include "qobject-internal.h"
16 
17 /**
18  * qstring_new(): Create a new empty QString
19  *
20  * Return strong reference.
21  */
22 QString *qstring_new(void)
23 {
24     return qstring_from_str("");
25 }
26 
27 /**
28  * qstring_get_length(): Get the length of a QString
29  */
30 size_t qstring_get_length(const QString *qstring)
31 {
32     return qstring->length;
33 }
34 
35 /**
36  * qstring_from_substr(): Create a new QString from a C string substring
37  *
38  * Return string reference
39  */
40 QString *qstring_from_substr(const char *str, size_t start, size_t end)
41 {
42     QString *qstring;
43 
44     assert(start <= end);
45 
46     qstring = g_malloc(sizeof(*qstring));
47     qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
48 
49     qstring->length = end - start;
50     qstring->capacity = qstring->length;
51 
52     assert(qstring->capacity < SIZE_MAX);
53     qstring->string = g_malloc(qstring->capacity + 1);
54     memcpy(qstring->string, str + start, qstring->length);
55     qstring->string[qstring->length] = 0;
56 
57     return qstring;
58 }
59 
60 /**
61  * qstring_from_str(): Create a new QString from a regular C string
62  *
63  * Return strong reference.
64  */
65 QString *qstring_from_str(const char *str)
66 {
67     return qstring_from_substr(str, 0, strlen(str));
68 }
69 
70 /**
71  * qstring_from_gstring(): Convert a GString to a QString
72  *
73  * Return strong reference.
74  */
75 
76 QString *qstring_from_gstring(GString *gstr)
77 {
78     QString *qstring;
79 
80     qstring = g_malloc(sizeof(*qstring));
81     qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
82     qstring->length = gstr->len;
83     qstring->capacity = gstr->allocated_len;
84     qstring->string = g_string_free(gstr, false);
85     return qstring;
86 }
87 
88 
89 static void capacity_increase(QString *qstring, size_t len)
90 {
91     if (qstring->capacity < (qstring->length + len)) {
92         assert(len <= SIZE_MAX - qstring->capacity);
93         qstring->capacity += len;
94         assert(qstring->capacity <= SIZE_MAX / 2);
95         qstring->capacity *= 2; /* use exponential growth */
96 
97         qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
98     }
99 }
100 
101 /* qstring_append(): Append a C string to a QString
102  */
103 void qstring_append(QString *qstring, const char *str)
104 {
105     size_t len = strlen(str);
106 
107     capacity_increase(qstring, len);
108     memcpy(qstring->string + qstring->length, str, len);
109     qstring->length += len;
110     qstring->string[qstring->length] = 0;
111 }
112 
113 void qstring_append_int(QString *qstring, int64_t value)
114 {
115     char num[32];
116 
117     snprintf(num, sizeof(num), "%" PRId64, value);
118     qstring_append(qstring, num);
119 }
120 
121 /**
122  * qstring_append_chr(): Append a C char to a QString
123  */
124 void qstring_append_chr(QString *qstring, int c)
125 {
126     capacity_increase(qstring, 1);
127     qstring->string[qstring->length++] = c;
128     qstring->string[qstring->length] = 0;
129 }
130 
131 /**
132  * qstring_get_str(): Return a pointer to the stored string
133  *
134  * NOTE: Should be used with caution, if the object is deallocated
135  * this pointer becomes invalid.
136  */
137 const char *qstring_get_str(const QString *qstring)
138 {
139     return qstring->string;
140 }
141 
142 /**
143  * qstring_is_equal(): Test whether the two QStrings are equal
144  */
145 bool qstring_is_equal(const QObject *x, const QObject *y)
146 {
147     return !strcmp(qobject_to(QString, x)->string,
148                    qobject_to(QString, y)->string);
149 }
150 
151 /**
152  * qstring_destroy_obj(): Free all memory allocated by a QString
153  * object
154  */
155 void qstring_destroy_obj(QObject *obj)
156 {
157     QString *qs;
158 
159     assert(obj != NULL);
160     qs = qobject_to(QString, obj);
161     g_free(qs->string);
162     g_free(qs);
163 }
164