pxd: initial work
[pspp] / src / data / variable.c
index 9f21b1497be391e5a1ea635a61f7d7d9602b8085..f22bc522e4bee26924071062256fbf814e4c9eae 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2009-2015 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
 #include "libpspp/compiler.h"
 #include "libpspp/hash-functions.h"
 #include "libpspp/i18n.h"
+#include "libpspp/intern.h"
 #include "libpspp/message.h"
 #include "libpspp/misc.h"
+#include "libpspp/pxd.h"
 #include "libpspp/str.h"
 
 #include "gl/minmax.h"
@@ -76,11 +78,13 @@ struct variable
     /* Custom attributes. */
     struct attrset attributes;
   };
+
+static void var_uncache (struct variable *);
 \f
 
 static void var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print);
 static void var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write);
-static bool var_set_label_quiet (struct variable *v, const char *label, bool issue_warning);
+static void var_set_label_quiet (struct variable *v, const char *label);
 static void var_set_name_quiet (struct variable *v, const char *name);
 
 /* Creates and returns a new variable with the given NAME and
@@ -121,10 +125,11 @@ var_destroy (struct variable *v)
   if (v != NULL)
     {
       assert (!var_has_vardict (v));
+      var_uncache (v);
       mv_destroy (&v->miss);
       var_clear_short_names (v);
       val_labs_destroy (v->val_labs);
-      var_set_label_quiet (v, NULL, false);
+      var_set_label_quiet (v, NULL);
       attrset_destroy (var_get_attributes (v));
       free (v->name);
       ds_destroy (&v->name_and_label);
@@ -156,6 +161,7 @@ var_set_name_quiet (struct variable *v, const char *name)
   v->name = xstrdup (name);
   ds_destroy (&v->name_and_label);
   ds_init_empty (&v->name_and_label);
+  var_uncache (v);
 }
 
 /* Sets V's name to NAME, a UTF-8 encoded string.
@@ -285,8 +291,11 @@ var_set_width_and_formats (struct variable *v, int new_width,
   if (fmt_resize (&v->write, new_width))
     traits |= VAR_TRAIT_WRITE_FORMAT;
 
-  v->width = new_width;
-  traits |= VAR_TRAIT_WIDTH;
+  if (v->width != new_width)
+    {
+      v->width = new_width;
+      traits |= VAR_TRAIT_WIDTH;
+    }
 
   if (print)
     {
@@ -300,7 +309,10 @@ var_set_width_and_formats (struct variable *v, int new_width,
       traits |= VAR_TRAIT_WRITE_FORMAT;
     }
 
-  dict_var_changed (v, traits, ov);
+  if (traits != 0)
+    dict_var_changed (v, traits, ov);
+
+  var_uncache (v);
 }
 
 /* Changes the width of V to NEW_WIDTH.
@@ -436,8 +448,14 @@ var_has_value_labels (const struct variable *v)
 static void
 var_set_value_labels_quiet (struct variable *v, const struct val_labs *vls)
 {
-  val_labs_destroy (v->val_labs);
-  v->val_labs = NULL;
+  bool changed = false;
+
+  if (v->val_labs != NULL)
+    {
+      val_labs_destroy (v->val_labs);
+      v->val_labs = NULL;
+      changed = true;
+    }
 
   if (vls != NULL)
     {
@@ -445,6 +463,9 @@ var_set_value_labels_quiet (struct variable *v, const struct val_labs *vls)
       v->val_labs = val_labs_clone (vls);
       val_labs_set_width (v->val_labs, v->width);
     }
+
+  if (changed)
+    var_uncache (v);
 }
 
 
@@ -479,8 +500,14 @@ bool
 var_add_value_label (struct variable *v,
                      const union value *value, const char *label)
 {
+  bool changed;
+
   alloc_value_labels (v);
-  return val_labs_add (v->val_labs, value, label);
+
+  changed = val_labs_add (v->val_labs, value, label);
+  if (changed)
+    var_uncache (v);
+  return changed;
 }
 
 /* Adds or replaces a value label with the given VALUE and UTF-8 encoded LABEL
@@ -493,6 +520,7 @@ var_replace_value_label (struct variable *v,
 {
   alloc_value_labels (v);
   val_labs_replace (v->val_labs, value, label);
+  var_uncache (v);
 }
 
 /* Removes V's value labels, if any. */
