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"
27 #include "dictionary.h"
29 #include "file-handle.h"
37 #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_reader *reader;
78 struct sfm_read_info info;
82 lex_match_id ("FILE");
89 reader = sfm_open_reader (h, &d, &info);
92 sfm_close_reader (reader);
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, handle_get_filename (h));
98 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
100 const char *label = dict_get_label (d);
102 label = _("No label.");
103 tab_text (t, 1, 1, TAB_LEFT, label);
105 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
106 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
107 info.creation_date, info.creation_time, info.product);
108 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
109 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
110 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
111 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
112 dict_get_var_cnt (d));
113 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
114 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
115 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
116 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
117 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
118 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
120 struct variable *weight_var = dict_get_weight (d);
121 tab_text (t, 1, 7, TAB_LEFT,
122 weight_var != NULL ? weight_var->name : _("Not weighted."));
124 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
125 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
126 _("Compression %s."), info.compressed ? _("on") : _("off"));
127 tab_dim (t, tab_natural_dimensions);
130 nr = 1 + 2 * dict_get_var_cnt (d);
131 t = tab_create (4, nr, 1);
132 tab_dim (t, sysfile_info_dim);
133 tab_headers (t, 0, 0, 1, 0);
134 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
135 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
136 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
137 tab_hline (t, TAL_2, 0, 3, 1);
138 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
140 struct variable *v = dict_get_var (d, i);
141 int nvl = val_labs_count (v->val_labs);
143 if (r + 10 + nvl > nr)
145 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
147 tab_realloc (t, 4, nr);
150 r = describe_variable (v, t, r, AS_DICTIONARY);
152 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
153 tab_vline (t, TAL_1, 0, 0, r);
154 tab_vline (t, TAL_1, 1, 0, r);
155 tab_vline (t, TAL_1, 3, 0, r);
156 tab_resize (t, -1, r);
157 tab_flags (t, SOMF_NO_TITLE);
162 return lex_end_of_command ();
165 /* DISPLAY utility. */
167 static void display_macros (void);
168 static void display_documents (void);
169 static void display_variables (struct variable **, int, int);
170 static void display_vectors (int sorted);
175 /* Whether to sort the list of variables alphabetically. */
178 /* Variables to display. */
180 struct variable **vl;
182 if (lex_match_id ("MACROS"))
184 else if (lex_match_id ("DOCUMENTS"))
185 display_documents ();
186 else if (lex_match_id ("FILE"))
189 if (!lex_force_match_id ("LABEL"))
191 if (dict_get_label (default_dict) == NULL)
192 tab_output_text (TAB_LEFT,
193 _("The active file does not have a file label."));
196 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
197 tab_output_text (TAB_LEFT | TAT_FIX, dict_get_label (default_dict));
202 static const char *sbc[] =
203 {"NAMES", "INDEX", "VARIABLES", "LABELS",
204 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
208 sorted = lex_match_id ("SORTED");
210 for (cp = sbc; *cp; cp++)
211 if (token == T_ID && lex_id_match (*cp, tokid))
223 display_vectors (sorted);
228 lex_match_id ("VARIABLES");
233 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
241 dict_get_vars (default_dict, &vl, &n, 0);
243 if (as == AS_SCRATCH)
246 for (i = 0, m = n; i < n; i++)
247 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
258 msg (SW, _("No variables to display."));
263 sort (vl, n, sizeof *vl, compare_variables, NULL);
265 display_variables (vl, n, as);
270 return lex_end_of_command ();
274 display_macros (void)
277 tab_output_text (TAB_LEFT, _("Macros not supported."));
281 display_documents (void)
283 const char *documents = dict_get_documents (default_dict);
286 if (documents == NULL)
287 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
288 "contain any documents."));
291 size_t n_lines = strlen (documents) / 80;
295 tab_output_text (TAB_LEFT | TAT_TITLE,
296 _("Documents in the active file:"));
299 for (i = 0; i < n_lines; i++)
303 memcpy (buf, &documents[i * 80], 80);
304 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
308 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
315 /* Sets the widths of all the columns and heights of all the rows in
316 table T for driver D. */
318 variables_dim (struct tab_table *t, struct outp_driver *d)
323 t->w[0] = tab_natural_width (t, d, 0);
324 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
326 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
327 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
332 t->w[pc] = tab_natural_width (t, d, pc);
334 for (i = 0; i < t->nr; i++)
335 t->h[i] = tab_natural_height (t, d, i);
339 display_variables (struct variable **vl, int n, int as)
341 struct variable **vp = vl; /* Variable pointer. */
343 int nc; /* Number of columns. */
344 int nr; /* Number of rows. */
345 int pc; /* `Position column' */
346 int r; /* Current row. */
363 t = tab_create (nc, n + 5, 1);
364 tab_headers (t, 0, 0, 1, 0);
366 tab_hline (t, TAL_2, 0, nc - 1, 1);
367 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
368 pc = (as == AS_INDEX ? 1 : 3);
370 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
371 if (as == AS_DICTIONARY || as == AS_VARIABLES)
372 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
373 else if (as == AS_LABELS)
374 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
375 tab_dim (t, variables_dim);
377 for (i = r = 1; i <= n; i++)
385 if (as == AS_DICTIONARY || as == AS_VARIABLES)
387 int nvl = val_labs_count (v->val_labs);
389 if (r + 10 + nvl > nr)
391 nr = max (nr * n / (i + 1), nr);
393 tab_realloc (t, nc, nr);
396 r = describe_variable (v, t, r, as);
398 tab_text (t, 0, r, TAB_LEFT, v->name);
400 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
401 v->label == NULL ? "(no label)" : v->label);
404 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
405 tab_hline (t, TAL_1, 0, nc - 1, r);
410 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
413 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
414 tab_vline (t, TAL_1, 1, 0, r - 1);
417 tab_flags (t, SOMF_NO_TITLE);
418 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
419 tab_vline (t, TAL_1, 3, 0, r - 1);
420 tab_resize (t, -1, r);
421 tab_columns (t, TAB_COL_DOWN, 1);
425 /* Puts a description of variable V into table T starting at row R.
426 The variable will be described in the format AS. Returns the next
427 row available for use in the table. */
429 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
431 /* Put the name, var label, and position into the first row. */
432 tab_text (t, 0, r, TAB_LEFT, v->name);
433 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
435 if (as == AS_DICTIONARY && v->label)
437 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
441 /* Print/write format, or print and write formats. */
442 if (v->print.type == v->write.type
443 && v->print.w == v->write.w
444 && v->print.d == v->write.d)
446 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
447 fmt_to_string (&v->print));
452 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
453 _("Print Format: %s"), fmt_to_string (&v->print));
455 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
456 _("Write Format: %s"), fmt_to_string (&v->write));
460 /* Missing values if any. */
461 if (v->miss_type != MISSING_NONE)
464 char *cp = stpcpy (buf, _("Missing Values: "));
466 if (v->type == NUMERIC)
467 switch (v->miss_type)
470 sprintf (cp, "%g", v->missing[0].f);
473 sprintf (cp, "%g; %g", v->missing[0].f, v->missing[1].f);
476 sprintf (cp, "%g; %g; %g", v->missing[0].f,
477 v->missing[1].f, v->missing[2].f);
480 sprintf (cp, "%g THRU %g", v->missing[0].f, v->missing[1].f);
483 sprintf (cp, "LOWEST THRU %g", v->missing[0].f);
486 sprintf (cp, "%g THRU HIGHEST", v->missing[0].f);
488 case MISSING_RANGE_1:
489 sprintf (cp, "%g THRU %g; %g",
490 v->missing[0].f, v->missing[1].f, v->missing[2].f);
493 sprintf (cp, "LOWEST THRU %g; %g",
494 v->missing[0].f, v->missing[1].f);
497 sprintf (cp, "%g THRU HIGHEST; %g",
498 v->missing[0].f, v->missing[1].f);
507 for (i = 0; i < v->miss_type; i++)
510 cp = stpcpy (cp, "; ");
512 memcpy (cp, v->missing[i].s, v->width);
519 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
524 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
526 struct val_labs_iterator *i;
531 tab_text (t, 1, r, TAB_LEFT, _("Value"));
532 tab_text (t, 2, r, TAB_LEFT, _("Label"));
536 tab_hline (t, TAL_1, 1, 2, r);
537 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
538 vl = val_labs_next (v->val_labs, &i))
542 if (v->type == ALPHA)
544 memcpy (buf, vl->value.s, v->width);
548 sprintf (buf, "%g", vl->value.f);
550 tab_text (t, 1, r, TAB_NONE, buf);
551 tab_text (t, 2, r, TAB_LEFT, vl->label);
555 tab_vline (t, TAL_1, 2, orig_r, r - 1);
558 /* Draw a line below the last row of information on this variable. */
559 tab_hline (t, TAL_1, 0, 3, r);
565 compare_vectors_by_name (const void *a_, const void *b_)
567 struct vector *const *pa = a_;
568 struct vector *const *pb = b_;
569 struct vector *a = *pa;
570 struct vector *b = *pb;
572 return strcmp (a->name, b->name);
575 /* Display a list of vectors. If SORTED is nonzero then they are
576 sorted alphabetically. */
578 display_vectors (int sorted)
580 const struct vector **vl;
585 nvec = dict_get_vector_cnt (default_dict);
588 msg (SW, _("No vectors defined."));
592 vl = xmalloc (sizeof *vl * nvec);
593 for (i = 0; i < nvec; i++)
594 vl[i] = dict_get_vector (default_dict, i);
596 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
598 t = tab_create (1, nvec + 1, 0);
599 tab_headers (t, 0, 0, 1, 0);
600 tab_columns (t, TAB_COL_DOWN, 1);
601 tab_dim (t, tab_natural_dimensions);
602 tab_hline (t, TAL_1, 0, 0, 1);
603 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
604 tab_flags (t, SOMF_NO_TITLE);
605 for (i = 0; i < nvec; i++)
606 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);