D-BUS 0.23 to 0.30 porting quickref

Havoc Pennington hp at redhat.com
Mon Jan 31 14:07:26 PST 2005


Hi,

On Mon, 2005-01-31 at 16:04 -0500, John (J5) Palmieri wrote:
> I wrote up a porting quick reference at
> http://people.redhat.com/johnp/files/dbus_0.23_to_0.30_porting_quickref.txt so package maintainers at Red Hat could use it to port their packages.  Thought it might be useful to everyone.  Patches welcomed for any glaring mistakes or omissions.  This should probably go on the wiki but alas I don't have an account yet.
> 

Nice job, one fix is that you can simplify this a lot:


		arg_type = dbus_message_iter_get_arg_type (&main_iter);
		/* you should use a switch here */
		if (arg_type == DBUS_TYPE_ARRAY) 
		  {
		    DBusMessageIter array_iter;
		    /* assuming that we know the array is an array
		       of strings*/

		    assert (dbus_message_iter_get_element_type (&main_iter) 
		             == DBUS_TYPE_STRING);

		    dbus_message_iter_recurse (&main_iter, &array_iter);

		    do {
		      const char *value;
		      char *str;
		      
		      dbus_message_iter_get_basic (&array_iter, &value);
		      str = g_strdup (value);
		      g_print ("The value of the array element is: %s\n", str);
		      g_free (str);

		    } while (dbus_message_iter_next(&array_iter));
		  }

There's one bug here - you'll read one array element even if the array is empty.

The easiest loop is:

 while (dbus_message_iter_get_arg_type (&array_iter) != DBUS_TYPE_INVALID)
   {
      dbus_message_iter_next (&array_iter);
   }

Or even more robust perhaps is:
 while (dbus_message_iter_get_arg_type (&array_iter) == DBUS_TYPE_STRING)

> One may also use the convinience function for fixed length arrays:

I think "arrays of fixed-length values" is more precise.

arg_type = dbus_message_iter_open_container (&main_iter,
		                                  DBUS_TYPE_ARRAY,
		                                  DBUS_TYPE_STRING_TO_STRING, 
						  &array_iter);

It returns bool for out-of-memory rather than arg_type I think, 
and the signature would be: DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING
Which after preprocessing is: "a" "s" and gets concatenated to "as"

This is why you have to use AS_STRING so they get concatenated.

Havoc





More information about the dbus mailing list