1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009 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/attributes.h>
23 #include <data/casereader.h>
24 #include <data/dictionary.h>
25 #include <data/file-handle-def.h>
26 #include <data/format.h>
27 #include <data/missing-values.h>
28 #include <data/procedure.h>
29 #include <data/sys-file-reader.h>
30 #include <data/value-labels.h>
31 #include <data/variable.h>
32 #include <data/vector.h>
33 #include <language/command.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/lexer/lexer.h>
36 #include <language/lexer/variable-parser.h>
37 #include <libpspp/array.h>
38 #include <libpspp/hash.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>
50 #define _(msgid) gettext (msgid)
52 /* Information to include in displaying a dictionary. */
55 DF_DICT_INDEX = 1 << 0,
57 DF_VALUE_LABELS = 1 << 2,
58 DF_VARIABLE_LABELS = 1 << 3,
59 DF_MISSING_VALUES = 1 << 4,
60 DF_AT_ATTRIBUTES = 1 << 5, /* Attributes whose names begin with @. */
61 DF_ATTRIBUTES = 1 << 6, /* All other attributes. */
66 static int describe_variable (const struct variable *v, struct tab_table *t,
67 int r, int pc, int flags);
69 /* Sets the widths of all the columns and heights of all the rows in
70 table T for driver D. */
72 sysfile_info_dim (struct tab_table *t, struct outp_driver *d, void *aux UNUSED)
74 static const int max[] = {20, 5, 35, 3, 0};
78 for (p = max; *p; p++)
79 t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
80 *p * d->prop_em_width);
81 for (i = 0; i < t->nr; i++)
82 t->h[i] = tab_natural_height (t, d, i);
85 /* SYSFILE INFO utility. */
87 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
89 struct file_handle *h;
92 struct casereader *reader;
93 struct sfm_read_info info;
96 lex_match_id (lexer, "FILE");
97 lex_match (lexer, '=');
99 h = fh_parse (lexer, FH_REF_FILE);
103 reader = sfm_open_reader (h, &d, &info);
109 casereader_destroy (reader);
111 t = tab_create (2, 11, 0);
112 tab_vline (t, TAL_GAP, 1, 0, 8);
113 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
114 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
115 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
117 const char *label = dict_get_label (d);
119 label = _("No label.");
120 tab_text (t, 1, 1, TAB_LEFT, label);
122 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
123 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
124 info.creation_date, info.creation_time, info.product);
125 tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
126 tab_text (t, 1, 3, TAB_LEFT,
127 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
128 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
130 tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
131 tab_text (t, 1, 4, TAB_LEFT,
132 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
133 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
134 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
135 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
136 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
138 tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
139 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%zu", dict_get_var_cnt (d));
140 tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
141 tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
142 info.case_cnt == -1 ? _("Unknown") : "%ld",
143 (long int) info.case_cnt);
144 tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
145 tab_text (t, 1, 7, TAB_LEFT, _("System File."));
146 tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
148 struct variable *weight_var = dict_get_weight (d);
149 tab_text (t, 1, 8, TAB_LEFT,
151 ? var_get_name (weight_var) : _("Not weighted.")));
153 tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
154 tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
155 _("Compression %s."), info.compressed ? _("on") : _("off"));
158 tab_text (t, 0, 10, TAB_LEFT, _("Charset:"));
159 tab_text (t, 1, 10, TAB_LEFT | TAT_PRINTF,
160 dict_get_encoding(d) ? dict_get_encoding(d) : _("Unknown"));
163 tab_dim (t, tab_natural_dimensions, NULL);
166 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d), 1);
167 tab_dim (t, sysfile_info_dim, NULL);
168 tab_headers (t, 0, 0, 1, 0);
169 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
170 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
171 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
172 tab_hline (t, TAL_2, 0, 3, 1);
173 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
174 r = describe_variable (dict_get_var (d, i), t, r, 3,
175 DF_ALL & ~DF_AT_ATTRIBUTES);
177 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
178 tab_vline (t, TAL_1, 1, 0, r);
179 tab_vline (t, TAL_1, 3, 0, r);
181 tab_resize (t, -1, r);
182 tab_flags (t, SOMF_NO_TITLE);
188 return lex_end_of_command (lexer);
191 /* DISPLAY utility. */
193 static void display_macros (void);
194 static void display_documents (const struct dictionary *dict);
195 static void display_variables (const struct variable **, size_t, int);
196 static void display_vectors (const struct dictionary *dict, int sorted);
197 static void display_data_file_attributes (struct attrset *, int flags);
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)));
231 sorted = lex_match_id (lexer, "SORTED");
233 if (lex_match_id (lexer, "VECTORS"))
235 display_vectors (dataset_dict(ds), sorted);
236 return lex_end_of_command (lexer);
238 else if (lex_match_id (lexer, "SCRATCH"))
240 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
250 static const struct subcommand subcommands[] =
252 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
253 {"ATTRIBUTES", DF_ATTRIBUTES},
254 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
255 {"INDEX", DF_DICT_INDEX},
256 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
259 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
262 const struct subcommand *sbc;
265 for (sbc = subcommands; sbc->name != NULL; sbc++)
266 if (lex_match_id (lexer, sbc->name))
272 lex_match (lexer, '/');
273 lex_match_id (lexer, "VARIABLES");
274 lex_match (lexer, '=');
276 if (lex_token (lexer) != '.')
278 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
286 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
291 sort (vl, n, sizeof *vl,
293 ? compare_var_ptrs_by_name
294 : compare_var_ptrs_by_dict_index), NULL);
295 display_variables (vl, n, flags);
298 msg (SW, _("No variables to display."));
301 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
302 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
306 return lex_end_of_command (lexer);
310 display_macros (void)
313 tab_output_text (TAB_LEFT, _("Macros not supported."));
317 display_documents (const struct dictionary *dict)
319 const char *documents = dict_get_documents (dict);
322 if (documents == NULL)
323 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
324 "contain any documents."));
327 struct string line = DS_EMPTY_INITIALIZER;
330 tab_output_text (TAB_LEFT | TAT_TITLE,
331 _("Documents in the active file:"));
333 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
335 dict_get_document_line (dict, i, &line);
336 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
344 /* Sets the widths of all the columns and heights of all the rows in
345 table T for driver D. */
347 variables_dim (struct tab_table *t, struct outp_driver *d, void *aux UNUSED)
352 t->w[0] = tab_natural_width (t, d, 0);
353 if (_flags & (DF_VALUE_LABELS | DF_VARIABLE_LABELS | DF_MISSING_VALUES
354 | DF_AT_ATTRIBUTES | DF_ATTRIBUTES))
356 t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
357 t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
362 if (_flags & DF_DICT_INDEX)
363 t->w[pc] = tab_natural_width (t, d, pc);
365 for (i = 0; i < t->nr; i++)
366 t->h[i] = tab_natural_height (t, d, i);
370 display_variables (const struct variable **vl, size_t n, int flags)
373 int nc; /* Number of columns. */
374 int pc; /* `Position column' */
375 int r; /* Current row. */
380 /* One column for the name,
381 two columns for general description,
382 one column for dictionary index. */
384 if (flags & ~DF_DICT_INDEX)
387 if (flags & DF_DICT_INDEX)
390 t = tab_create (nc, n + 5, 1);
391 tab_headers (t, 0, 0, 1, 0);
392 tab_hline (t, TAL_2, 0, nc - 1, 1);
393 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
394 if (flags & ~DF_DICT_INDEX)
395 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
396 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
397 ? _("Description") : _("Label")));
398 if (flags & DF_DICT_INDEX)
399 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
400 tab_dim (t, variables_dim, NULL);
403 for (i = 0; i < n; i++)
404 r = describe_variable (vl[i], t, r, pc, flags);
405 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 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 (flags & ~DF_DICT_INDEX)
414 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
415 tab_resize (t, -1, r);
416 tab_columns (t, TAB_COL_DOWN, 1);
421 is_at_name (const char *name)
423 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
427 count_attributes (const struct attrset *set, int flags)
429 struct attrset_iterator i;
430 struct attribute *attr;
434 for (attr = attrset_first (set, &i); attr != NULL;
435 attr = attrset_next (set, &i))
436 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
437 n_attrs += attribute_get_n_values (attr);
442 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
445 struct attrset_iterator i;
446 struct attribute *attr;
448 for (attr = attrset_first (set, &i); attr != NULL;
449 attr = attrset_next (set, &i))
451 const char *name = attribute_get_name (attr);
455 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
458 n_values = attribute_get_n_values (attr);
459 for (i = 0; i < n_values; i++)
462 tab_text (t, c, r, TAB_LEFT | TAT_PRINTF, "%s[%d]",
465 tab_text (t, c, r, TAB_LEFT, name);
466 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
473 display_data_file_attributes (struct attrset *set, int flags)
478 n_attrs = count_attributes (set, flags);
482 t = tab_create (2, n_attrs + 1, 0);
483 tab_headers (t, 0, 0, 1, 0);
484 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
485 tab_hline (t, TAL_2, 0, 1, 1);
486 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
487 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
488 display_attributes (t, set, flags, 0, 1);
489 tab_columns (t, TAB_COL_DOWN, 1);
490 tab_dim (t, tab_natural_dimensions, NULL);
491 tab_title (t, "Custom data file attributes.");
495 /* Puts a description of variable V into table T starting at row
496 R. The variable will be described in the format given by
497 FLAGS. Returns the next row available for use in the
500 describe_variable (const struct variable *v, struct tab_table *t, int r,
506 /* Make sure that enough rows are allocated. */
508 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
510 if (flags & DF_VALUE_LABELS)
511 need_rows += val_labs_count (var_get_value_labels (v));
512 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
514 n_attrs = count_attributes (var_get_attributes (v), flags);
515 need_rows += n_attrs;
517 if (r + need_rows > tab_nr (t))
519 int nr = MAX (r + need_rows, tab_nr (t) * 2);
520 tab_realloc (t, -1, nr);
523 /* Put the name, var label, and position into the first row. */
524 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
525 if (flags & DF_DICT_INDEX)
526 tab_text (t, pc, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
528 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
530 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
534 /* Print/write format, or print and write formats. */
535 if (flags & DF_FORMATS)
537 const struct fmt_spec *print = var_get_print_format (v);
538 const struct fmt_spec *write = var_get_write_format (v);
540 if (fmt_equal (print, write))
542 char str[FMT_STRING_LEN_MAX + 1];
543 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
544 _("Format: %s"), fmt_to_string (print, str));
549 char str[FMT_STRING_LEN_MAX + 1];
550 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
551 _("Print Format: %s"), fmt_to_string (print, str));
553 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
554 _("Write Format: %s"), fmt_to_string (write, str));
559 /* Measurement level, display width, alignment. */
562 enum measure m = var_get_measure (v);
563 enum alignment a = var_get_alignment (v);
565 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
567 m == MEASURE_NOMINAL ? _("Nominal")
568 : m == MEASURE_ORDINAL ? _("Ordinal")
571 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
572 _("Display Alignment: %s"),
573 a == ALIGN_LEFT ? _("Left")
574 : a == ALIGN_CENTRE ? _("Center")
577 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
578 _("Display Width: %d"), var_get_display_width (v));
582 /* Missing values if any. */
583 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
585 const struct missing_values *mv = var_get_missing_values (v);
591 cp = stpcpy (buf, _("Missing Values: "));
593 if (mv_has_range (mv))
596 mv_get_range (mv, &x, &y);
598 cp += sprintf (cp, "LOWEST THRU %g", y);
599 else if (y == HIGHEST)
600 cp += sprintf (cp, "%g THRU HIGHEST", x);
602 cp += sprintf (cp, "%g THRU %g", x, y);
605 for (i = 0; i < mv_n_values (mv); i++)
607 const union value *value = mv_get_value (mv, i);
609 cp += sprintf (cp, "; ");
610 if (var_is_numeric (v))
611 cp += sprintf (cp, "%g", value->f);
614 int width = var_get_width (v);
615 int mv_width = MIN (width, MV_MAX_STRING);
618 memcpy (cp, value_str (value, width), mv_width);
625 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
630 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
632 const struct val_labs *val_labs = var_get_value_labels (v);
633 size_t n_labels = val_labs_count (val_labs);
634 const struct val_lab **labels;
639 tab_text (t, 1, r, TAB_LEFT, _("Value"));
640 tab_text (t, 2, r, TAB_LEFT, _("Label"));
644 tab_hline (t, TAL_1, 1, 2, r);
646 labels = val_labs_sorted (val_labs);
647 for (i = 0; i < n_labels; i++)
649 const struct val_lab *vl = labels[i];
650 char buf[MAX_STRING + 1];
652 if (var_is_alpha (v))
654 int width = var_get_width (v);
655 memcpy (buf, value_str (&vl->value, width), width);
659 sprintf (buf, "%g", vl->value.f);
661 tab_text (t, 1, r, TAB_NONE, buf);
662 tab_text (t, 2, r, TAB_LEFT, val_lab_get_label (vl));
667 tab_vline (t, TAL_1, 2, orig_r, r - 1);
670 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
672 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
675 display_attributes (t, var_get_attributes (v), flags, 1, r);
679 /* Draw a line below the last row of information on this variable. */
680 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
685 /* Display a list of vectors. If SORTED is nonzero then they are
686 sorted alphabetically. */
688 display_vectors (const struct dictionary *dict, int sorted)
690 const struct vector **vl;
697 nvec = dict_get_vector_cnt (dict);
700 msg (SW, _("No vectors defined."));
704 vl = xnmalloc (nvec, sizeof *vl);
706 for (i = 0; i < nvec; i++)
708 vl[i] = dict_get_vector (dict, i);
709 nrow += vector_get_var_cnt (vl[i]);
712 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
714 t = tab_create (4, nrow + 1, 0);
715 tab_headers (t, 0, 0, 1, 0);
716 tab_columns (t, TAB_COL_DOWN, 1);
717 tab_dim (t, tab_natural_dimensions, NULL);
718 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
719 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
720 tab_hline (t, TAL_2, 0, 3, 1);
721 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
722 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
723 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
724 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
725 tab_flags (t, SOMF_NO_TITLE);
728 for (i = 0; i < nvec; i++)
730 const struct vector *vec = vl[i];
733 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
734 TAB_LEFT, vector_get_name (vl[i]));
736 for (j = 0; j < vector_get_var_cnt (vec); j++)
738 struct variable *var = vector_get_var (vec, j);
739 char fmt_string[FMT_STRING_LEN_MAX + 1];
740 fmt_to_string (var_get_print_format (var), fmt_string);
742 tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
743 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
744 tab_text (t, 3, row, TAB_LEFT, fmt_string);
747 tab_hline (t, TAL_1, 0, 3, row);