New module value-variant
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 13 Aug 2016 14:39:58 +0000 (16:39 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sat, 13 Aug 2016 14:39:58 +0000 (16:39 +0200)
src/ui/gui/automake.mk
src/ui/gui/value-variant.c [new file with mode: 0644]
src/ui/gui/value-variant.h [new file with mode: 0644]

index 762d37d7c5be2652d472fac7c5d83b5457cc4b39..dd2a342c51f1448487a6b3c0f5cfcf67df0605bd 100644 (file)
@@ -301,6 +301,8 @@ src_ui_gui_psppire_SOURCES = \
        src/ui/gui/var-display.h \
        src/ui/gui/var-type-dialog.c \
        src/ui/gui/var-type-dialog.h \
+       src/ui/gui/value-variant.c \
+       src/ui/gui/value-variant.h \
        src/ui/gui/widget-io.c \
        src/ui/gui/widget-io.h \
        src/ui/gui/widgets.c \
diff --git a/src/ui/gui/value-variant.c b/src/ui/gui/value-variant.c
new file mode 100644 (file)
index 0000000..e8c342b
--- /dev/null
@@ -0,0 +1,92 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2016  Free Software Foundation
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <gtk/gtk.h>
+
+#include <string.h>
+#include "value-variant.h"
+#include "data/value.h"
+
+
+enum
+  {
+    IDX_WIDTH,
+    IDX_DATA
+  };
+
+
+GVariant *
+value_variant_new (const union value *in, int width)
+{
+  GVariant *vv[2] = {NULL, NULL};
+  vv[IDX_WIDTH] = g_variant_new_int32 (width);
+
+  if (width == 0)
+    vv[IDX_DATA] = g_variant_new_double (in->f);
+  else if (width <= MAX_SHORT_STRING)
+    {
+      char xx[MAX_SHORT_STRING + 1];
+      memset (xx, '\0', MAX_SHORT_STRING + 1);
+      memcpy (xx, in->short_string, width);
+      vv[IDX_DATA] = g_variant_new_bytestring (xx);
+    }
+  else
+    {
+      gchar *q = xmalloc (width + 1);
+      memcpy (q, in->long_string, width);
+      q[width] = '\0';
+      vv[IDX_DATA] = g_variant_new_from_data (G_VARIANT_TYPE_BYTESTRING, q,
+                                             width + 1, FALSE, NULL, NULL);
+    }
+  
+  return g_variant_new_tuple (vv, 2);
+}
+
+void
+value_destroy_from_variant (union value *val, GVariant *v)
+{
+  GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
+  gint32 width = g_variant_get_int32 (vwidth);
+  g_variant_unref (vwidth);
+  value_destroy (val, width);
+}
+
+
+void
+value_variant_get (union value *val, GVariant *v)
+{
+  GVariant *vwidth = g_variant_get_child_value (v, IDX_WIDTH);
+  gint32 width = g_variant_get_int32 (vwidth);
+  g_variant_unref (vwidth);
+
+  GVariant *vdata = g_variant_get_child_value (v, IDX_DATA);
+
+  if (0 == width)
+    val->f = g_variant_get_double (vdata);
+  else
+    {
+      const gchar *data = g_variant_get_bytestring (vdata);
+      if (width <= MAX_SHORT_STRING)
+       memcpy (val->short_string, data, MAX_SHORT_STRING);
+      else
+       {
+         val->long_string = xmemdup (data, width);
+       }
+    }
+
+  g_variant_unref (vdata);
+}
diff --git a/src/ui/gui/value-variant.h b/src/ui/gui/value-variant.h
new file mode 100644 (file)
index 0000000..22cd176
--- /dev/null
@@ -0,0 +1,28 @@
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2016 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef VALUE_VARIANT_H
+#define VALUE_VARIANT_H
+
+union value;
+
+GVariant *value_variant_new (const union value *in, int width);
+
+void value_variant_get (union value *val, GVariant *v);
+
+void value_destroy_from_variant (union value *val, GVariant *v);
+
+#endif