2003-01-07 Anders Carlsson <andersca@codefactory.se>
authorAnders Carlsson <andersca@codefactory.se>
Mon, 6 Jan 2003 22:09:16 +0000 (22:09 +0000)
committerAnders Carlsson <andersca@codefactory.se>
Mon, 6 Jan 2003 22:09:16 +0000 (22:09 +0000)
* dbus/dbus-marshal.c: (_dbus_marshal_string),
(_dbus_demarshal_string), (_dbus_marshal_test):
* dbus/dbus-marshal.h:
Document these functions.

* dbus/dbus-message.c: (dbus_message_get_name),
(dbus_message_append_int32), (dbus_message_append_uint32),
(dbus_message_append_double), (dbus_message_append_string),
(dbus_message_append_byte_array):
* dbus/dbus-message.h:
Add functions for adding message fields of different types.

* dbus/dbus-protocol.h:
Add the different types.

ChangeLog
dbus/dbus-marshal.c
dbus/dbus-marshal.h
dbus/dbus-message.c
dbus/dbus-message.h
dbus/dbus-protocol.h

index 21a1833..976e0d4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2003-01-07  Anders Carlsson  <andersca@codefactory.se>
+
+       * dbus/dbus-marshal.c: (_dbus_marshal_string),
+       (_dbus_demarshal_string), (_dbus_marshal_test):
+       * dbus/dbus-marshal.h:
+       * dbus/dbus-message.c: (dbus_message_get_name),
+       Document these functions.
+       
+       (dbus_message_append_int32), (dbus_message_append_uint32),
+       (dbus_message_append_double), (dbus_message_append_string),
+       (dbus_message_append_byte_array):
+       * dbus/dbus-message.h:
+       Add functions for adding message fields of different types.
+       
+       * dbus/dbus-protocol.h:
+       Add the different types.
+
 2003-01-05  Havoc Pennington  <hp@pobox.com>
 
        * bus/connection.c: implement routines for handling connections,
