1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 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/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/file-handle-def.h"
27 #include "data/format.h"
28 #include "data/missing-values.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/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/string-array.h"
41 #include "output/tab.h"
43 #include "gl/minmax.h"
44 #include "gl/xalloc.h"
47 #define _(msgid) gettext (msgid)
49 /* Information to include in displaying a dictionary. */
52 DF_DICT_INDEX = 1 << 0,
54 DF_VALUE_LABELS = 1 << 2,
55 DF_VARIABLE_LABELS = 1 << 3,
56 DF_MISSING_VALUES = 1 << 4,
57 DF_AT_ATTRIBUTES = 1 << 5, /* Attributes whose names begin with @. */
58 DF_ATTRIBUTES = 1 << 6, /* All other attributes. */
63 static int describe_variable (const struct variable *v, struct tab_table *t,
64 int r, int pc, int flags);
66 /* SYSFILE INFO utility. */
68 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
70 struct file_handle *h;
73 struct casereader *reader;
74 struct sfm_read_info info;
77 lex_match_id (lexer, "FILE");
78 lex_match (lexer, T_EQUALS);
80 h = fh_parse (lexer, FH_REF_FILE, NULL);
84 reader = sfm_open_reader (h, NULL, &d, &info);
90 casereader_destroy (reader);
92 t = tab_create (2, 11);
93 tab_vline (t, TAL_GAP, 1, 0, 8);
94 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
95 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
96 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
98 const char *label = dict_get_label (d);
100 label = _("No label.");
101 tab_text (t, 1, 1, TAB_LEFT, label);
103 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
104 tab_text_format (t, 1, 2, TAB_LEFT, "%s %s by %s",
105 info.creation_date, info.creation_time, info.product);
106 tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
107 tab_text (t, 1, 3, TAB_LEFT,
108 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
109 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
111 tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
112 tab_text (t, 1, 4, TAB_LEFT,
113 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
114 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
115 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
116 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
117 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
119 tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
120 tab_text_format (t, 1, 5, TAB_LEFT, "%zu", dict_get_var_cnt (d));
121 tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
122 if (info.case_cnt == -1)
123 tab_text (t, 1, 6, TAB_LEFT, _("Unknown"));
125 tab_text_format (t, 1, 6, TAB_LEFT, "%ld", (long int) info.case_cnt);
126 tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
127 tab_text (t, 1, 7, TAB_LEFT, _("System File"));
128 tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
130 struct variable *weight_var = dict_get_weight (d);
131 tab_text (t, 1, 8, TAB_LEFT,
133 ? var_get_name (weight_var) : _("Not weighted.")));
135 tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
136 tab_text_format (t, 1, 9, TAB_LEFT,
137 _("Compression %s."), info.compressed ? _("on") : _("off"));
140 tab_text (t, 0, 10, TAB_LEFT, _("Charset:"));
141 tab_text (t, 1, 10, TAB_LEFT, dict_get_encoding (d));
146 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d));
147 tab_headers (t, 0, 0, 1, 0);
148 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
149 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
150 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
151 tab_hline (t, TAL_2, 0, 3, 1);
152 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
153 r = describe_variable (dict_get_var (d, i), t, r, 3,
154 DF_ALL & ~DF_AT_ATTRIBUTES);
156 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
157 tab_vline (t, TAL_1, 1, 0, r);
158 tab_vline (t, TAL_1, 3, 0, r);
160 tab_resize (t, -1, r);
166 sfm_read_info_destroy (&info);
170 /* DISPLAY utility. */
172 static void display_macros (void);
173 static void display_documents (const struct dictionary *dict);
174 static void display_variables (const struct variable **, size_t, int);
175 static void display_vectors (const struct dictionary *dict, int sorted);
176 static void display_data_file_attributes (struct attrset *, int flags);
179 cmd_display (struct lexer *lexer, struct dataset *ds)
181 /* Whether to sort the list of variables alphabetically. */
184 /* Variables to display. */
186 const struct variable **vl;
188 if (lex_match_id (lexer, "MACROS"))
190 else if (lex_match_id (lexer, "DOCUMENTS"))
191 display_documents (dataset_dict (ds));
192 else if (lex_match_id (lexer, "FILE"))
194 if (!lex_force_match_id (lexer, "LABEL"))
196 if (dict_get_label (dataset_dict (ds)) == NULL)
197 tab_output_text (TAB_LEFT,
198 _("The active dataset does not have a file label."));
200 tab_output_text_format (TAB_LEFT, _("File label: %s"),
201 dict_get_label (dataset_dict (ds)));
207 sorted = lex_match_id (lexer, "SORTED");
209 if (lex_match_id (lexer, "VECTORS"))
211 display_vectors (dataset_dict(ds), sorted);
214 else if (lex_match_id (lexer, "SCRATCH"))
216 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
226 static const struct subcommand subcommands[] =
228 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
229 {"ATTRIBUTES", DF_ATTRIBUTES},
230 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
231 {"INDEX", DF_DICT_INDEX},
232 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
235 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
238 const struct subcommand *sbc;
241 for (sbc = subcommands; sbc->name != NULL; sbc++)
242 if (lex_match_id (lexer, sbc->name))
248 lex_match (lexer, T_SLASH);
249 lex_match_id (lexer, "VARIABLES");
250 lex_match (lexer, T_EQUALS);
252 if (lex_token (lexer) != T_ENDCMD)
254 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
262 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
267 sort (vl, n, sizeof *vl,
269 ? compare_var_ptrs_by_name
270 : compare_var_ptrs_by_dict_index), NULL);
271 display_variables (vl, n, flags);
274 msg (SW, _("No variables to display."));
277 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
278 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
286 display_macros (void)
288 tab_output_text (TAB_LEFT, _("Macros not supported."));
292 display_documents (const struct dictionary *dict)
294 const struct string_array *documents = dict_get_documents (dict);
296 if (string_array_is_empty (documents))
297 tab_output_text (TAB_LEFT, _("The active dataset dictionary does not "
298 "contain any documents."));
303 tab_output_text (TAB_LEFT | TAT_TITLE,
304 _("Documents in the active dataset:"));
305 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
306 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_document_line (dict, i));
311 display_variables (const struct variable **vl, size_t n, int flags)
314 int nc; /* Number of columns. */
315 int pc; /* `Position column' */
316 int r; /* Current row. */
319 /* One column for the name,
320 two columns for general description,
321 one column for dictionary index. */
323 if (flags & ~DF_DICT_INDEX)
326 if (flags & DF_DICT_INDEX)
329 t = tab_create (nc, n + 5);
330 tab_headers (t, 0, 0, 1, 0);
331 tab_hline (t, TAL_2, 0, nc - 1, 1);
332 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
333 if (flags & ~DF_DICT_INDEX)
334 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
335 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
336 ? _("Description") : _("Label")));
337 if (flags & DF_DICT_INDEX)
338 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
341 for (i = 0; i < n; i++)
342 r = describe_variable (vl[i], t, r, pc, flags);
343 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
346 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
347 tab_vline (t, TAL_1, 1, 0, r - 1);
349 if (flags & ~DF_DICT_INDEX)
350 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
351 tab_resize (t, -1, r);
356 is_at_name (const char *name)
358 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
362 count_attributes (const struct attrset *set, int flags)
364 struct attrset_iterator i;
365 struct attribute *attr;
369 for (attr = attrset_first (set, &i); attr != NULL;
370 attr = attrset_next (set, &i))
371 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
372 n_attrs += attribute_get_n_values (attr);
377 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
380 struct attribute **attrs;
384 n_attrs = attrset_count (set);
385 attrs = attrset_sorted (set);
386 for (i = 0; i < n_attrs; i++)
388 const struct attribute *attr = attrs[i];
389 const char *name = attribute_get_name (attr);
393 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
396 n_values = attribute_get_n_values (attr);
397 for (i = 0; i < n_values; i++)
400 tab_text_format (t, c, r, TAB_LEFT, "%s[%zu]", name, i + 1);
402 tab_text (t, c, r, TAB_LEFT, name);
403 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
411 display_data_file_attributes (struct attrset *set, int flags)
416 n_attrs = count_attributes (set, flags);
420 t = tab_create (2, n_attrs + 1);
421 tab_headers (t, 0, 0, 1, 0);
422 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
423 tab_hline (t, TAL_2, 0, 1, 1);
424 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
425 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
426 display_attributes (t, set, flags, 0, 1);
427 tab_title (t, "Custom data file attributes.");
431 /* Puts a description of variable V into table T starting at row
432 R. The variable will be described in the format given by
433 FLAGS. Returns the next row available for use in the
436 describe_variable (const struct variable *v, struct tab_table *t, int r,
442 /* Make sure that enough rows are allocated. */
444 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
446 if (flags & DF_VALUE_LABELS)
447 need_rows += val_labs_count (var_get_value_labels (v));
448 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
450 n_attrs = count_attributes (var_get_attributes (v), flags);
451 need_rows += n_attrs;
453 if (r + need_rows > tab_nr (t))
455 int nr = MAX (r + need_rows, tab_nr (t) * 2);
456 tab_realloc (t, -1, nr);
459 /* Put the name, var label, and position into the first row. */
460 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
461 if (flags & DF_DICT_INDEX)
462 tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
464 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
466 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
470 /* Print/write format, or print and write formats. */
471 if (flags & DF_FORMATS)
473 const struct fmt_spec *print = var_get_print_format (v);
474 const struct fmt_spec *write = var_get_write_format (v);
476 if (fmt_equal (print, write))
478 char str[FMT_STRING_LEN_MAX + 1];
479 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
480 _("Format: %s"), fmt_to_string (print, str));
485 char str[FMT_STRING_LEN_MAX + 1];
486 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
487 _("Print Format: %s"),
488 fmt_to_string (print, str));
490 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
491 _("Write Format: %s"),
492 fmt_to_string (write, str));
497 /* Measurement level, display width, alignment. */
500 enum measure m = var_get_measure (v);
501 enum alignment a = var_get_alignment (v);
503 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
505 m == MEASURE_NOMINAL ? _("Nominal")
506 : m == MEASURE_ORDINAL ? _("Ordinal")
509 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
510 _("Display Alignment: %s"),
511 a == ALIGN_LEFT ? _("Left")
512 : a == ALIGN_CENTRE ? _("Center")
515 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
516 _("Display Width: %d"),
517 var_get_display_width (v));
521 /* Missing values if any. */
522 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
524 const struct missing_values *mv = var_get_missing_values (v);
530 cp = stpcpy (buf, _("Missing Values: "));
532 if (mv_has_range (mv))
535 mv_get_range (mv, &x, &y);
537 cp += sprintf (cp, "LOWEST THRU %g", y);
538 else if (y == HIGHEST)
539 cp += sprintf (cp, "%g THRU HIGHEST", x);
541 cp += sprintf (cp, "%g THRU %g", x, y);
544 for (i = 0; i < mv_n_values (mv); i++)
546 const union value *value = mv_get_value (mv, i);
548 cp += sprintf (cp, "; ");
549 if (var_is_numeric (v))
550 cp += sprintf (cp, "%g", value->f);
553 int width = var_get_width (v);
554 int mv_width = MIN (width, MV_MAX_STRING);
557 memcpy (cp, value_str (value, width), mv_width);
564 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
569 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
571 const struct val_labs *val_labs = var_get_value_labels (v);
572 size_t n_labels = val_labs_count (val_labs);
573 const struct val_lab **labels;
578 tab_text (t, 1, r, TAB_LEFT, _("Value"));
579 tab_text (t, 2, r, TAB_LEFT, _("Label"));
583 tab_hline (t, TAL_1, 1, 2, r);
585 labels = val_labs_sorted (val_labs);
586 for (i = 0; i < n_labels; i++)
588 const struct val_lab *vl = labels[i];
590 tab_value (t, 1, r, TAB_NONE, &vl->value, v, NULL);
591 tab_text (t, 2, r, TAB_LEFT, val_lab_get_escaped_label (vl));
596 tab_vline (t, TAL_1, 2, orig_r, r - 1);
599 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
601 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
604 display_attributes (t, var_get_attributes (v), flags, 1, r);
608 /* Draw a line below the last row of information on this variable. */
609 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
614 /* Display a list of vectors. If SORTED is nonzero then they are
615 sorted alphabetically. */
617 display_vectors (const struct dictionary *dict, int sorted)
619 const struct vector **vl;
626 nvec = dict_get_vector_cnt (dict);
629 msg (SW, _("No vectors defined."));
633 vl = xnmalloc (nvec, sizeof *vl);
635 for (i = 0; i < nvec; i++)
637 vl[i] = dict_get_vector (dict, i);
638 nrow += vector_get_var_cnt (vl[i]);
641 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
643 t = tab_create (4, nrow + 1);
644 tab_headers (t, 0, 0, 1, 0);
645 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
646 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
647 tab_hline (t, TAL_2, 0, 3, 1);
648 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
649 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
650 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
651 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
654 for (i = 0; i < nvec; i++)
656 const struct vector *vec = vl[i];
659 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
660 TAB_LEFT, vector_get_name (vl[i]));
662 for (j = 0; j < vector_get_var_cnt (vec); j++)
664 struct variable *var = vector_get_var (vec, j);
665 char fmt_string[FMT_STRING_LEN_MAX + 1];
666 fmt_to_string (var_get_print_format (var), fmt_string);
668 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
669 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
670 tab_text (t, 3, row, TAB_LEFT, fmt_string);
673 tab_hline (t, TAL_1, 0, 3, row);