1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2016 Free Software Foundation
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "value-variant.h"
22 #include "data/value.h"
33 value_variant_new (const union value *in, int width)
35 GVariant *vv[2] = {NULL, NULL};
36 vv[IDX_WIDTH] = g_variant_new_int32 (width);
39 vv[IDX_DATA] = g_variant_new_double (in->f);
40 else if (width <= MAX_SHORT_STRING)
42 char xx[MAX_SHORT_STRING + 1];
43 memset (xx, '\0', MAX_SHORT_STRING + 1);
44 memcpy (xx, in->short_string, width);
45 vv[IDX_DATA] = g_variant_new_bytestring (xx);
49 gchar *q = xmalloc (width + 1);
50 memcpy (q, in->long_string, width);
52 vv[IDX_DATA] = g_variant_new_from_data (G_VARIANT_TYPE_BYTESTRING, q,
53 width + 1, FALSE, NULL, NULL);
56 return g_variant_new_tuple (vv, 2);
60 value_destroy_from_variant (union value *val, GVariant *v)
62 GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
63 gint32 width = g_variant_get_int32 (vwidth);
64 g_variant_unref (vwidth);
65 value_destroy (val, width);
70 value_variant_get (union value *val, GVariant *v)
72 GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
73 gint32 width = g_variant_get_int32 (vwidth);
74 g_variant_unref (vwidth);
76 GVariant *vdata = g_variant_get_child_value (v, IDX_DATA);
79 val->f = g_variant_get_double (vdata);
82 const gchar *data = g_variant_get_bytestring (vdata);
83 if (width <= MAX_SHORT_STRING)
84 memcpy (val->short_string, data, MAX_SHORT_STRING);
87 val->long_string = xmemdup (data, width);
91 g_variant_unref (vdata);