@@ -581,6 +609,7 @@ var_set_print_format_quiet (struct variable *v, const struct fmt_spec *print)
     {
       assert (fmt_check_width_compat (print, v->width));
       v->print = *print;
+      var_uncache (v);
     }
 }
 
@@ -594,6 +623,7 @@ var_set_print_format (struct variable *v, const struct fmt_spec *print)
   struct variable *ov = var_clone (v);
   var_set_print_format_quiet (v, print);
   dict_var_changed (v, VAR_TRAIT_PRINT_FORMAT, ov);
+  var_uncache (v);
 }
 
 /* Returns V's write format specification. */
@@ -614,6 +644,7 @@ var_set_write_format_quiet (struct variable *v, const struct fmt_spec *write)
     {
       assert (fmt_check_width_compat (write, v->width));
       v->write = *write;
+      var_uncache (v);
     }
 }
 
@@ -711,71 +742,31 @@ var_get_label (const struct variable *v)
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
    empty string (after stripping white space), then V's variable label (if any)
-   is removed.
-
-   Variable labels are limited to 255 bytes in V's encoding (as returned by
-   var_get_encoding()).  If LABEL fits within this limit, this function returns
-   true.  Otherwise, the variable label is set to a truncated value, this
-   function returns false and, if ISSUE_WARNING is true, issues a warning.  */
-static bool
-var_set_label_quiet (struct variable *v, const char *label, bool issue_warning)
+   is removed. */
+static void
+var_set_label_quiet (struct variable *v, const char *label)
 {
-  bool truncated = false;
-
   free (v->label);
   v->label = NULL;
 
   if (label != NULL && label[strspn (label, CC_SPACES)])
-    {
-      const char *dict_encoding = var_get_encoding (v);
-      struct substring s = ss_cstr (label);
-      size_t trunc_len;
-
-      if (dict_encoding != NULL)
-        {
-          enum { MAX_LABEL_LEN = 255 };
-
-          trunc_len = utf8_encoding_trunc_len (label, dict_encoding,
-                                               MAX_LABEL_LEN);
-          if (ss_length (s) > trunc_len)
-            {
-              if (issue_warning)
-                msg (SW, _("Truncating variable label for variable `%s' to %d "
-                           "bytes."), var_get_name (v), MAX_LABEL_LEN);
-              ss_truncate (&s, trunc_len);
-              truncated = true;
-            }
-        }
-
-        v->label = ss_xstrdup (s);
-    }
+    v->label = xstrdup (label);
 
   ds_destroy (&v->name_and_label);
   ds_init_empty (&v->name_and_label);
-
-  return truncated;
+  var_uncache (v);
 }
 
-
-
 /* Sets V's variable label to UTF-8 encoded string LABEL, stripping off leading
    and trailing white space.  If LABEL is a null pointer or if LABEL is an
    empty string (after stripping white space), then V's variable label (if any)
-   is removed.
-
-   Variable labels are limited to 255 bytes in V's encoding (as returned by
-   var_get_encoding()).  If LABEL fits within this limit, this function returns
-   true.  Otherwise, the variable label is set to a truncated value, this
-   function returns false and, if ISSUE_WARNING is true, issues a warning.  */
-bool
-var_set_label (struct variable *v, const char *label, bool issue_warning)
+   is removed. */
+void
+var_set_label (struct variable *v, const char *label)
 {
   struct variable *ov = var_clone (v);
-  bool truncated = var_set_label_quiet (v, label, issue_warning);
-
+  var_set_label_quiet (v, label);
   dict_var_changed (v, VAR_TRAIT_LABEL, ov);
-
-  return truncated;
 }
 
 
