1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <data/casereader.h>
23 #include <data/dictionary.h>
24 #include <data/file-handle-def.h>
25 #include <data/format.h>
26 #include <data/missing-values.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 <data/vector.h>
32 #include <language/command.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/array.h>
38 #include <libpspp/hash.h>
39 #include <libpspp/magic.h>
40 #include <libpspp/message.h>
41 #include <libpspp/message.h>
42 #include <libpspp/misc.h>
43 #include <output/manager.h>
44 #include <output/output.h>
45 #include <output/table.h>
50 #define _(msgid) gettext (msgid)
52 /* Constants for DISPLAY utility. */
64 static int describe_variable (const struct variable *v, struct tab_table *t, int r, int as);
66 /* Sets the widths of all the columns and heights of all the rows in
67 table T for driver D. */
69 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
71 static const int max[] = {20, 5, 35, 3, 0};
75 for (p = max; *p; p++)
76 t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
77 *p * d->prop_em_width);
78 for (i = 0; i < t->nr; i++)
79 t->h[i] = tab_natural_height (t, d, i);
82 /* SYSFILE INFO utility. */
84 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
86 struct file_handle *h;
89 struct casereader *reader;
90 struct sfm_read_info info;
94 lex_match_id (lexer, "FILE");
95 lex_match (lexer, '=');
97 h = fh_parse (lexer, FH_REF_FILE);
101 reader = sfm_open_reader (h, &d, &info);
104 casereader_destroy (reader);
106 t = tab_create (2, 10, 0);
107 tab_vline (t, TAL_GAP, 1, 0, 8);
108 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
109 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
110 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
112 const char *label = dict_get_label (d);
114 label = _("No label.");
115 tab_text (t, 1, 1, TAB_LEFT, label);
117 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
118 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
119 info.creation_date, info.creation_time, info.product);
120 tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
121 tab_text (t, 1, 3, TAB_LEFT,
122 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
123 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
125 tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
126 tab_text (t, 1, 4, TAB_LEFT,
127 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
128 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
129 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
130 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
131 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
133 tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
134 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%u",
135 (unsigned int) dict_get_var_cnt (d));
136 tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
137 tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
138 info.case_cnt == -1 ? _("Unknown") : "%ld",
139 (long int) info.case_cnt);
140 tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
141 tab_text (t, 1, 7, TAB_LEFT, _("System File."));
142 tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
144 struct variable *weight_var = dict_get_weight (d);
145 tab_text (t, 1, 8, TAB_LEFT,
147 ? var_get_name (weight_var) : _("Not weighted.")));
149 tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
150 tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
151 _("Compression %s."), info.compressed ? _("on") : _("off"));
152 tab_dim (t, tab_natural_dimensions);
155 nr = 1 + 2 * dict_get_var_cnt (d);
157 t = tab_create (4, nr, 1);
158 tab_dim (t, sysfile_info_dim);
159 tab_headers (t, 0, 0, 1, 0);
160 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
161 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
162 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
163 tab_hline (t, TAL_2, 0, 3, 1);
164 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
166 struct variable *v = dict_get_var (d, i);
167 const int nvl = val_labs_count (var_get_value_labels (v));
169 if (r + 13 + nvl > nr)
171 nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
173 tab_realloc (t, 4, nr);
176 r = describe_variable (v, t, r, AS_DICTIONARY);
179 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
180 tab_vline (t, TAL_1, 1, 0, r);
181 tab_vline (t, TAL_1, 3, 0, r);
183 tab_resize (t, -1, r);
184 tab_flags (t, SOMF_NO_TITLE);
189 return lex_end_of_command (lexer);
192 /* DISPLAY utility. */
194 static void display_macros (void);
195 static void display_documents (const struct dictionary *dict);
196 static void display_variables (const struct variable **, size_t, int);
197 static void display_vectors (const struct dictionary *dict, int sorted);
200 cmd_display (struct lexer *lexer, struct dataset *ds)
202 /* Whether to sort the list of variables alphabetically. */
205 /* Variables to display. */
207 const struct variable **vl;
209 if (lex_match_id (lexer, "MACROS"))
211 else if (lex_match_id (lexer, "DOCUMENTS"))
212 display_documents (dataset_dict (ds));
213 else if (lex_match_id (lexer, "FILE"))
216 if (!lex_force_match_id (lexer, "LABEL"))
218 if (dict_get_label (dataset_dict (ds)) == NULL)
219 tab_output_text (TAB_LEFT,
220 _("The active file does not have a file label."));
223 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
224 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
229 static const char *sbc[] =
230 {"NAMES", "INDEX", "VARIABLES", "LABELS",
231 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
235 sorted = lex_match_id (lexer, "SORTED");
237 for (cp = sbc; *cp; cp++)
238 if (lex_token (lexer) == T_ID
239 && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
251 display_vectors (dataset_dict(ds), sorted);
255 lex_match (lexer, '/');
256 lex_match_id (lexer, "VARIABLES");
257 lex_match (lexer, '=');
259 if (lex_token (lexer) != '.')
261 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
269 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
271 if (as == AS_SCRATCH)
274 for (i = 0, m = n; i < n; i++)
275 if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
286 msg (SW, _("No variables to display."));
291 sort (vl, n, sizeof *vl, compare_var_ptrs_by_name, NULL);
293 display_variables (vl, n, as);
298 return lex_end_of_command (lexer);
302 display_macros (void)
305 tab_output_text (TAB_LEFT, _("Macros not supported."));
309 display_documents (const struct dictionary *dict)
311 const char *documents = dict_get_documents (dict);
314 if (documents == NULL)
315 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
316 "contain any documents."));
319 struct string line = DS_EMPTY_INITIALIZER;
322 tab_output_text (TAB_LEFT | TAT_TITLE,
323 _("Documents in the active file:"));
325 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
327 dict_get_document_line (dict, i, &line);
328 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
336 /* Sets the widths of all the columns and heights of all the rows in
337 table T for driver D. */
339 variables_dim (struct tab_table *t, struct outp_driver *d)
344 t->w[0] = tab_natural_width (t, d, 0);
345 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
347 t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
348 t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
353 t->w[pc] = tab_natural_width (t, d, pc);
355 for (i = 0; i < t->nr; i++)
356 t->h[i] = tab_natural_height (t, d, i);
360 display_variables (const struct variable **vl, size_t n, int as)
362 const struct variable **vp = vl; /* Variable pointer. */
364 int nc; /* Number of columns. */
365 int nr; /* Number of rows. */
366 int pc; /* `Position column' */
367 int r; /* Current row. */
384 t = tab_create (nc, n + 5, 1);
385 tab_headers (t, 0, 0, 1, 0);
387 tab_hline (t, TAL_2, 0, nc - 1, 1);
388 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
389 pc = (as == AS_INDEX ? 1 : 3);
391 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
392 if (as == AS_DICTIONARY || as == AS_VARIABLES)
393 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
394 else if (as == AS_LABELS)
395 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
396 tab_dim (t, variables_dim);
398 for (i = r = 1; i <= n; i++)
400 const struct variable *v;
406 if (as == AS_DICTIONARY || as == AS_VARIABLES)
408 int nvl = val_labs_count (var_get_value_labels (v));
410 if (r + 13 + nvl > nr)
412 nr = MAX (nr * n / (i + 1), nr);
414 tab_realloc (t, nc, nr);
417 r = describe_variable (v, t, r, as);
419 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
422 const char *label = var_get_label (v);
423 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
424 label != NULL ? "(no label)" : label);
428 tab_text (t, pc, r, TAT_PRINTF, "%d",
429 (int) var_get_dict_index (v) + 1);
430 tab_hline (t, TAL_1, 0, nc - 1, r);
435 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
438 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
439 tab_vline (t, TAL_1, 1, 0, r - 1);
442 tab_flags (t, SOMF_NO_TITLE);
443 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
444 tab_vline (t, TAL_1, 3, 0, r - 1);
445 tab_resize (t, -1, r);
446 tab_columns (t, TAB_COL_DOWN, 1);
450 /* Puts a description of variable V into table T starting at row R.
451 The variable will be described in the format AS. Returns the next
452 row available for use in the table. */
454 describe_variable (const struct variable *v, struct tab_table *t, int r, int as)
456 const struct fmt_spec *print = var_get_print_format (v);
457 const struct fmt_spec *write = var_get_write_format (v);
458 enum measure m = var_get_measure (v);
459 enum alignment a = var_get_alignment (v);
461 /* Put the name, var label, and position into the first row. */
462 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
463 tab_text (t, 3, r, TAT_PRINTF, "%d", (int) var_get_dict_index (v) + 1);
465 if (as == AS_DICTIONARY && var_has_label (v))
467 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
471 /* Print/write format, or print and write formats. */
472 if (fmt_equal (print, write))
474 char str[FMT_STRING_LEN_MAX + 1];
475 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
476 fmt_to_string (print, str));
481 char str[FMT_STRING_LEN_MAX + 1];
482 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
483 _("Print Format: %s"), fmt_to_string (print, str));
485 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
486 _("Write Format: %s"), fmt_to_string (write, str));
490 /* Measurement level, display width, alignment. */
491 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
493 m == MEASURE_NOMINAL ? _("Nominal")
494 : m == MEASURE_ORDINAL ? _("Ordinal")
497 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
498 _("Display Alignment: %s"),
499 a == ALIGN_LEFT ? _("Left")
500 : a == ALIGN_CENTRE ? _("Center")
503 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
504 _("Display Width: %d"), var_get_display_width (v));
507 /* Missing values if any. */
508 if (var_has_missing_values (v))
512 struct missing_values mv;
515 cp = stpcpy (buf, _("Missing Values: "));
517 mv_copy (&mv, var_get_missing_values (v));
518 if (mv_has_range (&mv))
521 mv_pop_range (&mv, &x, &y);
523 cp += sprintf (cp, "LOWEST THRU %g", y);
524 else if (y == HIGHEST)
525 cp += sprintf (cp, "%g THRU HIGHEST", x);
527 cp += sprintf (cp, "%g THRU %g", x, y);
530 while (mv_has_value (&mv))
533 mv_pop_value (&mv, &value);
535 cp += sprintf (cp, "; ");
536 if (var_is_numeric (v))
537 cp += sprintf (cp, "%g", value.f);
541 memcpy (cp, value.s, var_get_width (v));
542 cp += var_get_width (v);
548 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
553 if (as == AS_DICTIONARY && var_has_value_labels (v))
555 const struct val_labs *val_labs = var_get_value_labels (v);
556 struct val_labs_iterator *i;
561 tab_text (t, 1, r, TAB_LEFT, _("Value"));
562 tab_text (t, 2, r, TAB_LEFT, _("Label"));
566 tab_hline (t, TAL_1, 1, 2, r);
567 for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
568 vl = val_labs_next (val_labs, &i))
572 if (var_is_alpha (v))
574 memcpy (buf, vl->value.s, var_get_width (v));
575 buf[var_get_width (v)] = 0;
578 sprintf (buf, "%g", vl->value.f);
580 tab_text (t, 1, r, TAB_NONE, buf);
581 tab_text (t, 2, r, TAB_LEFT, vl->label);
585 tab_vline (t, TAL_1, 2, orig_r, r - 1);
588 /* Draw a line below the last row of information on this variable. */
589 tab_hline (t, TAL_1, 0, 3, r);
594 /* Display a list of vectors. If SORTED is nonzero then they are
595 sorted alphabetically. */
597 display_vectors (const struct dictionary *dict, int sorted)
599 const struct vector **vl;
606 nvec = dict_get_vector_cnt (dict);
609 msg (SW, _("No vectors defined."));
613 vl = xnmalloc (nvec, sizeof *vl);
615 for (i = 0; i < nvec; i++)
617 vl[i] = dict_get_vector (dict, i);
618 nrow += vector_get_var_cnt (vl[i]);
621 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
623 t = tab_create (4, nrow + 1, 0);
624 tab_headers (t, 0, 0, 1, 0);
625 tab_columns (t, TAB_COL_DOWN, 1);
626 tab_dim (t, tab_natural_dimensions);
627 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
628 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
629 tab_hline (t, TAL_2, 0, 3, 1);
630 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
631 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
632 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
633 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
634 tab_flags (t, SOMF_NO_TITLE);
637 for (i = 0; i < nvec; i++)
639 const struct vector *vec = vl[i];
642 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
643 TAB_LEFT, vector_get_name (vl[i]));
645 for (j = 0; j < vector_get_var_cnt (vec); j++)
647 struct variable *var = vector_get_var (vec, j);
648 char fmt_string[FMT_STRING_LEN_MAX + 1];
649 fmt_to_string (var_get_print_format (var), fmt_string);
651 tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%d", (int) j + 1);
652 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
653 tab_text (t, 3, row, TAB_LEFT, fmt_string);
656 tab_hline (t, TAL_1, 0, 3, row);