#include <language/data-io/data-reader.h>
#include <language/data-io/file-handle.h>
#include <language/lexer/lexer.h>
+#include <language/lexer/variable-parser.h>
#include <libpspp/alloc.h>
#include <libpspp/array.h>
+#include <libpspp/assertion.h>
#include <libpspp/compiler.h>
#include <libpspp/message.h>
#include <libpspp/message.h>
};
/* Name of each content type. */
-static const char *content_names[PROX + 1] =
+static const char *const content_names[PROX + 1] =
{
"N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
"DFE", "MAT", "COV", "CORR", "PROX",
/* Continuous variables. */
int n_continuous; /* Number of continuous variables. */
- int first_continuous; /* Index into default_dict.var of
+ int first_continuous; /* Index into dictionary of
first continuous variable. */
};
static int compare_variables_by_mxd_var_type (const void *pa,
const void *pb);
-static bool read_matrices_without_rowtype (struct matrix_data_pgm *);
-static bool read_matrices_with_rowtype (struct matrix_data_pgm *);
+static bool read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *);
+static bool read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *);
static int string_to_content_type (char *, int *);
static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
int
-cmd_matrix_data (void)
+cmd_matrix_data (struct dataset *ds)
{
struct pool *pool;
struct matrix_data_pgm *mx;
unsigned seen = 0;
- discard_variables ();
+ discard_variables (ds);
pool = pool_create ();
mx = pool_alloc (pool, sizeof *mx);
if (strcasecmp (v[i], "ROWTYPE_"))
{
- new_var = dict_create_var_assert (default_dict, v[i], 0);
+ new_var = dict_create_var_assert (dataset_dict (ds), v[i], 0);
attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
}
else
free (v);
}
- mx->rowtype_ = dict_create_var_assert (default_dict,
+ mx->rowtype_ = dict_create_var_assert (dataset_dict (ds),
"ROWTYPE_", 8);
attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
}
goto lossage;
}
- if (dict_lookup_var (default_dict, tokid) == NULL
+ if (dict_lookup_var (dataset_dict (ds), tokid) == NULL
&& (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
{
if (!strcasecmp (tokid, "ROWTYPE_")
goto lossage;
}
- mx->single_split = dict_create_var_assert (default_dict,
+ mx->single_split = dict_create_var_assert (dataset_dict (ds),
tokid, 0);
attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
lex_get ();
- dict_set_split_vars (default_dict, &mx->single_split, 1);
+ dict_set_split_vars (dataset_dict (ds), &mx->single_split, 1);
}
else
{
struct variable **split;
size_t n;
- if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
+ if (!parse_variables (dataset_dict (ds), &split, &n, PV_NO_DUPLICATE))
goto lossage;
- dict_set_split_vars (default_dict, split, n);
+ dict_set_split_vars (dataset_dict (ds), split, n);
}
{
- struct variable *const *split = dict_get_split_vars (default_dict);
- size_t split_cnt = dict_get_split_cnt (default_dict);
+ struct variable *const *split = dict_get_split_vars (dataset_dict (ds));
+ size_t split_cnt = dict_get_split_cnt (dataset_dict (ds));
int i;
for (i = 0; i < split_cnt; i++)
}
seen |= 4;
- if (!parse_variables (default_dict, &mx->factors, &mx->n_factors,
+ if (!parse_variables (dataset_dict (ds), &mx->factors, &mx->n_factors,
PV_NONE))
goto lossage;
}
/* Create VARNAME_. */
- mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
+ mx->varname_ = dict_create_var_assert (dataset_dict (ds), "VARNAME_", 8);
attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
/* Sort the dictionary variables into the desired order for the
struct variable **v;
size_t nv;
- dict_get_vars (default_dict, &v, &nv, 0);
+ dict_get_vars (dataset_dict (ds), &v, &nv, 0);
qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
- dict_reorder_vars (default_dict, v, nv);
+ dict_reorder_vars (dataset_dict (ds), v, nv);
free (v);
}
int i;
mx->first_continuous = -1;
- for (i = 0; i < dict_get_var_cnt (default_dict); i++)
+ for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
{
- struct variable *v = dict_get_var (default_dict, i);
+ struct variable *v = dict_get_var (dataset_dict (ds), i);
struct mxd_var *mv = v->aux;
int type = mv->var_type;
goto lossage;
if (mx->explicit_rowtype)
- ok = read_matrices_with_rowtype (mx);
+ ok = read_matrices_with_rowtype (ds, mx);
else
- ok = read_matrices_without_rowtype (mx);
+ ok = read_matrices_without_rowtype (ds, mx);
dfm_close_reader (mx->reader);
return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
lossage:
- discard_variables ();
+ discard_variables (ds);
free (mx->factors);
pool_destroy (mx->container);
return CMD_CASCADING_FAILURE;
printf (" '%.*s'", token->length, token->string);
break;
default:
- assert (0);
+ NOT_REACHED ();
}
fflush (stdout);
}
}
/* Is there at least one token left in the data file? */
-static int
+static bool
another_token (struct dfm_reader *reader)
{
for (;;)
size_t space_cnt;
if (dfm_eof (reader))
- return 0;
+ return false;
p = dfm_get_record (reader);
space_cnt = ss_span (p, ss_cstr (CC_SPACES));
if (space_cnt < ss_length (p))
{
dfm_forward_columns (reader, space_cnt);
- return 1;
+ return true;
}
dfm_forward_record (reader);
}
+ NOT_REACHED();
}
/* Parse a MATRIX DATA token from READER into TOKEN. */
struct nr_aux_data
{
+ const struct dictionary *dict; /* The dictionary */
struct matrix_data_pgm *mx; /* MATRIX DATA program. */
double ***data; /* MATRIX DATA data. */
double *factor_values; /* Factor values. */
double *split_values; /* SPLIT FILE variable values. */
};
-static int nr_read_splits (struct nr_aux_data *, int compare);
-static int nr_read_factors (struct nr_aux_data *, int cell);
+static bool nr_read_splits (struct nr_aux_data *, int compare);
+static bool nr_read_factors (struct nr_aux_data *, int cell);
static bool nr_output_data (struct nr_aux_data *, struct ccase *,
write_case_func *, write_case_data);
static bool matrix_data_read_without_rowtype (struct case_source *source,
/* Read from the data file and write it to the active file.
Returns true if successful, false if an I/O error occurred. */
static bool
-read_matrices_without_rowtype (struct matrix_data_pgm *mx)
+read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
{
struct nr_aux_data nr;
bool ok;
mx->cells = 1;
nr.mx = mx;
+ nr.dict = dataset_dict (ds);
nr.data = NULL;
nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
sizeof *nr.factor_values);
nr.max_cell_idx = 0;
- nr.split_values = xnmalloc (dict_get_split_cnt (default_dict),
+ nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (ds)),
sizeof *nr.split_values);
- proc_set_source (create_case_source (
+ proc_set_source (ds, create_case_source (
&matrix_data_without_rowtype_source_class, &nr));
- ok = procedure (NULL, NULL);
+ ok = procedure (ds, NULL, NULL);
free (nr.split_values);
free (nr.factor_values);
n_cols = mx->n_continuous;
break;
default:
- assert (0);
- abort ();
+ NOT_REACHED ();
}
break;
case 2:
n_cols = 1;
break;
default:
- assert (0);
- abort ();
+ NOT_REACHED ();
}
{
if (token.type != MNUM)
{
msg (SE, _("expecting value for %s %s"),
- dict_get_var (default_dict, j)->name,
+ dict_get_var (nr->dict, j)->name,
context (mx->reader));
return 0;
}
if (!nr_output_data (nr, c, write_case, wc_data))
return false;
- if (dict_get_split_cnt (default_dict) == 0
+ if (dict_get_split_cnt (nr->dict) == 0
|| !another_token (mx->reader))
return true;
}
}
/* Read the split file variables. If COMPARE is 1, compares the
- values read to the last values read and returns 1 if they're equal,
- 0 otherwise. */
-static int
+ values read to the last values read and returns true if they're equal,
+ false otherwise. */
+static bool
nr_read_splits (struct nr_aux_data *nr, int compare)
{
struct matrix_data_pgm *mx = nr->mx;
if (compare && just_read)
{
just_read = 0;
- return 1;
+ return true;
}
- if (dict_get_split_vars (default_dict) == NULL)
- return 1;
+ if (dict_get_split_vars (nr->dict) == NULL)
+ return true;
if (mx->single_split)
{
if (!compare)
{
- struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
+ struct mxd_var *mv = dict_get_split_vars (nr->dict)[0]->aux;
nr->split_values[0] = ++mv->sub_type;
}
- return 1;
+ return true;
}
if (!compare)
just_read = 1;
- split_cnt = dict_get_split_cnt (default_dict);
+ split_cnt = dict_get_split_cnt (nr->dict);
for (i = 0; i < split_cnt; i++)
{
struct matrix_token token;
if (!mget_token (&token, mx->reader))
- return 0;
+ return false;
if (token.type != MNUM)
{
msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
context (mx->reader));
- return 0;
+ return false;
}
if (!compare)
{
msg (SE, _("Expecting value %g for %s."),
nr->split_values[i],
- dict_get_split_vars (default_dict)[i]->name);
- return 0;
+ dict_get_split_vars (nr->dict)[i]->name);
+ return false;
}
}
- return 1;
+ return true;
}
/* Read the factors for cell CELL. If COMPARE is 1, compares the
- values read to the last values read and returns 1 if they're equal,
- 0 otherwise. */
-static int
+ values read to the last values read and returns true if they're equal,
+ false otherwise. */
+static bool
nr_read_factors (struct nr_aux_data *nr, int cell)
{
struct matrix_data_pgm *mx = nr->mx;
- int compare;
+ bool compare;
if (mx->n_factors == 0)
- return 1;
+ return true;
assert (nr->max_cell_idx >= cell);
if (cell != nr->max_cell_idx)
- compare = 1;
+ compare = true;
else
{
- compare = 0;
+ compare = false;
nr->max_cell_idx++;
}
{
struct matrix_token token;
if (!mget_token (&token, mx->reader))
- return 0;
+ return false;
if (token.type != MNUM)
{
msg (SE, _("Syntax error expecting factor value %s."),
context (mx->reader));
- return 0;
+ return false;
}
if (!compare)
msg (SE, _("Syntax error expecting value %g for %s %s."),
nr->factor_values[i + mx->n_factors * cell],
mx->factors[i]->name, context (mx->reader));
- return 0;
+ return false;
}
}
}
- return 1;
+ return true;
}
/* Write the contents of a cell having content type CONTENT and data
CP to the active file.
Returns true if successful, false if an I/O error occurred. */
static bool
-dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
+dump_cell_content (const struct dictionary *dict,
+ struct matrix_data_pgm *mx, int content, double *cp,
struct ccase *c,
write_case_func *write_case, write_case_data wc_data)
{
for (j = 0; j < mx->n_continuous; j++)
{
- int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
+ int fv = dict_get_var (dict, mx->first_continuous + j)->fv;
case_data_rw (c, fv)->f = *cp;
cp++;
}
if (type == 1)
buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
- dict_get_var (default_dict,
+ dict_get_var (dict,
mx->first_continuous + i)->name);
if (!write_case (wc_data))
return false;
size_t split_cnt;
size_t i;
- split_cnt = dict_get_split_cnt (default_dict);
- split = dict_get_split_vars (default_dict);
+ split_cnt = dict_get_split_cnt (nr->dict);
+ split = dict_get_split_vars (nr->dict);
for (i = 0; i < split_cnt; i++)
case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
}
assert (nr->data[content] != NULL
&& nr->data[content][cell] != NULL);
- if (!dump_cell_content (mx, content, nr->data[content][cell],
+ if (!dump_cell_content (nr->dict, mx,
+ content, nr->data[content][cell],
c, write_case, wc_data))
return false;
}
for (content = 0; content <= PROX; content++)
if (!mx->is_per_factor[content] && nr->data[content] != NULL)
{
- if (!dump_cell_content (mx, content, nr->data[content][0],
+ if (!dump_cell_content (nr->dict, mx, content, nr->data[content][0],
c, write_case, wc_data))
return false;
}
/* With ROWTYPE_ auxiliary data. */
struct wr_aux_data
{
+ const struct dictionary *dict; /* The dictionary */
struct matrix_data_pgm *mx; /* MATRIX DATA program. */
int content; /* Type of current row. */
double *split_values; /* SPLIT FILE variable values. */
struct factor_data *current; /* Current factor. */
};
-static int wr_read_splits (struct wr_aux_data *, struct ccase *,
+static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
write_case_func *, write_case_data);
static bool wr_output_data (struct wr_aux_data *, struct ccase *,
write_case_func *, write_case_data);
-static int wr_read_rowtype (struct wr_aux_data *,
+static bool wr_read_rowtype (struct wr_aux_data *,
const struct matrix_token *, struct dfm_reader *);
-static int wr_read_factors (struct wr_aux_data *);
-static int wr_read_indeps (struct wr_aux_data *);
+static bool wr_read_factors (struct wr_aux_data *);
+static bool wr_read_indeps (struct wr_aux_data *);
static bool matrix_data_read_with_rowtype (struct case_source *,
struct ccase *,
write_case_func *,
them to the output file.
Returns true if successful, false if an I/O error occurred. */
static bool
-read_matrices_with_rowtype (struct matrix_data_pgm *mx)
+read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
{
struct wr_aux_data wr;
bool ok;
wr.split_values = NULL;
wr.data = NULL;
wr.current = NULL;
+ wr.dict = dataset_dict (ds);
mx->cells = 0;
- proc_set_source (create_case_source (&matrix_data_with_rowtype_source_class,
+ proc_set_source (ds,
+ create_case_source (&matrix_data_with_rowtype_source_class,
&wr));
- ok = procedure (NULL, NULL);
+ ok = procedure (ds, NULL, NULL);
free (wr.split_values);
return ok;
/* Read the split file variables. If they differ from the previous
set of split variables then output the data. Returns success. */
-static int
+static bool
wr_read_splits (struct wr_aux_data *wr,
struct ccase *c,
write_case_func *write_case, write_case_data wc_data)
{
struct matrix_data_pgm *mx = wr->mx;
- int compare;
+ bool compare;
size_t split_cnt;
- split_cnt = dict_get_split_cnt (default_dict);
+ split_cnt = dict_get_split_cnt (wr->dict);
if (split_cnt == 0)
- return 1;
+ return true;
if (wr->split_values)
- compare = 1;
+ compare = true;
else
{
- compare = 0;
+ compare = false;
wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
}
{
- int different = 0;
+ bool different = false;
int i;
for (i = 0; i < split_cnt; i++)
{
struct matrix_token token;
if (!mget_token (&token, mx->reader))
- return 0;
+ return false;
if (token.type != MNUM)
{
msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
context (mx->reader));
- return 0;
+ return false;
}
if (compare && wr->split_values[i] != token.number && !different)
{
if (!wr_output_data (wr, c, write_case, wc_data))
return 0;
- different = 1;
+ different = true;
mx->cells = 0;
}
wr->split_values[i] = token.number;
}
}
- return 1;
+ return true;
}
/* Compares doubles A and B, treating SYSMIS as greatest. */
static int
-compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
+compare_doubles (const void *a_, const void *b_, const void *aux UNUSED)
{
const double *a = a_;
const double *b = b_;
/* Return strcmp()-type comparison of the MX->n_factors factors at _A and
_B. Sort missing values toward the end. */
static int
-compare_factors (const void *a_, const void *b_, void *mx_)
+compare_factors (const void *a_, const void *b_, const void *mx_)
{
- struct matrix_data_pgm *mx = mx_;
+ const struct matrix_data_pgm *mx = mx_;
struct factor_data *const *pa = a_;
struct factor_data *const *pb = b_;
const double *a = (*pa)->factors;
size_t split_cnt;
size_t i;
- split_cnt = dict_get_split_cnt (default_dict);
- split = dict_get_split_vars (default_dict);
+ split_cnt = dict_get_split_cnt (wr->dict);
+ split = dict_get_split_vars (wr->dict);
for (i = 0; i < split_cnt; i++)
case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
}
fill_matrix (mx, content, iter->data[content]);
- ok = dump_cell_content (mx, content, iter->data[content],
+ ok = dump_cell_content (wr->dict, mx, content,
+ iter->data[content],
c, write_case, wc_data);
if (!ok)
break;
/* Sets ROWTYPE_ based on the given TOKEN read from READER.
Return success. */
-static int
+static bool
wr_read_rowtype (struct wr_aux_data *wr,
const struct matrix_token *token,
struct dfm_reader *reader)
if (wr->content != -1)
{
msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
- return 0;
+ return false;
}
if (token->type != MSTR)
{
msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
context (reader));
- return 0;
+ return false;
}
{
return 0;
}
- return 1;
+ return true;
}
/* Read the factors for the current row. Select a set of factors and
point wr_current to it. */
-static int
+static bool
wr_read_factors (struct wr_aux_data *wr)
{
struct matrix_data_pgm *mx = wr->mx;
winnage:
local_free (factor_values);
- return 1;
+ return true;
lossage:
local_free (factor_values);
- return 0;
+ return false;
}
/* Read the independent variables into wr->current. */
-static int
+static bool
wr_read_indeps (struct wr_aux_data *wr)
{
struct matrix_data_pgm *mx = wr->mx;
{
msg (SE, _("Duplicate specification for %s."),
content_names[wr->content]);
- return 0;
+ return false;
}
if (type == 0)
n_cols = mx->n_continuous;
{
msg (SE, _("Too many rows of matrix data for %s."),
content_names[wr->content]);
- return 0;
+ return false;
}
switch (mx->section)
n_cols = mx->n_continuous;
break;
default:
- assert (0);
- abort ();
+ NOT_REACHED ();
}
break;
default:
- assert (0);
- abort ();
+ NOT_REACHED ();
}
c->n_rows[wr->content]++;
{
struct matrix_token token;
if (!mget_token (&token, mx->reader))
- return 0;
+ return false;
if (token.type != MNUM)
{
msg (SE, _("Syntax error expecting value for %s %s."),
- dict_get_var (default_dict, mx->first_continuous + j)->name,
+ dict_get_var (wr->dict, mx->first_continuous + j)->name,
context (mx->reader));
- return 0;
+ return false;
}
*cp++ = token.number;
}
if (mx->fmt != FREE
&& !force_eol (mx->reader, content_names[wr->content]))
- return 0;
+ return false;
}
- return 1;
+ return true;
}
\f
/* Matrix source. */