Revision 66f70487
b/Makefile | ||
---|---|---|
91 | 91 |
obj-y += qemu-char.o aio.o net-checksum.o savevm.o |
92 | 92 |
obj-y += msmouse.o ps2.o |
93 | 93 |
obj-y += qdev.o qdev-properties.o ssi.o |
94 |
obj-y += qint.o |
|
94 |
obj-y += qint.o qstring.o
|
|
95 | 95 |
|
96 | 96 |
obj-$(CONFIG_BRLAPI) += baum.o |
97 | 97 |
obj-$(CONFIG_WIN32) += tap-win32.o |
b/qobject.h | ||
---|---|---|
38 | 38 |
typedef enum { |
39 | 39 |
QTYPE_NONE, |
40 | 40 |
QTYPE_QINT, |
41 |
QTYPE_QSTRING, |
|
41 | 42 |
} qtype_code; |
42 | 43 |
|
43 | 44 |
struct QObject; |
b/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 const QType qstring_type; |
|
17 |
|
|
18 |
/** |
|
19 |
* qstring_from_str(): Create a new QString from a regular C string |
|
20 |
* |
|
21 |
* Return strong reference. |
|
22 |
*/ |
|
23 |
QString *qstring_from_str(const char *str) |
|
24 |
{ |
|
25 |
QString *qstring; |
|
26 |
|
|
27 |
qstring = qemu_malloc(sizeof(*qstring)); |
|
28 |
qstring->string = qemu_strdup(str); |
|
29 |
QOBJECT_INIT(qstring, &qstring_type); |
|
30 |
|
|
31 |
return qstring; |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* qobject_to_qstring(): Convert a QObject to a QString |
|
36 |
*/ |
|
37 |
QString *qobject_to_qstring(const QObject *obj) |
|
38 |
{ |
|
39 |
if (qobject_type(obj) != QTYPE_QSTRING) |
|
40 |
return NULL; |
|
41 |
|
|
42 |
return container_of(obj, QString, base); |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* qstring_get_str(): Return a pointer to the stored string |
|
47 |
* |
|
48 |
* NOTE: Should be used with caution, if the object is deallocated |
|
49 |
* this pointer becomes invalid. |
|
50 |
*/ |
|
51 |
const char *qstring_get_str(const QString *qstring) |
|
52 |
{ |
|
53 |
return qstring->string; |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* qstring_destroy_obj(): Free all memory allocated by a QString |
|
58 |
* object |
|
59 |
*/ |
|
60 |
static void qstring_destroy_obj(QObject *obj) |
|
61 |
{ |
|
62 |
QString *qs; |
|
63 |
|
|
64 |
assert(obj != NULL); |
|
65 |
qs = qobject_to_qstring(obj); |
|
66 |
qemu_free(qs->string); |
|
67 |
qemu_free(qs); |
|
68 |
} |
|
69 |
|
|
70 |
static const QType qstring_type = { |
|
71 |
.code = QTYPE_QSTRING, |
|
72 |
.destroy = qstring_destroy_obj, |
|
73 |
}; |
b/qstring.h | ||
---|---|---|
1 |
#ifndef QSTRING_H |
|
2 |
#define QSTRING_H |
|
3 |
|
|
4 |
#include "qobject.h" |
|
5 |
|
|
6 |
typedef struct QString { |
|
7 |
QObject_HEAD; |
|
8 |
char *string; |
|
9 |
} QString; |
|
10 |
|
|
11 |
QString *qstring_from_str(const char *str); |
|
12 |
const char *qstring_get_str(const QString *qstring); |
|
13 |
QString *qobject_to_qstring(const QObject *obj); |
|
14 |
|
|
15 |
#endif /* QSTRING_H */ |
Also available in: Unified diff