use moments.c to compute moments
[pspp-builds.git] / src / language / stats / flip.c
index c28de4600ac055247c9dcd58ea0859ef8e642dc5..8b7e2ffec86d10d5c7774299b617fa70ecef5dbd 100644 (file)
@@ -1,6 +1,5 @@
 /* PSPP - computes sample statistics.
    Copyright (C) 1997-9, 2000 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
@@ -78,10 +77,13 @@ struct flip_pgm
     struct varname *new_names_tail; /* Last new variable. */
 
     FILE *file;                 /* Temporary file containing data. */
+    union value *input_buf;     /* Input buffer for temporary file. */
+    size_t cases_read;          /* Number of cases already read. */
+    bool error;                 /* Error reading temporary file? */
   };
 
 static void destroy_flip_pgm (struct flip_pgm *);
-static struct case_sink *flip_sink_create (struct dictionary *d, struct flip_pgm *);
+static struct case_sink *flip_sink_create (struct dataset *ds, struct flip_pgm *);
 static struct case_source *flip_source_create (struct flip_pgm *);
 static bool flip_file (struct flip_pgm *);
 static int build_dictionary (struct dictionary *, struct flip_pgm *);
@@ -112,6 +114,9 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   flip->new_names_head = NULL;
   flip->new_names_tail = NULL;
   flip->file = NULL;
+  flip->input_buf = NULL;
+  flip->cases_read = 0;
+  flip->error = false;
 
   lex_match (lexer, '/');
   if (lex_match_id (lexer, "VARIABLES"))
@@ -153,7 +158,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   /* Read the active file into a flip_sink. */
   flip->case_cnt = 0;
   proc_make_temporary_transformations_permanent (ds);
-  sink = flip_sink_create (dict, flip);
+  sink = flip_sink_create (ds, flip);
   if (sink == NULL)
     goto error;
   proc_set_sink (ds, sink);
@@ -284,7 +289,7 @@ build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
      
 /* Creates a flip sink based on FLIP. */
 static struct case_sink *
-flip_sink_create (struct dictionary *dict, struct flip_pgm *flip) 
+flip_sink_create (struct dataset *ds, struct flip_pgm *flip) 
 {
   size_t i;
 
@@ -294,7 +299,8 @@ flip_sink_create (struct dictionary *dict, struct flip_pgm *flip)
   flip->file = pool_tmpfile (flip->pool);
   if (flip->file == NULL)
     {
-      msg (SE, _("Could not create temporary file for FLIP."));
+      msg (SE, _("Could not create temporary file for FLIP: %s."),
+           strerror (errno));
       return NULL;
     }
 
@@ -311,7 +317,10 @@ flip_sink_create (struct dictionary *dict, struct flip_pgm *flip)
 
   flip->case_cnt = 1;
 
-  return create_case_sink (&flip_sink_class, dict, flip);
+  return create_case_sink (&flip_sink_class,
+                          dataset_dict (ds),
+                          dataset_get_casefile_factory (ds),
+                          flip);
 }
 
 /* Writes case C to the FLIP sink.
@@ -440,7 +449,10 @@ flip_file (struct flip_pgm *flip)
 
       if (read_cases != fread (input_buf, case_bytes, read_cases, input_file)) 
         {
-          msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
+          if (ferror (input_file))
+            msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
+          else
+            msg (SE, _("Unexpected end of file reading FLIP file.")); 
           return false;
         }
 
@@ -517,54 +529,54 @@ flip_source_create (struct flip_pgm *pgm)
   return create_case_source (&flip_source_class, pgm);
 }
 
-/* Reads the FLIP stream.  Copies each case into C and calls
-   WRITE_CASE passing WC_DATA.
-   Returns true if successful, false if an I/O error occurred. */
+/* Reads one case into C.
+   Returns true if successful, false at end of file or if an
+   I/O error occurred. */
 static bool
-flip_source_read (struct case_source *source,
-                  struct ccase *c,
-                  write_case_func *write_case, write_case_data wc_data)
+flip_source_read (struct case_source *source, struct ccase *c)
 {
   struct flip_pgm *flip = source->aux;
-  union value *input_buf;
   size_t i;
-  bool ok = true;
 
-  input_buf = xnmalloc (flip->case_cnt, sizeof *input_buf);
-  for (i = 0; ok && i < flip->var_cnt; i++)
-    {
-      size_t j;
-      
-      if (fread (input_buf, sizeof *input_buf, flip->case_cnt,
-                 flip->file) != flip->case_cnt) 
-        {
-          if (ferror (flip->file))
-            msg (SE, _("Error reading FLIP temporary file: %s."),
-                 strerror (errno));
-          else if (feof (flip->file))
-            msg (SE, _("Unexpected end of file reading FLIP temporary file."));
-          else
-            NOT_REACHED ();
-          ok = false;
-          break;
-        }
+  if (flip->error || flip->cases_read >= flip->var_cnt)
+    return false;
+  
+  if (flip->input_buf == NULL)
+    flip->input_buf = pool_nmalloc (flip->pool,
+                                    flip->case_cnt, sizeof *flip->input_buf);
 
-      for (j = 0; j < flip->case_cnt; j++)
-        case_data_rw_idx (c, j)->f = input_buf[j].f;
-      ok = write_case (wc_data);
+  if (fread (flip->input_buf, sizeof *flip->input_buf, flip->case_cnt,
+             flip->file) != flip->case_cnt) 
+    {
+      if (ferror (flip->file))
+        msg (SE, _("Error reading FLIP temporary file: %s."),
+             strerror (errno));
+      else if (feof (flip->file))
+        msg (SE, _("Unexpected end of file reading FLIP temporary file."));
+      else
+        NOT_REACHED ();
+      flip->error = true;
+      return false;
     }
-  free (input_buf);
 
-  return ok;
+  for (i = 0; i < flip->case_cnt; i++)
+    case_data_rw_idx (c, i)->f = flip->input_buf[i].f;
+
+  flip->cases_read++;
+
+  return true;
 }
 
-/* Destroy internal data in SOURCE. */
-static void
+/* Destroys the source.
+   Returns true if successful read, false if an I/O occurred
+   during destruction or previously. */
+static bool
 flip_source_destroy (struct case_source *source)
 {
   struct flip_pgm *flip = source->aux;
-
+  bool ok = !flip->error;
   destroy_flip_pgm (flip);
+  return ok;
 }
 
 static const struct case_source_class flip_source_class =