]>
Commit | Line | Data |
---|---|---|
66f70487 | 1 | /* |
41836a9f | 2 | * QString Module |
66f70487 LC |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * | |
41836a9f LC |
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. | |
66f70487 | 11 | */ |
41836a9f | 12 | |
f2ad72b3 | 13 | #include "qemu/osdep.h" |
7b1b5d19 | 14 | #include "qapi/qmp/qstring.h" |
80d71121 | 15 | #include "qobject-internal.h" |
66f70487 | 16 | |
d30ec846 AL |
17 | /** |
18 | * qstring_new(): Create a new empty QString | |
19 | * | |
20 | * Return strong reference. | |
21 | */ | |
22 | QString *qstring_new(void) | |
23 | { | |
24 | return qstring_from_str(""); | |
25 | } | |
26 | ||
66f70487 | 27 | /** |
4b5c5766 | 28 | * qstring_from_substr(): Create a new QString from a C string substring |
66f70487 | 29 | * |
4b5c5766 | 30 | * Return string reference |
66f70487 | 31 | */ |
ad63c549 | 32 | QString *qstring_from_substr(const char *str, size_t start, size_t end) |
66f70487 LC |
33 | { |
34 | QString *qstring; | |
35 | ||
ba891d68 | 36 | assert(start <= end); |
7267c094 | 37 | qstring = g_malloc(sizeof(*qstring)); |
55e1819c | 38 | qobject_init(QOBJECT(qstring), QTYPE_QSTRING); |
4ac76ba4 | 39 | qstring->string = g_strndup(str + start, end - start); |
66f70487 LC |
40 | return qstring; |
41 | } | |
42 | ||
4b5c5766 LC |
43 | /** |
44 | * qstring_from_str(): Create a new QString from a regular C string | |
45 | * | |
46 | * Return strong reference. | |
47 | */ | |
48 | QString *qstring_from_str(const char *str) | |
49 | { | |
ba891d68 | 50 | return qstring_from_substr(str, 0, strlen(str)); |
4b5c5766 LC |
51 | } |
52 | ||
f1cc129d MA |
53 | /** |
54 | * qstring_from_gstring(): Convert a GString to a QString | |
55 | * | |
56 | * Return strong reference. | |
57 | */ | |
58 | ||
59 | QString *qstring_from_gstring(GString *gstr) | |
60 | { | |
61 | QString *qstring; | |
62 | ||
63 | qstring = g_malloc(sizeof(*qstring)); | |
64 | qobject_init(QOBJECT(qstring), QTYPE_QSTRING); | |
f1cc129d MA |
65 | qstring->string = g_string_free(gstr, false); |
66 | return qstring; | |
67 | } | |
68 | ||
69 | ||
66f70487 LC |
70 | /** |
71 | * qstring_get_str(): Return a pointer to the stored string | |
72 | * | |
73 | * NOTE: Should be used with caution, if the object is deallocated | |
74 | * this pointer becomes invalid. | |
75 | */ | |
76 | const char *qstring_get_str(const QString *qstring) | |
77 | { | |
78 | return qstring->string; | |
79 | } | |
80 | ||
b38dd678 HR |
81 | /** |
82 | * qstring_is_equal(): Test whether the two QStrings are equal | |
83 | */ | |
84 | bool qstring_is_equal(const QObject *x, const QObject *y) | |
85 | { | |
7dc847eb HR |
86 | return !strcmp(qobject_to(QString, x)->string, |
87 | qobject_to(QString, y)->string); | |
b38dd678 HR |
88 | } |
89 | ||
66f70487 LC |
90 | /** |
91 | * qstring_destroy_obj(): Free all memory allocated by a QString | |
92 | * object | |
93 | */ | |
55e1819c | 94 | void qstring_destroy_obj(QObject *obj) |
66f70487 | 95 | { |
88e25b1e MA |
96 | QString *qs; |
97 | ||
66f70487 | 98 | assert(obj != NULL); |
88e25b1e | 99 | qs = qobject_to(QString, obj); |
4ac76ba4 | 100 | g_free((char *)qs->string); |
88e25b1e | 101 | g_free(qs); |
66f70487 | 102 | } |
d709bbf3 MAL |
103 | |
104 | void qstring_unref(QString *q) | |
105 | { | |
106 | qobject_unref(q); | |
107 | } |