/* 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
#include <libpspp/str.h>
#include "intprops.h"
+#include "minmax.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
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 *);
/* Parses and executes FLIP. */
int
-cmd_flip (struct dataset *ds)
+cmd_flip (struct lexer *lexer, struct dataset *ds)
{
struct flip_pgm *flip;
struct case_sink *sink;
flip = pool_create_container (struct flip_pgm, pool);
flip->var = NULL;
- flip->idx_to_fv = dict_get_compacted_idx_to_fv (dict);
+ flip->idx_to_fv = dict_get_compacted_dict_index_to_case_index (dict);
pool_register (flip->pool, free, flip->idx_to_fv);
flip->var_cnt = 0;
flip->case_cnt = 0;
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 ('/');
- if (lex_match_id ("VARIABLES"))
+ lex_match (lexer, '/');
+ if (lex_match_id (lexer, "VARIABLES"))
{
- lex_match ('=');
- if (!parse_variables (dict, &flip->var, &flip->var_cnt,
+ lex_match (lexer, '=');
+ if (!parse_variables (lexer, dict, &flip->var, &flip->var_cnt,
PV_NO_DUPLICATE))
goto error;
- lex_match ('/');
+ lex_match (lexer, '/');
}
else
dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
pool_register (flip->pool, free, flip->var);
- lex_match ('/');
- if (lex_match_id ("NEWNAMES"))
+ lex_match (lexer, '/');
+ if (lex_match_id (lexer, "NEWNAMES"))
{
- lex_match ('=');
- flip->new_names = parse_variable (dict);
+ lex_match (lexer, '=');
+ flip->new_names = parse_variable (lexer, dict);
if (!flip->new_names)
goto error;
}
/* 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);
/* Set up flipped data for reading. */
proc_set_source (ds, flip_source_create (flip));
- return ok ? lex_end_of_command () : CMD_CASCADING_FAILURE;
+ return ok ? lex_end_of_command (lexer) : CMD_CASCADING_FAILURE;
error:
destroy_flip_pgm (flip);
for (i = 1; i < 10000000; i++)
{
- int ofs = min (7 - intlog10 (i), len);
+ int ofs = MIN (7 - intlog10 (i), len);
memcpy (n, name, ofs);
sprintf (&n[ofs], "%d", i);
/* 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->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;
}
/* Write variable names as first case. */
for (i = 0; i < flip->var_cnt; i++)
buf_copy_str_rpad (flip->output_buf[i].s, MAX_SHORT_STRING,
- flip->var[i]->name);
+ var_get_name (flip->var[i]));
if (fwrite (flip->output_buf, sizeof *flip->output_buf,
flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
{
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.
if (flip->new_names != NULL)
{
struct varname *v = pool_alloc (flip->pool, sizeof *v);
+ int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)];
v->next = NULL;
- if (flip->new_names->type == NUMERIC)
+ if (var_is_numeric (flip->new_names))
{
- double f = case_num (c, flip->idx_to_fv[flip->new_names->index]);
+ double f = case_num_idx (c, fv);
if (f == SYSMIS)
strcpy (v->name, "VSYSMIS");
}
else
{
- int width = min (flip->new_names->width, MAX_SHORT_STRING);
- memcpy (v->name, case_str (c, flip->idx_to_fv[flip->new_names->index]),
- width);
+ int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
+ memcpy (v->name, case_str_idx (c, fv), width);
v->name[width] = 0;
}
{
double out;
- if (flip->var[i]->type == NUMERIC)
- out = case_num (c, flip->idx_to_fv[flip->var[i]->index]);
+ if (var_is_numeric (flip->var[i]))
+ {
+ int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])];
+ out = case_num_idx (c, fv);
+ }
else
out = SYSMIS;
flip->output_buf[i].f = out;
for (case_idx = 0; case_idx < flip->case_cnt; )
{
- unsigned long read_cases = min (flip->case_cnt - case_idx,
+ unsigned long read_cases = MIN (flip->case_cnt - case_idx,
case_capacity);
size_t i;
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;
}
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 (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 =