Actually implement the new procedure code and adapt all of its clients
[pspp-builds.git] / src / data / sys-file-writer.c
index ddfb4ae75c47288ec4d4ff2683c89ed76c2091fd..c7b988da04b4497e988bae9b8fa734f157d3c681 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
-   Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -20,7 +19,6 @@
 #include <config.h>
 
 #include "sys-file-writer.h"
-#include "sfm-private.h"
 #include "sys-file-private.h"
 
 #include <ctype.h>
 #include <libpspp/str.h>
 #include <libpspp/version.h>
 
-#include "case.h"
-#include "dictionary.h"
-#include "file-handle-def.h"
-#include "format.h"
-#include "missing-values.h"
-#include "settings.h"
-#include "value-labels.h"
-#include "variable.h"
+#include <data/case.h>
+#include <data/casewriter-provider.h>
+#include <data/casewriter.h>
+#include <data/dictionary.h>
+#include <data/file-handle-def.h>
+#include <data/format.h>
+#include <data/missing-values.h>
+#include <data/settings.h>
+#include <data/value-labels.h>
+#include <data/variable.h>
 
-#include "stat-macros.h"
 #include "minmax.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
+/* Find 64-bit floating-point type. */
+#if SIZEOF_FLOAT == 8
+  #define flt64 float
+  #define FLT64_MAX FLT_MAX
+#elif SIZEOF_DOUBLE == 8
+  #define flt64 double
+  #define FLT64_MAX DBL_MAX
+#elif SIZEOF_LONG_DOUBLE == 8
+  #define flt64 long double
+  #define FLT64_MAX LDBL_MAX
+#else
+  #error Which one of your basic types is 64-bit floating point?
+#endif
+
+/* Figure out SYSMIS value for flt64. */
+#include <libpspp/magic.h>
+#if SIZEOF_DOUBLE == 8
+#define second_lowest_flt64 second_lowest_value
+#else
+#error Must define second_lowest_flt64 for your architecture.
+#endif
+
+/* Record Type 1: General Information. */
+struct sysfile_header
+  {
+    char rec_type[4] ;         /* 00: Record-type code, "$FL2". */
+    char prod_name[60] ;       /* 04: Product identification. */
+    int32_t layout_code ;      /* 40: 2. */
+    int32_t nominal_case_size ;        /* 44: Number of `value's per case. 
+                                  Note: some systems set this to -1 */
+    int32_t compress ;         /* 48: 1=compressed, 0=not compressed. */
+    int32_t weight_idx ;         /* 4c: 1-based index of weighting var, or 0. */
+    int32_t case_cnt ;         /* 50: Number of cases, -1 if unknown. */
+    flt64 bias ;               /* 54: Compression bias (100.0). */
+    char creation_date[9] ;    /* 5c: `dd mmm yy' creation date of file. */
+    char creation_time[8] ;    /* 65: `hh:mm:ss' 24-hour creation time. */
+    char file_label[64] ;      /* 6d: File label. */
+    char padding[3] ;          /* ad: Ignored padding. */
+  } ATTRIBUTE((packed)) ;
+
+/* Record Type 2: Variable. */
+struct sysfile_variable
+  {
+    int32_t rec_type ;         /* 2. */
+    int32_t type ;             /* 0=numeric, 1-255=string width,
+                                  -1=continued string. */
+    int32_t has_var_label ;    /* 1=has a variable label, 0=doesn't. */
+    int32_t n_missing_values ; /* Missing value code of -3,-2,0,1,2, or 3. */
+    int32_t print ;            /* Print format. */
+    int32_t write ;            /* Write format. */
+    char name[SHORT_NAME_LEN] ; /* Variable name. */
+    /* The rest of the structure varies. */
+  } ATTRIBUTE((packed)) ;
+
 /* Compression bias used by PSPP.  Values between (1 -
    COMPRESSION_BIAS) and (251 - COMPRESSION_BIAS) inclusive can be
    compressed. */
@@ -93,6 +146,8 @@ struct sfm_var
     size_t flt64_cnt;           /* Number of flt64 elements. */
   };
 
+static struct casewriter_class sys_file_casewriter_class;
+
 static char *append_string_max (char *, const char *, const char *);
 static void write_header (struct sfm_writer *, const struct dictionary *);
 static void buf_write (struct sfm_writer *, const void *, size_t);
