vmxnet3: Change offsets of msi/msix pci capabilities
[qemu.git] / qobject / qstring.c
blobf44c5c424d1ed9cbb0b343b0647a74ea991f24f0
1 /*
2 * QString Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "qapi/qmp/qobject.h"
14 #include "qapi/qmp/qstring.h"
15 #include "qemu-common.h"
17 /**
18 * qstring_new(): Create a new empty QString
20 * Return strong reference.
22 QString *qstring_new(void)
24 return qstring_from_str("");
27 /**
28 * qstring_get_length(): Get the length of a QString
30 size_t qstring_get_length(const QString *qstring)
32 return qstring->length;
35 /**
36 * qstring_from_substr(): Create a new QString from a C string substring
38 * Return string reference
40 QString *qstring_from_substr(const char *str, int start, int end)
42 QString *qstring;
44 qstring = g_malloc(sizeof(*qstring));
45 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
47 qstring->length = end - start + 1;
48 qstring->capacity = qstring->length;
50 qstring->string = g_malloc(qstring->capacity + 1);
51 memcpy(qstring->string, str + start, qstring->length);
52 qstring->string[qstring->length] = 0;
55 return qstring;
58 /**
59 * qstring_from_str(): Create a new QString from a regular C string
61 * Return strong reference.
63 QString *qstring_from_str(const char *str)
65 return qstring_from_substr(str, 0, strlen(str) - 1);
68 static void capacity_increase(QString *qstring, size_t len)
70 if (qstring->capacity < (qstring->length + len)) {
71 qstring->capacity += len;
72 qstring->capacity *= 2; /* use exponential growth */
74 qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
78 /* qstring_append(): Append a C string to a QString
80 void qstring_append(QString *qstring, const char *str)
82 size_t len = strlen(str);
84 capacity_increase(qstring, len);
85 memcpy(qstring->string + qstring->length, str, len);
86 qstring->length += len;
87 qstring->string[qstring->length] = 0;
90 void qstring_append_int(QString *qstring, int64_t value)
92 char num[32];
94 snprintf(num, sizeof(num), "%" PRId64, value);
95 qstring_append(qstring, num);
98 /**
99 * qstring_append_chr(): Append a C char to a QString
101 void qstring_append_chr(QString *qstring, int c)
103 capacity_increase(qstring, 1);
104 qstring->string[qstring->length++] = c;
105 qstring->string[qstring->length] = 0;
109 * qobject_to_qstring(): Convert a QObject to a QString
111 QString *qobject_to_qstring(const QObject *obj)
113 if (!obj || qobject_type(obj) != QTYPE_QSTRING) {
114 return NULL;
116 return container_of(obj, QString, base);
120 * qstring_get_str(): Return a pointer to the stored string
122 * NOTE: Should be used with caution, if the object is deallocated
123 * this pointer becomes invalid.
125 const char *qstring_get_str(const QString *qstring)
127 return qstring->string;
131 * qstring_destroy_obj(): Free all memory allocated by a QString
132 * object
134 void qstring_destroy_obj(QObject *obj)
136 QString *qs;
138 assert(obj != NULL);
139 qs = qobject_to_qstring(obj);
140 g_free(qs->string);
141 g_free(qs);