PPC: Use macio IDE controller for Newworld
[qemu/agraf.git] / qstring.c
blob740a1067d2d7567f73d3e36da37b6aa9d4134f27
1 /*
2 * QString data type.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 #include "qobject.h"
13 #include "qstring.h"
14 #include "qemu-common.h"
16 static void qstring_destroy_obj(QObject *obj);
18 static const QType qstring_type = {
19 .code = QTYPE_QSTRING,
20 .destroy = qstring_destroy_obj,
23 /**
24 * qstring_new(): Create a new empty QString
26 * Return strong reference.
28 QString *qstring_new(void)
30 return qstring_from_str("");
33 /**
34 * qstring_from_substr(): Create a new QString from a C string substring
36 * Return string reference
38 QString *qstring_from_substr(const char *str, int start, int end)
40 QString *qstring;
42 qstring = qemu_malloc(sizeof(*qstring));
44 qstring->length = end - start + 1;
45 qstring->capacity = qstring->length;
47 qstring->string = qemu_malloc(qstring->capacity + 1);
48 memcpy(qstring->string, str + start, qstring->length);
49 qstring->string[qstring->length] = 0;
51 QOBJECT_INIT(qstring, &qstring_type);
53 return qstring;
56 /**
57 * qstring_from_str(): Create a new QString from a regular C string
59 * Return strong reference.
61 QString *qstring_from_str(const char *str)
63 return qstring_from_substr(str, 0, strlen(str) - 1);
66 static void capacity_increase(QString *qstring, size_t len)
68 if (qstring->capacity < (qstring->length + len)) {
69 qstring->capacity += len;
70 qstring->capacity *= 2; /* use exponential growth */
72 qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
76 /* qstring_append(): Append a C string to a QString
78 void qstring_append(QString *qstring, const char *str)
80 size_t len = strlen(str);
82 capacity_increase(qstring, len);
83 memcpy(qstring->string + qstring->length, str, len);
84 qstring->length += len;
85 qstring->string[qstring->length] = 0;
88 void qstring_append_int(QString *qstring, int64_t value)
90 char num[32];
92 snprintf(num, sizeof(num), "%" PRId64, value);
93 qstring_append(qstring, num);
96 /**
97 * qstring_append_chr(): Append a C char to a QString
99 void qstring_append_chr(QString *qstring, int c)
101 capacity_increase(qstring, 1);
102 qstring->string[qstring->length++] = c;
103 qstring->string[qstring->length] = 0;
107 * qobject_to_qstring(): Convert a QObject to a QString
109 QString *qobject_to_qstring(const QObject *obj)
111 if (qobject_type(obj) != QTYPE_QSTRING)
112 return NULL;
114 return container_of(obj, QString, base);
118 * qstring_get_str(): Return a pointer to the stored string
120 * NOTE: Should be used with caution, if the object is deallocated
121 * this pointer becomes invalid.
123 const char *qstring_get_str(const QString *qstring)
125 return qstring->string;
129 * qstring_destroy_obj(): Free all memory allocated by a QString
130 * object
132 static void qstring_destroy_obj(QObject *obj)
134 QString *qs;
136 assert(obj != NULL);
137 qs = qobject_to_qstring(obj);
138 qemu_free(qs->string);
139 qemu_free(qs);