1 /* 2 * QString Module 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 LGPL, version 2.1 or later. 10 * See the COPYING.LIB file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qapi/qmp/qobject.h" 15 #include "qapi/qmp/qstring.h" 16 #include "qemu-common.h" 17 18 /** 19 * qstring_new(): Create a new empty QString 20 * 21 * Return strong reference. 22 */ 23 QString *qstring_new(void) 24 { 25 return qstring_from_str(""); 26 } 27 28 /** 29 * qstring_get_length(): Get the length of a QString 30 */ 31 size_t qstring_get_length(const QString *qstring) 32 { 33 return qstring->length; 34 } 35 36 /** 37 * qstring_from_substr(): Create a new QString from a C string substring 38 * 39 * Return string reference 40 */ 41 QString *qstring_from_substr(const char *str, int start, int end) 42 { 43 QString *qstring; 44 45 qstring = g_malloc(sizeof(*qstring)); 46 qobject_init(QOBJECT(qstring), QTYPE_QSTRING); 47 48 qstring->length = end - start + 1; 49 qstring->capacity = qstring->length; 50 51 qstring->string = g_malloc(qstring->capacity + 1); 52 memcpy(qstring->string, str + start, qstring->length); 53 qstring->string[qstring->length] = 0; 54 55 56 return qstring; 57 } 58 59 /** 60 * qstring_from_str(): Create a new QString from a regular C string 61 * 62 * Return strong reference. 63 */ 64 QString *qstring_from_str(const char *str) 65 { 66 return qstring_from_substr(str, 0, strlen(str) - 1); 67 } 68 69 static void capacity_increase(QString *qstring, size_t len) 70 { 71 if (qstring->capacity < (qstring->length + len)) { 72 qstring->capacity += len; 73 qstring->capacity *= 2; /* use exponential growth */ 74 75 qstring->string = g_realloc(qstring->string, qstring->capacity + 1); 76 } 77 } 78 79 /* qstring_append(): Append a C string to a QString 80 */ 81 void qstring_append(QString *qstring, const char *str) 82 { 83 size_t len = strlen(str); 84 85 capacity_increase(qstring, len); 86 memcpy(qstring->string + qstring->length, str, len); 87 qstring->length += len; 88 qstring->string[qstring->length] = 0; 89 } 90 91 void qstring_append_int(QString *qstring, int64_t value) 92 { 93 char num[32]; 94 95 snprintf(num, sizeof(num), "%" PRId64, value); 96 qstring_append(qstring, num); 97 } 98 99 /** 100 * qstring_append_chr(): Append a C char to a QString 101 */ 102 void qstring_append_chr(QString *qstring, int c) 103 { 104 capacity_increase(qstring, 1); 105 qstring->string[qstring->length++] = c; 106 qstring->string[qstring->length] = 0; 107 } 108 109 /** 110 * qobject_to_qstring(): Convert a QObject to a QString 111 */ 112 QString *qobject_to_qstring(const QObject *obj) 113 { 114 if (!obj || qobject_type(obj) != QTYPE_QSTRING) { 115 return NULL; 116 } 117 return container_of(obj, QString, base); 118 } 119 120 /** 121 * qstring_get_str(): Return a pointer to the stored string 122 * 123 * NOTE: Should be used with caution, if the object is deallocated 124 * this pointer becomes invalid. 125 */ 126 const char *qstring_get_str(const QString *qstring) 127 { 128 return qstring->string; 129 } 130 131 /** 132 * qstring_is_equal(): Test whether the two QStrings are equal 133 */ 134 bool qstring_is_equal(const QObject *x, const QObject *y) 135 { 136 return !strcmp(qobject_to_qstring(x)->string, 137 qobject_to_qstring(y)->string); 138 } 139 140 /** 141 * qstring_destroy_obj(): Free all memory allocated by a QString 142 * object 143 */ 144 void qstring_destroy_obj(QObject *obj) 145 { 146 QString *qs; 147 148 assert(obj != NULL); 149 qs = qobject_to_qstring(obj); 150 g_free(qs->string); 151 g_free(qs); 152 } 153