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"
55 #include "debug-print.h"
57 /* FIXME: /N subcommand not implemented. It should be pretty simple,
60 /* Format type enums. */
67 /* Matrix section enums. */
75 /* Diagonal inclusion enums. */
103 /* 0=vector, 1=matrix, 2=scalar. */
104 static int content_type[PROX + 1] =
106 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
109 /* Name of each content type. */
110 static const char *content_names[PROX + 1] =
112 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
113 "DFE", "MAT", "COV", "CORR", "PROX",
116 /* The data file to be read. */
117 static struct file_handle *data_file;
120 static int fmt; /* LIST or FREE. */
121 static int section; /* LOWER or UPPER or FULL. */
122 static int diag; /* DIAGONAL or NODIAGONAL. */
124 /* Arena used for all the MATRIX DATA allocations. */
125 static struct pool *container;
127 /* ROWTYPE_ specified explicitly in data? */
128 static int explicit_rowtype;
130 /* ROWTYPE_, VARNAME_ variables. */
131 static struct variable *rowtype_, *varname_;
133 /* Is is per-factor data? */
134 int is_per_factor[PROX + 1];
136 /* Single SPLIT FILE variable. */
137 static struct variable *single_split;
139 /* Factor variables. */
140 static int n_factors;
141 static struct variable **factors;
143 /* Number of cells, or -1 if none. */
146 /* Population N specified by user. */
149 /* CONTENTS subcommand. */
150 static int contents[EOC * 3 + 1];
151 static int n_contents;
153 /* Number of continuous variables. */
154 static int n_continuous;
156 /* Index into default_dict.var of first continuous variables. */
157 static int first_continuous;
159 static int compare_variables_by_mxd_vartype (const void *pa,
161 static void read_matrices_without_rowtype (void);
162 static void read_matrices_with_rowtype (void);
163 static int string_to_content_type (char *, int *);
166 static void debug_print (void);
170 cmd_matrix_data (void)
174 lex_match_id ("MATRIX");
175 lex_match_id ("DATA");
177 container = pool_create ();
179 discard_variables ();
181 data_file = inline_file;
195 if (lex_match_id ("VARIABLES"))
202 msg (SE, _("VARIABLES subcommand multiply specified."));
208 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
214 for (i = 0; i < nv; i++)
215 if (!strcmp (v[i], "VARNAME_"))
217 msg (SE, _("VARNAME_ cannot be explicitly specified on "
219 for (i = 0; i < nv; i++)
229 for (i = 0; i < nv; i++)
231 struct variable *new_var;
233 if (strcmp (v[i], "ROWTYPE_"))
235 new_var = force_create_variable (&default_dict, v[i],
237 new_var->p.mxd.vartype = MXD_CONTINUOUS;
238 new_var->p.mxd.subtype = i;
241 explicit_rowtype = 1;
248 rowtype_ = force_create_variable (&default_dict, "ROWTYPE_",
250 rowtype_->p.mxd.vartype = MXD_ROWTYPE;
251 rowtype_->p.mxd.subtype = 0;
254 else if (lex_match_id ("FILE"))
257 data_file = fh_parse_file_handle ();
261 else if (lex_match_id ("FORMAT"))
265 while (token == T_ID)
267 if (lex_match_id ("LIST"))
269 else if (lex_match_id ("FREE"))
271 else if (lex_match_id ("LOWER"))
273 else if (lex_match_id ("UPPER"))
275 else if (lex_match_id ("FULL"))
277 else if (lex_match_id ("DIAGONAL"))
279 else if (lex_match_id ("NODIAGONAL"))
283 lex_error (_("in FORMAT subcommand"));
288 else if (lex_match_id ("SPLIT"))
294 msg (SE, _("SPLIT subcommand multiply specified."));
301 lex_error (_("in SPLIT subcommand"));
305 if (!is_varname (tokid)
306 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
308 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
310 msg (SE, _("Split variable may not be named ROWTYPE_ "
315 single_split = force_create_variable (&default_dict, tokid,
319 single_split->p.mxd.vartype = MXD_CONTINUOUS;
321 default_dict.n_splits = 1;
322 default_dict.splits = xmalloc (2 * sizeof *default_dict.splits);
323 default_dict.splits[0] = single_split;
324 default_dict.splits[1] = NULL;
331 if (!parse_variables (NULL, &v, &n, PV_NO_DUPLICATE))
334 default_dict.n_splits = n;
335 default_dict.splits = v = xrealloc (v, sizeof *v * (n + 1));
342 for (i = 0; i < default_dict.n_splits; i++)
344 if (default_dict.splits[i]->p.mxd.vartype != MXD_CONTINUOUS)
346 msg (SE, _("Split variable %s is already another type."),
350 default_dict.splits[i]->p.mxd.vartype = MXD_SPLIT;
351 default_dict.splits[i]->p.mxd.subtype = i;
355 else if (lex_match_id ("FACTORS"))
361 msg (SE, _("FACTORS subcommand multiply specified."));
366 if (!parse_variables (NULL, &factors, &n_factors, PV_NONE))
372 for (i = 0; i < n_factors; i++)
374 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
376 msg (SE, _("Factor variable %s is already another type."),
380 factors[i]->p.mxd.vartype = MXD_FACTOR;
381 factors[i]->p.mxd.subtype = i;
385 else if (lex_match_id ("CELLS"))
391 msg (SE, _("CELLS subcommand multiply specified."));
395 if (!lex_integer_p () || lex_integer () < 1)
397 lex_error (_("expecting positive integer"));
401 cells = lex_integer ();
404 else if (lex_match_id ("N"))
410 msg (SE, _("N subcommand multiply specified."));
414 if (!lex_integer_p () || lex_integer () < 1)
416 lex_error (_("expecting positive integer"));
420 pop_n = lex_integer ();
423 else if (lex_match_id ("CONTENTS"))
425 int inside_parens = 0;
426 unsigned collide = 0;
431 msg (SE, _("CONTENTS subcommand multiply specified."));
441 for (i = 0; i <= PROX; i++)
442 is_per_factor[i] = 0;
451 msg (SE, _("Nested parentheses not allowed."));
457 else if (lex_match (')'))
461 msg (SE, _("Mismatched right parenthesis (`(')."));
464 if (contents[n_contents - 1] == LPAREN)
466 msg (SE, _("Empty parentheses not allowed."));
479 lex_error (_("in CONTENTS subcommand"));
483 content_type = string_to_content_type (tokid,
485 if (content_type == -1)
487 lex_error (_("in CONTENTS subcommand"));
492 if (collide & (1 << collide_index))
494 msg (SE, _("Content multiply specified for %s."),
495 content_names[content_type]);
498 collide |= (1 << collide_index);
501 is_per_factor[item] = inside_parens;
503 contents[n_contents++] = item;
505 if (token == '/' || token == '.')
511 msg (SE, _("Missing right parenthesis."));
514 contents[n_contents] = EOC;
525 lex_error (_("expecting end of command"));
531 msg (SE, _("Missing VARIABLES subcommand."));
535 if (!n_contents && !explicit_rowtype)
537 msg (SW, _("CONTENTS subcommand not specified: assuming file "
538 "contains only CORR matrix."));
545 if (n_factors && !explicit_rowtype && cells == -1)
547 msg (SE, _("Missing CELLS subcommand. CELLS is required "
548 "when ROWTYPE_ is not given in the data and "
549 "factors are present."));
553 if (explicit_rowtype && single_split)
555 msg (SE, _("Split file values must be present in the data when "
556 "ROWTYPE_ is present."));
560 /* Create VARNAME_. */
562 varname_ = force_create_variable (&default_dict, "VARNAME_",
564 varname_->p.mxd.vartype = MXD_VARNAME;
565 varname_->p.mxd.subtype = 0;
568 /* Sort the dictionary variables into the desired order for the
569 system file output. */
573 qsort (default_dict.var, default_dict.nvar, sizeof *default_dict.var,
574 compare_variables_by_mxd_vartype);
576 for (i = 0; i < default_dict.nvar; i++)
577 default_dict.var[i]->index = i;
582 static const struct fmt_spec fmt_tab[MXD_COUNT] =
593 first_continuous = -1;
594 for (i = 0; i < default_dict.nvar; i++)
596 struct variable *v = default_dict.var[i];
597 int type = v->p.mxd.vartype;
599 assert (type >= 0 && type < MXD_COUNT);
600 v->print = v->write = fmt_tab[type];
602 if (type == MXD_CONTINUOUS)
604 if (first_continuous == -1 && type == MXD_CONTINUOUS)
605 first_continuous = i;
609 if (n_continuous == 0)
611 msg (SE, _("No continuous variables specified."));
619 if (explicit_rowtype)
620 read_matrices_with_rowtype ();
622 read_matrices_without_rowtype ();
624 pool_destroy (container);
629 discard_variables ();
631 pool_destroy (container);
635 /* Look up string S as a content-type name and return the
636 corresponding enumerated value, or -1 if there is no match. If
637 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
638 as a bit-index) which can be used for determining whether a related
639 statistic has already been used. */
641 string_to_content_type (char *s, int *collide)
652 {N_VECTOR, 0, "N_VECTOR"},
654 {N_SCALAR, 0, "N_SCALAR"},
655 {N_MATRIX, 1, "N_MATRIX"},
657 {STDDEV, 3, "STDDEV"},
669 for (tp = tab; tp->value != -1; tp++)
670 if (!strcmp (s, tp->string))
673 *collide = tp->collide;
680 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
683 compare_variables_by_mxd_vartype (const void *pa, const void *pb)
685 struct matrix_data_proc *a = &(*((struct variable **) pa))->p.mxd;
686 struct matrix_data_proc *b = &(*((struct variable **) pb))->p.mxd;
688 return (a->vartype != b->vartype
689 ? a->vartype - b->vartype
690 : a->subtype - b->subtype);
694 /* Print out the command as input. */
698 printf ("MATRIX DATA\n\t/VARIABLES=");
703 for (i = 0; i < default_dict.nvar; i++)
704 printf ("%s ", default_dict.var[i]->name);
708 printf ("\t/FORMAT=");
711 else if (fmt == FREE)
715 if (section == LOWER)
717 else if (section == UPPER)
719 else if (section == FULL)
723 if (diag == DIAGONAL)
724 printf (" DIAGONAL\n");
725 else if (diag == NODIAGONAL)
726 printf (" NODIAGONAL\n");
730 if (default_dict.n_splits)
734 printf ("\t/SPLIT=");
735 for (i = 0; i < default_dict.n_splits; i++)
736 printf ("%s ", default_dict.splits[i]->name);
738 printf ("\t/* single split");
746 printf ("\t/FACTORS=");
747 for (i = 0; i < n_factors; i++)
748 printf ("%s ", factors[i]->name);
753 printf ("\t/CELLS=%d\n", cells);
756 printf ("\t/N=%d\n", pop_n);
763 printf ("\t/CONTENTS=");
764 for (i = 0; i < n_contents; i++)
766 if (contents[i] == LPAREN)
773 else if (contents[i] == RPAREN)
781 assert (contents[i] >= 0 && contents[i] <= PROX);
784 printf ("%s", content_names[contents[i]]);
791 #endif /* DEBUGGING */
793 /* Matrix tokenizer. */
795 /* Matrix token types. */
798 MNULL, /* No token. */
801 MSTOP /* End of file. */
804 /* Current matrix token. */
807 /* Token string if applicable; not null-terminated. */
808 static char *mtokstr;
810 /* Length of mtokstr in characters. */
813 /* Token value if applicable. */
814 static double mtokval;
816 static int mget_token (void);
819 #define mget_token() mget_token_dump()
822 mget_token_dump (void)
824 int result = (mget_token) ();
835 printf (" <NULLTOK>");
838 printf (" #%g", mtokval);
841 printf (" #'%.*s'", mtoklen, mtokstr);
853 /* Return the current position in the data file. */
859 char *p = dfm_get_record (data_file, &len);
862 strcpy (buf, "at end of line");
866 int n_copy = min (10, len);
867 cp = stpcpy (buf, "before `");
868 while (n_copy && isspace ((unsigned char) *p))
870 while (n_copy && !isspace ((unsigned char) *p))
871 *cp++ = *p++, n_copy--;
879 /* Is there at least one token left in the data file? */
891 cp = dfm_get_record (data_file, &len);
896 while (isspace ((unsigned char) *cp) && cp < ep)
902 dfm_fwd_record (data_file);
905 dfm_set_record (data_file, cp);
910 /* Parse a MATRIX DATA token from data_file into mtok*. */
920 cp = dfm_get_record (data_file, &len);
930 while (isspace ((unsigned char) *cp) && cp < ep)
936 dfm_fwd_record (data_file);
939 dfm_set_record (data_file, cp);
940 first_column = dfm_get_cur_col (data_file) + 1;
942 /* Three types of fields: quoted with ', quoted with ", unquoted. */
943 if (*cp == '\'' || *cp == '"')
949 while (cp < ep && *cp != quote)
951 mtoklen = cp - mtokstr;
955 msg (SW, _("Scope of string exceeds line."));
959 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
962 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
963 && *cp != '-' && *cp != '+')
965 if (isdigit ((unsigned char) *cp))
968 if ((tolower ((unsigned char) *cp) == 'd'
969 || tolower ((unsigned char) *cp) == 'e')
970 && (cp[1] == '+' || cp[1] == '-'))
976 mtoklen = cp - mtokstr;
984 di.e = mtokstr + mtoklen;
985 di.v = (union value *) &mtokval;
986 di.f1 = first_column;
987 di.format.type = FMT_F;
988 di.format.w = mtoklen;
998 dfm_set_record (data_file, cp);
1003 /* Forcibly skip the end of a line for content type CONTENT in
1006 force_eol (const char *content)
1014 cp = dfm_get_record (data_file, &len);
1017 while (len && isspace (*cp))
1022 msg (SE, _("End of line expected %s while reading %s."),
1023 context (), content);
1027 dfm_fwd_record (data_file);
1032 /* Back end, omitting ROWTYPE_. */
1034 /* MATRIX DATA data. */
1035 static double ***nr_data;
1037 /* Factor values. */
1038 static double *nr_factor_values;
1040 /* Largest-numbered cell that we have read in thus far, plus one. */
1041 static int max_cell_index;
1043 /* SPLIT FILE variable values. */
1044 static double *split_values;
1046 static int nr_read_splits (int compare);
1047 static int nr_read_factors (int cell);
1048 static void nr_output_data (void);
1049 static int matrix_data_read_without_rowtype (void);
1051 /* Read from the data file and write it to the active file. */
1053 read_matrices_without_rowtype (void)
1059 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1060 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1063 matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1064 vfm_source = &matrix_data_source;
1066 procedure (NULL, NULL, NULL);
1068 free (split_values);
1069 free (nr_factor_values);
1071 fh_close_handle (data_file);
1074 /* Mirror data across the diagonal of matrix CP which contains
1075 CONTENT type data. */
1077 fill_matrix (int content, double *cp)
1079 int type = content_type[content];
1081 if (type == 1 && section != FULL)
1083 if (diag == NODIAGONAL)
1085 const double fill = content == CORR ? 1.0 : SYSMIS;
1088 for (i = 0; i < n_continuous; i++)
1089 cp[i * (1 + n_continuous)] = fill;
1095 if (section == LOWER)
1097 int n_lines = n_continuous;
1098 if (section != FULL && diag == NODIAGONAL)
1101 for (r = 1; r < n_lines; r++)
1102 for (c = 0; c < r; c++)
1103 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1107 assert (section == UPPER);
1108 for (r = 1; r < n_continuous; r++)
1109 for (c = 0; c < r; c++)
1110 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1118 for (c = 1; c < n_continuous; c++)
1123 /* Read data lines for content type CONTENT from the data file. If
1124 PER_FACTOR is nonzero, then factor information is read from the
1125 data file. Data is for cell number CELL. */
1127 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1130 const int type = content_type[content];
1132 /* Number of lines that must be parsed from the data file for this
1136 /* Current position in vector or matrix. */
1146 n_lines = n_continuous;
1147 if (section != FULL && diag == NODIAGONAL)
1151 cp = nr_data[content][cell];
1152 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1155 for (i = 0; i < n_lines; i++)
1159 if (!nr_read_splits (1))
1161 if (per_factor && !nr_read_factors (cell))
1168 n_cols = n_continuous;
1178 n_cols = n_continuous - i;
1179 if (diag == NODIAGONAL)
1186 n_cols = n_continuous;
1202 for (j = 0; j < n_cols; j++)
1208 msg (SE, _("expecting value for %s %s"),
1209 default_dict.var[j]->name, context ());
1215 if (!force_eol (content_names[content]))
1217 debug_printf (("\n"));
1220 if (section == LOWER)
1221 cp += n_continuous - n_cols;
1224 fill_matrix (content, nr_data[content][cell]);
1229 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1230 writes them to the output file. Returns success. */
1232 matrix_data_read_without_rowtype (void)
1237 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1242 for (i = 0; i <= PROX; i++)
1246 for (cp = contents; *cp != EOC; cp++)
1247 if (*cp != LPAREN && *cp != RPAREN)
1249 int per_factor = is_per_factor[*cp];
1252 n_entries = n_continuous;
1253 if (content_type[*cp] == 1)
1254 n_entries *= n_continuous;
1257 int n_vectors = per_factor ? cells : 1;
1260 nr_data[*cp] = pool_alloc (container,
1261 n_vectors * sizeof **nr_data);
1263 for (i = 0; i < n_vectors; i++)
1264 nr_data[*cp][i] = pool_alloc (container,
1265 n_entries * sizeof ***nr_data);
1274 if (!nr_read_splits (0))
1277 for (bp = contents; *bp != EOC; bp = np)
1281 /* Trap the CONTENTS that we should parse in this pass
1282 between bp and ep. Set np to the starting bp for next
1287 while (*ep != RPAREN)
1295 while (*ep != EOC && *ep != LPAREN)
1304 for (i = 0; i < (per_factor ? cells : 1); i++)
1308 for (cp = bp; cp < ep; cp++)
1309 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1317 if (default_dict.n_splits == 0 || !another_token ())
1322 /* Read the split file variables. If COMPARE is 1, compares the
1323 values read to the last values read and returns 1 if they're equal,
1326 nr_read_splits (int compare)
1328 static int just_read = 0;
1330 if (compare && just_read)
1336 if (default_dict.n_splits == 0)
1342 split_values[0] = ++default_dict.splits[0]->p.mxd.subtype;
1352 for (i = 0; i < default_dict.n_splits; i++)
1358 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1364 split_values[i] = mtokval;
1365 else if (split_values[i] != mtokval)
1367 msg (SE, _("Expecting value %g for %s."),
1368 split_values[i], default_dict.splits[i]->name);
1377 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1378 values read to the last values read and returns 1 if they're equal,
1381 nr_read_factors (int cell)
1388 assert (max_cell_index >= cell);
1389 if (cell != max_cell_index)
1400 for (i = 0; i < n_factors; i++)
1406 msg (SE, _("Syntax error expecting factor value %s."),
1412 nr_factor_values[i + n_factors * cell] = mtokval;
1413 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1415 msg (SE, _("Syntax error expecting value %g for %s %s."),
1416 nr_factor_values[i + n_factors * cell],
1417 factors[i]->name, context ());
1426 /* Write the contents of a cell having content type CONTENT and data
1427 CP to the active file. */
1429 dump_cell_content (int content, double *cp)
1431 int type = content_type[content];
1434 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1435 content_names[content], 8);
1438 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1442 int n_lines = (type == 1) ? n_continuous : 1;
1445 for (i = 0; i < n_lines; i++)
1449 for (j = 0; j < n_continuous; j++)
1451 temp_case->data[(default_dict.var
1452 [first_continuous + j]->fv)].f = *cp;
1453 debug_printf (("c:%s(%g) ",
1454 default_dict.var[first_continuous + j]->name,
1459 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1460 default_dict.var[first_continuous + i]->name,
1462 debug_printf (("\n"));
1468 /* Finally dump out everything from nr_data[] to the output file. */
1470 nr_output_data (void)
1475 for (i = 0; i < default_dict.n_splits; i++)
1476 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1483 for (cell = 0; cell < cells; cell++)
1488 for (factor = 0; factor < n_factors; factor++)
1490 temp_case->data[factors[factor]->fv].f
1491 = nr_factor_values[factor + cell * n_factors];
1492 debug_printf (("f:%s ", factors[factor]->name));
1499 for (content = 0; content <= PROX; content++)
1500 if (is_per_factor[content])
1502 assert (nr_data[content] != NULL
1503 && nr_data[content][cell] != NULL);
1505 dump_cell_content (content, nr_data[content][cell]);
1517 for (factor = 0; factor < n_factors; factor++)
1518 temp_case->data[factors[factor]->fv].f = SYSMIS;
1521 for (content = 0; content <= PROX; content++)
1522 if (!is_per_factor[content] && nr_data[content] != NULL)
1523 dump_cell_content (content, nr_data[content][0]);
1527 /* Back end, with ROWTYPE_. */
1529 /* Type of current row. */
1530 static int wr_content;
1532 /* All the data for one set of factor values. */
1536 int n_rows[PROX + 1];
1537 double *data[PROX + 1];
1538 struct factor_data *next;
1541 /* All the data, period. */
1542 struct factor_data *wr_data;
1544 /* Current factor. */
1545 struct factor_data *wr_current;
1547 static int wr_read_splits (void);
1548 static int wr_output_data (void);
1549 static int wr_read_rowtype (void);
1550 static int wr_read_factors (void);
1551 static int wr_read_indeps (void);
1552 static int matrix_data_read_with_rowtype (void);
1554 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1555 them to the output file. */
1557 read_matrices_with_rowtype (void)
1560 wr_data = wr_current = NULL;
1561 split_values = NULL;
1564 matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1565 vfm_source = &matrix_data_source;
1567 procedure (NULL, NULL, NULL);
1569 free (split_values);
1570 fh_close_handle (data_file);
1573 /* Read from the data file and write it to the active file. */
1575 matrix_data_read_with_rowtype (void)
1579 if (!wr_read_splits ())
1582 if (!wr_read_factors ())
1585 if (!wr_read_indeps ())
1588 while (another_token ());
1594 /* Read the split file variables. If they differ from the previous
1595 set of split variables then output the data. Returns success. */
1597 wr_read_splits (void)
1601 if (default_dict.n_splits == 0)
1609 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1616 for (i = 0; i < default_dict.n_splits; i++)
1622 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1627 if (compare && split_values[i] != mtokval && !different)
1629 if (!wr_output_data ())
1634 split_values[i] = mtokval;
1641 /* Return strcmp()-type comparison of the n_factors factors at _A and
1642 _B. Sort missing values toward the end. */
1644 compare_factors (const void *pa, const void *pb)
1646 const double *a = (*(struct factor_data **) pa)->factors;
1647 const double *b = (*(struct factor_data **) pb)->factors;
1650 for (i = 0; i < n_factors; i++, a++, b++)
1657 else if (*b == SYSMIS)
1660 return *a - *b < 0 ? -1 : 1;
1666 /* Write out the data for the current split file to the active
1669 wr_output_data (void)
1674 for (i = 0; i < default_dict.n_splits; i++)
1675 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1678 /* Sort the wr_data list. */
1680 struct factor_data **factors;
1681 struct factor_data *iter;
1684 factors = xmalloc (sizeof *factors * cells);
1686 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1689 qsort (factors, cells, sizeof *factors, compare_factors);
1691 wr_data = factors[0];
1692 for (i = 0; i < cells - 1; i++)
1693 factors[i]->next = factors[i + 1];
1694 factors[cells - 1]->next = NULL;
1699 /* Write out records for every set of factor values. */
1701 struct factor_data *iter;
1703 for (iter = wr_data; iter; iter = iter->next)
1708 for (factor = 0; factor < n_factors; factor++)
1710 temp_case->data[factors[factor]->fv].f
1711 = iter->factors[factor];
1712 debug_printf (("f:%s ", factors[factor]->name));
1719 for (content = 0; content <= PROX; content++)
1721 if (!iter->n_rows[content])
1725 int type = content_type[content];
1726 int n_lines = (type == 1
1728 - (section != FULL && diag == NODIAGONAL))
1731 if (n_lines != iter->n_rows[content])
1733 msg (SE, _("Expected %d lines of data for %s content; "
1734 "actually saw %d lines. No data will be "
1735 "output for this content."),
1736 n_lines, content_names[content],
1737 iter->n_rows[content]);
1742 fill_matrix (content, iter->data[content]);
1744 dump_cell_content (content, iter->data[content]);
1750 pool_destroy (container);
1751 container = pool_create ();
1753 wr_data = wr_current = NULL;
1758 /* Read ROWTYPE_ from the data file. Return success. */
1760 wr_read_rowtype (void)
1762 if (wr_content != -1)
1764 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1769 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1777 memcpy (s, mtokstr, min (15, mtoklen));
1778 s[min (15, mtoklen)] = 0;
1780 for (cp = s; *cp; cp++)
1781 *cp = toupper ((unsigned char) *cp);
1783 wr_content = string_to_content_type (s, NULL);
1786 if (wr_content == -1)
1788 msg (SE, _("Syntax error %s."), context ());
1795 /* Read the factors for the current row. Select a set of factors and
1796 point wr_current to it. */
1798 wr_read_factors (void)
1800 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1806 for (i = 0; i < n_factors; i++)
1812 if (!wr_read_rowtype ())
1819 msg (SE, _("Syntax error expecting factor value %s."),
1824 factor_values[i] = mtokval;
1827 if (wr_content == -1)
1831 if (!wr_read_rowtype ())
1835 /* Try the most recent factor first as a simple caching
1841 for (i = 0; i < n_factors; i++)
1842 if (factor_values[i] != wr_current->factors[i])
1847 /* Linear search through the list. */
1850 struct factor_data *iter;
1852 for (iter = wr_data; iter; iter = iter->next)
1856 for (i = 0; i < n_factors; i++)
1857 if (factor_values[i] != iter->factors[i])
1867 /* Not found. Make a new item. */
1869 struct factor_data *new = pool_alloc (container, sizeof *new);
1871 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1876 for (i = 0; i < n_factors; i++)
1877 new->factors[i] = factor_values[i];
1883 for (i = 0; i <= PROX; i++)
1886 new->data[i] = NULL;
1890 new->next = wr_data;
1891 wr_data = wr_current = new;
1896 local_free (factor_values);
1900 local_free (factor_values);
1904 /* Read the independent variables into wr_current. */
1906 wr_read_indeps (void)
1908 struct factor_data *c = wr_current;
1909 const int type = content_type[wr_content];
1910 const int n_rows = c->n_rows[wr_content];
1914 /* Allocate room for data if necessary. */
1915 if (c->data[wr_content] == NULL)
1917 int n_items = n_continuous;
1919 n_items *= n_continuous;
1921 c->data[wr_content] = pool_alloc (container,
1922 sizeof **c->data * n_items);
1925 cp = &c->data[wr_content][n_rows * n_continuous];
1927 /* Figure out how much to read from this line. */
1934 msg (SE, _("Duplicate specification for %s."),
1935 content_names[wr_content]);
1939 n_cols = n_continuous;
1944 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1946 msg (SE, _("Too many rows of matrix data for %s."),
1947 content_names[wr_content]);
1954 n_cols = n_rows + 1;
1955 if (diag == NODIAGONAL)
1960 n_cols = n_continuous - n_rows;
1961 if (diag == NODIAGONAL)
1968 n_cols = n_continuous;
1977 c->n_rows[wr_content]++;
1979 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1981 /* Read N_COLS items at CP. */
1985 for (j = 0; j < n_cols; j++)
1991 msg (SE, _("Syntax error expecting value for %s %s."),
1992 default_dict.var[first_continuous + j]->name, context ());
1998 if (!force_eol (content_names[wr_content]))
2000 debug_printf (("\n"));
2006 /* Matrix source. */
2008 struct case_stream matrix_data_source =