iothread: add iothread_by_id() API
[qemu/ar7.git] / qobject / qstring.c
blob74182a1c029a9fa8747366ec069fc76497392df2
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 "qemu/osdep.h"
14 #include "qapi/qmp/qobject.h"
15 #include "qapi/qmp/qstring.h"
16 #include "qemu-common.h"
18 /**
19 * qstring_new(): Create a new empty QString
21 * Return strong reference.
23 QString *qstring_new(void)
25 return qstring_from_str("");
28 /**
29 * qstring_get_length(): Get the length of a QString
31 size_t qstring_get_length(const QString *qstring)
33 return qstring->length;
36 /**
37 * qstring_from_substr(): Create a new QString from a C string substring
39 * Return string reference
41 QString *qstring_from_substr(const char *str, int start, int end)
43 QString *qstring;
45 qstring = g_malloc(sizeof(*qstring));
46 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
48 qstring->length = end - start + 1;
49 qstring->capacity = qstring->length;
51 qstring->string = g_malloc(qstring->capacity + 1);
52 memcpy(qstring->string, str + start, qstring->length);
53 qstring->string[qstring->length] = 0;
56 return qstring;
59 /**
60 * qstring_from_str(): Create a new QString from a regular C string
62 * Return strong reference.
64 QString *qstring_from_str(const char *str)
66 return qstring_from_substr(str, 0, strlen(str) - 1);
69 static void capacity_increase(QString *qstring, size_t len)
71 if (qstring->capacity < (qstring->length + len)) {
72 qstring->capacity += len;
73 qstring->capacity *= 2; /* use exponential growth */
75 qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
79 /* qstring_append(): Append a C string to a QString
81 void qstring_append(QString *qstring, const char *str)
83 size_t len = strlen(str);
85 capacity_increase(qstring, len);
86 memcpy(qstring->string + qstring->length, str, len);
87 qstring->length += len;
88 qstring->string[qstring->length] = 0;
91 void qstring_append_int(QString *qstring, int64_t value)
93 char num[32];
95 snprintf(num, sizeof(num), "%" PRId64, value);
96 qstring_append(qstring, num);
99 /**
100 * qstring_append_chr(): Append a C char to a QString
102 void qstring_append_chr(QString *qstring, int c)
104 capacity_increase(qstring, 1);
105 qstring->string[qstring->length++] = c;
106 qstring->string[qstring->length] = 0;
110 * qobject_to_qstring(): Convert a QObject to a QString
112 QString *qobject_to_qstring(const QObject *obj)
114 if (!obj || qobject_type(obj) != QTYPE_QSTRING) {
115 return NULL;
117 return container_of(obj, QString, base);
121 * qstring_get_str(): Return a pointer to the stored string
123 * NOTE: Should be used with caution, if the object is deallocated
124 * this pointer becomes invalid.
126 const char *qstring_get_str(const QString *qstring)
128 return qstring->string;
132 * qstring_is_equal(): Test whether the two QStrings are equal
134 bool qstring_is_equal(const QObject *x, const QObject *y)
136 return !strcmp(qobject_to_qstring(x)->string,
137 qobject_to_qstring(y)->string);
141 * qstring_destroy_obj(): Free all memory allocated by a QString
142 * object
144 void qstring_destroy_obj(QObject *obj)
146 QString *qs;
148 assert(obj != NULL);
149 qs = qobject_to_qstring(obj);
150 g_free(qs->string);
151 g_free(qs);