psppire.xml: Add PsppireAcr and PsppireValChooser
[pspp] / src / ui / gui / value-variant.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2016, 2020  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include <gtk/gtk.h>
19
20 #include "value-variant.h"
21 #include "data/value.h"
22
23 enum
24   {
25     IDX_WIDTH,
26     IDX_DATA
27   };
28
29 /* Returns a GVariant containing the data contained
30    in IN and WIDTH.  The returned GVariant has a floating
31    reference.
32  */
33 GVariant *
34 value_variant_new (const union value *in, int width)
35 {
36   GVariant *vv[2] = {NULL, NULL};
37   vv[IDX_WIDTH] = g_variant_new_int32 (width);
38
39   if (width == 0)
40     vv[IDX_DATA] = g_variant_new_double (in->f);
41   else
42     {
43       vv[IDX_DATA] = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
44                                                 in->s, width,
45                                                 sizeof (gchar));
46     }
47
48   return g_variant_new_tuple (vv, 2);
49 }
50
51 /* Destroy the contents of VAL.  Also unref V */
52 void
53 value_destroy_from_variant (union value *val, GVariant *v)
54 {
55   GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
56   gint32 width = g_variant_get_int32 (vwidth); /* v is unreffed here */
57   g_variant_unref (vwidth);
58   value_destroy (val, width);
59 }
60
61
62 /* Fills VAL with the value data held in V.
63    When VAL is no longer required it must be destroyed using
64    value_destroy_from_variant. */
65 void
66 value_variant_get (union value *val, GVariant *v)
67 {
68   GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
69   gint32 width = g_variant_get_int32 (vwidth);
70   g_variant_unref (vwidth);
71
72   GVariant *vdata = g_variant_get_child_value (v, IDX_DATA);
73
74   if (0 == width)
75     val->f = g_variant_get_double (vdata);
76   else
77     {
78       gsize w;
79       const gchar *data =
80         g_variant_get_fixed_array (vdata, &w, sizeof (gchar));
81
82       if (w != width)
83         g_critical ("Value variant's width does not match its array size");
84       val->s = xmemdup (data, w);
85     }
86
87   g_variant_unref (vdata);
88 }