@@ -783,7 +774,7 @@ var_set_label (struct variable *v, const char *label, bool issue_warning)
 void
 var_clear_label (struct variable *v)
 {
-  var_set_label (v, NULL, false);
+  var_set_label (v, NULL);
 }
 
 /* Returns true if V has a variable V,
@@ -858,7 +849,6 @@ var_set_measure_quiet (struct variable *v, enum measure measure)
   v->measure = measure;
 }
 
-
 /* Sets V's measurement level to MEASURE. */
 void
 var_set_measure (struct variable *v, enum measure measure)
@@ -888,7 +878,7 @@ var_role_is_valid (enum var_role role)
     {
     case ROLE_NONE:
     case ROLE_INPUT:
-    case ROLE_OUTPUT:
+    case ROLE_TARGET:
     case ROLE_BOTH:
     case ROLE_PARTITION:
     case ROLE_SPLIT:
@@ -908,7 +898,7 @@ var_role_to_string (enum var_role role)
     case ROLE_INPUT:
       return _("Input");
 
-    case ROLE_OUTPUT:
+    case ROLE_TARGET:
       return _("Output");
 
     case ROLE_BOTH:
@@ -937,7 +927,7 @@ var_role_to_syntax (enum var_role role)
     case ROLE_INPUT:
       return "INPUT";
 
-    case ROLE_OUTPUT:
+    case ROLE_TARGET:
       return "TARGET";
 
     case ROLE_BOTH:
@@ -1002,12 +992,14 @@ var_set_display_width_quiet (struct variable *v, int new_width)
 void
 var_set_display_width (struct variable *v, int new_width)
 {
-  struct variable *ov = var_clone (v);
-  var_set_display_width_quiet (v, new_width);
-  dict_var_changed (v, VAR_TRAIT_DISPLAY_WIDTH, ov);
+  if (v->display_width != new_width)
+    {
+      struct variable *ov = var_clone (v);
+      var_set_display_width_quiet (v, new_width);
+      dict_var_changed (v, VAR_TRAIT_DISPLAY_WIDTH, ov);
+    }
 }
 
-
 /* Returns the default display width for a variable of the given
    WIDTH, as set by var_create.  The return value can be used to
    reset a variable's display width to the default. */
@@ -1206,13 +1198,17 @@ var_set_short_name (struct variable *var, size_t idx, const char *short_name)
 void
 var_clear_short_names (struct variable *v)
 {
-  size_t i;
-
-  for (i = 0; i < v->short_name_cnt; i++)
-    free (v->short_names[i]);
-  free (v->short_names);
-  v->short_names = NULL;
-  v->short_name_cnt = 0;
+  if (v->short_name_cnt > 0)
+    {
+      size_t i;
+
+      for (i = 0; i < v->short_name_cnt; i++)
+        free (v->short_names[i]);
+      free (v->short_names);
+      v->short_names = NULL;
+      v->short_name_cnt = 0;
+      var_uncache (v);
+    }
 }
 \f
 /* Relationship with dictionary. */
@@ -1254,6 +1250,7 @@ var_set_attributes_quiet (struct variable *v, const struct attrset *attrs)
 {
   attrset_destroy (&v->attributes);
   attrset_clone (&v->attributes, attrs);
+  var_uncache (v);
 }
 
 /* Replaces variable V's attributes set by a copy of ATTRS. */
@@ -1294,7 +1291,7 @@ var_clone (const struct variable *old_var)
   var_set_print_format_quiet (new_var, var_get_print_format (old_var));
   var_set_write_format_quiet (new_var, var_get_write_format (old_var));
   var_set_value_labels_quiet (new_var, var_get_value_labels (old_var));
-  var_set_label_quiet (new_var, var_get_label (old_var), false);
+  var_set_label_quiet (new_var, var_get_label (old_var));
   var_set_measure_quiet (new_var, var_get_measure (old_var));
   var_set_role_quiet (new_var, var_get_role (old_var));
   var_set_display_width_quiet (new_var, var_get_display_width (old_var));
@@ -1345,3 +1342,125 @@ var_clear_vardict (struct variable *v)
 {
   v->vardict = NULL;
 }
+\f
+struct pxd_object *
+var_save (const struct variable *var, struct pxd *pxd)
+{
+  struct pxd_builder b;
+  size_t i;
+
+  pxd_builder_init (&b, pxd);
+
+  pxd_builder_put_string (&b, var->name);
+  pxd_builder_put_u16 (&b, var->width);
+  pxd_builder_put_link (&b, mv_save (&var->miss, pxd));
+  pxd_builder_put_u32 (&b, fmt_to_uint (&var->print));
+  pxd_builder_put_u32 (&b, fmt_to_uint (&var->write));
+
+  if (var_has_value_labels (var))
+    {
+      pxd_builder_put_bool (&b, true);
+      pxd_builder_put_link (&b, val_labs_save (var->val_labs, pxd));
+    }
+  else
+    pxd_builder_put_bool (&b, false);
+
+  pxd_builder_put_string (&b, var->label != NULL ? var->label : "");
+
+  pxd_builder_put_u8 (&b, var->measure);
+  pxd_builder_put_u32 (&b, var->display_width);
+  pxd_builder_put_u8 (&b, var->alignment);
+
+  pxd_builder_put_bool (&b, var->leave);
+
+  pxd_builder_put_size_t (&b, var->short_name_cnt);
+  for (i = 0; i < var->short_name_cnt; i++)
+    pxd_builder_put_string (&b, var->short_names[i]);
+
+  pxd_builder_put_link (&b, attrset_save (&var->attributes, pxd));
+
+  return pxd_builder_commit (&b);
+}
+
+struct variable *
+var_load (struct pxd_object *object, const struct pxd *pxd)
+{
+  struct fmt_spec print, write;
+  struct missing_values mv;
+  struct variable *var;
+  struct pxd_parser p;
+  char *label;
+  char *name;
+  int width;
+  size_t i;
+
+#if 0
+  cached_var = pxd_cache_search (&var_cache, pxd_object_id (object));
+  if (cached_var != NULL)
+    {
+      size_t n;
+
+      var = var_clone (cached_var);
+
+      /* var_clone() doesn't copy short names, so we need to. */
+      n = cached_var->short_name_cnt;
+      if (n > 0)
+        {
+          size_t i;
+
+          var->short_name_cnt = n;
+          var->short_names = xmalloc (n * sizeof *var->short_names);
+          for (i = 0; i < n; i++)
+            var->short_names[i] = xstrdup (cached_var->short_names[i]);
+        }
+
+      return var;
+    }
+#endif
+
+  pxd_parser_init (&p, object, pxd);
+
+  name = pxd_parser_get_string (&p);
+  width = pxd_parser_get_u16 (&p);
+  var = var_create (name, width);
+
+  mv_load (&mv, pxd_parser_get_link (&p), pxd);
+  var_set_missing_values (var, &mv);
+  mv_destroy (&mv);
+
+  fmt_from_uint (&print, pxd_parser_get_u32 (&p));
+  var_set_print_format (var, &print);
+
+  fmt_from_uint (&write, pxd_parser_get_u32 (&p));
+  var_set_write_format (var, &write);
+
+  if (pxd_parser_get_bool (&p))
+    var->val_labs = val_labs_load (pxd_parser_get_link (&p), pxd);
+
+  label = pxd_parser_get_string (&p);
+  if (label[0])
+    var_set_label (var, label);
+  free (label);
+
+  var->measure = pxd_parser_get_u8 (&p);
+  var->display_width = pxd_parser_get_u32 (&p);
+  var->alignment = pxd_parser_get_u8 (&p);
+
+  var->leave = pxd_parser_get_bool (&p);
+
+  var->short_name_cnt = pxd_parser_get_size_t (&p);
+  var->short_names = xmalloc (var->short_name_cnt * sizeof *var->short_names);
+  for (i = 0; i < var->short_name_cnt; i++)
+    var->short_names[i] = pxd_parser_get_string (&p);
+
+  attrset_load (&var->attributes, pxd_parser_get_link (&p), pxd);
+
+  pxd_parser_destroy (&p);
+
+  return var;
+}
+
+static void
+var_uncache (struct variable *var UNUSED)
+{
+}