From 5191a44547cf406463e0d22206c659a6c0c0391b Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Thu, 23 Sep 2010 19:22:53 +0200 Subject: [PATCH] Implement dbus_message_iter_get_n_elements See https://bugs.freedesktop.org/show_bug.cgi?id=30350 --- dbus/dbus-message-util.c | 20 ++++++++++++++++++++ dbus/dbus-message.c | 34 +++++++++++++++++++++++++++------- dbus/dbus-message.h | 3 +++ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/dbus/dbus-message-util.c b/dbus/dbus-message-util.c index f972c8a..9fcddb6 100644 --- a/dbus/dbus-message-util.c +++ b/dbus/dbus-message-util.c @@ -1396,6 +1396,26 @@ _dbus_message_test (const char *test_data_dir) check_memleaks (); _dbus_check_fdleaks(); + /* Test enumeration of array elements */ + message = dbus_message_new_method_call ("de.ende.test", + "/de/ende/test", + "de.ende.Test", + "ArtistName"); + _dbus_assert (message != NULL); + dbus_message_iter_init_append (message, &iter); + dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, + DBUS_TYPE_STRING_AS_STRING, &array_iter); + s = "SomeThingToSay"; + dbus_message_iter_append_basic (&array_iter, DBUS_TYPE_STRING, &s); + s = "MoreHumbugToCome"; + dbus_message_iter_append_basic (&array_iter, DBUS_TYPE_STRING, &s); + s = "WerewolvesMinotaursKirins"; + dbus_message_iter_append_basic (&array_iter, DBUS_TYPE_STRING, &s); + dbus_message_iter_close_container (&iter, &array_iter); + dbus_message_iter_init (message, &iter); + _dbus_assert (dbus_message_iter_get_n_elements (&iter) == 3); + dbus_message_unref (message); + /* Check that we can abandon a container */ message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService", "/org/freedesktop/TestPath", diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index b19697e..c0e4247 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -2177,20 +2177,40 @@ dbus_message_iter_get_basic (DBusMessageIter *iter, } /** + * Returns the number of elements in the array-typed value pointed + * to by the iterator. + * + * @param iter the iterator + * @returns the number of elements in the array + */ +int dbus_message_iter_get_n_elements (DBusMessageIter* iter) +{ + DBusMessageRealIter *real = (DBusMessageRealIter *)iter; + DBusTypeReader array; + int n_elements = 0; + + _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0); + _dbus_return_val_if_fail (_dbus_type_reader_get_current_type (&real->u.reader) + == DBUS_TYPE_ARRAY, 0); + + _dbus_type_reader_recurse (&real->u.reader, &array); + while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID) + { + ++n_elements; + _dbus_type_reader_next (&array); + } + return n_elements; +} + +/** * Returns the number of bytes in the array as marshaled in the wire * protocol. The iterator must currently be inside an array-typed * value. * * This function is deprecated on the grounds that it is stupid. Why * would you want to know how many bytes are in the array as marshaled - * in the wire protocol? For now, use the n_elements returned from - * dbus_message_iter_get_fixed_array() instead, or iterate over the - * array values and count them. + * in the wire protocol? Use dbus_message_iter_get_n_elements() instead. * - * @todo introduce a variant of this get_n_elements that returns - * the number of elements, though with a non-fixed array it will not - * be very efficient, so maybe it's not good. - * * @param iter the iterator * @returns the number of bytes in the array */ diff --git a/dbus/dbus-message.h b/dbus/dbus-message.h index 5500492..2543c7c 100644 --- a/dbus/dbus-message.h +++ b/dbus/dbus-message.h @@ -226,6 +226,9 @@ void dbus_message_iter_recurse (DBusMessageIter *iter, DBUS_EXPORT void dbus_message_iter_get_basic (DBusMessageIter *iter, void *value); +DBUS_EXPORT +int dbus_message_iter_get_n_elements (DBusMessageIter *iter); + #ifndef DBUS_DISABLE_DEPRECATED /* This function returns the wire protocol size of the array in bytes, * you do not want to know that probably -- 1.7.2.3