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
24 #include "algorithm.h"
28 #include "file-handle.h"
36 #include "value-labels.h"
40 /* Constants for DISPLAY utility. */
52 int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
54 /* Sets the widths of all the columns and heights of all the rows in
55 table T for driver D. */
57 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
59 static const int max[] = {20, 5, 35, 3, 0};
63 for (p = max; *p; p++)
64 t->w[p - max] = min (tab_natural_width (t, d, p - max),
65 *p * d->prop_em_width);
66 for (i = 0; i < t->nr; i++)
67 t->h[i] = tab_natural_height (t, d, i);
70 /* SYSFILE INFO utility. */
72 cmd_sysfile_info (void)
74 struct file_handle *h;
77 struct sfm_read_info inf;
81 lex_match_id ("SYSFILE");
82 lex_match_id ("INFO");
84 lex_match_id ("FILE");
87 h = fh_parse_file_handle ();
91 d = sfm_read_dictionary (h, &inf);
96 t = tab_create (2, 9, 0);
97 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
98 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
99 tab_text (t, 1, 0, TAB_LEFT, fh_handle_filename (h));
100 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
101 tab_text (t, 1, 1, TAB_LEFT,
102 d->label ? d->label : _("No label."));
103 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
104 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
105 inf.creation_date, inf.creation_time, inf.product);
106 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
107 tab_text (t, 1, 3, TAB_LEFT, inf.bigendian ? _("Big.") : _("Little."));
108 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
109 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
111 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
112 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
113 inf.ncases == -1 ? _("Unknown") : "%d", inf.ncases);
114 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
115 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
116 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
117 tab_text (t, 1, 7, TAB_LEFT,
118 d->weight_var[0] ? d->weight_var : _("Not weighted."));
119 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
120 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
121 _("Compression %s."), inf.compressed ? _("on") : _("off"));
122 tab_dim (t, tab_natural_dimensions);
125 nr = 1 + 2 * d->nvar;
126 t = tab_create (4, nr, 1);
127 tab_dim (t, sysfile_info_dim);
128 tab_headers (t, 0, 0, 1, 0);
129 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
130 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
131 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
132 tab_hline (t, TAL_2, 0, 3, 1);
133 for (r = 1, i = 0; i < d->nvar; i++)
135 int nvl = val_labs_count (d->var[i]->val_labs);
137 if (r + 10 + nvl > nr)
139 nr = max (nr * d->nvar / (i + 1), nr);
141 tab_realloc (t, 4, nr);
144 r = describe_variable (d->var[i], t, r, AS_DICTIONARY);
146 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
147 tab_vline (t, TAL_1, 0, 0, r);
148 tab_vline (t, TAL_1, 1, 0, r);
149 tab_vline (t, TAL_1, 3, 0, r);
150 tab_resize (t, -1, r);
151 tab_flags (t, SOMF_NO_TITLE);
156 return lex_end_of_command ();
159 /* DISPLAY utility. */
161 static void display_macros (void);
162 static void display_documents (void);
163 static void display_variables (struct variable **, int, int);
164 static void display_vectors (int sorted);
169 /* Whether to sort the list of variables alphabetically. */
172 /* Variables to display. */
174 struct variable **vl;
176 lex_match_id ("DISPLAY");
178 if (lex_match_id ("MACROS"))
180 else if (lex_match_id ("DOCUMENTS"))
181 display_documents ();
182 else if (lex_match_id ("FILE"))
185 if (!lex_force_match_id ("LABEL"))
187 if (default_dict.label == NULL)
188 tab_output_text (TAB_LEFT,
189 _("The active file does not have a file label."));
192 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
193 tab_output_text (TAB_LEFT | TAT_FIX, default_dict.label);
198 static const char *sbc[] =
199 {"NAMES", "INDEX", "VARIABLES", "LABELS",
200 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
204 sorted = lex_match_id ("SORTED");
206 for (cp = sbc; *cp; cp++)
207 if (token == T_ID && lex_id_match (*cp, tokid))
219 display_vectors (sorted);
224 lex_match_id ("VARIABLES");
229 if (!parse_variables (NULL, &vl, &n, PV_NONE))
237 fill_all_vars (&vl, &n, FV_NONE);
239 if (as == AS_SCRATCH)
242 for (i = 0, m = n; i < n; i++)
243 if (vl[i]->name[0] != '#')
254 msg (SW, _("No variables to display."));
259 sort (vl, n, sizeof *vl, compare_variables, NULL);
261 display_variables (vl, n, as);
266 return lex_end_of_command ();
270 display_macros (void)
273 tab_output_text (TAB_LEFT, _("Macros not supported."));
277 display_documents (void)
280 if (default_dict.n_documents == 0)
281 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
282 "contain any documents."));
288 tab_output_text (TAB_LEFT | TAT_TITLE,
289 _("Documents in the active file:"));
292 for (i = 0; i < default_dict.n_documents; i++)
296 memcpy (buf, &default_dict.documents[i * 80], 80);
297 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
301 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
308 /* Sets the widths of all the columns and heights of all the rows in
309 table T for driver D. */
311 variables_dim (struct tab_table *t, struct outp_driver *d)
316 t->w[0] = tab_natural_width (t, d, 0);
317 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
319 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
320 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
325 t->w[pc] = tab_natural_width (t, d, pc);
327 for (i = 0; i < t->nr; i++)
328 t->h[i] = tab_natural_height (t, d, i);
332 display_variables (struct variable **vl, int n, int as)
334 struct variable **vp = vl; /* Variable pointer. */
336 int nc; /* Number of columns. */
337 int nr; /* Number of rows. */
338 int pc; /* `Position column' */
339 int r; /* Current row. */
356 t = tab_create (nc, n + 5, 1);
357 tab_headers (t, 0, 0, 1, 0);
359 tab_hline (t, TAL_2, 0, nc - 1, 1);
360 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
363 pc = (as == AS_INDEX ? 1 : 3);
364 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
366 if (as == AS_DICTIONARY || as == AS_VARIABLES)
367 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
368 else if (as == AS_LABELS)
369 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
370 tab_dim (t, variables_dim);
372 for (i = r = 1; i <= n; i++)
380 if (as == AS_DICTIONARY || as == AS_VARIABLES)
382 int nvl = val_labs_count (v->val_labs);
384 if (r + 10 + nvl > nr)
386 nr = max (nr * n / (i + 1), nr);
388 tab_realloc (t, nc, nr);
391 r = describe_variable (v, t, r, as);
393 tab_text (t, 0, r, TAB_LEFT, v->name);
395 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
396 v->label == NULL ? "(no label)" : v->label);
399 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
400 tab_hline (t, TAL_1, 0, nc - 1, r);
405 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
408 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
409 tab_vline (t, TAL_1, 1, 0, r - 1);
412 tab_flags (t, SOMF_NO_TITLE);
413 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
414 tab_vline (t, TAL_1, 3, 0, r - 1);
415 tab_resize (t, -1, r);
416 tab_columns (t, TAB_COL_DOWN, 1);
420 /* Puts a description of variable V into table T starting at row R.
421 The variable will be described in the format AS. Returns the next
422 row available for use in the table. */
424 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
426 /* Put the name, var label, and position into the first row. */
427 tab_text (t, 0, r, TAB_LEFT, v->name);
428 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
430 if (as == AS_DICTIONARY && v->label)
432 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
436 /* Print/write format, or print and write formats. */
437 if (v->print.type == v->write.type
438 && v->print.w == v->write.w
439 && v->print.d == v->write.d)
441 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
442 fmt_to_string (&v->print));
447 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
448 _("Print Format: %s"), fmt_to_string (&v->print));
450 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
451 _("Write Format: %s"), fmt_to_string (&v->write));
455 /* Missing values if any. */
456 if (v->miss_type != MISSING_NONE)
459 char *cp = stpcpy (buf, _("Missing Values: "));
461 if (v->type == NUMERIC)
462 switch (v->miss_type)
465 sprintf (cp, "%g", v->missing[0].f);
468 sprintf (cp, "%g; %g", v->missing[0].f, v->missing[1].f);
471 sprintf (cp, "%g; %g; %g", v->missing[0].f,
472 v->missing[1].f, v->missing[2].f);
475 sprintf (cp, "%g THRU %g", v->missing[0].f, v->missing[1].f);
478 sprintf (cp, "LOWEST THRU %g", v->missing[0].f);
481 sprintf (cp, "%g THRU HIGHEST", v->missing[0].f);
483 case MISSING_RANGE_1:
484 sprintf (cp, "%g THRU %g; %g",
485 v->missing[0].f, v->missing[1].f, v->missing[2].f);
488 sprintf (cp, "LOWEST THRU %g; %g",
489 v->missing[0].f, v->missing[1].f);
492 sprintf (cp, "%g THRU HIGHEST; %g",
493 v->missing[0].f, v->missing[1].f);
502 for (i = 0; i < v->miss_type; i++)
505 cp = stpcpy (cp, "; ");
507 memcpy (cp, v->missing[i].s, v->width);
514 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
519 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
521 struct val_labs_iterator *i;
526 tab_text (t, 1, r, TAB_LEFT, _("Value"));
527 tab_text (t, 2, r, TAB_LEFT, _("Label"));
531 tab_hline (t, TAL_1, 1, 2, r);
532 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
533 vl = val_labs_next (v->val_labs, &i))
537 if (v->type == ALPHA)
539 memcpy (buf, vl->value.s, v->width);
543 sprintf (buf, "%g", vl->value.f);
545 tab_text (t, 1, r, TAB_NONE, buf);
546 tab_text (t, 2, r, TAB_LEFT, vl->label);
550 tab_vline (t, TAL_1, 2, orig_r, r - 1);
553 /* Draw a line below the last row of information on this variable. */
554 tab_hline (t, TAL_1, 0, 3, r);
560 compare_vectors_by_name (const void *a, const void *b)
562 return strcmp ((*((struct vector **) a))->name, (*((struct vector **) b))->name);
565 /* Display a list of vectors. If SORTED is nonzero then they are
566 sorted alphabetically. */
568 display_vectors (int sorted)
576 msg (SW, _("No vectors defined."));
580 vl = xmalloc (sizeof *vl * nvec);
581 for (i = 0; i < nvec; i++)
584 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
586 t = tab_create (1, nvec + 1, 0);
587 tab_headers (t, 0, 0, 1, 0);
588 tab_columns (t, TAB_COL_DOWN, 1);
589 tab_dim (t, tab_natural_dimensions);
590 tab_hline (t, TAL_1, 0, 0, 1);
591 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
592 tab_flags (t, SOMF_NO_TITLE);
593 for (i = 0; i < nvec; i++)
594 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);