/* 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
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 *);
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"))
/* 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);
/* 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;
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.
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 =