Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 1 | /* |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 2 | * QString Module |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 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. |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 11 | */ |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 12 | |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 13 | #include "qobject.h" |
| 14 | #include "qstring.h" |
| 15 | #include "qemu-common.h" |
| 16 | |
Blue Swirl | aa43d9c | 2009-09-04 17:43:37 +0000 | [diff] [blame] | 17 | static void qstring_destroy_obj(QObject *obj); |
| 18 | |
| 19 | static const QType qstring_type = { |
| 20 | .code = QTYPE_QSTRING, |
| 21 | .destroy = qstring_destroy_obj, |
| 22 | }; |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 23 | |
| 24 | /** |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 25 | * qstring_new(): Create a new empty QString |
| 26 | * |
| 27 | * Return strong reference. |
| 28 | */ |
| 29 | QString *qstring_new(void) |
| 30 | { |
| 31 | return qstring_from_str(""); |
| 32 | } |
| 33 | |
| 34 | /** |
Luiz Capitulino | 4b5c576 | 2009-11-18 23:05:27 -0200 | [diff] [blame] | 35 | * qstring_from_substr(): Create a new QString from a C string substring |
| 36 | * |
| 37 | * Return string reference |
| 38 | */ |
| 39 | QString *qstring_from_substr(const char *str, int start, int end) |
| 40 | { |
| 41 | QString *qstring; |
| 42 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 43 | qstring = g_malloc(sizeof(*qstring)); |
Luiz Capitulino | 4b5c576 | 2009-11-18 23:05:27 -0200 | [diff] [blame] | 44 | |
| 45 | qstring->length = end - start + 1; |
| 46 | qstring->capacity = qstring->length; |
| 47 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 48 | qstring->string = g_malloc(qstring->capacity + 1); |
Luiz Capitulino | 4b5c576 | 2009-11-18 23:05:27 -0200 | [diff] [blame] | 49 | memcpy(qstring->string, str + start, qstring->length); |
| 50 | qstring->string[qstring->length] = 0; |
| 51 | |
| 52 | QOBJECT_INIT(qstring, &qstring_type); |
| 53 | |
| 54 | return qstring; |
| 55 | } |
| 56 | |
| 57 | /** |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 58 | * qstring_from_str(): Create a new QString from a regular C string |
| 59 | * |
| 60 | * Return strong reference. |
| 61 | */ |
| 62 | QString *qstring_from_str(const char *str) |
| 63 | { |
Luiz Capitulino | 4b5c576 | 2009-11-18 23:05:27 -0200 | [diff] [blame] | 64 | return qstring_from_substr(str, 0, strlen(str) - 1); |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 65 | } |
| 66 | |
Luiz Capitulino | 6fe9565 | 2009-11-18 23:05:25 -0200 | [diff] [blame] | 67 | static void capacity_increase(QString *qstring, size_t len) |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 68 | { |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 69 | if (qstring->capacity < (qstring->length + len)) { |
| 70 | qstring->capacity += len; |
| 71 | qstring->capacity *= 2; /* use exponential growth */ |
| 72 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 73 | qstring->string = g_realloc(qstring->string, qstring->capacity + 1); |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 74 | } |
Luiz Capitulino | 6fe9565 | 2009-11-18 23:05:25 -0200 | [diff] [blame] | 75 | } |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 76 | |
Luiz Capitulino | 6fe9565 | 2009-11-18 23:05:25 -0200 | [diff] [blame] | 77 | /* qstring_append(): Append a C string to a QString |
| 78 | */ |
| 79 | void qstring_append(QString *qstring, const char *str) |
| 80 | { |
| 81 | size_t len = strlen(str); |
| 82 | |
| 83 | capacity_increase(qstring, len); |
Anthony Liguori | d30ec84 | 2009-11-11 10:49:51 -0600 | [diff] [blame] | 84 | memcpy(qstring->string + qstring->length, str, len); |
| 85 | qstring->length += len; |
| 86 | qstring->string[qstring->length] = 0; |
| 87 | } |
| 88 | |
Luiz Capitulino | 764c1ca | 2009-11-18 23:05:26 -0200 | [diff] [blame] | 89 | void qstring_append_int(QString *qstring, int64_t value) |
| 90 | { |
| 91 | char num[32]; |
| 92 | |
| 93 | snprintf(num, sizeof(num), "%" PRId64, value); |
| 94 | qstring_append(qstring, num); |
| 95 | } |
| 96 | |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 97 | /** |
Luiz Capitulino | 6fe9565 | 2009-11-18 23:05:25 -0200 | [diff] [blame] | 98 | * qstring_append_chr(): Append a C char to a QString |
| 99 | */ |
| 100 | void qstring_append_chr(QString *qstring, int c) |
| 101 | { |
| 102 | capacity_increase(qstring, 1); |
| 103 | qstring->string[qstring->length++] = c; |
| 104 | qstring->string[qstring->length] = 0; |
| 105 | } |
| 106 | |
| 107 | /** |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 108 | * qobject_to_qstring(): Convert a QObject to a QString |
| 109 | */ |
| 110 | QString *qobject_to_qstring(const QObject *obj) |
| 111 | { |
| 112 | if (qobject_type(obj) != QTYPE_QSTRING) |
| 113 | return NULL; |
| 114 | |
| 115 | return container_of(obj, QString, base); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * qstring_get_str(): Return a pointer to the stored string |
| 120 | * |
| 121 | * NOTE: Should be used with caution, if the object is deallocated |
| 122 | * this pointer becomes invalid. |
| 123 | */ |
| 124 | const char *qstring_get_str(const QString *qstring) |
| 125 | { |
| 126 | return qstring->string; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * qstring_destroy_obj(): Free all memory allocated by a QString |
| 131 | * object |
| 132 | */ |
| 133 | static void qstring_destroy_obj(QObject *obj) |
| 134 | { |
| 135 | QString *qs; |
| 136 | |
| 137 | assert(obj != NULL); |
| 138 | qs = qobject_to_qstring(obj); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 139 | g_free(qs->string); |
| 140 | g_free(qs); |
Luiz Capitulino | 66f7048 | 2009-08-28 15:27:06 -0300 | [diff] [blame] | 141 | } |