]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/qstring.c
qobject: introduce qobject_get_try_str()
[mirror_qemu.git] / qobject / qstring.c
CommitLineData
66f70487 1/*
41836a9f 2 * QString Module
66f70487
LC
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
41836a9f
LC
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.
66f70487 11 */
41836a9f 12
f2ad72b3 13#include "qemu/osdep.h"
7b1b5d19 14#include "qapi/qmp/qstring.h"
66f70487
LC
15#include "qemu-common.h"
16
d30ec846
AL
17/**
18 * qstring_new(): Create a new empty QString
19 *
20 * Return strong reference.
21 */
22QString *qstring_new(void)
23{
24 return qstring_from_str("");
25}
26
54d49ac9
LC
27/**
28 * qstring_get_length(): Get the length of a QString
29 */
30size_t qstring_get_length(const QString *qstring)
31{
32 return qstring->length;
33}
34
66f70487 35/**
4b5c5766 36 * qstring_from_substr(): Create a new QString from a C string substring
66f70487 37 *
4b5c5766 38 * Return string reference
66f70487 39 */
4b5c5766 40QString *qstring_from_substr(const char *str, int start, int end)
66f70487
LC
41{
42 QString *qstring;
43
7267c094 44 qstring = g_malloc(sizeof(*qstring));
55e1819c 45 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
d30ec846 46
4b5c5766 47 qstring->length = end - start + 1;
d30ec846
AL
48 qstring->capacity = qstring->length;
49
7267c094 50 qstring->string = g_malloc(qstring->capacity + 1);
4b5c5766 51 memcpy(qstring->string, str + start, qstring->length);
d30ec846
AL
52 qstring->string[qstring->length] = 0;
53
66f70487
LC
54
55 return qstring;
56}
57
4b5c5766
LC
58/**
59 * qstring_from_str(): Create a new QString from a regular C string
60 *
61 * Return strong reference.
62 */
63QString *qstring_from_str(const char *str)
64{
65 return qstring_from_substr(str, 0, strlen(str) - 1);
66}
67
6fe9565c 68static void capacity_increase(QString *qstring, size_t len)
d30ec846 69{
d30ec846
AL
70 if (qstring->capacity < (qstring->length + len)) {
71 qstring->capacity += len;
72 qstring->capacity *= 2; /* use exponential growth */
73
7267c094 74 qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
d30ec846 75 }
6fe9565c
LC
76}
77
78/* qstring_append(): Append a C string to a QString
79 */
80void qstring_append(QString *qstring, const char *str)
81{
82 size_t len = strlen(str);
d30ec846 83
6fe9565c 84 capacity_increase(qstring, len);
d30ec846
AL
85 memcpy(qstring->string + qstring->length, str, len);
86 qstring->length += len;
87 qstring->string[qstring->length] = 0;
88}
89
764c1cae
LC
90void qstring_append_int(QString *qstring, int64_t value)
91{
92 char num[32];
93
94 snprintf(num, sizeof(num), "%" PRId64, value);
95 qstring_append(qstring, num);
96}
97
6fe9565c
LC
98/**
99 * qstring_append_chr(): Append a C char to a QString
100 */
101void qstring_append_chr(QString *qstring, int c)
102{
103 capacity_increase(qstring, 1);
104 qstring->string[qstring->length++] = c;
105 qstring->string[qstring->length] = 0;
106}
107
66f70487
LC
108/**
109 * qstring_get_str(): Return a pointer to the stored string
110 *
111 * NOTE: Should be used with caution, if the object is deallocated
112 * this pointer becomes invalid.
113 */
114const char *qstring_get_str(const QString *qstring)
115{
116 return qstring->string;
117}
118
77593202
PX
119/**
120 * qstring_get_try_str(): Return a pointer to the stored string
121 *
122 * NOTE: will return NULL if qstring is not provided.
123 */
124const char *qstring_get_try_str(const QString *qstring)
125{
126 return qstring ? qstring_get_str(qstring) : NULL;
127}
128
b26ae1cb
PX
129/**
130 * qobject_get_try_str(): Return a pointer to the corresponding string
131 *
132 * NOTE: the string will only be returned if the object is valid, and
133 * its type is QString, otherwise NULL is returned.
134 */
135const char *qobject_get_try_str(const QObject *qstring)
136{
137 return qstring_get_try_str(qobject_to(QString, qstring));
138}
139
b38dd678
HR
140/**
141 * qstring_is_equal(): Test whether the two QStrings are equal
142 */
143bool qstring_is_equal(const QObject *x, const QObject *y)
144{
7dc847eb
HR
145 return !strcmp(qobject_to(QString, x)->string,
146 qobject_to(QString, y)->string);
b38dd678
HR
147}
148
66f70487
LC
149/**
150 * qstring_destroy_obj(): Free all memory allocated by a QString
151 * object
152 */
55e1819c 153void qstring_destroy_obj(QObject *obj)
66f70487
LC
154{
155 QString *qs;
156
157 assert(obj != NULL);
7dc847eb 158 qs = qobject_to(QString, obj);
7267c094
AL
159 g_free(qs->string);
160 g_free(qs);
66f70487 161}