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
28 #include "file-handle.h"
38 /* Constants for DISPLAY utility. */
50 int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
52 /* Sets the widths of all the columns and heights of all the rows in
53 table T for driver D. */
55 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
57 static const int max[] = {20, 5, 35, 3, 0};
61 for (p = max; *p; p++)
62 t->w[p - max] = min (tab_natural_width (t, d, p - max),
63 *p * d->prop_em_width);
64 for (i = 0; i < t->nr; i++)
65 t->h[i] = tab_natural_height (t, d, i);
68 /* SYSFILE INFO utility. */
70 cmd_sysfile_info (void)
72 struct file_handle *h;
75 struct sfm_read_info inf;
79 lex_match_id ("SYSFILE");
80 lex_match_id ("INFO");
82 lex_match_id ("FILE");
85 h = fh_parse_file_handle ();
89 d = sfm_read_dictionary (h, &inf);
94 t = tab_create (2, 9, 0);
95 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
96 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
97 tab_text (t, 1, 0, TAB_LEFT, fh_handle_filename (h));
98 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
99 tab_text (t, 1, 1, TAB_LEFT,
100 d->label ? d->label : _("No label."));
101 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
102 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
103 inf.creation_date, inf.creation_time, inf.product);
104 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
105 tab_text (t, 1, 3, TAB_LEFT, inf.bigendian ? _("Big.") : _("Little."));
106 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
107 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
109 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
110 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
111 inf.ncases == -1 ? _("Unknown") : "%d", inf.ncases);
112 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
113 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
114 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
115 tab_text (t, 1, 7, TAB_LEFT,
116 d->weight_var[0] ? d->weight_var : _("Not weighted."));
117 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
118 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
119 _("Compression %s."), inf.compressed ? _("on") : _("off"));
120 tab_dim (t, tab_natural_dimensions);
123 nr = 1 + 2 * d->nvar;
124 t = tab_create (4, nr, 1);
125 tab_dim (t, sysfile_info_dim);
126 tab_headers (t, 0, 0, 1, 0);
127 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
128 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
129 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
130 tab_hline (t, TAL_2, 0, 3, 1);
131 for (r = 1, i = 0; i < d->nvar; i++)
133 int nvl = d->var[i]->val_lab ? avl_count (d->var[i]->val_lab) : 0;
135 if (r + 10 + nvl > nr)
137 nr = max (nr * d->nvar / (i + 1), nr);
139 tab_realloc (t, 4, nr);
142 r = describe_variable (d->var[i], t, r, AS_DICTIONARY);
144 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
145 tab_vline (t, TAL_1, 0, 0, r);
146 tab_vline (t, TAL_1, 1, 0, r);
147 tab_vline (t, TAL_1, 3, 0, r);
148 tab_resize (t, -1, r);
149 tab_flags (t, SOMF_NO_TITLE);
154 return lex_end_of_command ();
157 /* DISPLAY utility. */
159 static void display_macros (void);
160 static void display_documents (void);
161 static void display_variables (struct variable **, int, int);
162 static void display_vectors (int sorted);
164 static int cmp_var_by_name (const void *, const void *);
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 qsort (vl, n, sizeof *vl, cmp_var_by_name);
261 display_variables (vl, n, as);
266 return lex_end_of_command ();
270 cmp_var_by_name (const void *a, const void *b)
272 return strcmp ((*((struct variable **) a))->name, (*((struct variable **) b))->name);
276 display_macros (void)
279 tab_output_text (TAB_LEFT, _("Macros not supported."));
283 display_documents (void)
286 if (default_dict.n_documents == 0)
287 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
288 "contain any documents."));
294 tab_output_text (TAB_LEFT | TAT_TITLE,
295 _("Documents in the active file:"));
298 for (i = 0; i < default_dict.n_documents; i++)
302 memcpy (buf, &default_dict.documents[i * 80], 80);
303 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
307 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
314 /* Sets the widths of all the columns and heights of all the rows in
315 table T for driver D. */
317 variables_dim (struct tab_table *t, struct outp_driver *d)
322 t->w[0] = tab_natural_width (t, d, 0);
323 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
325 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
326 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
331 t->w[pc] = tab_natural_width (t, d, pc);
333 for (i = 0; i < t->nr; i++)
334 t->h[i] = tab_natural_height (t, d, i);
338 display_variables (struct variable **vl, int n, int as)
340 struct variable **vp = vl; /* Variable pointer. */
342 int nc; /* Number of columns. */
343 int nr; /* Number of rows. */
344 int pc; /* `Position column' */
345 int r; /* Current row. */
362 t = tab_create (nc, n + 5, 1);
363 tab_headers (t, 0, 0, 1, 0);
365 tab_hline (t, TAL_2, 0, nc - 1, 1);
366 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
369 pc = (as == AS_INDEX ? 1 : 3);
370 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
372 if (as == AS_DICTIONARY || as == AS_VARIABLES)
373 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
374 else if (as == AS_LABELS)
375 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
376 tab_dim (t, variables_dim);
378 for (i = r = 1; i <= n; i++)
386 if (as == AS_DICTIONARY || as == AS_VARIABLES)
388 int nvl = v->val_lab ? avl_count (v->val_lab) : 0;
390 if (r + 10 + nvl > nr)
392 nr = max (nr * n / (i + 1), nr);
394 tab_realloc (t, nc, nr);
397 r = describe_variable (v, t, r, as);
399 tab_text (t, 0, r, TAB_LEFT, v->name);
401 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
402 v->label == NULL ? "(no label)" : v->label);
405 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
406 tab_hline (t, TAL_1, 0, nc - 1, r);
411 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
414 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
415 tab_vline (t, TAL_1, 1, 0, r - 1);
418 tab_flags (t, SOMF_NO_TITLE);
419 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
420 tab_vline (t, TAL_1, 3, 0, r - 1);
421 tab_resize (t, -1, r);
422 tab_columns (t, TAB_COL_DOWN, 1);
426 /* Puts a description of variable V into table T starting at row R.
427 The variable will be described in the format AS. Returns the next
428 row available for use in the table. */
430 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
432 /* Put the name, var label, and position into the first row. */
433 tab_text (t, 0, r, TAB_LEFT, v->name);
434 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
436 if (as == AS_DICTIONARY && v->label)
438 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
442 /* Print/write format, or print and write formats. */
443 if (v->print.type == v->write.type
444 && v->print.w == v->write.w
445 && v->print.d == v->write.d)
447 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
448 fmt_to_string (&v->print));
453 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
454 _("Print Format: %s"), fmt_to_string (&v->print));
456 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
457 _("Write Format: %s"), fmt_to_string (&v->write));
461 /* Missing values if any. */
462 if (v->miss_type != MISSING_NONE)
465 char *cp = stpcpy (buf, _("Missing Values: "));
467 if (v->type == NUMERIC)
468 switch (v->miss_type)
471 sprintf (cp, "%g", v->missing[0].f);
474 sprintf (cp, "%g; %g", v->missing[0].f, v->missing[1].f);
477 sprintf (cp, "%g; %g; %g", v->missing[0].f,
478 v->missing[1].f, v->missing[2].f);
481 sprintf (cp, "%g THRU %g", v->missing[0].f, v->missing[1].f);
484 sprintf (cp, "LOWEST THRU %g", v->missing[0].f);
487 sprintf (cp, "%g THRU HIGHEST", v->missing[0].f);
489 case MISSING_RANGE_1:
490 sprintf (cp, "%g THRU %g; %g",
491 v->missing[0].f, v->missing[1].f, v->missing[2].f);
494 sprintf (cp, "LOWEST THRU %g; %g",
495 v->missing[0].f, v->missing[1].f);
498 sprintf (cp, "%g THRU HIGHEST; %g",
499 v->missing[0].f, v->missing[1].f);
508 for (i = 0; i < v->miss_type; i++)
511 cp = stpcpy (cp, "; ");
513 memcpy (cp, v->missing[i].s, v->width);
520 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
525 if (as == AS_DICTIONARY && v->val_lab)
528 struct value_label *vl;
529 int nvl = avl_count (v->val_lab);
534 tab_text (t, 1, r, TAB_LEFT, _("Value"));
535 tab_text (t, 2, r, TAB_LEFT, _("Label"));
539 tab_hline (t, TAL_1, 1, 2, r);
540 avl_traverser_init (trav);
541 for (i = 1, vl = avl_traverse (v->val_lab, &trav); vl;
542 i++, vl = avl_traverse (v->val_lab, &trav))
546 if (v->type == ALPHA)
548 memcpy (buf, vl->v.s, v->width);
552 sprintf (buf, "%g", vl->v.f);
554 tab_text (t, 1, r, TAB_NONE, buf);
555 tab_text (t, 2, r, TAB_LEFT, vl->s);
566 vl = avl_traverse (v->val_lab, &trav);
569 tab_vline (t, TAL_1, 2, orig_r, r - 1);
572 /* Draw a line below the last row of information on this variable. */
573 tab_hline (t, TAL_1, 0, 3, r);
579 compare_vectors_by_name (const void *a, const void *b)
581 return strcmp ((*((struct vector **) a))->name, (*((struct vector **) b))->name);
584 /* Display a list of vectors. If SORTED is nonzero then they are
585 sorted alphabetically. */
587 display_vectors (int sorted)
595 msg (SW, _("No vectors defined."));
599 vl = xmalloc (sizeof *vl * nvec);
600 for (i = 0; i < nvec; i++)
603 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
605 t = tab_create (1, nvec + 1, 0);
606 tab_headers (t, 0, 0, 1, 0);
607 tab_columns (t, TAB_COL_DOWN, 1);
608 tab_dim (t, tab_natural_dimensions);
609 tab_hline (t, TAL_1, 0, 0, 1);
610 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
611 tab_flags (t, SOMF_NO_TITLE);
612 for (i = 0; i < nvec; i++)
613 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);