]> git.proxmox.com Git - qemu.git/blob - qstring.c
Shuffle lines to avoid gcc 3 warning about redundant redeclaration
[qemu.git] / qstring.c
1 /*
2 * QString data type.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 */
12 #include "qobject.h"
13 #include "qstring.h"
14 #include "qemu-common.h"
15
16 static void qstring_destroy_obj(QObject *obj);
17
18 static const QType qstring_type = {
19 .code = QTYPE_QSTRING,
20 .destroy = qstring_destroy_obj,
21 };
22
23 /**
24 * qstring_from_str(): Create a new QString from a regular C string
25 *
26 * Return strong reference.
27 */
28 QString *qstring_from_str(const char *str)
29 {
30 QString *qstring;
31
32 qstring = qemu_malloc(sizeof(*qstring));
33 qstring->string = qemu_strdup(str);
34 QOBJECT_INIT(qstring, &qstring_type);
35
36 return qstring;
37 }
38
39 /**
40 * qobject_to_qstring(): Convert a QObject to a QString
41 */
42 QString *qobject_to_qstring(const QObject *obj)
43 {
44 if (qobject_type(obj) != QTYPE_QSTRING)
45 return NULL;
46
47 return container_of(obj, QString, base);
48 }
49
50 /**
51 * qstring_get_str(): Return a pointer to the stored string
52 *
53 * NOTE: Should be used with caution, if the object is deallocated
54 * this pointer becomes invalid.
55 */
56 const char *qstring_get_str(const QString *qstring)
57 {
58 return qstring->string;
59 }
60
61 /**
62 * qstring_destroy_obj(): Free all memory allocated by a QString
63 * object
64 */
65 static void qstring_destroy_obj(QObject *obj)
66 {
67 QString *qs;
68
69 assert(obj != NULL);
70 qs = qobject_to_qstring(obj);
71 qemu_free(qs->string);
72 qemu_free(qs);
73 }