1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ofdbus/dbus-glib/dbus/dbus-gsignature.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,210 @@
1.4 +/* -*- mode: C; c-file-style: "gnu" -*- */
1.5 +/* dbus-gsignature.c Mapping from dbus type signatures to GType
1.6 + *
1.7 + * Copyright (C) 2005 Red Hat, Inc.
1.8 + *
1.9 + * Licensed under the Academic Free License version 2.1
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + *
1.21 + * You should have received a copy of the GNU General Public License
1.22 + * along with this program; if not, write to the Free Software
1.23 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.24 + *
1.25 + */
1.26 +
1.27 +#include "config.h"
1.28 +#include "dbus-gtest.h"
1.29 +#include "dbus-gsignature.h"
1.30 +#include "dbus-gvalue-utils.h"
1.31 +#include <string.h>
1.32 +#include <glib.h>
1.33 +
1.34 +#define MAP_BASIC(d_t, g_t) \
1.35 + case DBUS_TYPE_##d_t: \
1.36 + return G_TYPE_##g_t;
1.37 +static GType
1.38 +typecode_to_gtype (int type)
1.39 +{
1.40 + switch (type)
1.41 + {
1.42 + MAP_BASIC (BOOLEAN, BOOLEAN);
1.43 + MAP_BASIC (BYTE, UCHAR);
1.44 + MAP_BASIC (INT16, INT);
1.45 + MAP_BASIC (INT32, INT);
1.46 + MAP_BASIC (UINT16, UINT);
1.47 + MAP_BASIC (UINT32, UINT);
1.48 + MAP_BASIC (INT64, INT64);
1.49 + MAP_BASIC (UINT64, UINT64);
1.50 + MAP_BASIC (DOUBLE, DOUBLE);
1.51 + MAP_BASIC (STRING, STRING);
1.52 + default:
1.53 + return G_TYPE_INVALID;
1.54 + }
1.55 +}
1.56 +#undef MAP_BASIC
1.57 +
1.58 +static gboolean
1.59 +dbus_typecode_maps_to_basic (int typecode)
1.60 +{
1.61 + return typecode_to_gtype (typecode) != G_TYPE_INVALID;
1.62 +}
1.63 +
1.64 +GType
1.65 +_dbus_gtype_from_basic_typecode (int typecode)
1.66 +{
1.67 + g_assert (dbus_type_is_basic (typecode));
1.68 + g_assert (dbus_typecode_maps_to_basic (typecode));
1.69 + return typecode_to_gtype (typecode);
1.70 +}
1.71 +
1.72 +static GType
1.73 +signature_iter_to_g_type_dict (const DBusSignatureIter *subiter, gboolean is_client)
1.74 +{
1.75 + DBusSignatureIter iter;
1.76 + GType key_gtype;
1.77 + GType value_gtype;
1.78 +
1.79 + g_assert (dbus_signature_iter_get_current_type (subiter) == DBUS_TYPE_DICT_ENTRY);
1.80 +
1.81 + dbus_signature_iter_recurse (subiter, &iter);
1.82 +
1.83 + key_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
1.84 + if (key_gtype == G_TYPE_INVALID)
1.85 + return G_TYPE_INVALID;
1.86 +
1.87 + dbus_signature_iter_next (&iter);
1.88 + value_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
1.89 + if (value_gtype == G_TYPE_INVALID)
1.90 + return G_TYPE_INVALID;
1.91 +
1.92 + if (!_dbus_gtype_is_valid_hash_key (key_gtype)
1.93 + || !_dbus_gtype_is_valid_hash_value (value_gtype))
1.94 + /* Later we need to return DBUS_TYPE_G_VALUE */
1.95 + return G_TYPE_INVALID;
1.96 +
1.97 + return dbus_g_type_get_map ("GHashTable", key_gtype, value_gtype);
1.98 +}
1.99 +
1.100 +static GType
1.101 +signature_iter_to_g_type_array (DBusSignatureIter *iter, gboolean is_client)
1.102 +{
1.103 + GType elt_gtype;
1.104 +
1.105 + elt_gtype = _dbus_gtype_from_signature_iter (iter, is_client);
1.106 + if (elt_gtype == G_TYPE_INVALID)
1.107 + return G_TYPE_INVALID;
1.108 +
1.109 + if (elt_gtype == G_TYPE_OBJECT)
1.110 + return DBUS_TYPE_G_OBJECT_ARRAY;
1.111 + if (elt_gtype == G_TYPE_STRING)
1.112 + return G_TYPE_STRV;
1.113 + if (_dbus_g_type_is_fixed (elt_gtype))
1.114 + return dbus_g_type_get_collection ("GArray", elt_gtype);
1.115 + else if (g_type_is_a (elt_gtype, G_TYPE_OBJECT)
1.116 + || g_type_is_a (elt_gtype, G_TYPE_BOXED))
1.117 + return dbus_g_type_get_collection ("GPtrArray", elt_gtype);
1.118 +
1.119 + /* Later we need to return DBUS_TYPE_G_VALUE */
1.120 + return G_TYPE_INVALID;
1.121 +}
1.122 +
1.123 +static GType
1.124 +signature_iter_to_g_type_struct (DBusSignatureIter *iter, gboolean is_client)
1.125 +{
1.126 + GArray *types;
1.127 + GType ret;
1.128 + types = g_array_new (FALSE, FALSE, sizeof (GType));
1.129 + do
1.130 + {
1.131 + GType curtype;
1.132 + curtype = _dbus_gtype_from_signature_iter (iter, is_client);
1.133 + g_array_append_val (types, curtype);
1.134 + }
1.135 + while (dbus_signature_iter_next (iter));
1.136 +
1.137 + ret = dbus_g_type_get_structv ("GValueArray", types->len, (GType*) types->data);
1.138 + g_array_free (types, TRUE);
1.139 + return ret;
1.140 +}
1.141 +
1.142 +GType
1.143 +_dbus_gtype_from_signature_iter (DBusSignatureIter *iter, gboolean is_client)
1.144 +{
1.145 + int current_type;
1.146 +
1.147 + current_type = dbus_signature_iter_get_current_type (iter);
1.148 + /* TODO: handle type 0? */
1.149 + if (dbus_typecode_maps_to_basic (current_type))
1.150 + return _dbus_gtype_from_basic_typecode (current_type);
1.151 + else if (current_type == DBUS_TYPE_OBJECT_PATH)
1.152 + return DBUS_TYPE_G_OBJECT_PATH;
1.153 + else
1.154 + {
1.155 + DBusSignatureIter subiter;
1.156 +
1.157 + g_assert (dbus_type_is_container (current_type));
1.158 +
1.159 + if (current_type == DBUS_TYPE_VARIANT)
1.160 + return G_TYPE_VALUE;
1.161 +
1.162 + dbus_signature_iter_recurse (iter, &subiter);
1.163 +
1.164 + if (current_type == DBUS_TYPE_ARRAY)
1.165 + {
1.166 + int elt_type = dbus_signature_iter_get_current_type (&subiter);
1.167 + if (elt_type == DBUS_TYPE_DICT_ENTRY)
1.168 + return signature_iter_to_g_type_dict (&subiter, is_client);
1.169 + else
1.170 + return signature_iter_to_g_type_array (&subiter, is_client);
1.171 + }
1.172 + else if (current_type == DBUS_TYPE_STRUCT)
1.173 + {
1.174 + return signature_iter_to_g_type_struct (&subiter, is_client);
1.175 + }
1.176 + else
1.177 + {
1.178 + g_assert_not_reached ();
1.179 + return G_TYPE_INVALID;
1.180 + }
1.181 + }
1.182 +}
1.183 +
1.184 +GType
1.185 +_dbus_gtype_from_signature (const char *signature, gboolean is_client)
1.186 +{
1.187 + DBusSignatureIter iter;
1.188 +
1.189 + dbus_signature_iter_init (&iter, signature);
1.190 +
1.191 + return _dbus_gtype_from_signature_iter (&iter, is_client);
1.192 +}
1.193 +
1.194 +GArray *
1.195 +_dbus_gtypes_from_arg_signature (const char *argsig, gboolean is_client)
1.196 +{
1.197 + GArray *ret;
1.198 + int current_type;
1.199 + DBusSignatureIter sigiter;
1.200 +
1.201 + ret = g_array_new (FALSE, FALSE, sizeof (GType));
1.202 +
1.203 + dbus_signature_iter_init (&sigiter, argsig);
1.204 + while ((current_type = dbus_signature_iter_get_current_type (&sigiter)) != DBUS_TYPE_INVALID)
1.205 + {
1.206 + GType curtype;
1.207 +
1.208 + curtype = _dbus_gtype_from_signature_iter (&sigiter, is_client);
1.209 + g_array_append_val (ret, curtype);
1.210 + dbus_signature_iter_next (&sigiter);
1.211 + }
1.212 + return ret;
1.213 +}