/* 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
#include "intprops.h"
#include "size_max.h"
-#include <data/case.h>
+#include <data/casegrouper.h>
+#include <data/casereader.h>
#include <data/dictionary.h>
#include <data/data-out.h>
#include <data/format.h>
/* Parsed command. */
static struct cmd_list cmd;
-/* Current case number. */
-static int case_idx;
-
/* Line buffer. */
static struct string line_buffer;
static void write_line (struct outp_driver *d, const char *s);
/* Other functions. */
-static bool list_cases (const struct ccase *, void *, const struct dataset *);
+static void list_case (struct ccase *, casenumber case_idx,
+ const struct dataset *);
static void determine_layout (void);
static void clean_up (void);
static void write_header (struct outp_driver *);
-static void write_all_headers (const struct ccase *, void *, const struct dataset*);
+static void write_all_headers (struct casereader *, const struct dataset*);
/* Returns the number of text lines that can fit on the remainder of
the page. */
int
cmd_list (struct lexer *lexer, struct dataset *ds)
{
- struct variable casenum_var;
+ struct dictionary *dict = dataset_dict (ds);
+ struct variable *casenum_var = NULL;
+ struct casegrouper *grouper;
+ struct casereader *group;
+ casenumber case_idx;
bool ok;
if (!parse_list (lexer, ds, &cmd, NULL))
if (cmd.last == NOT_LONG)
cmd.last = LONG_MAX;
if (!cmd.sbc_variables)
- dict_get_vars (dataset_dict (ds), &cmd.v_variables, &cmd.n_variables,
+ dict_get_vars (dict, &cmd.v_variables, &cmd.n_variables,
(1u << DC_SYSTEM) | (1u << DC_SCRATCH));
if (cmd.n_variables == 0)
{
/* Weighting variable. */
if (cmd.weight == LST_WEIGHT)
{
- if (dict_get_weight (dataset_dict (ds)) != NULL)
+ if (dict_get_weight (dict) != NULL)
{
size_t i;
for (i = 0; i < cmd.n_variables; i++)
- if (cmd.v_variables[i] == dict_get_weight (dataset_dict (ds)))
+ if (cmd.v_variables[i] == dict_get_weight (dict))
break;
if (i >= cmd.n_variables)
{
cmd.v_variables = xnrealloc (cmd.v_variables, cmd.n_variables,
sizeof *cmd.v_variables);
cmd.v_variables[cmd.n_variables - 1]
- = dict_get_weight (dataset_dict (ds));
+ = dict_get_weight (dict);
}
}
else
/* Initialize the case-number variable. */
int width = cmd.last == LONG_MAX ? 5 : intlog10 (cmd.last);
struct fmt_spec format = fmt_for_output (FMT_F, width, 0);
- var_set_name (&casenum_var, "Case#");
- casenum_var.width = 0;
- casenum_var.fv = -1;
- var_set_both_formats (&casenum_var, &format);
+ casenum_var = var_create ("Case#", 0);
+ var_set_both_formats (casenum_var, &format);
/* Add the weight variable at the beginning of the variable list. */
cmd.n_variables++;
cmd.n_variables, sizeof *cmd.v_variables);
memmove (&cmd.v_variables[1], &cmd.v_variables[0],
(cmd.n_variables - 1) * sizeof *cmd.v_variables);
- cmd.v_variables[0] = &casenum_var;
+ cmd.v_variables[0] = casenum_var;
}
determine_layout ();
case_idx = 0;
- ok = procedure_with_splits (ds, write_all_headers, list_cases, NULL, NULL);
+ for (grouper = casegrouper_create_splits (proc_open (ds), dict);
+ casegrouper_get_next_group (grouper, &group);
+ casereader_destroy (group))
+ {
+ struct ccase c;
+
+ write_all_headers (group, ds);
+ for (; casereader_read (group, &c); case_destroy (&c))
+ {
+ case_idx++;
+ if (case_idx >= cmd.first && case_idx <= cmd.last
+ && (case_idx - cmd.first) % cmd.step == 0)
+ list_case (&c, case_idx, ds);
+ }
+ }
+ ok = casegrouper_destroy (grouper);
+ ok = proc_commit (ds) && ok;
+
ds_destroy(&line_buffer);
clean_up ();
+ var_destroy (casenum_var);
+
return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
}
/* Writes headers to all devices. This is done at the beginning of
each SPLIT FILE group. */
static void
-write_all_headers (const struct ccase *c, void *aux UNUSED, const struct dataset *ds)
+write_all_headers (struct casereader *input, const struct dataset *ds)
{
struct outp_driver *d;
+ struct ccase c;
+
+ if (!casereader_peek (input, 0, &c))
+ return;
+ output_split_file_values (ds, &c);
+ case_destroy (&c);
- output_split_file_values (ds, c);
for (d = outp_drivers (NULL); d; d = outp_drivers (d))
{
if (!d->class->special)
/* Put in vertical names. */
for (i = x = 0; i < prc->n_vertical; i++)
{
- struct variable *v = cmd.v_variables[i];
+ const struct variable *v = cmd.v_variables[i];
const char *name = var_get_name (v);
size_t name_len = strlen (name);
const struct fmt_spec *print = var_get_print_format (v);
/* Put in horizontal names. */
for (; i < cmd.n_variables; i++)
{
- struct variable *v = cmd.v_variables[i];
+ const struct variable *v = cmd.v_variables[i];
const char *name = var_get_name (v);
size_t name_len = strlen (name);
const struct fmt_spec *print = var_get_print_format (v);
/* Try layout #1. */
for (width = cmd.n_variables - 1, column = 0; column < cmd.n_variables; column++)
{
- struct variable *v = cmd.v_variables[column];
+ const struct variable *v = cmd.v_variables[column];
int fmt_width = var_get_print_format (v)->w;
int name_len = strlen (var_get_name (v));
width += MAX (fmt_width, name_len);
column < cmd.n_variables && width <= max_width;
column++)
{
- struct variable *v = cmd.v_variables[column];
+ const struct variable *v = cmd.v_variables[column];
int fmt_width = var_get_print_format (v)->w;
size_t name_len = strlen (var_get_name (v));
width += fmt_width;
#endif
for (column = cmd.n_variables; column-- != 0; )
{
- struct variable *v = cmd.v_variables[column];
+ const struct variable *v = cmd.v_variables[column];
int name_len = strlen (var_get_name (v));
int fmt_width = var_get_print_format (v)->w;
int trial_width = width - fmt_width + MAX (fmt_width, name_len);
column < prc->n_vertical;
column++)
{
- struct variable *var = cmd.v_variables[column];
+ const struct variable *var = cmd.v_variables[column];
size_t name_len = strlen (var_get_name (var));
prc->header_rows = MAX (prc->header_rows, name_len);
}
}
/* Writes case C to output. */
-static bool
-list_cases (const struct ccase *c, void *aux UNUSED, const struct dataset *ds UNUSED)
+static void
+list_case (struct ccase *c, casenumber case_idx, const struct dataset *ds)
{
+ struct dictionary *dict = dataset_dict (ds);
struct outp_driver *d;
- case_idx++;
- if (case_idx < cmd.first || case_idx > cmd.last
- || (cmd.step != 1 && (case_idx - cmd.first) % cmd.step))
- return true;
-
for (d = outp_drivers (NULL); d; d = outp_drivers (d))
if (d->class->special == 0)
{
for (column = 0; column < cmd.n_variables; column++)
{
- struct variable *v = cmd.v_variables[column];
+ const struct variable *v = cmd.v_variables[column];
const struct fmt_spec *print = var_get_print_format (v);
int width;
if (width > print->w)
ds_put_char_multiple(&line_buffer, ' ', width - print->w);
- if (fmt_is_string (print->type) || v->fv != -1)
+ if (fmt_is_string (print->type)
+ || dict_contains_var (dict, v))
{
- data_out (case_data (c, v->fv), print,
+ data_out (case_data (c, v), print,
ds_put_uninit (&line_buffer, print->w));
}
else
for (column = 0; column < cmd.n_variables; column++)
{
- struct variable *v = cmd.v_variables[column];
+ const struct variable *v = cmd.v_variables[column];
const struct fmt_spec *print = var_get_print_format (v);
char buf[256];
- if (fmt_is_string (print->type) || v->fv != -1)
- data_out (case_data (c, v->fv), print, buf);
+ if (fmt_is_string (print->type)
+ || dict_contains_var (dict, v))
+ data_out (case_data (c, v), print, buf);
else
{
union value case_idx_value;
}
else
NOT_REACHED ();
-
- return true;
}
/*