SPARC64: treat UA2007 ASI_BLK_* as translating ASIs.
[qemu/mdroth.git] / qstring.c
blob4e2ba083b71269e53c9e805718817a57031b2598
1 /*
2 * QString Module
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 LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qobject.h"
14 #include "qstring.h"
15 #include "qemu-common.h"
17 static void qstring_destroy_obj(QObject *obj);
19 static const QType qstring_type = {
20 .code = QTYPE_QSTRING,
21 .destroy = qstring_destroy_obj,
24 /**
25 * qstring_new(): Create a new empty QString
27 * Return strong reference.
29 QString *qstring_new(void)
31 return qstring_from_str("");
34 /**
35 * qstring_from_substr(): Create a new QString from a C string substring
37 * Return string reference
39 QString *qstring_from_substr(const char *str, int start, int end)
41 QString *qstring;
43 qstring = qemu_malloc(sizeof(*qstring));
45 qstring->length = end - start + 1;
46 qstring->capacity = qstring->length;
48 qstring->string = qemu_malloc(qstring->capacity + 1);
49 memcpy(qstring->string, str + start, qstring->length);
50 qstring->string[qstring->length] = 0;
52 QOBJECT_INIT(qstring, &qstring_type);
54 return qstring;
57 /**
58 * qstring_from_str(): Create a new QString from a regular C string
60 * Return strong reference.
62 QString *qstring_from_str(const char *str)
64 return qstring_from_substr(str, 0, strlen(str) - 1);
67 static void capacity_increase(QString *qstring, size_t len)
69 if (qstring->capacity < (qstring->length + len)) {
70 qstring->capacity += len;
71 qstring->capacity *= 2; /* use exponential growth */
73 qstring->string = qemu_realloc(qstring->string, qstring->capacity + 1);
77 /* qstring_append(): Append a C string to a QString
79 void qstring_append(QString *qstring, const char *str)
81 size_t len = strlen(str);
83 capacity_increase(qstring, len);
84 memcpy(qstring->string + qstring->length, str, len);
85 qstring->length += len;
86 qstring->string[qstring->length] = 0;
89 void qstring_append_int(QString *qstring, int64_t value)
91 char num[32];
93 snprintf(num, sizeof(num), "%" PRId64, value);
94 qstring_append(qstring, num);
97 /**
98 * qstring_append_chr(): Append a C char to a QString
100 void qstring_append_chr(QString *qstring, int c)
102 capacity_increase(qstring, 1);
103 qstring->string[qstring->length++] = c;
104 qstring->string[qstring->length] = 0;
108 * qobject_to_qstring(): Convert a QObject to a QString
110 QString *qobject_to_qstring(const QObject *obj)
112 if (qobject_type(obj) != QTYPE_QSTRING)
113 return NULL;
115 return container_of(obj, QString, base);
119 * qstring_get_str(): Return a pointer to the stored string
121 * NOTE: Should be used with caution, if the object is deallocated
122 * this pointer becomes invalid.
124 const char *qstring_get_str(const QString *qstring)
126 return qstring->string;
130 * qstring_destroy_obj(): Free all memory allocated by a QString
131 * object
133 static void qstring_destroy_obj(QObject *obj)
135 QString *qs;
137 assert(obj != NULL);
138 qs = qobject_to_qstring(obj);
139 qemu_free(qs->string);
140 qemu_free(qs);