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