index da72635..6e4f414 100644 (file)
@@ -124,6 +124,14 @@ unpack_int32 (int                  byte_order,
  * @{
  */
 
+/**
+ * Marshals a double value.
+ *
+ * @param str the string to append the marshalled value to
+ * @param byte_order the byte order to use
+ * @param value the value
+ * @returns #TRUE on success
+ */
 dbus_bool_t
 _dbus_marshal_double (DBusString *str,
                      int         byte_order,
@@ -140,6 +148,14 @@ _dbus_marshal_double (DBusString *str,
   return _dbus_string_append_len (str, (const char *)&value, sizeof (double));
 }
 
+/**
+ * Marshals a 32 bit signed integer value.
+ *
+ * @param str the string to append the marshalled value to
+ * @param byte_order the byte order to use
+ * @param value the value
+ * @returns #TRUE on success
+ */
 dbus_bool_t
 _dbus_marshal_int32  (DBusString   *str,
                      int           byte_order,
@@ -156,6 +172,14 @@ _dbus_marshal_int32  (DBusString   *str,
   return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_int32_t));
 }
 
+/**
+ * Marshals a 32 bit unsigned integer value.
+ *
+ * @param str the string to append the marshalled value to
+ * @param byte_order the byte order to use
+ * @param value the value
+ * @returns #TRUE on success
+ */
 dbus_bool_t
 _dbus_marshal_uint32 (DBusString    *str,
                      int            byte_order,
@@ -172,10 +196,18 @@ _dbus_marshal_uint32 (DBusString    *str,
   return _dbus_string_append_len (str, (const char *)&value, sizeof (dbus_uint32_t));
 }
 
+/**
+ * Marshals a UTF-8 string
+ *
+ * @param str the string to append the marshalled value to
+ * @param byte_order the byte order to use
+ * @param value the string
+ * @returns #TRUE on success
+ */
 dbus_bool_t
-_dbus_marshal_utf8_string (DBusString    *str,
-                          int            byte_order,
-                          const char    *value)
+_dbus_marshal_string (DBusString    *str,
+                     int            byte_order,
+                     const char    *value)
 {
   int len;
 
@@ -187,6 +219,15 @@ _dbus_marshal_utf8_string (DBusString    *str,
   return _dbus_string_append_len (str, value, len + 1);
 }
 
+/**
+ * Marshals a byte array
+ *
+ * @param str the string to append the marshalled value to
+ * @param byte_order the byte order to use
+ * @param value the byte array
+ * @param len the length of the byte array
+ * @returns #TRUE on success
+ */
 dbus_bool_t
 _dbus_marshal_byte_array (DBusString          *str,
                          int                  byte_order,
@@ -199,6 +240,15 @@ _dbus_marshal_byte_array (DBusString          *str,
   return _dbus_string_append_len (str, value, len);
 }
 
+/**
+ * Demarshals a double.
+ *
+ * @param str the string containing the data
+ * @param byte_order the byte order
+ * @param pos the position in the string
+ * @param new_pos the new position of the string
+ * @returns the demarshaled double.
+ */
 double
 _dbus_demarshal_double (DBusString  *str,
                        int          byte_order,
@@ -223,6 +273,15 @@ _dbus_demarshal_double (DBusString  *str,
   return retval;  
 }
 
+/**
+ * Demarshals a 32 bit signed integer.
+ *
+ * @param str the string containing the data
+ * @param byte_order the byte order
+ * @param pos the position in the string
+ * @param new_pos the new position of the string
+ * @returns the demarshaled integer.
+ */
 dbus_int32_t
 _dbus_demarshal_int32  (DBusString *str,
                        int         byte_order,
@@ -241,6 +300,15 @@ _dbus_demarshal_int32  (DBusString *str,
   return unpack_int32 (byte_order, buffer);
 }
 
+/**
+ * Demarshals a 32 bit unsigned integer.
+ *
+ * @param str the string containing the data
+ * @param byte_order the byte order
+ * @param pos the position in the string
+ * @param new_pos the new position of the string
+ * @returns the demarshaled integer.
+ */
 dbus_uint32_t
 _dbus_demarshal_uint32  (DBusString *str,
                         int         byte_order,
@@ -259,11 +327,24 @@ _dbus_demarshal_uint32  (DBusString *str,
   return unpack_uint32 (byte_order, buffer);
 }
 
+/**
+ * Demarshals an UTF-8 string.
+ *
+ * @todo Should we check the string to make sure
+ * that it's  valid UTF-8, and maybe "fix" the string
+ * if it's broken?
+ *
+ * @param str the string containing the data
+ * @param byte_order the byte order
+ * @param pos the position in the string
+ * @param new_pos the new position of the string
+ * @returns the demarshaled string.
+ */
 char *
-_dbus_demarshal_utf8_string (DBusString *str,
-                            int         byte_order,
-                            int         pos,
-                            int        *new_pos)
+_dbus_demarshal_string (DBusString *str,
+                       int         byte_order,
+                       int         pos,
+                       int        *new_pos)
 {
   int len;
   char *retval;
@@ -417,16 +498,16 @@ _dbus_marshal_test (void)
 
   /* Marshal strings */
   tmp1 = "This is the dbus test string";
-  if (!_dbus_marshal_utf8_string (&str, DBUS_BIG_ENDIAN, tmp1))
+  if (!_dbus_marshal_string (&str, DBUS_BIG_ENDIAN, tmp1))
     _dbus_assert_not_reached ("could not marshal string");
-  tmp2 = _dbus_demarshal_utf8_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
+  tmp2 = _dbus_demarshal_string (&str, DBUS_BIG_ENDIAN, pos, &pos);
   _dbus_assert (strcmp (tmp1, tmp2) == 0);
   dbus_free (tmp2);
 
   tmp1 = "This is the dbus test string";
-  if (!_dbus_marshal_utf8_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
+  if (!_dbus_marshal_string (&str, DBUS_LITTLE_ENDIAN, tmp1))
     _dbus_assert_not_reached ("could not marshal string");
-  tmp2 = _dbus_demarshal_utf8_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
+  tmp2 = _dbus_demarshal_string (&str, DBUS_LITTLE_ENDIAN, pos, &pos);
   _dbus_assert (strcmp (tmp1, tmp2) == 0);
   dbus_free (tmp2);
 
index 88a3b1a..6a9d752 100644 (file)
 #define DBUS_COMPILER_BYTE_ORDER DBUS_LITTLE_ENDIAN
 #endif
 
-dbus_bool_t _dbus_marshal_double      (DBusString          *str,
-                                      int                  byte_order,
-                                      double               value);
-dbus_bool_t _dbus_marshal_int32       (DBusString          *str,
-                                      int                  byte_order,
-                                      dbus_int32_t         value);
-dbus_bool_t _dbus_marshal_uint32      (DBusString          *str,
-                                      int                  byte_order,
-                                      dbus_uint32_t        value);
-dbus_bool_t _dbus_marshal_utf8_string (DBusString          *str,
-                                      int                  byte_order,
-                                      const char          *value);
-dbus_bool_t _dbus_marshal_byte_array  (DBusString          *str,
-                                      int                  byte_order,
-                                      const unsigned char *value,
-                                      int                  len);
+dbus_bool_t _dbus_marshal_double     (DBusString          *str,
+                                     int                  byte_order,
+                                     double               value);
+dbus_bool_t _dbus_marshal_int32      (DBusString          *str,
+                                     int                  byte_order,
+                                     dbus_int32_t         value);
+dbus_bool_t _dbus_marshal_uint32     (DBusString          *str,
+                                     int                  byte_order,
+                                     dbus_uint32_t        value);
+dbus_bool_t _dbus_marshal_string     (DBusString          *str,
+                                     int                  byte_order,
+                                     const char          *value);
+dbus_bool_t _dbus_marshal_byte_array (DBusString          *str,
+                                     int                  byte_order,
+                                     const unsigned char *value,
+                                     int                  len);
 
+double        _dbus_demarshal_double (DBusString *str,
+                                     int         byte_order,
+                                     int         pos,
+                                     int        *new_pos);
+dbus_int32_t  _dbus_demarshal_int32  (DBusString *str,
+                                     int         byte_order,
+                                     int         pos,
+                                     int        *new_pos);
+dbus_uint32_t _dbus_demarshal_uint32 (DBusString *str,
+                                     int         byte_order,
+                                     int         pos,
+                                     int        *new_pos);
+char *        _dbus_demarshal_string (DBusString *str,
+                                     int         byte_order,
+                                     int         pos,
+                                     int        *new_pos);
 
-double        _dbus_demarshal_double      (DBusString *str,
-                                          int         byte_order,
-                                          int         pos,
-                                          int        *new_pos);
-dbus_int32_t  _dbus_demarshal_int32       (DBusString *str,
-                                          int         byte_order,
-                                          int         pos,
-                                          int        *new_pos);
-dbus_uint32_t _dbus_demarshal_uint32      (DBusString *str,
-                                          int         byte_order,
-                                          int         pos,
-                                          int        *new_pos);
-char *        _dbus_demarshal_utf8_string (DBusString *str,
-                                          int         byte_order,
-                                          int         pos,
-                                          int        *new_pos);
 
 
 
index aed943f..b7f6664 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include "dbus-internals.h"
+#include "dbus-marshal.h"
 #include "dbus-message.h"
 #include "dbus-message-internal.h"
 #include "dbus-memory.h"
@@ -202,6 +203,7 @@ dbus_message_unref (DBusMessage *message)
 
 /**
  * Gets the name of a message.
+ *
  * @param message the message
  * @returns the message name (should not be freed)
  */
@@ -213,6 +215,115 @@ dbus_message_get_name (DBusMessage *message)
   return NULL;
 }
 
+/**
+ * Appends a 32 bit signed integer to the message.
+ *
+ * @param message the message
+ * @param value the integer value
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_int32 (DBusMessage  *message,
+                          dbus_int32_t  value)
+{
+  _dbus_assert (message != NULL);  
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
+    return FALSE;
+  
+  return _dbus_marshal_int32 (&message->body,
+                             DBUS_COMPILER_BYTE_ORDER, value);
+}
+
+/**
+ * Appends a 32 bit unsigned integer to the message.
+ *
+ * @param message the message
+ * @param value the integer value
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_uint32 (DBusMessage   *message,
+                           dbus_uint32_t  value)
+{
+  _dbus_assert (message != NULL);  
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UINT32))
+    return FALSE;
+  
+  return _dbus_marshal_uint32 (&message->body,
+                              DBUS_COMPILER_BYTE_ORDER, value);
+}
+
+/**
+ * Appends a double value to the message.
+ *
+ * @param message the message
+ * @param value the double value
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_double (DBusMessage *message,
+                           double       value)
+{
+  _dbus_assert (message != NULL);  
+  _dbus_assert (!message->locked);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_INT32))
+    return FALSE;
+  
+  return _dbus_marshal_double (&message->body,
+                              DBUS_COMPILER_BYTE_ORDER, value);
+}
+
+/**
+ * Appends a UTF-8 string to the message.
+ *
+ * @param message the message
+ * @param value the string
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_string (DBusMessage *message,
+                           const char  *value)
+{
+  _dbus_assert (message != NULL);
+  _dbus_assert (!message->locked);
+  _dbus_assert (value != NULL);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_UTF8_STRING))
+    return FALSE;
+  
+  return _dbus_marshal_string (&message->body,
+                              DBUS_COMPILER_BYTE_ORDER, value);
+}
+
+/**
+ * Appends a byte array to the message.
+ *
+ * @param message the message
+ * @param value the array
+ * @param len the length of the array
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+dbus_message_append_byte_array (DBusMessage         *message,
+                               unsigned const char *value,
+                               int                 len)
+{
+  _dbus_assert (message != NULL);
+  _dbus_assert (!message->locked);
+  _dbus_assert (value != NULL);
+
+  if (!_dbus_string_append_byte (&message->body, DBUS_TYPE_BYTE_ARRAY))
+    return FALSE;
+  
+  return _dbus_marshal_byte_array (&message->body,
+                                  DBUS_COMPILER_BYTE_ORDER, value, len);
+}
+
 /** @} */
 
 /**
index ae8b84e..f427780 100644 (file)
@@ -41,6 +41,19 @@ void         dbus_message_unref (DBusMessage *message);
 
 const char*  dbus_message_get_name (DBusMessage *message);
 
+dbus_bool_t dbus_message_append_int32      (DBusMessage         *message,
+                                           dbus_int32_t         value);
+dbus_bool_t dbus_message_append_uint32     (DBusMessage         *message,
+                                           dbus_uint32_t        value);
+dbus_bool_t dbus_message_append_double     (DBusMessage         *message,
+                                           double               value);
+dbus_bool_t dbus_message_append_string     (DBusMessage         *message,
+                                           const char          *value);
+dbus_bool_t dbus_message_append_byte_array (DBusMessage         *message,
+                                           unsigned const char *value,
+                                           int                  len);
+
+
 DBUS_END_DECLS;
 
 #endif /* DBUS_MESSAGE_H */
index 0e00924..e1a17b5 100644 (file)
@@ -36,6 +36,17 @@ extern "C" {
 #define DBUS_LITTLE_ENDIAN ('l')  /* LSB first */
 #define DBUS_BIG_ENDIAN    ('B')  /* MSB first */    
 
+/* Data types */
+#define DBUS_TYPE_INVALID       0
+#define DBUS_TYPE_INT32         1
+#define DBUS_TYPE_UINT32        2
+#define DBUS_TYPE_DOUBLE        3
+#define DBUS_TYPE_INT32_ARRAY   4
+#define DBUS_TYPE_UINT32_ARRAY  5
+#define DBUS_TYPE_DOUBLE_ARRAY  6
+#define DBUS_TYPE_BYTE_ARRAY    7
+#define DBUS_TYPE_UTF8_STRING   8
+  
 #ifdef __cplusplus
 }
 #endif