4 * Copyright (C) 2009 Red Hat Inc.
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"
18 * qstring_new(): Create a new empty QString
20 * Return strong reference.
22 QString
*qstring_new(void)
24 return qstring_from_str("");
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
)
37 qstring
= g_malloc(sizeof(*qstring
));
38 qobject_init(QOBJECT(qstring
), QTYPE_QSTRING
);
39 qstring
->string
= g_strndup(str
+ start
, end
- start
);
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
));
54 * qstring_from_gstring(): Convert a GString to a QString
56 * Return strong reference.
59 QString
*qstring_from_gstring(GString
*gstr
)
63 qstring
= g_malloc(sizeof(*qstring
));
64 qobject_init(QOBJECT(qstring
), QTYPE_QSTRING
);
65 qstring
->string
= g_string_free(gstr
, false);
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
;
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
);
91 * qstring_destroy_obj(): Free all memory allocated by a QString
94 void qstring_destroy_obj(QObject
*obj
)
99 qs
= qobject_to(QString
, obj
);
100 g_free((char *)qs
->string
);
104 void qstring_unref(QString
*q
)