1a372823aSPaolo Bonzini /* 2a372823aSPaolo Bonzini * QString Module 3a372823aSPaolo Bonzini * 4a372823aSPaolo Bonzini * Copyright (C) 2009 Red Hat Inc. 5a372823aSPaolo Bonzini * 6a372823aSPaolo Bonzini * Authors: 7a372823aSPaolo Bonzini * Luiz Capitulino <lcapitulino@redhat.com> 8a372823aSPaolo Bonzini * 9a372823aSPaolo Bonzini * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 10a372823aSPaolo Bonzini * See the COPYING.LIB file in the top-level directory. 11a372823aSPaolo Bonzini */ 12a372823aSPaolo Bonzini 13*f2ad72b3SPeter Maydell #include "qemu/osdep.h" 14a372823aSPaolo Bonzini #include "qapi/qmp/qobject.h" 15a372823aSPaolo Bonzini #include "qapi/qmp/qstring.h" 16a372823aSPaolo Bonzini #include "qemu-common.h" 17a372823aSPaolo Bonzini 18a372823aSPaolo Bonzini /** 19a372823aSPaolo Bonzini * qstring_new(): Create a new empty QString 20a372823aSPaolo Bonzini * 21a372823aSPaolo Bonzini * Return strong reference. 22a372823aSPaolo Bonzini */ 23a372823aSPaolo Bonzini QString *qstring_new(void) 24a372823aSPaolo Bonzini { 25a372823aSPaolo Bonzini return qstring_from_str(""); 26a372823aSPaolo Bonzini } 27a372823aSPaolo Bonzini 28a372823aSPaolo Bonzini /** 2954d49ac9SLuiz Capitulino * qstring_get_length(): Get the length of a QString 3054d49ac9SLuiz Capitulino */ 3154d49ac9SLuiz Capitulino size_t qstring_get_length(const QString *qstring) 3254d49ac9SLuiz Capitulino { 3354d49ac9SLuiz Capitulino return qstring->length; 3454d49ac9SLuiz Capitulino } 3554d49ac9SLuiz Capitulino 3654d49ac9SLuiz Capitulino /** 37a372823aSPaolo Bonzini * qstring_from_substr(): Create a new QString from a C string substring 38a372823aSPaolo Bonzini * 39a372823aSPaolo Bonzini * Return string reference 40a372823aSPaolo Bonzini */ 41a372823aSPaolo Bonzini QString *qstring_from_substr(const char *str, int start, int end) 42a372823aSPaolo Bonzini { 43a372823aSPaolo Bonzini QString *qstring; 44a372823aSPaolo Bonzini 45a372823aSPaolo Bonzini qstring = g_malloc(sizeof(*qstring)); 4655e1819cSEric Blake qobject_init(QOBJECT(qstring), QTYPE_QSTRING); 47a372823aSPaolo Bonzini 48a372823aSPaolo Bonzini qstring->length = end - start + 1; 49a372823aSPaolo Bonzini qstring->capacity = qstring->length; 50a372823aSPaolo Bonzini 51a372823aSPaolo Bonzini qstring->string = g_malloc(qstring->capacity + 1); 52a372823aSPaolo Bonzini memcpy(qstring->string, str + start, qstring->length); 53a372823aSPaolo Bonzini qstring->string[qstring->length] = 0; 54a372823aSPaolo Bonzini 55a372823aSPaolo Bonzini 56a372823aSPaolo Bonzini return qstring; 57a372823aSPaolo Bonzini } 58a372823aSPaolo Bonzini 59a372823aSPaolo Bonzini /** 60a372823aSPaolo Bonzini * qstring_from_str(): Create a new QString from a regular C string 61a372823aSPaolo Bonzini * 62a372823aSPaolo Bonzini * Return strong reference. 63a372823aSPaolo Bonzini */ 64a372823aSPaolo Bonzini QString *qstring_from_str(const char *str) 65a372823aSPaolo Bonzini { 66a372823aSPaolo Bonzini return qstring_from_substr(str, 0, strlen(str) - 1); 67a372823aSPaolo Bonzini } 68a372823aSPaolo Bonzini 69a372823aSPaolo Bonzini static void capacity_increase(QString *qstring, size_t len) 70a372823aSPaolo Bonzini { 71a372823aSPaolo Bonzini if (qstring->capacity < (qstring->length + len)) { 72a372823aSPaolo Bonzini qstring->capacity += len; 73a372823aSPaolo Bonzini qstring->capacity *= 2; /* use exponential growth */ 74a372823aSPaolo Bonzini 75a372823aSPaolo Bonzini qstring->string = g_realloc(qstring->string, qstring->capacity + 1); 76a372823aSPaolo Bonzini } 77a372823aSPaolo Bonzini } 78a372823aSPaolo Bonzini 79a372823aSPaolo Bonzini /* qstring_append(): Append a C string to a QString 80a372823aSPaolo Bonzini */ 81a372823aSPaolo Bonzini void qstring_append(QString *qstring, const char *str) 82a372823aSPaolo Bonzini { 83a372823aSPaolo Bonzini size_t len = strlen(str); 84a372823aSPaolo Bonzini 85a372823aSPaolo Bonzini capacity_increase(qstring, len); 86a372823aSPaolo Bonzini memcpy(qstring->string + qstring->length, str, len); 87a372823aSPaolo Bonzini qstring->length += len; 88a372823aSPaolo Bonzini qstring->string[qstring->length] = 0; 89a372823aSPaolo Bonzini } 90a372823aSPaolo Bonzini 91a372823aSPaolo Bonzini void qstring_append_int(QString *qstring, int64_t value) 92a372823aSPaolo Bonzini { 93a372823aSPaolo Bonzini char num[32]; 94a372823aSPaolo Bonzini 95a372823aSPaolo Bonzini snprintf(num, sizeof(num), "%" PRId64, value); 96a372823aSPaolo Bonzini qstring_append(qstring, num); 97a372823aSPaolo Bonzini } 98a372823aSPaolo Bonzini 99a372823aSPaolo Bonzini /** 100a372823aSPaolo Bonzini * qstring_append_chr(): Append a C char to a QString 101a372823aSPaolo Bonzini */ 102a372823aSPaolo Bonzini void qstring_append_chr(QString *qstring, int c) 103a372823aSPaolo Bonzini { 104a372823aSPaolo Bonzini capacity_increase(qstring, 1); 105a372823aSPaolo Bonzini qstring->string[qstring->length++] = c; 106a372823aSPaolo Bonzini qstring->string[qstring->length] = 0; 107a372823aSPaolo Bonzini } 108a372823aSPaolo Bonzini 109a372823aSPaolo Bonzini /** 110a372823aSPaolo Bonzini * qobject_to_qstring(): Convert a QObject to a QString 111a372823aSPaolo Bonzini */ 112a372823aSPaolo Bonzini QString *qobject_to_qstring(const QObject *obj) 113a372823aSPaolo Bonzini { 1147f027843SMarkus Armbruster if (!obj || qobject_type(obj) != QTYPE_QSTRING) { 115a372823aSPaolo Bonzini return NULL; 1167f027843SMarkus Armbruster } 117a372823aSPaolo Bonzini return container_of(obj, QString, base); 118a372823aSPaolo Bonzini } 119a372823aSPaolo Bonzini 120a372823aSPaolo Bonzini /** 121a372823aSPaolo Bonzini * qstring_get_str(): Return a pointer to the stored string 122a372823aSPaolo Bonzini * 123a372823aSPaolo Bonzini * NOTE: Should be used with caution, if the object is deallocated 124a372823aSPaolo Bonzini * this pointer becomes invalid. 125a372823aSPaolo Bonzini */ 126a372823aSPaolo Bonzini const char *qstring_get_str(const QString *qstring) 127a372823aSPaolo Bonzini { 128a372823aSPaolo Bonzini return qstring->string; 129a372823aSPaolo Bonzini } 130a372823aSPaolo Bonzini 131a372823aSPaolo Bonzini /** 132a372823aSPaolo Bonzini * qstring_destroy_obj(): Free all memory allocated by a QString 133a372823aSPaolo Bonzini * object 134a372823aSPaolo Bonzini */ 13555e1819cSEric Blake void qstring_destroy_obj(QObject *obj) 136a372823aSPaolo Bonzini { 137a372823aSPaolo Bonzini QString *qs; 138a372823aSPaolo Bonzini 139a372823aSPaolo Bonzini assert(obj != NULL); 140a372823aSPaolo Bonzini qs = qobject_to_qstring(obj); 141a372823aSPaolo Bonzini g_free(qs->string); 142a372823aSPaolo Bonzini g_free(qs); 143a372823aSPaolo Bonzini } 144