summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-string.c
diff options
context:
space:
mode:
authorMichael Meeks <michael@ximian.com>2004-05-28 13:10:36 +0000
committerMichael Meeks <michael@ximian.com>2004-05-28 13:10:36 +0000
commit0123e6a927772af6d56ddbe3f75a443ab40646d1 (patch)
tree1c31412a2a31d036a0051d364203f7a24ac8be0e /dbus/dbus-string.c
parentf33553eae306f4864a0198c5f9c56f1059536cf3 (diff)
2004-05-28 Michael Meeks <michael@ximian.com>
* glib/dbus-gvalue.c (dbus_gvalue_marshal, dbus_gvalue_demarshal): fix no int64 case. * dbus/dbus-string.c (_dbus_string_parse_basic_type): impl. * dbus/dbus-message.c (_dbus_message_iter_get_basic_type), (_dbus_message_iter_get_basic_type_array): impl. drastically simplify ~all relevant _get methods to use these. (_dbus_message_iter_append_basic_array), (dbus_message_iter_append_basic): impl drastically simplify ~all relevant _append methods to use these. * dbus/dbus-message-builder.c (parse_basic_type) (parse_basic_array, lookup_basic_type): impl. (_dbus_message_data_load): prune scads of duplicate / cut & paste coding. * dbus/dbus-marshal.c (_dbus_demarshal_basic_type_array) (_dbus_demarshal_basic_type): implement, (demarshal_and_validate_len/arg): beef up debug. (_dbus_marshal_basic_type, _dbus_marshal_basic_type_array): impl.
Diffstat (limited to 'dbus/dbus-string.c')
-rw-r--r--dbus/dbus-string.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c
index 8820273d..a1bab820 100644
--- a/dbus/dbus-string.c
+++ b/dbus/dbus-string.c
@@ -2816,6 +2816,113 @@ _dbus_string_zero (DBusString *str)
#include "dbus-test.h"
#include <stdio.h>
+/**
+ * Parses a basic type defined by type contained in a DBusString. The
+ * end_return parameter may be #NULL if you aren't interested in it. The
+ * type is parsed and stored in value_return. Return parameters are not
+ * initialized if the function returns #FALSE.
+ *
+ * @param str the string
+ * @param type the type of the basic type
+ * @param start the byte index of the start of the type
+ * @param value_return return location of the value or #NULL
+ * @param end_return return location of the end of the type, or #NULL
+ * @returns #TRUE on success
+ */
+dbus_bool_t
+_dbus_string_parse_basic_type (const DBusString *str,
+ char type,
+ int start,
+ void *value,
+ int *end_return)
+{
+ int end = start;
+
+ switch (type)
+ {
+ case DBUS_TYPE_BOOLEAN:
+ {
+ int len = _dbus_string_get_length (str) - start;
+ if (len >= 5 && _dbus_string_find_to (str, start, start + 5, "false", NULL))
+ {
+ end += 5;
+ *(unsigned char *) value = TRUE;
+ }
+ else if (len >= 4 && _dbus_string_find_to (str, start, start + 4, "true", NULL))
+ {
+ end += 4;
+ *(unsigned char *) value = FALSE;
+ }
+ else
+ _dbus_warn ("could not parse BOOLEAN\n");
+ break;
+ }
+ case DBUS_TYPE_BYTE:
+ {
+ long val = 0;
+
+ if (_dbus_string_get_byte (str, start) == '\'' &&
+ _dbus_string_get_length (str) >= start + 4 &&
+ _dbus_string_get_byte (str, start + 1) == '\\' &&
+ _dbus_string_get_byte (str, start + 2) == '\'' &&
+ _dbus_string_get_byte (str, start + 3) == '\'')
+ {
+ val = '\'';
+ end += 4;
+ }
+ else if (_dbus_string_get_byte (str, start) == '\'' &&
+ _dbus_string_get_length (str) >= start + 3 &&
+ _dbus_string_get_byte (str, start + 2) == '\'')
+ {
+ val = _dbus_string_get_byte (str, start + 1);
+ end += 3;
+ }
+ else
+ {
+ if (!_dbus_string_parse_int (str, start, &val, &end))
+ _dbus_warn ("Failed to parse integer for BYTE\n");
+ }
+
+ if (val > 255)
+ _dbus_warn ("A byte must be in range 0-255 not %ld\n", val);
+
+ *(unsigned char *) value = val;
+ break;
+ }
+ case DBUS_TYPE_INT32:
+ {
+ long val;
+ if (_dbus_string_parse_int (str, start, &val, &end))
+ *(dbus_int32_t *)value = val;
+ break;
+ }
+ case DBUS_TYPE_UINT32:
+ {
+ unsigned long val;
+ if (_dbus_string_parse_uint (str, start, &val, &end))
+ *(dbus_uint32_t *)value = val;
+ break;
+ }
+#ifdef DBUS_HAVE_INT64
+ case DBUS_TYPE_INT64:
+ case DBUS_TYPE_UINT64:
+ /* use stroll oull */
+ _dbus_assert_not_reached ("string -> [u]int64 not supported yet");
+ break;
+#endif /* DBUS_HAVE_INT64 */
+ case DBUS_TYPE_DOUBLE:
+ _dbus_string_parse_double (str, start, value, &end);
+ break;
+ default:
+ _dbus_assert_not_reached ("not a basic type");
+ break;
+ }
+ if (end_return)
+ *end_return = end;
+
+ return end != start;
+}
+
static void
test_max_len (DBusString *str,
int max_len)