pxd: initial work
[pspp] / src / data / vector.c
index fd65d284d43ea6d75d1db2885f1b9ad87d37bce9..af56427899151f7b4ec55e4cd0225340df5365b5 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2006  Free Software Foundation, Inc.
+   Copyright (C) 2006, 2010, 2011, 2012  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
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include "vector.h"
 
-#include "dictionary.h"
+#include "data/vector.h"
 
-#include <libpspp/assertion.h>
-#include <libpspp/str.h>
+#include <stdlib.h>
 
-#include "xalloc.h"
+#include "data/dictionary.h"
+#include "data/identifier.h"
+#include "data/variable.h"
+#include "libpspp/assertion.h"
+#include "libpspp/i18n.h"
+#include "libpspp/pxd.h"
+#include "libpspp/str.h"
+
+#include "gl/xalloc.h"
 
 /* Vector of variables. */
 struct vector
   {
-    char name[VAR_NAME_LEN + 1];       /* Name. */
+    char *name;                         /* Name. */
     struct variable **vars;             /* Set of variables. */
     size_t var_cnt;                     /* Number of variables. */
   };
@@ -44,19 +50,19 @@ check_widths (const struct vector *vector)
     assert (width == var_get_width (vector->vars[i]));
 }
 
-/* Creates and returns a new vector with the given NAME
+/* Creates and returns a new vector with the given UTF-8 encoded NAME
    that contains the VAR_CNT variables in VARS.
    All variables in VARS must have the same type and width. */
 struct vector *
-vector_create (const char *name,
-               struct variable **vars, size_t var_cnt)
+vector_create (const char *name, struct variable **vars, size_t var_cnt)
 {
   struct vector *vector = xmalloc (sizeof *vector);
 
   assert (var_cnt > 0);
-  assert (var_is_plausible_name (name, false));
-  str_copy_trunc (vector->name, sizeof vector->name, name);
+  assert (id_is_plausible (name, false));
+  vector->name = xstrdup (name);
 
+  vector->name = xstrdup (name);
   vector->vars = xmemdup (vars, var_cnt * sizeof *vector->vars);
   vector->var_cnt = var_cnt;
   check_widths (vector);
@@ -77,8 +83,7 @@ vector_clone (const struct vector *old,
   struct vector *new = xmalloc (sizeof *new);
   size_t i;
 
-  strcpy (new->name, old->name);
-
+  new->name = xstrdup (old->name);
   new->vars = xnmalloc (old->var_cnt, sizeof *new->vars);
   new->var_cnt = old->var_cnt;
   for (i = 0; i < new->var_cnt; i++)
@@ -96,11 +101,12 @@ vector_clone (const struct vector *old,
 void
 vector_destroy (struct vector *vector)
 {
+  free (vector->name);
   free (vector->vars);
   free (vector);
 }
 
-/* Returns VECTOR's name. */
+/* Returns VECTOR's name, as a UTF-8 encoded string. */
 const char *
 vector_get_name (const struct vector *vector)
 {
@@ -138,6 +144,44 @@ compare_vector_ptrs_by_name (const void *a_, const void *b_)
   struct vector *a = *pa;
   struct vector *b = *pb;
 
-  return strcasecmp (a->name, b->name);
+  return utf8_strcasecmp (a->name, b->name);
+}
+
+struct pxd_object *
+vector_save (const struct vector *vector, struct pxd *pxd)
+{
+  struct pxd_builder b;
+  size_t i;
+
+  pxd_builder_init (&b, pxd);
+
+  pxd_builder_put_string (&b, vector->name);
+
+  pxd_builder_put_size_t (&b, vector->var_cnt);
+  for (i = 0; i < vector->var_cnt; i++)
+    pxd_builder_put_size_t (&b, var_get_dict_index (vector->vars[i]));
+
+  return pxd_builder_commit (&b);
 }
 
+struct vector *
+vector_load (struct pxd_object *object, const struct pxd *pxd,
+             const struct dictionary *dict)
+{
+  struct pxd_parser p;
+  struct vector *vector;
+  size_t i;
+
+  vector = xzalloc (sizeof *vector);
+
+  pxd_parser_init (&p, object, pxd);
+
+  vector->name = pxd_parser_get_string (&p);
+
+  vector->var_cnt = pxd_parser_get_size_t (&p);
+  vector->vars = xmalloc (vector->var_cnt * sizeof *vector->vars);
+  for (i = 0; i < vector->var_cnt; i++)
+    vector->vars[i] = dict_get_var (dict, pxd_parser_get_size_t (&p));
+
+  return vector;
+}