Added most of the calculations for the ONEWAY command.
[pspp-builds.git] / src / pfm-write.c
index eaa5c6033e7806bf875ed9bd46b674a92c88eacb..1d553ed7875d774aa5912f07125304638371c369 100644 (file)
@@ -18,7 +18,8 @@
    02111-1307, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "pfm.h"
+#include "error.h"
 #include <ctype.h>
 #include <errno.h>
 #include <float.h>
 #include <stdlib.h>
 #include <time.h>
 #include "alloc.h"
-#include "avl.h"
 #include "error.h"
 #include "file-handle.h"
 #include "gmp.h"
+#include "hash.h"
 #include "magic.h"
-#include "pfm.h"
 #include "str.h"
+#include "value-labels.h"
 #include "var.h"
 #include "version.h"
 
-#undef DEBUGGING
-/*#define DEBUGGING 1 */
 #include "debug-print.h"
 
 /* pfm writer file_handle extension. */
@@ -71,22 +70,23 @@ pfm_write_dictionary (struct file_handle *handle, struct dictionary *dict)
     {
       msg (ME, _("Cannot write file %s as portable file: already opened "
                 "for %s."),
-          fh_handle_name (handle), handle->class->name);
+          handle_get_name (handle), handle->class->name);
       return 0;
     }
 
   msg (VM (1), _("%s: Opening portable-file handle %s for writing."),
-       fh_handle_filename (handle), fh_handle_name (handle));
+       handle_get_filename (handle), handle_get_name (handle));
   
   /* Open the physical disk file. */
   handle->class = &pfm_w_class;
   handle->ext = ext = xmalloc (sizeof (struct pfm_fhuser_ext));
-  ext->file = fopen (handle->norm_fn, "wb");
+  ext->file = fopen (handle_get_filename (handle), "wb");
   ext->lc = 0;
   if (ext->file == NULL)
     {
       msg (ME, _("An error occurred while opening \"%s\" for writing "
-          "as a portable file: %s."), handle->fn, strerror (errno));
+          "as a portable file: %s."),
+           handle_get_filename (handle), strerror (errno));
       err_cond_fail ();
       free (ext);
       return 0;
@@ -95,10 +95,10 @@ pfm_write_dictionary (struct file_handle *handle, struct dictionary *dict)
   {
     int i;
 
-    ext->nvars = dict->nvar;
-    ext->vars = xmalloc (sizeof *ext->vars * dict->nvar);
-    for (i = 0; i < dict->nvar; i++)
-      ext->vars[i] = dict->var[i]->width;
+    ext->nvars = dict_get_var_cnt (dict);
+    ext->vars = xmalloc (sizeof *ext->vars * ext->nvars);
+    for (i = 0; i < ext->nvars; i++)
+      ext->vars[i] = dict_get_var (dict, i)->width;
   }
 
   /* Write the file header. */
@@ -137,8 +137,9 @@ lossage:
 /* Write NBYTES starting at BUF to the portable file represented by
    H.  Break lines properly every 80 characters.  */
 static int
-bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
+bufwrite (struct file_handle *h, const void *buf_, size_t nbytes)
 {
+  const char *buf = buf_;
   struct pfm_fhuser_ext *ext = h->ext;
 
   assert (buf != NULL);
@@ -154,7 +155,7 @@ bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
        goto lossage;
 
       nbytes -= n;
-      *((char **) &buf) += n;
+      buf += n;
       ext->lc = 0;
     }
 
@@ -166,7 +167,8 @@ bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
 
  lossage:
   abort ();
-  msg (ME, _("%s: Writing portable file: %s."), h->fn, strerror (errno));
+  msg (ME, _("%s: Writing portable file: %s."),
+       handle_get_filename (h), strerror (errno));
   return 0;
 }
 
@@ -386,11 +388,11 @@ write_variables (struct file_handle *h, struct dictionary *dict)
 {
   int i;
   
-  if (!bufwrite (h, "4", 1) || !write_int (h, dict->nvar)
+  if (!bufwrite (h, "4", 1) || !write_int (h, dict_get_var_cnt (dict))
       || !write_int (h, 161))
     return 0;
 
-  for (i = 0; i < dict->nvar; i++)
+  for (i = 0; i < dict_get_var_cnt (dict); i++)
     {
       static const char *miss_types[MISSING_COUNT] =
        {
@@ -400,7 +402,7 @@ write_variables (struct file_handle *h, struct dictionary *dict)
       const char *m;
       int j;
 
-      struct variable *v = dict->var[i];
+      struct variable *v = dict_get_var (dict, i);
       
       if (!bufwrite (h, "7", 1) || !write_int (h, v->width)
          || !write_string (h, v->name)
@@ -425,26 +427,29 @@ write_value_labels (struct file_handle *h, struct dictionary *dict)
 {
   int i;
 
-  for (i = 0; i < dict->nvar; i++)
+  for (i = 0; i < dict_get_var_cnt (dict); i++)
     {
-      avl_traverser iter;
-      struct variable *v = dict->var[i];
-      struct value_label *vl;
+      struct val_labs_iterator *j;
+      struct variable *v = dict_get_var (dict, i);
+      struct val_lab *vl;
 
-      if (v->val_lab == NULL)
+      if (!val_labs_count (v->val_labs))
        continue;
 
       if (!bufwrite (h, "D", 1)
          || !write_int (h, 1)
          || !write_string (h, v->name)
-         || !write_int (h, avl_count (v->val_lab)))
+         || !write_int (h, val_labs_count (v->val_labs)))
        return 0;
 
-      avl_traverser_init (iter);
-      while (NULL != (vl = avl_traverse (v->val_lab, &iter)))
-       if (!write_value (h, &vl->v, v)
-           || !write_string (h, vl->s))
-         return 0;
+      for (vl = val_labs_first_sorted (v->val_labs, &j); vl != NULL;
+           vl = val_labs_next (v->val_labs, &j)) 
+       if (!write_value (h, &vl->value, v)
+           || !write_string (h, vl->label)) 
+          {
+            val_labs_done (&j);
+            return 0; 
+          }
     }
 
   return 1;
@@ -470,7 +475,7 @@ pfm_write_case (struct file_handle *h, const union value *elem)
        }
       else
        {
-         if (!write_int (h, width) || !bufwrite (h, elem->c, width))
+         if (!write_int (h, width) || !bufwrite (h, elem[i].c, width))
            return 0;
        }
     }
@@ -496,7 +501,8 @@ pfm_close (struct file_handle *h)
   }
 
   if (EOF == fclose (ext->file))
-    msg (ME, _("%s: Closing portable file: %s."), h->fn, strerror (errno));
+    msg (ME, _("%s: Closing portable file: %s."),
+         handle_get_filename (h), strerror (errno));
 
   free (ext->vars);
   free (ext);