@@ -113,6 +168,9 @@ static void write_variable_display_parameters (struct sfm_writer *w,
 
 static void write_documents (struct sfm_writer *, const struct dictionary *);
 
+bool write_error (const struct sfm_writer *);
+bool close_writer (struct sfm_writer *);
+
 static inline int
 var_flt64_cnt (const struct variable *v) 
 {
@@ -168,7 +226,7 @@ cont_var_name(const char *sn, int idx)
    No reference to D is retained, so it may be modified or
    destroyed at will after this function returns.  D is not
    modified by this function, except to assign short names. */
-struct sfm_writer *
+struct casewriter *
 sfm_open_writer (struct file_handle *fh, struct dictionary *d,
                  struct sfm_write_options opts)
 {
@@ -323,13 +381,13 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
       w->y = (unsigned char *) w->ptr;
     }
 
-  if (sfm_write_error (w))
+  if (write_error (w))
     goto error;
   
-  return w;
+  return casewriter_create (&sys_file_casewriter_class, w);
 
  error:
-  sfm_close_writer (w);
+  close_writer (w);
   return NULL;
 
  open_error:
@@ -611,16 +669,15 @@ write_documents (struct sfm_writer *w, const struct dictionary *d)
     int32_t n_lines ;          /* Number of lines of documents. */
   } ATTRIBUTE((packed)) rec_6;
 
-  const char *documents;
-  size_t n_lines;
+  const char * documents = dict_get_documents (d);
+  size_t doc_bytes = strlen (documents);
 
-  documents = dict_get_documents (d);
-  n_lines = strlen (documents) / 80;
+  assert (doc_bytes % 80 == 0);
 
   rec_6.rec_type = 6;
-  rec_6.n_lines = n_lines;
+  rec_6.n_lines = doc_bytes / 80;
   buf_write (w, &rec_6, sizeof rec_6);
-  buf_write (w, documents, 80 * n_lines);
+  buf_write (w, documents, 80 * rec_6.n_lines);
 }
 
 /* Write the alignment, width and scale values */
@@ -657,9 +714,13 @@ write_variable_display_parameters (struct sfm_writer *w,
 
       v = dict_get_var(dict, i);
 
-      params.measure = var_get_measure (v);
+      params.measure = (var_get_measure (v) == MEASURE_NOMINAL ? 1
+                        : var_get_measure (v) == MEASURE_ORDINAL ? 2
+                        : 3);
       params.width = var_get_display_width (v);
-      params.align = var_get_alignment (v);
+      params.align = (var_get_alignment (v) == ALIGN_LEFT ? 0
+                      : var_get_alignment (v) == ALIGN_RIGHT ? 1
+                      : 2);
       
       buf_write (w, &params, sizeof(params));
 
@@ -871,13 +932,18 @@ ensure_buf_space (struct sfm_writer *w)
 
 static void write_compressed_data (struct sfm_writer *w, const flt64 *elem);
 
-/* Writes case C to system file W.
-   Returns 1 if successful, 0 if an I/O error occurred. */
-int
-sfm_write_case (struct sfm_writer *w, const struct ccase *c)
+/* Writes case C to system file W. */
+static void
+sys_file_casewriter_write (struct casewriter *writer, void *w_,
+                           struct ccase *c)
 {
-  if (ferror (w->file))
-    return 0;
+  struct sfm_writer *w = w_;
+  if (ferror (w->file)) 
+    {
+      casewriter_force_error (writer);
+      case_destroy (c);
+      return; 
+    }
   
   w->case_cnt++;
 
@@ -936,8 +1002,16 @@ sfm_write_case (struct sfm_writer *w, const struct ccase *c)
 
       local_free (bounce); 
     }
-  
-  return !sfm_write_error (w);
+
+  case_destroy (c);
+}
+
+static void
+sys_file_casewriter_destroy (struct casewriter *writer, void *w_) 
+{
+  struct sfm_writer *w = w_;
+  if (!close_writer (w))
+    casewriter_force_error (writer);
 }
 
 static void
@@ -1003,7 +1077,7 @@ write_compressed_data (struct sfm_writer *w, const flt64 *elem)
 
 /* Returns true if an I/O error has occurred on WRITER, false otherwise. */
 bool
-sfm_write_error (const struct sfm_writer *writer)
+write_error (const struct sfm_writer *writer)
 {
   return ferror (writer->file);
 }
@@ -1011,7 +1085,7 @@ sfm_write_error (const struct sfm_writer *writer)
 /* Closes a system file after we're done with it.
    Returns true if successful, false if an I/O error occurred. */
 bool
-sfm_close_writer (struct sfm_writer *w)
+close_writer (struct sfm_writer *w)
 {
   bool ok;
   
@@ -1029,7 +1103,7 @@ sfm_close_writer (struct sfm_writer *w)
         }
       fflush (w->file);
 
-      ok = !sfm_write_error (w);
+      ok = !write_error (w);
 
       /* Seek back to the beginning and update the number of cases.
          This is just a courtesy to later readers, so there's no need
@@ -1058,3 +1132,10 @@ sfm_close_writer (struct sfm_writer *w)
 
   return ok;
 }
+\f
+static struct casewriter_class sys_file_casewriter_class = 
+  {
+    sys_file_casewriter_write,
+    sys_file_casewriter_destroy,
+    NULL,
+  };