From 36b67d53683e4e1c8c1918f75752707e6cc1808b 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_element_count According unit tests are added to _dbus_message_test. See https://bugs.freedesktop.org/show_bug.cgi?id=30350 --- dbus/dbus-message-util.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ dbus/dbus-message.c | 45 ++++++++++++++++++++++++++++++++++------ dbus/dbus-message.h | 3 ++ 3 files changed, 92 insertions(+), 7 deletions(-) diff --git a/dbus/dbus-message-util.c b/dbus/dbus-message-util.c index f972c8a..e85cc99 100644 --- a/dbus/dbus-message-util.c +++ b/dbus/dbus-message-util.c @@ -1000,6 +1000,16 @@ _dbus_message_test (const char *test_data_dir) int v_UNIX_FD; #endif char **decomposed; + char basic_types[] = DBUS_TYPE_BYTE_AS_STRING \ + DBUS_TYPE_BOOLEAN_AS_STRING \ + DBUS_TYPE_INT16_AS_STRING \ + DBUS_TYPE_INT32_AS_STRING \ + DBUS_TYPE_INT64_AS_STRING \ + DBUS_TYPE_UINT16_AS_STRING \ + DBUS_TYPE_UINT32_AS_STRING \ + DBUS_TYPE_UINT64_AS_STRING \ + DBUS_TYPE_DOUBLE_AS_STRING \ + DBUS_TYPE_STRING_AS_STRING; message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService", "/org/freedesktop/TestPath", @@ -1396,6 +1406,47 @@ _dbus_message_test (const char *test_data_dir) check_memleaks (); _dbus_check_fdleaks(); + /* Test enumeration of array elements */ + for (i = strlen (basic_types) - 1; i > 0; i--) + { + int some; + s = "SomeThingToSay"; + 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, + basic_types + i, &array_iter); + for (some = 0; some < 3; some++) + if (basic_types[i] == DBUS_TYPE_STRING) + dbus_message_iter_append_basic (&array_iter, DBUS_TYPE_STRING, &s); + else + dbus_message_iter_append_basic (&array_iter, basic_types[i], &some); + dbus_message_iter_close_container (&iter, &array_iter); + dbus_message_iter_init (message, &iter); + _dbus_assert (dbus_message_iter_get_element_count (&iter) == some); + dbus_message_unref (message); + basic_types[i] = '\0'; + } + /* Array of structs */ + 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_STRUCT_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING + DBUS_STRUCT_END_CHAR_AS_STRING, &array_iter); + dbus_message_iter_open_container (&array_iter, DBUS_TYPE_STRUCT, + NULL, &struct_iter); + s = "SpamAndEggs"; + dbus_message_iter_append_basic (&struct_iter, DBUS_TYPE_STRING, &s); + dbus_message_iter_close_container (&array_iter, &struct_iter); + dbus_message_iter_close_container (&iter, &array_iter); + dbus_message_iter_init (message, &iter); + _dbus_assert (dbus_message_iter_get_element_count (&iter) == 1); + 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..5bd6b42 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -2177,20 +2177,51 @@ dbus_message_iter_get_basic (DBusMessageIter *iter, } /** + * Returns the number of elements in the array-typed value pointed + * to by the iterator. + * Note that this function is O(1) for fixed-size arrays but O(n) + * otherwise, so it may be a bad idea to use it. + * + * @param iter the iterator + * @returns the number of elements in the array + */ +int dbus_message_iter_get_element_count (DBusMessageIter* iter) +{ + DBusMessageRealIter *real = (DBusMessageRealIter *)iter; + int atype = _dbus_type_reader_get_current_type(&real->u.reader); + DBusTypeReader array; + int element_type; + int n_elements = 0; + + _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0); + _dbus_return_val_if_fail (atype == DBUS_TYPE_ARRAY, 0); + + element_type = _dbus_type_reader_get_element_type (&real->u.reader); + _dbus_type_reader_recurse (&real->u.reader, &array); + if (dbus_type_is_fixed (element_type)) + { + int alignment = _dbus_type_get_alignment (element_type); + int total_len = _dbus_type_reader_get_array_length (&array); + return total_len / alignment; + } + + 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_element_count() 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..fa1b2d4 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_element_count(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