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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <data/dictionary.h>
26 #include <data/file-handle-def.h>
27 #include <data/procedure.h>
28 #include <data/sys-file-reader.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/file-handle.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/array.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/magic.h>
39 #include <libpspp/message.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <output/manager.h>
43 #include <output/output.h>
44 #include <output/table.h>
47 #define _(msgid) gettext (msgid)
49 /* Constants for DISPLAY utility. */
61 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
63 /* Sets the widths of all the columns and heights of all the rows in
64 table T for driver D. */
66 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
68 static const int max[] = {20, 5, 35, 3, 0};
72 for (p = max; *p; p++)
73 t->w[p - max] = min (tab_natural_width (t, d, p - max),
74 *p * d->prop_em_width);
75 for (i = 0; i < t->nr; i++)
76 t->h[i] = tab_natural_height (t, d, i);
79 /* SYSFILE INFO utility. */
81 cmd_sysfile_info (void)
83 struct file_handle *h;
86 struct sfm_reader *reader;
87 struct sfm_read_info info;
91 lex_match_id ("FILE");
94 h = fh_parse (FH_REF_FILE);
98 reader = sfm_open_reader (h, &d, &info);
101 sfm_close_reader (reader);
103 t = tab_create (2, 9, 0);
104 tab_vline (t, TAL_GAP, 1, 0, 8);
105 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
106 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
107 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
109 const char *label = dict_get_label (d);
111 label = _("No label.");
112 tab_text (t, 1, 1, TAB_LEFT, label);
114 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
115 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
116 info.creation_date, info.creation_time, info.product);
117 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
118 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
119 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
120 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
121 dict_get_var_cnt (d));
122 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
123 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
124 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
125 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
126 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
127 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
129 struct variable *weight_var = dict_get_weight (d);
130 tab_text (t, 1, 7, TAB_LEFT,
131 weight_var != NULL ? weight_var->name : _("Not weighted."));
133 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
134 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
135 _("Compression %s."), info.compressed ? _("on") : _("off"));
136 tab_dim (t, tab_natural_dimensions);
139 nr = 1 + 2 * dict_get_var_cnt (d);
141 t = tab_create (4, nr, 1);
142 tab_dim (t, sysfile_info_dim);
143 tab_headers (t, 0, 0, 1, 0);
144 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
145 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
146 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
147 tab_hline (t, TAL_2, 0, 3, 1);
148 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
150 struct variable *v = dict_get_var (d, i);
151 const int nvl = val_labs_count (v->val_labs);
153 if (r + 10 + nvl > nr)
155 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
157 tab_realloc (t, 4, nr);
160 r = describe_variable (v, t, r, AS_DICTIONARY);
163 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
164 tab_vline (t, TAL_1, 1, 0, r);
165 tab_vline (t, TAL_1, 3, 0, r);
167 tab_resize (t, -1, r);
168 tab_flags (t, SOMF_NO_TITLE);
173 return lex_end_of_command ();
176 /* DISPLAY utility. */
178 static void display_macros (void);
179 static void display_documents (void);
180 static void display_variables (struct variable **, size_t, int);
181 static void display_vectors (int sorted);
186 /* Whether to sort the list of variables alphabetically. */
189 /* Variables to display. */
191 struct variable **vl;
193 if (lex_match_id ("MACROS"))
195 else if (lex_match_id ("DOCUMENTS"))
196 display_documents ();
197 else if (lex_match_id ("FILE"))
200 if (!lex_force_match_id ("LABEL"))
202 if (dict_get_label (dataset_dict (current_dataset)) == NULL)
203 tab_output_text (TAB_LEFT,
204 _("The active file does not have a file label."));
207 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
208 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (current_dataset)));
213 static const char *sbc[] =
214 {"NAMES", "INDEX", "VARIABLES", "LABELS",
215 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
219 sorted = lex_match_id ("SORTED");
221 for (cp = sbc; *cp; cp++)
222 if (token == T_ID && lex_id_match (*cp, tokid))
234 display_vectors (sorted);
239 lex_match_id ("VARIABLES");
244 if (!parse_variables (dataset_dict (current_dataset), &vl, &n, PV_NONE))
252 dict_get_vars (dataset_dict (current_dataset), &vl, &n, 0);
254 if (as == AS_SCRATCH)
257 for (i = 0, m = n; i < n; i++)
258 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
269 msg (SW, _("No variables to display."));
274 sort (vl, n, sizeof *vl, compare_var_names, NULL);
276 display_variables (vl, n, as);
281 return lex_end_of_command ();
285 display_macros (void)
288 tab_output_text (TAB_LEFT, _("Macros not supported."));
292 display_documents (void)
294 const char *documents = dict_get_documents (dataset_dict (current_dataset));
297 if (documents == NULL)
298 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
299 "contain any documents."));
302 size_t n_lines = strlen (documents) / 80;
306 tab_output_text (TAB_LEFT | TAT_TITLE,
307 _("Documents in the active file:"));
310 for (i = 0; i < n_lines; i++)
314 memcpy (buf, &documents[i * 80], 80);
315 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
319 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
326 /* Sets the widths of all the columns and heights of all the rows in
327 table T for driver D. */
329 variables_dim (struct tab_table *t, struct outp_driver *d)
334 t->w[0] = tab_natural_width (t, d, 0);
335 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
337 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
338 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
343 t->w[pc] = tab_natural_width (t, d, pc);
345 for (i = 0; i < t->nr; i++)
346 t->h[i] = tab_natural_height (t, d, i);
350 display_variables (struct variable **vl, size_t n, int as)
352 struct variable **vp = vl; /* Variable pointer. */
354 int nc; /* Number of columns. */
355 int nr; /* Number of rows. */
356 int pc; /* `Position column' */
357 int r; /* Current row. */
374 t = tab_create (nc, n + 5, 1);
375 tab_headers (t, 0, 0, 1, 0);
377 tab_hline (t, TAL_2, 0, nc - 1, 1);
378 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
379 pc = (as == AS_INDEX ? 1 : 3);
381 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
382 if (as == AS_DICTIONARY || as == AS_VARIABLES)
383 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
384 else if (as == AS_LABELS)
385 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
386 tab_dim (t, variables_dim);
388 for (i = r = 1; i <= n; i++)
396 if (as == AS_DICTIONARY || as == AS_VARIABLES)
398 int nvl = val_labs_count (v->val_labs);
400 if (r + 10 + nvl > nr)
402 nr = max (nr * n / (i + 1), nr);
404 tab_realloc (t, nc, nr);
407 r = describe_variable (v, t, r, as);
409 tab_text (t, 0, r, TAB_LEFT, v->name);
411 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
412 v->label == NULL ? "(no label)" : v->label);
415 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
416 tab_hline (t, TAL_1, 0, nc - 1, r);
421 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
424 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
425 tab_vline (t, TAL_1, 1, 0, r - 1);
428 tab_flags (t, SOMF_NO_TITLE);
429 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
430 tab_vline (t, TAL_1, 3, 0, r - 1);
431 tab_resize (t, -1, r);
432 tab_columns (t, TAB_COL_DOWN, 1);
436 /* Puts a description of variable V into table T starting at row R.
437 The variable will be described in the format AS. Returns the next
438 row available for use in the table. */
440 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
442 /* Put the name, var label, and position into the first row. */
443 tab_text (t, 0, r, TAB_LEFT, v->name);
444 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
446 if (as == AS_DICTIONARY && v->label)
448 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
452 /* Print/write format, or print and write formats. */
453 if (v->print.type == v->write.type
454 && v->print.w == v->write.w
455 && v->print.d == v->write.d)
457 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
458 fmt_to_string (&v->print));
463 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
464 _("Print Format: %s"), fmt_to_string (&v->print));
466 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
467 _("Write Format: %s"), fmt_to_string (&v->write));
471 /* Missing values if any. */
472 if (!mv_is_empty (&v->miss))
476 struct missing_values mv;
479 cp = stpcpy (buf, _("Missing Values: "));
480 mv_copy (&mv, &v->miss);
481 if (mv_has_range (&mv))
484 mv_pop_range (&mv, &x, &y);
486 cp += sprintf (cp, "LOWEST THRU %g", y);
487 else if (y == HIGHEST)
488 cp += sprintf (cp, "%g THRU HIGHEST", x);
490 cp += sprintf (cp, "%g THRU %g", x, y);
493 while (mv_has_value (&mv))
496 mv_pop_value (&mv, &value);
498 cp += sprintf (cp, "; ");
499 if (v->type == NUMERIC)
500 cp += sprintf (cp, "%g", value.f);
504 memcpy (cp, value.s, v->width);
511 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
516 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
518 struct val_labs_iterator *i;
523 tab_text (t, 1, r, TAB_LEFT, _("Value"));
524 tab_text (t, 2, r, TAB_LEFT, _("Label"));
528 tab_hline (t, TAL_1, 1, 2, r);
529 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
530 vl = val_labs_next (v->val_labs, &i))
534 if (v->type == ALPHA)
536 memcpy (buf, vl->value.s, v->width);
540 sprintf (buf, "%g", vl->value.f);
542 tab_text (t, 1, r, TAB_NONE, buf);
543 tab_text (t, 2, r, TAB_LEFT, vl->label);
547 tab_vline (t, TAL_1, 2, orig_r, r - 1);
550 /* Draw a line below the last row of information on this variable. */
551 tab_hline (t, TAL_1, 0, 3, r);
557 compare_vectors_by_name (const void *a_, const void *b_)
559 struct vector *const *pa = a_;
560 struct vector *const *pb = b_;
561 struct vector *a = *pa;
562 struct vector *b = *pb;
564 return strcasecmp (a->name, b->name);
567 /* Display a list of vectors. If SORTED is nonzero then they are
568 sorted alphabetically. */
570 display_vectors (int sorted)
572 const struct vector **vl;
577 nvec = dict_get_vector_cnt (dataset_dict (current_dataset));
580 msg (SW, _("No vectors defined."));
584 vl = xnmalloc (nvec, sizeof *vl);
585 for (i = 0; i < nvec; i++)
586 vl[i] = dict_get_vector (dataset_dict (current_dataset), i);
588 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
590 t = tab_create (1, nvec + 1, 0);
591 tab_headers (t, 0, 0, 1, 0);
592 tab_columns (t, TAB_COL_DOWN, 1);
593 tab_dim (t, tab_natural_dimensions);
594 tab_hline (t, TAL_1, 0, 0, 1);
595 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
596 tab_flags (t, SOMF_NO_TITLE);
597 for (i = 0; i < nvec; i++)
598 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);