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_rendering *r, void *aux UNUSED)
74 const struct tab_table *t = r->table;
75 static const int max[] = {20, 5, 35, 3, 0};
79 for (p = max; *p; p++)
80 r->w[p - max] = MIN (tab_natural_width (r, p - max),
81 *p * r->driver->prop_em_width);
82 for (i = 0; i < tab_nr (t); i++)
83 r->h[i] = tab_natural_height (r, i);
86 /* SYSFILE INFO utility. */
88 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
90 struct file_handle *h;
93 struct casereader *reader;
94 struct sfm_read_info info;
97 lex_match_id (lexer, "FILE");
98 lex_match (lexer, '=');
100 h = fh_parse (lexer, FH_REF_FILE);
104 reader = sfm_open_reader (h, &d, &info);
110 casereader_destroy (reader);
112 t = tab_create (2, 11);
113 tab_vline (t, TAL_GAP, 1, 0, 8);
114 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
115 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
116 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
118 const char *label = dict_get_label (d);
120 label = _("No label.");
121 tab_text (t, 1, 1, TAB_LEFT, label);
123 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
124 tab_text_format (t, 1, 2, TAB_LEFT, "%s %s by %s",
125 info.creation_date, info.creation_time, info.product);
126 tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
127 tab_text (t, 1, 3, TAB_LEFT,
128 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
129 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
131 tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
132 tab_text (t, 1, 4, TAB_LEFT,
133 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
134 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
135 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
136 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
137 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
139 tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
140 tab_text_format (t, 1, 5, TAB_LEFT, "%zu", dict_get_var_cnt (d));
141 tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
142 tab_text_format (t, 1, 6, TAB_LEFT,
143 info.case_cnt == -1 ? _("Unknown") : "%ld",
144 (long int) info.case_cnt);
145 tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
146 tab_text (t, 1, 7, TAB_LEFT, _("System File"));
147 tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
149 struct variable *weight_var = dict_get_weight (d);
150 tab_text (t, 1, 8, TAB_LEFT,
152 ? var_get_name (weight_var) : _("Not weighted.")));
154 tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
155 tab_text_format (t, 1, 9, TAB_LEFT,
156 _("Compression %s."), info.compressed ? _("on") : _("off"));
159 tab_text (t, 0, 10, TAB_LEFT, _("Charset:"));
160 tab_text_format (t, 1, 10, TAB_LEFT,
161 dict_get_encoding(d) ? dict_get_encoding(d) : _("Unknown"));
164 tab_dim (t, tab_natural_dimensions, NULL, NULL);
167 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d));
168 tab_dim (t, sysfile_info_dim, NULL, NULL);
169 tab_headers (t, 0, 0, 1, 0);
170 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
171 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
172 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
173 tab_hline (t, TAL_2, 0, 3, 1);
174 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
175 r = describe_variable (dict_get_var (d, i), t, r, 3,
176 DF_ALL & ~DF_AT_ATTRIBUTES);
178 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
179 tab_vline (t, TAL_1, 1, 0, r);
180 tab_vline (t, TAL_1, 3, 0, r);
182 tab_resize (t, -1, r);
183 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);
198 static void display_data_file_attributes (struct attrset *, int flags);
201 cmd_display (struct lexer *lexer, struct dataset *ds)
203 /* Whether to sort the list of variables alphabetically. */
206 /* Variables to display. */
208 const struct variable **vl;
210 if (lex_match_id (lexer, "MACROS"))
212 else if (lex_match_id (lexer, "DOCUMENTS"))
213 display_documents (dataset_dict (ds));
214 else if (lex_match_id (lexer, "FILE"))
217 if (!lex_force_match_id (lexer, "LABEL"))
219 if (dict_get_label (dataset_dict (ds)) == NULL)
220 tab_output_text (TAB_LEFT,
221 _("The active file does not have a file label."));
224 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
225 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
232 sorted = lex_match_id (lexer, "SORTED");
234 if (lex_match_id (lexer, "VECTORS"))
236 display_vectors (dataset_dict(ds), sorted);
237 return lex_end_of_command (lexer);
239 else if (lex_match_id (lexer, "SCRATCH"))
241 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
251 static const struct subcommand subcommands[] =
253 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
254 {"ATTRIBUTES", DF_ATTRIBUTES},
255 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
256 {"INDEX", DF_DICT_INDEX},
257 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
260 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
263 const struct subcommand *sbc;
266 for (sbc = subcommands; sbc->name != NULL; sbc++)
267 if (lex_match_id (lexer, sbc->name))
273 lex_match (lexer, '/');
274 lex_match_id (lexer, "VARIABLES");
275 lex_match (lexer, '=');
277 if (lex_token (lexer) != '.')
279 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
287 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
292 sort (vl, n, sizeof *vl,
294 ? compare_var_ptrs_by_name
295 : compare_var_ptrs_by_dict_index), NULL);
296 display_variables (vl, n, flags);
299 msg (SW, _("No variables to display."));
302 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
303 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
307 return lex_end_of_command (lexer);
311 display_macros (void)
314 tab_output_text (TAB_LEFT, _("Macros not supported."));
318 display_documents (const struct dictionary *dict)
320 const char *documents = dict_get_documents (dict);
323 if (documents == NULL)
324 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
325 "contain any documents."));
328 struct string line = DS_EMPTY_INITIALIZER;
331 tab_output_text (TAB_LEFT | TAT_TITLE,
332 _("Documents in the active file:"));
334 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
336 dict_get_document_line (dict, i, &line);
337 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
343 struct variables_dim_aux
348 /* Sets the widths of all the columns and heights of all the rows in
349 table T for driver D. */
351 variables_dim (struct tab_rendering *r, void *aux_)
353 const struct outp_driver *d = r->driver;
354 struct variables_dim_aux *aux = aux_;
356 tab_natural_dimensions (r, NULL);
357 if (aux->flags & (DF_VALUE_LABELS | DF_VARIABLE_LABELS | DF_MISSING_VALUES
358 | DF_AT_ATTRIBUTES | DF_ATTRIBUTES))
360 r->w[1] = MAX (r->w[1], d->prop_em_width * 5);
361 r->w[2] = MAX (r->w[2], d->prop_em_width * 35);
366 variables_dim_free (void *aux_)
368 struct variables_dim_aux *aux = aux_;
373 display_variables (const struct variable **vl, size_t n, int flags)
376 struct variables_dim_aux *aux;
377 int nc; /* Number of columns. */
378 int pc; /* `Position column' */
379 int r; /* Current row. */
382 /* One column for the name,
383 two columns for general description,
384 one column for dictionary index. */
386 if (flags & ~DF_DICT_INDEX)
389 if (flags & DF_DICT_INDEX)
392 t = tab_create (nc, n + 5);
393 tab_headers (t, 0, 0, 1, 0);
394 tab_hline (t, TAL_2, 0, nc - 1, 1);
395 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
396 if (flags & ~DF_DICT_INDEX)
397 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
398 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
399 ? _("Description") : _("Label")));
400 if (flags & DF_DICT_INDEX)
401 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
403 aux = xmalloc (sizeof *aux);
405 tab_dim (t, variables_dim, variables_dim_free, aux);
408 for (i = 0; i < n; i++)
409 r = describe_variable (vl[i], t, r, pc, flags);
410 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 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 (flags & ~DF_DICT_INDEX)
419 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
420 tab_resize (t, -1, r);
421 tab_columns (t, TAB_COL_DOWN);
426 is_at_name (const char *name)
428 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
432 count_attributes (const struct attrset *set, int flags)
434 struct attrset_iterator i;
435 struct attribute *attr;
439 for (attr = attrset_first (set, &i); attr != NULL;
440 attr = attrset_next (set, &i))
441 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
442 n_attrs += attribute_get_n_values (attr);
447 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
450 struct attrset_iterator i;
451 struct attribute *attr;
453 for (attr = attrset_first (set, &i); attr != NULL;
454 attr = attrset_next (set, &i))
456 const char *name = attribute_get_name (attr);
460 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
463 n_values = attribute_get_n_values (attr);
464 for (i = 0; i < n_values; i++)
467 tab_text_format (t, c, r, TAB_LEFT, "%s[%d]", name, i + 1);
469 tab_text (t, c, r, TAB_LEFT, name);
470 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
477 display_data_file_attributes (struct attrset *set, int flags)
482 n_attrs = count_attributes (set, flags);
486 t = tab_create (2, n_attrs + 1);
487 tab_headers (t, 0, 0, 1, 0);
488 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
489 tab_hline (t, TAL_2, 0, 1, 1);
490 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
491 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
492 display_attributes (t, set, flags, 0, 1);
493 tab_columns (t, TAB_COL_DOWN);
494 tab_dim (t, tab_natural_dimensions, NULL, NULL);
495 tab_title (t, "Custom data file attributes.");
499 /* Puts a description of variable V into table T starting at row
500 R. The variable will be described in the format given by
501 FLAGS. Returns the next row available for use in the
504 describe_variable (const struct variable *v, struct tab_table *t, int r,
510 /* Make sure that enough rows are allocated. */
512 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
514 if (flags & DF_VALUE_LABELS)
515 need_rows += val_labs_count (var_get_value_labels (v));
516 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
518 n_attrs = count_attributes (var_get_attributes (v), flags);
519 need_rows += n_attrs;
521 if (r + need_rows > tab_nr (t))
523 int nr = MAX (r + need_rows, tab_nr (t) * 2);
524 tab_realloc (t, -1, nr);
527 /* Put the name, var label, and position into the first row. */
528 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
529 if (flags & DF_DICT_INDEX)
530 tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
532 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
534 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
538 /* Print/write format, or print and write formats. */
539 if (flags & DF_FORMATS)
541 const struct fmt_spec *print = var_get_print_format (v);
542 const struct fmt_spec *write = var_get_write_format (v);
544 if (fmt_equal (print, write))
546 char str[FMT_STRING_LEN_MAX + 1];
547 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
548 _("Format: %s"), fmt_to_string (print, str));
553 char str[FMT_STRING_LEN_MAX + 1];
554 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
555 _("Print Format: %s"),
556 fmt_to_string (print, str));
558 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
559 _("Write Format: %s"),
560 fmt_to_string (write, str));
565 /* Measurement level, display width, alignment. */
568 enum measure m = var_get_measure (v);
569 enum alignment a = var_get_alignment (v);
571 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
573 m == MEASURE_NOMINAL ? _("Nominal")
574 : m == MEASURE_ORDINAL ? _("Ordinal")
577 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
578 _("Display Alignment: %s"),
579 a == ALIGN_LEFT ? _("Left")
580 : a == ALIGN_CENTRE ? _("Center")
583 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
584 _("Display Width: %d"),
585 var_get_display_width (v));
589 /* Missing values if any. */
590 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
592 const struct missing_values *mv = var_get_missing_values (v);
598 cp = stpcpy (buf, _("Missing Values: "));
600 if (mv_has_range (mv))
603 mv_get_range (mv, &x, &y);
605 cp += sprintf (cp, "LOWEST THRU %g", y);
606 else if (y == HIGHEST)
607 cp += sprintf (cp, "%g THRU HIGHEST", x);
609 cp += sprintf (cp, "%g THRU %g", x, y);
612 for (i = 0; i < mv_n_values (mv); i++)
614 const union value *value = mv_get_value (mv, i);
616 cp += sprintf (cp, "; ");
617 if (var_is_numeric (v))
618 cp += sprintf (cp, "%g", value->f);
621 int width = var_get_width (v);
622 int mv_width = MIN (width, MV_MAX_STRING);
625 memcpy (cp, value_str (value, width), mv_width);
632 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
637 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
639 const struct val_labs *val_labs = var_get_value_labels (v);
640 size_t n_labels = val_labs_count (val_labs);
641 const struct val_lab **labels;
646 tab_text (t, 1, r, TAB_LEFT, _("Value"));
647 tab_text (t, 2, r, TAB_LEFT, _("Label"));
651 tab_hline (t, TAL_1, 1, 2, r);
653 labels = val_labs_sorted (val_labs);
654 for (i = 0; i < n_labels; i++)
656 const struct val_lab *vl = labels[i];
657 char buf[MAX_STRING + 1];
659 if (var_is_alpha (v))
661 int width = var_get_width (v);
662 memcpy (buf, value_str (&vl->value, width), width);
666 sprintf (buf, "%g", vl->value.f);
668 tab_text (t, 1, r, TAB_NONE, buf);
669 tab_text (t, 2, r, TAB_LEFT, val_lab_get_label (vl));
674 tab_vline (t, TAL_1, 2, orig_r, r - 1);
677 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
679 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
682 display_attributes (t, var_get_attributes (v), flags, 1, r);
686 /* Draw a line below the last row of information on this variable. */
687 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
692 /* Display a list of vectors. If SORTED is nonzero then they are
693 sorted alphabetically. */
695 display_vectors (const struct dictionary *dict, int sorted)
697 const struct vector **vl;
704 nvec = dict_get_vector_cnt (dict);
707 msg (SW, _("No vectors defined."));
711 vl = xnmalloc (nvec, sizeof *vl);
713 for (i = 0; i < nvec; i++)
715 vl[i] = dict_get_vector (dict, i);
716 nrow += vector_get_var_cnt (vl[i]);
719 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
721 t = tab_create (4, nrow + 1);
722 tab_headers (t, 0, 0, 1, 0);
723 tab_columns (t, TAB_COL_DOWN);
724 tab_dim (t, tab_natural_dimensions, NULL, NULL);
725 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
726 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
727 tab_hline (t, TAL_2, 0, 3, 1);
728 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
729 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
730 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
731 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
732 tab_flags (t, SOMF_NO_TITLE);
735 for (i = 0; i < nvec; i++)
737 const struct vector *vec = vl[i];
740 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
741 TAB_LEFT, vector_get_name (vl[i]));
743 for (j = 0; j < vector_get_var_cnt (vec); j++)
745 struct variable *var = vector_get_var (vec, j);
746 char fmt_string[FMT_STRING_LEN_MAX + 1];
747 fmt_to_string (var_get_print_format (var), fmt_string);
749 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
750 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
751 tab_text (t, 3, row, TAB_LEFT, fmt_string);
754 tab_hline (t, TAL_1, 0, 3, row);