1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 /* AIX requires this to be the first thing in the file. */
23 #define alloca __builtin_alloca
31 #ifndef alloca /* predefined by HP cc +Olibcalls */
47 #include "file-handle.h"
56 /*#define DEBUGGING 1*/
57 #include "debug-print.h"
59 /* FIXME: /N subcommand not implemented. It should be pretty simple,
62 /* Format type enums. */
69 /* Matrix section enums. */
77 /* Diagonal inclusion enums. */
105 /* 0=vector, 1=matrix, 2=scalar. */
106 static int content_type[PROX + 1] =
108 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
111 /* Name of each content type. */
112 static const char *content_names[PROX + 1] =
114 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
115 "DFE", "MAT", "COV", "CORR", "PROX",
118 /* The data file to be read. */
119 static struct file_handle *data_file;
122 static int fmt; /* LIST or FREE. */
123 static int section; /* LOWER or UPPER or FULL. */
124 static int diag; /* DIAGONAL or NODIAGONAL. */
126 /* Arena used for all the MATRIX DATA allocations. */
127 static struct pool *container;
129 /* ROWTYPE_ specified explicitly in data? */
130 static int explicit_rowtype;
132 /* ROWTYPE_, VARNAME_ variables. */
133 static struct variable *rowtype_, *varname_;
135 /* Is is per-factor data? */
136 int is_per_factor[PROX + 1];
138 /* Single SPLIT FILE variable. */
139 static struct variable *single_split;
141 /* Factor variables. */
142 static int n_factors;
143 static struct variable **factors;
145 /* Number of cells, or -1 if none. */
148 /* Population N specified by user. */
151 /* CONTENTS subcommand. */
152 static int contents[EOC * 3 + 1];
153 static int n_contents;
155 /* Number of continuous variables. */
156 static int n_continuous;
158 /* Index into default_dict.var of first continuous variables. */
159 static int first_continuous;
161 static int compare_variables_by_mxd_vartype (const void *pa,
163 static void read_matrices_without_rowtype (void);
164 static void read_matrices_with_rowtype (void);
165 static int string_to_content_type (char *, int *);
168 static void debug_print (void);
172 cmd_matrix_data (void)
176 lex_match_id ("MATRIX");
177 lex_match_id ("DATA");
179 container = pool_create ();
181 discard_variables ();
183 data_file = inline_file;
197 if (lex_match_id ("VARIABLES"))
204 msg (SE, _("VARIABLES subcommand multiply specified."));
210 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
216 for (i = 0; i < nv; i++)
217 if (!strcmp (v[i], "VARNAME_"))
219 msg (SE, _("VARNAME_ cannot be explicitly specified on "
221 for (i = 0; i < nv; i++)
231 for (i = 0; i < nv; i++)
233 struct variable *new_var;
235 if (strcmp (v[i], "ROWTYPE_"))
237 new_var = force_create_variable (&default_dict, v[i],
239 new_var->p.mxd.vartype = MXD_CONTINUOUS;
240 new_var->p.mxd.subtype = i;
243 explicit_rowtype = 1;
250 rowtype_ = force_create_variable (&default_dict, "ROWTYPE_",
252 rowtype_->p.mxd.vartype = MXD_ROWTYPE;
253 rowtype_->p.mxd.subtype = 0;
256 else if (lex_match_id ("FILE"))
259 data_file = fh_parse_file_handle ();
263 else if (lex_match_id ("FORMAT"))
267 while (token == T_ID)
269 if (lex_match_id ("LIST"))
271 else if (lex_match_id ("FREE"))
273 else if (lex_match_id ("LOWER"))
275 else if (lex_match_id ("UPPER"))
277 else if (lex_match_id ("FULL"))
279 else if (lex_match_id ("DIAGONAL"))
281 else if (lex_match_id ("NODIAGONAL"))
285 lex_error (_("in FORMAT subcommand"));
290 else if (lex_match_id ("SPLIT"))
296 msg (SE, _("SPLIT subcommand multiply specified."));
303 lex_error (_("in SPLIT subcommand"));
307 if (!is_varname (tokid)
308 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
310 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
312 msg (SE, _("Split variable may not be named ROWTYPE_ "
317 single_split = force_create_variable (&default_dict, tokid,
321 single_split->p.mxd.vartype = MXD_CONTINUOUS;
323 default_dict.n_splits = 1;
324 default_dict.splits = xmalloc (2 * sizeof *default_dict.splits);
325 default_dict.splits[0] = single_split;
326 default_dict.splits[1] = NULL;
333 if (!parse_variables (NULL, &v, &n, PV_NO_DUPLICATE))
336 default_dict.n_splits = n;
337 default_dict.splits = v = xrealloc (v, sizeof *v * (n + 1));
344 for (i = 0; i < default_dict.n_splits; i++)
346 if (default_dict.splits[i]->p.mxd.vartype != MXD_CONTINUOUS)
348 msg (SE, _("Split variable %s is already another type."),
352 default_dict.splits[i]->p.mxd.vartype = MXD_SPLIT;
353 default_dict.splits[i]->p.mxd.subtype = i;
357 else if (lex_match_id ("FACTORS"))
363 msg (SE, _("FACTORS subcommand multiply specified."));
368 if (!parse_variables (NULL, &factors, &n_factors, PV_NONE))
374 for (i = 0; i < n_factors; i++)
376 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
378 msg (SE, _("Factor variable %s is already another type."),
382 factors[i]->p.mxd.vartype = MXD_FACTOR;
383 factors[i]->p.mxd.subtype = i;
387 else if (lex_match_id ("CELLS"))
393 msg (SE, _("CELLS subcommand multiply specified."));
397 if (!lex_integer_p () || lex_integer () < 1)
399 lex_error (_("expecting positive integer"));
403 cells = lex_integer ();
406 else if (lex_match_id ("N"))
412 msg (SE, _("N subcommand multiply specified."));
416 if (!lex_integer_p () || lex_integer () < 1)
418 lex_error (_("expecting positive integer"));
422 pop_n = lex_integer ();
425 else if (lex_match_id ("CONTENTS"))
427 int inside_parens = 0;
428 unsigned collide = 0;
433 msg (SE, _("CONTENTS subcommand multiply specified."));
443 for (i = 0; i <= PROX; i++)
444 is_per_factor[i] = 0;
453 msg (SE, _("Nested parentheses not allowed."));
459 else if (lex_match (')'))
463 msg (SE, _("Mismatched right parenthesis (`(')."));
466 if (contents[n_contents - 1] == LPAREN)
468 msg (SE, _("Empty parentheses not allowed."));
481 lex_error (_("in CONTENTS subcommand"));
485 content_type = string_to_content_type (tokid,
487 if (content_type == -1)
489 lex_error (_("in CONTENTS subcommand"));
494 if (collide & (1 << collide_index))
496 msg (SE, _("Content multiply specified for %s."),
497 content_names[content_type]);
500 collide |= (1 << collide_index);
503 is_per_factor[item] = inside_parens;
505 contents[n_contents++] = item;
507 if (token == '/' || token == '.')
513 msg (SE, _("Missing right parenthesis."));
516 contents[n_contents] = EOC;
527 lex_error (_("expecting end of command"));
533 msg (SE, _("Missing VARIABLES subcommand."));
537 if (!n_contents && !explicit_rowtype)
539 msg (SW, _("CONTENTS subcommand not specified: assuming file "
540 "contains only CORR matrix."));
547 if (n_factors && !explicit_rowtype && cells == -1)
549 msg (SE, _("Missing CELLS subcommand. CELLS is required "
550 "when ROWTYPE_ is not given in the data and "
551 "factors are present."));
555 if (explicit_rowtype && single_split)
557 msg (SE, _("Split file values must be present in the data when "
558 "ROWTYPE_ is present."));
562 /* Create VARNAME_. */
564 varname_ = force_create_variable (&default_dict, "VARNAME_",
566 varname_->p.mxd.vartype = MXD_VARNAME;
567 varname_->p.mxd.subtype = 0;
570 /* Sort the dictionary variables into the desired order for the
571 system file output. */
575 qsort (default_dict.var, default_dict.nvar, sizeof *default_dict.var,
576 compare_variables_by_mxd_vartype);
578 for (i = 0; i < default_dict.nvar; i++)
579 default_dict.var[i]->index = i;
584 static const struct fmt_spec fmt_tab[MXD_COUNT] =
595 first_continuous = -1;
596 for (i = 0; i < default_dict.nvar; i++)
598 struct variable *v = default_dict.var[i];
599 int type = v->p.mxd.vartype;
601 assert (type >= 0 && type < MXD_COUNT);
602 v->print = v->write = fmt_tab[type];
604 if (type == MXD_CONTINUOUS)
606 if (first_continuous == -1 && type == MXD_CONTINUOUS)
607 first_continuous = i;
611 if (n_continuous == 0)
613 msg (SE, _("No continuous variables specified."));
621 if (explicit_rowtype)
622 read_matrices_with_rowtype ();
624 read_matrices_without_rowtype ();
626 pool_destroy (container);
631 discard_variables ();
633 pool_destroy (container);
637 /* Look up string S as a content-type name and return the
638 corresponding enumerated value, or -1 if there is no match. If
639 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
640 as a bit-index) which can be used for determining whether a related
641 statistic has already been used. */
643 string_to_content_type (char *s, int *collide)
654 {N_VECTOR, 0, "N_VECTOR"},
656 {N_SCALAR, 0, "N_SCALAR"},
657 {N_MATRIX, 1, "N_MATRIX"},
659 {STDDEV, 3, "STDDEV"},
671 for (tp = tab; tp->value != -1; tp++)
672 if (!strcmp (s, tp->string))
675 *collide = tp->collide;
682 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
685 compare_variables_by_mxd_vartype (const void *pa, const void *pb)
687 struct matrix_data_proc *a = &(*((struct variable **) pa))->p.mxd;
688 struct matrix_data_proc *b = &(*((struct variable **) pb))->p.mxd;
690 return (a->vartype != b->vartype
691 ? a->vartype - b->vartype
692 : a->subtype - b->subtype);
696 /* Print out the command as input. */
700 printf ("MATRIX DATA\n\t/VARIABLES=");
705 for (i = 0; i < default_dict.nvar; i++)
706 printf ("%s ", default_dict.var[i]->name);
710 printf ("\t/FORMAT=");
713 else if (fmt == FREE)
717 if (section == LOWER)
719 else if (section == UPPER)
721 else if (section == FULL)
725 if (diag == DIAGONAL)
726 printf (" DIAGONAL\n");
727 else if (diag == NODIAGONAL)
728 printf (" NODIAGONAL\n");
732 if (default_dict.n_splits)
736 printf ("\t/SPLIT=");
737 for (i = 0; i < default_dict.n_splits; i++)
738 printf ("%s ", default_dict.splits[i]->name);
740 printf ("\t/* single split");
748 printf ("\t/FACTORS=");
749 for (i = 0; i < n_factors; i++)
750 printf ("%s ", factors[i]->name);
755 printf ("\t/CELLS=%d\n", cells);
758 printf ("\t/N=%d\n", pop_n);
765 printf ("\t/CONTENTS=");
766 for (i = 0; i < n_contents; i++)
768 if (contents[i] == LPAREN)
775 else if (contents[i] == RPAREN)
783 assert (contents[i] >= 0 && contents[i] <= PROX);
786 printf ("%s", content_names[contents[i]]);
793 #endif /* DEBUGGING */
795 /* Matrix tokenizer. */
797 /* Matrix token types. */
800 MNULL, /* No token. */
803 MSTOP /* End of file. */
806 /* Current matrix token. */
809 /* Token string if applicable; not null-terminated. */
810 static char *mtokstr;
812 /* Length of mtokstr in characters. */
815 /* Token value if applicable. */
816 static double mtokval;
818 static int mget_token (void);
821 #define mget_token() mget_token_dump()
824 mget_token_dump (void)
826 int result = (mget_token) ();
837 printf (" <NULLTOK>");
840 printf (" #%g", mtokval);
843 printf (" #'%.*s'", mtoklen, mtokstr);
855 /* Return the current position in the data file. */
861 char *p = dfm_get_record (data_file, &len);
864 strcpy (buf, "at end of line");
868 int n_copy = min (10, len);
869 cp = stpcpy (buf, "before `");
870 while (n_copy && isspace ((unsigned char) *p))
872 while (n_copy && !isspace ((unsigned char) *p))
873 *cp++ = *p++, n_copy--;
881 /* Is there at least one token left in the data file? */
893 cp = dfm_get_record (data_file, &len);
898 while (isspace ((unsigned char) *cp) && cp < ep)
904 dfm_fwd_record (data_file);
907 dfm_set_record (data_file, cp);
912 /* Parse a MATRIX DATA token from data_file into mtok*. */
922 cp = dfm_get_record (data_file, &len);
932 while (isspace ((unsigned char) *cp) && cp < ep)
938 dfm_fwd_record (data_file);
941 dfm_set_record (data_file, cp);
942 first_column = dfm_get_cur_col (data_file) + 1;
944 /* Three types of fields: quoted with ', quoted with ", unquoted. */
945 if (*cp == '\'' || *cp == '"')
951 while (cp < ep && *cp != quote)
953 mtoklen = cp - mtokstr;
957 msg (SW, _("Scope of string exceeds line."));
961 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
964 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
965 && *cp != '-' && *cp != '+')
967 if (isdigit ((unsigned char) *cp))
970 if ((tolower ((unsigned char) *cp) == 'd'
971 || tolower ((unsigned char) *cp) == 'e')
972 && (cp[1] == '+' || cp[1] == '-'))
978 mtoklen = cp - mtokstr;
986 di.e = mtokstr + mtoklen;
987 di.v = (union value *) &mtokval;
988 di.f1 = first_column;
989 di.format.type = FMT_F;
990 di.format.w = mtoklen;
1000 dfm_set_record (data_file, cp);
1005 /* Forcibly skip the end of a line for content type CONTENT in
1008 force_eol (const char *content)
1016 cp = dfm_get_record (data_file, &len);
1019 while (len && isspace (*cp))
1024 msg (SE, _("End of line expected %s while reading %s."),
1025 context (), content);
1029 dfm_fwd_record (data_file);
1034 /* Back end, omitting ROWTYPE_. */
1036 /* MATRIX DATA data. */
1037 static double ***nr_data;
1039 /* Factor values. */
1040 static double *nr_factor_values;
1042 /* Largest-numbered cell that we have read in thus far, plus one. */
1043 static int max_cell_index;
1045 /* SPLIT FILE variable values. */
1046 static double *split_values;
1048 static int nr_read_splits (int compare);
1049 static int nr_read_factors (int cell);
1050 static void nr_output_data (void);
1051 static int matrix_data_read_without_rowtype (void);
1053 /* Read from the data file and write it to the active file. */
1055 read_matrices_without_rowtype (void)
1061 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1062 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1065 matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1066 vfm_source = &matrix_data_source;
1068 procedure (NULL, NULL, NULL);
1070 free (split_values);
1071 free (nr_factor_values);
1073 fh_close_handle (data_file);
1076 /* Mirror data across the diagonal of matrix CP which contains
1077 CONTENT type data. */
1079 fill_matrix (int content, double *cp)
1081 int type = content_type[content];
1083 if (type == 1 && section != FULL)
1085 if (diag == NODIAGONAL)
1087 const double fill = content == CORR ? 1.0 : SYSMIS;
1090 for (i = 0; i < n_continuous; i++)
1091 cp[i * (1 + n_continuous)] = fill;
1097 if (section == LOWER)
1099 int n_lines = n_continuous;
1100 if (section != FULL && diag == NODIAGONAL)
1103 for (r = 1; r < n_lines; r++)
1104 for (c = 0; c < r; c++)
1105 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1109 assert (section == UPPER);
1110 for (r = 1; r < n_continuous; r++)
1111 for (c = 0; c < r; c++)
1112 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1120 for (c = 1; c < n_continuous; c++)
1125 /* Read data lines for content type CONTENT from the data file. If
1126 PER_FACTOR is nonzero, then factor information is read from the
1127 data file. Data is for cell number CELL. */
1129 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1132 const int type = content_type[content];
1134 /* Number of lines that must be parsed from the data file for this
1138 /* Current position in vector or matrix. */
1148 n_lines = n_continuous;
1149 if (section != FULL && diag == NODIAGONAL)
1153 cp = nr_data[content][cell];
1154 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1157 for (i = 0; i < n_lines; i++)
1161 if (!nr_read_splits (1))
1163 if (per_factor && !nr_read_factors (cell))
1170 n_cols = n_continuous;
1180 n_cols = n_continuous - i;
1181 if (diag == NODIAGONAL)
1188 n_cols = n_continuous;
1204 for (j = 0; j < n_cols; j++)
1210 msg (SE, _("expecting value for %s %s"),
1211 default_dict.var[j]->name, context ());
1217 if (!force_eol (content_names[content]))
1219 debug_printf (("\n"));
1222 if (section == LOWER)
1223 cp += n_continuous - n_cols;
1226 fill_matrix (content, nr_data[content][cell]);
1231 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1232 writes them to the output file. Returns success. */
1234 matrix_data_read_without_rowtype (void)
1239 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1244 for (i = 0; i <= PROX; i++)
1248 for (cp = contents; *cp != EOC; cp++)
1249 if (*cp != LPAREN && *cp != RPAREN)
1251 int per_factor = is_per_factor[*cp];
1254 n_entries = n_continuous;
1255 if (content_type[*cp] == 1)
1256 n_entries *= n_continuous;
1259 int n_vectors = per_factor ? cells : 1;
1262 nr_data[*cp] = pool_alloc (container,
1263 n_vectors * sizeof **nr_data);
1265 for (i = 0; i < n_vectors; i++)
1266 nr_data[*cp][i] = pool_alloc (container,
1267 n_entries * sizeof ***nr_data);
1276 if (!nr_read_splits (0))
1279 for (bp = contents; *bp != EOC; bp = np)
1283 /* Trap the CONTENTS that we should parse in this pass
1284 between bp and ep. Set np to the starting bp for next
1289 while (*ep != RPAREN)
1297 while (*ep != EOC && *ep != LPAREN)
1306 for (i = 0; i < (per_factor ? cells : 1); i++)
1310 for (cp = bp; cp < ep; cp++)
1311 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1319 if (default_dict.n_splits == 0 || !another_token ())
1324 /* Read the split file variables. If COMPARE is 1, compares the
1325 values read to the last values read and returns 1 if they're equal,
1328 nr_read_splits (int compare)
1330 static int just_read = 0;
1332 if (compare && just_read)
1338 if (default_dict.n_splits == 0)
1344 split_values[0] = ++default_dict.splits[0]->p.mxd.subtype;
1354 for (i = 0; i < default_dict.n_splits; i++)
1360 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1366 split_values[i] = mtokval;
1367 else if (split_values[i] != mtokval)
1369 msg (SE, _("Expecting value %g for %s."),
1370 split_values[i], default_dict.splits[i]->name);
1379 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1380 values read to the last values read and returns 1 if they're equal,
1383 nr_read_factors (int cell)
1390 assert (max_cell_index >= cell);
1391 if (cell != max_cell_index)
1402 for (i = 0; i < n_factors; i++)
1408 msg (SE, _("Syntax error expecting factor value %s."),
1414 nr_factor_values[i + n_factors * cell] = mtokval;
1415 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1417 msg (SE, _("Syntax error expecting value %g for %s %s."),
1418 nr_factor_values[i + n_factors * cell],
1419 factors[i]->name, context ());
1428 /* Write the contents of a cell having content type CONTENT and data
1429 CP to the active file. */
1431 dump_cell_content (int content, double *cp)
1433 int type = content_type[content];
1436 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1437 content_names[content], 8);
1440 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1444 int n_lines = (type == 1) ? n_continuous : 1;
1447 for (i = 0; i < n_lines; i++)
1451 for (j = 0; j < n_continuous; j++)
1453 temp_case->data[(default_dict.var
1454 [first_continuous + j]->fv)].f = *cp;
1455 debug_printf (("c:%s(%g) ",
1456 default_dict.var[first_continuous + j]->name,
1461 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1462 default_dict.var[first_continuous + i]->name,
1464 debug_printf (("\n"));
1470 /* Finally dump out everything from nr_data[] to the output file. */
1472 nr_output_data (void)
1477 for (i = 0; i < default_dict.n_splits; i++)
1478 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1485 for (cell = 0; cell < cells; cell++)
1490 for (factor = 0; factor < n_factors; factor++)
1492 temp_case->data[factors[factor]->fv].f
1493 = nr_factor_values[factor + cell * n_factors];
1494 debug_printf (("f:%s ", factors[factor]->name));
1501 for (content = 0; content <= PROX; content++)
1502 if (is_per_factor[content])
1504 assert (nr_data[content] != NULL
1505 && nr_data[content][cell] != NULL);
1507 dump_cell_content (content, nr_data[content][cell]);
1519 for (factor = 0; factor < n_factors; factor++)
1520 temp_case->data[factors[factor]->fv].f = SYSMIS;
1523 for (content = 0; content <= PROX; content++)
1524 if (!is_per_factor[content] && nr_data[content] != NULL)
1525 dump_cell_content (content, nr_data[content][0]);
1529 /* Back end, with ROWTYPE_. */
1531 /* Type of current row. */
1532 static int wr_content;
1534 /* All the data for one set of factor values. */
1538 int n_rows[PROX + 1];
1539 double *data[PROX + 1];
1540 struct factor_data *next;
1543 /* All the data, period. */
1544 struct factor_data *wr_data;
1546 /* Current factor. */
1547 struct factor_data *wr_current;
1549 static int wr_read_splits (void);
1550 static int wr_output_data (void);
1551 static int wr_read_rowtype (void);
1552 static int wr_read_factors (void);
1553 static int wr_read_indeps (void);
1554 static int matrix_data_read_with_rowtype (void);
1556 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1557 them to the output file. */
1559 read_matrices_with_rowtype (void)
1562 wr_data = wr_current = NULL;
1563 split_values = NULL;
1566 matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1567 vfm_source = &matrix_data_source;
1569 procedure (NULL, NULL, NULL);
1571 free (split_values);
1572 fh_close_handle (data_file);
1575 /* Read from the data file and write it to the active file. */
1577 matrix_data_read_with_rowtype (void)
1581 if (!wr_read_splits ())
1584 if (!wr_read_factors ())
1587 if (!wr_read_indeps ())
1590 while (another_token ());
1596 /* Read the split file variables. If they differ from the previous
1597 set of split variables then output the data. Returns success. */
1599 wr_read_splits (void)
1603 if (default_dict.n_splits == 0)
1611 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1618 for (i = 0; i < default_dict.n_splits; i++)
1624 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1629 if (compare && split_values[i] != mtokval && !different)
1631 if (!wr_output_data ())
1636 split_values[i] = mtokval;
1643 /* Return strcmp()-type comparison of the n_factors factors at _A and
1644 _B. Sort missing values toward the end. */
1646 compare_factors (const void *pa, const void *pb)
1648 const double *a = (*(struct factor_data **) pa)->factors;
1649 const double *b = (*(struct factor_data **) pb)->factors;
1652 for (i = 0; i < n_factors; i++, a++, b++)
1659 else if (*b == SYSMIS)
1662 return *a - *b < 0 ? -1 : 1;
1668 /* Write out the data for the current split file to the active
1671 wr_output_data (void)
1676 for (i = 0; i < default_dict.n_splits; i++)
1677 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1680 /* Sort the wr_data list. */
1682 struct factor_data **factors;
1683 struct factor_data *iter;
1686 factors = xmalloc (sizeof *factors * cells);
1688 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1691 qsort (factors, cells, sizeof *factors, compare_factors);
1693 wr_data = factors[0];
1694 for (i = 0; i < cells - 1; i++)
1695 factors[i]->next = factors[i + 1];
1696 factors[cells - 1]->next = NULL;
1701 /* Write out records for every set of factor values. */
1703 struct factor_data *iter;
1705 for (iter = wr_data; iter; iter = iter->next)
1710 for (factor = 0; factor < n_factors; factor++)
1712 temp_case->data[factors[factor]->fv].f
1713 = iter->factors[factor];
1714 debug_printf (("f:%s ", factors[factor]->name));
1721 for (content = 0; content <= PROX; content++)
1723 if (!iter->n_rows[content])
1727 int type = content_type[content];
1728 int n_lines = (type == 1
1730 - (section != FULL && diag == NODIAGONAL))
1733 if (n_lines != iter->n_rows[content])
1735 msg (SE, _("Expected %d lines of data for %s content; "
1736 "actually saw %d lines. No data will be "
1737 "output for this content."),
1738 n_lines, content_names[content],
1739 iter->n_rows[content]);
1744 fill_matrix (content, iter->data[content]);
1746 dump_cell_content (content, iter->data[content]);
1752 pool_destroy (container);
1753 container = pool_create ();
1755 wr_data = wr_current = NULL;
1760 /* Read ROWTYPE_ from the data file. Return success. */
1762 wr_read_rowtype (void)
1764 if (wr_content != -1)
1766 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1771 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1779 memcpy (s, mtokstr, min (15, mtoklen));
1780 s[min (15, mtoklen)] = 0;
1782 for (cp = s; *cp; cp++)
1783 *cp = toupper ((unsigned char) *cp);
1785 wr_content = string_to_content_type (s, NULL);
1788 if (wr_content == -1)
1790 msg (SE, _("Syntax error %s."), context ());
1797 /* Read the factors for the current row. Select a set of factors and
1798 point wr_current to it. */
1800 wr_read_factors (void)
1802 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1808 for (i = 0; i < n_factors; i++)
1814 if (!wr_read_rowtype ())
1821 msg (SE, _("Syntax error expecting factor value %s."),
1826 factor_values[i] = mtokval;
1829 if (wr_content == -1)
1833 if (!wr_read_rowtype ())
1837 /* Try the most recent factor first as a simple caching
1843 for (i = 0; i < n_factors; i++)
1844 if (factor_values[i] != wr_current->factors[i])
1849 /* Linear search through the list. */
1852 struct factor_data *iter;
1854 for (iter = wr_data; iter; iter = iter->next)
1858 for (i = 0; i < n_factors; i++)
1859 if (factor_values[i] != iter->factors[i])
1869 /* Not found. Make a new item. */
1871 struct factor_data *new = pool_alloc (container, sizeof *new);
1873 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1878 for (i = 0; i < n_factors; i++)
1879 new->factors[i] = factor_values[i];
1885 for (i = 0; i <= PROX; i++)
1888 new->data[i] = NULL;
1892 new->next = wr_data;
1893 wr_data = wr_current = new;
1898 local_free (factor_values);
1902 local_free (factor_values);
1906 /* Read the independent variables into wr_current. */
1908 wr_read_indeps (void)
1910 struct factor_data *c = wr_current;
1911 const int type = content_type[wr_content];
1912 const int n_rows = c->n_rows[wr_content];
1916 /* Allocate room for data if necessary. */
1917 if (c->data[wr_content] == NULL)
1919 int n_items = n_continuous;
1921 n_items *= n_continuous;
1923 c->data[wr_content] = pool_alloc (container,
1924 sizeof **c->data * n_items);
1927 cp = &c->data[wr_content][n_rows * n_continuous];
1929 /* Figure out how much to read from this line. */
1936 msg (SE, _("Duplicate specification for %s."),
1937 content_names[wr_content]);
1941 n_cols = n_continuous;
1946 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1948 msg (SE, _("Too many rows of matrix data for %s."),
1949 content_names[wr_content]);
1956 n_cols = n_rows + 1;
1957 if (diag == NODIAGONAL)
1962 n_cols = n_continuous - n_rows;
1963 if (diag == NODIAGONAL)
1970 n_cols = n_continuous;
1979 c->n_rows[wr_content]++;
1981 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1983 /* Read N_COLS items at CP. */
1987 for (j = 0; j < n_cols; j++)
1993 msg (SE, _("Syntax error expecting value for %s %s."),
1994 default_dict.var[first_continuous + j]->name, context ());
2000 if (!force_eol (content_names[wr_content]))
2002 debug_printf (("\n"));
2008 /* Matrix source. */
2010 struct case_stream matrix_data_source =