stats: Move QMP commands from monitor/ to stats/
[qemu/armbru.git] / qobject / qstring.c
blob794f8c93578a831a5feecfc9a0d8dfa50335ff48
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/qstring.h"
15 #include "qobject-internal.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_from_substr(): Create a new QString from a C string substring
30 * Return string reference
32 QString *qstring_from_substr(const char *str, size_t start, size_t end)
34 QString *qstring;
36 assert(start <= end);
37 qstring = g_malloc(sizeof(*qstring));
38 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
39 qstring->string = g_strndup(str + start, end - start);
40 return qstring;
43 /**
44 * qstring_from_str(): Create a new QString from a regular C string
46 * Return strong reference.
48 QString *qstring_from_str(const char *str)
50 return qstring_from_substr(str, 0, strlen(str));
53 /**
54 * qstring_from_gstring(): Convert a GString to a QString
56 * Return strong reference.
59 QString *qstring_from_gstring(GString *gstr)
61 QString *qstring;
63 qstring = g_malloc(sizeof(*qstring));
64 qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
65 qstring->string = g_string_free(gstr, false);
66 return qstring;
70 /**
71 * qstring_get_str(): Return a pointer to the stored string
73 * NOTE: Should be used with caution, if the object is deallocated
74 * this pointer becomes invalid.
76 const char *qstring_get_str(const QString *qstring)
78 return qstring->string;
81 /**
82 * qstring_is_equal(): Test whether the two QStrings are equal
84 bool qstring_is_equal(const QObject *x, const QObject *y)
86 return !strcmp(qobject_to(QString, x)->string,
87 qobject_to(QString, y)->string);
90 /**
91 * qstring_destroy_obj(): Free all memory allocated by a QString
92 * object
94 void qstring_destroy_obj(QObject *obj)
96 QString *qs;
98 assert(obj != NULL);
99 qs = qobject_to(QString, obj);
100 g_free((char *)qs->string);
101 g_free(qs);
104 void qstring_unref(QString *q)
106 qobject_unref(q);