Add helper function to encode D-Bus object paths
authorMarcel Holtmann <marcel@holtmann.org>
Wed, 1 Apr 2009 01:33:09 +0000 (18:33 -0700)
committerMarcel Holtmann <marcel@holtmann.org>
Wed, 1 Apr 2009 01:33:09 +0000 (18:33 -0700)
include/dbus.h
src/dbus.c

index 77766c72ac2098801ac96389f8c5b1a8b54c0299..277e69a83d14dce0a37427bbbfbcf711d5f7622b 100644 (file)
@@ -52,6 +52,8 @@ extern void connman_dbus_dict_append_array(DBusMessageIter *dict,
 extern void connman_dbus_dict_append_variant(DBusMessageIter *dict,
                                        const char *key, int type, void *val);
 
+extern char *connman_dbus_encode_string(const char *value);
+
 #ifdef __cplusplus
 }
 #endif
index 062cc7ab105d19855ae25da27aa213cf8ccfdf17..daa2239c536926cb79809f45c2b35693bda091f5 100644 (file)
 #include <config.h>
 #endif
 
+#include <string.h>
+
 #include "connman.h"
 
+char *connman_dbus_encode_string(const char *value)
+{
+       GString *str;
+       unsigned int i, size;
+
+       if (value == NULL)
+               return NULL;
+
+       size = strlen(value);
+
+       str = g_string_new(NULL);
+       if (str == NULL)
+               return NULL;
+
+       for (i = 0; i < size; i++) {
+               const char tmp = value[i];
+               if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
+                                               (tmp < 'a' || tmp > 'z'))
+                       g_string_append_printf(str, "_%02x", tmp);
+               else
+                       str = g_string_append_c(str, tmp);
+       }
+
+       return g_string_free(str, FALSE);
+}
+
 void connman_dbus_property_append_variant(DBusMessageIter *iter,
                                        const char *key, int type, void *val)
 {