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/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)
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, 10, 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"));
156 tab_dim (t, tab_natural_dimensions);
159 t = tab_create (4, 1 + 2 * dict_get_var_cnt (d), 1);
160 tab_dim (t, sysfile_info_dim);
161 tab_headers (t, 0, 0, 1, 0);
162 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
163 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
164 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
165 tab_hline (t, TAL_2, 0, 3, 1);
166 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
167 r = describe_variable (dict_get_var (d, i), t, r, 3,
168 DF_ALL & ~DF_AT_ATTRIBUTES);
170 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
171 tab_vline (t, TAL_1, 1, 0, r);
172 tab_vline (t, TAL_1, 3, 0, r);
174 tab_resize (t, -1, r);
175 tab_flags (t, SOMF_NO_TITLE);
181 return lex_end_of_command (lexer);
184 /* DISPLAY utility. */
186 static void display_macros (void);
187 static void display_documents (const struct dictionary *dict);
188 static void display_variables (const struct variable **, size_t, int);
189 static void display_vectors (const struct dictionary *dict, int sorted);
190 static void display_data_file_attributes (struct attrset *, int flags);
193 cmd_display (struct lexer *lexer, struct dataset *ds)
195 /* Whether to sort the list of variables alphabetically. */
198 /* Variables to display. */
200 const struct variable **vl;
202 if (lex_match_id (lexer, "MACROS"))
204 else if (lex_match_id (lexer, "DOCUMENTS"))
205 display_documents (dataset_dict (ds));
206 else if (lex_match_id (lexer, "FILE"))
209 if (!lex_force_match_id (lexer, "LABEL"))
211 if (dict_get_label (dataset_dict (ds)) == NULL)
212 tab_output_text (TAB_LEFT,
213 _("The active file does not have a file label."));
216 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
217 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
224 sorted = lex_match_id (lexer, "SORTED");
226 if (lex_match_id (lexer, "VECTORS"))
228 display_vectors (dataset_dict(ds), sorted);
229 return lex_end_of_command (lexer);
231 else if (lex_match_id (lexer, "SCRATCH"))
233 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
243 static const struct subcommand subcommands[] =
245 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
246 {"ATTRIBUTES", DF_ATTRIBUTES},
247 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
248 {"INDEX", DF_DICT_INDEX},
249 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
252 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
255 const struct subcommand *sbc;
258 for (sbc = subcommands; sbc->name != NULL; sbc++)
259 if (lex_match_id (lexer, sbc->name))
265 lex_match (lexer, '/');
266 lex_match_id (lexer, "VARIABLES");
267 lex_match (lexer, '=');
269 if (lex_token (lexer) != '.')
271 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
279 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
284 sort (vl, n, sizeof *vl,
286 ? compare_var_ptrs_by_name
287 : compare_var_ptrs_by_dict_index), NULL);
288 display_variables (vl, n, flags);
291 msg (SW, _("No variables to display."));
294 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
295 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
299 return lex_end_of_command (lexer);
303 display_macros (void)
306 tab_output_text (TAB_LEFT, _("Macros not supported."));
310 display_documents (const struct dictionary *dict)
312 const char *documents = dict_get_documents (dict);
315 if (documents == NULL)
316 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
317 "contain any documents."));
320 struct string line = DS_EMPTY_INITIALIZER;
323 tab_output_text (TAB_LEFT | TAT_TITLE,
324 _("Documents in the active file:"));
326 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
328 dict_get_document_line (dict, i, &line);
329 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
337 /* Sets the widths of all the columns and heights of all the rows in
338 table T for driver D. */
340 variables_dim (struct tab_table *t, struct outp_driver *d)
345 t->w[0] = tab_natural_width (t, d, 0);
346 if (_flags & (DF_VALUE_LABELS | DF_VARIABLE_LABELS | DF_MISSING_VALUES
347 | DF_AT_ATTRIBUTES | DF_ATTRIBUTES))
349 t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
350 t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
355 if (_flags & DF_DICT_INDEX)
356 t->w[pc] = tab_natural_width (t, d, pc);
358 for (i = 0; i < t->nr; i++)
359 t->h[i] = tab_natural_height (t, d, i);
363 display_variables (const struct variable **vl, size_t n, int flags)
366 int nc; /* Number of columns. */
367 int pc; /* `Position column' */
368 int r; /* Current row. */
373 /* One column for the name,
374 two columns for general description,
375 one column for dictionary index. */
377 if (flags & ~DF_DICT_INDEX)
380 if (flags & DF_DICT_INDEX)
383 t = tab_create (nc, n + 5, 1);
384 tab_headers (t, 0, 0, 1, 0);
385 tab_hline (t, TAL_2, 0, nc - 1, 1);
386 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
387 if (flags & ~DF_DICT_INDEX)
388 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
389 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
390 ? _("Description") : _("Label")));
391 if (flags & DF_DICT_INDEX)
392 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
393 tab_dim (t, variables_dim);
396 for (i = 0; i < n; i++)
397 r = describe_variable (vl[i], t, r, pc, flags);
398 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
401 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
402 tab_vline (t, TAL_1, 1, 0, r - 1);
405 tab_flags (t, SOMF_NO_TITLE);
406 if (flags & ~DF_DICT_INDEX)
407 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
408 tab_resize (t, -1, r);
409 tab_columns (t, TAB_COL_DOWN, 1);
414 is_at_name (const char *name)
416 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
420 count_attributes (const struct attrset *set, int flags)
422 struct attrset_iterator i;
423 struct attribute *attr;
427 for (attr = attrset_first (set, &i); attr != NULL;
428 attr = attrset_next (set, &i))
429 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
430 n_attrs += attribute_get_n_values (attr);
435 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
438 struct attrset_iterator i;
439 struct attribute *attr;
441 for (attr = attrset_first (set, &i); attr != NULL;
442 attr = attrset_next (set, &i))
444 const char *name = attribute_get_name (attr);
448 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
451 n_values = attribute_get_n_values (attr);
452 for (i = 0; i < n_values; i++)
455 tab_text (t, c, r, TAB_LEFT | TAT_PRINTF, "%s[%d]",
458 tab_text (t, c, r, TAB_LEFT, name);
459 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
466 display_data_file_attributes (struct attrset *set, int flags)
471 n_attrs = count_attributes (set, flags);
475 t = tab_create (2, n_attrs + 1, 0);
476 tab_headers (t, 0, 0, 1, 0);
477 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
478 tab_hline (t, TAL_2, 0, 1, 1);
479 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
480 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
481 display_attributes (t, set, flags, 0, 1);
482 tab_columns (t, TAB_COL_DOWN, 1);
483 tab_dim (t, tab_natural_dimensions);
484 tab_title (t, "Custom data file attributes.");
488 /* Puts a description of variable V into table T starting at row
489 R. The variable will be described in the format given by
490 FLAGS. Returns the next row available for use in the
493 describe_variable (const struct variable *v, struct tab_table *t, int r,
499 /* Make sure that enough rows are allocated. */
501 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
503 if (flags & DF_VALUE_LABELS)
504 need_rows += val_labs_count (var_get_value_labels (v));
505 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
507 n_attrs = count_attributes (var_get_attributes (v), flags);
508 need_rows += n_attrs;
510 if (r + need_rows > tab_nr (t))
512 int nr = MAX (r + need_rows, tab_nr (t) * 2);
513 tab_realloc (t, -1, nr);
516 /* Put the name, var label, and position into the first row. */
517 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
518 if (flags & DF_DICT_INDEX)
519 tab_text (t, pc, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
521 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
523 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
527 /* Print/write format, or print and write formats. */
528 if (flags & DF_FORMATS)
530 const struct fmt_spec *print = var_get_print_format (v);
531 const struct fmt_spec *write = var_get_write_format (v);
533 if (fmt_equal (print, write))
535 char str[FMT_STRING_LEN_MAX + 1];
536 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
537 _("Format: %s"), fmt_to_string (print, str));
542 char str[FMT_STRING_LEN_MAX + 1];
543 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
544 _("Print Format: %s"), fmt_to_string (print, str));
546 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
547 _("Write Format: %s"), fmt_to_string (write, str));
552 /* Measurement level, display width, alignment. */
555 enum measure m = var_get_measure (v);
556 enum alignment a = var_get_alignment (v);
558 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
560 m == MEASURE_NOMINAL ? _("Nominal")
561 : m == MEASURE_ORDINAL ? _("Ordinal")
564 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
565 _("Display Alignment: %s"),
566 a == ALIGN_LEFT ? _("Left")
567 : a == ALIGN_CENTRE ? _("Center")
570 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
571 _("Display Width: %d"), var_get_display_width (v));
575 /* Missing values if any. */
576 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
580 struct missing_values mv;
583 cp = stpcpy (buf, _("Missing Values: "));
585 mv_copy (&mv, var_get_missing_values (v));
586 if (mv_has_range (&mv))
589 mv_pop_range (&mv, &x, &y);
591 cp += sprintf (cp, "LOWEST THRU %g", y);
592 else if (y == HIGHEST)
593 cp += sprintf (cp, "%g THRU HIGHEST", x);
595 cp += sprintf (cp, "%g THRU %g", x, y);
598 while (mv_has_value (&mv))
601 mv_pop_value (&mv, &value);
603 cp += sprintf (cp, "; ");
604 if (var_is_numeric (v))
605 cp += sprintf (cp, "%g", value.f);
609 memcpy (cp, value.s, var_get_width (v));
610 cp += var_get_width (v);
616 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
621 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
623 const struct val_labs *val_labs = var_get_value_labels (v);
624 struct val_labs_iterator *i;
629 tab_text (t, 1, r, TAB_LEFT, _("Value"));
630 tab_text (t, 2, r, TAB_LEFT, _("Label"));
634 tab_hline (t, TAL_1, 1, 2, r);
635 for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
636 vl = val_labs_next (val_labs, &i))
640 if (var_is_alpha (v))
642 memcpy (buf, vl->value.s, var_get_width (v));
643 buf[var_get_width (v)] = 0;
646 sprintf (buf, "%g", vl->value.f);
648 tab_text (t, 1, r, TAB_NONE, buf);
649 tab_text (t, 2, r, TAB_LEFT, vl->label);
653 tab_vline (t, TAL_1, 2, orig_r, r - 1);
656 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
658 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
661 display_attributes (t, var_get_attributes (v), flags, 1, r);
665 /* Draw a line below the last row of information on this variable. */
666 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
671 /* Display a list of vectors. If SORTED is nonzero then they are
672 sorted alphabetically. */
674 display_vectors (const struct dictionary *dict, int sorted)
676 const struct vector **vl;
683 nvec = dict_get_vector_cnt (dict);
686 msg (SW, _("No vectors defined."));
690 vl = xnmalloc (nvec, sizeof *vl);
692 for (i = 0; i < nvec; i++)
694 vl[i] = dict_get_vector (dict, i);
695 nrow += vector_get_var_cnt (vl[i]);
698 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
700 t = tab_create (4, nrow + 1, 0);
701 tab_headers (t, 0, 0, 1, 0);
702 tab_columns (t, TAB_COL_DOWN, 1);
703 tab_dim (t, tab_natural_dimensions);
704 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
705 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
706 tab_hline (t, TAL_2, 0, 3, 1);
707 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
708 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
709 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
710 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
711 tab_flags (t, SOMF_NO_TITLE);
714 for (i = 0; i < nvec; i++)
716 const struct vector *vec = vl[i];
719 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
720 TAB_LEFT, vector_get_name (vl[i]));
722 for (j = 0; j < vector_get_var_cnt (vec); j++)
724 struct variable *var = vector_get_var (vec, j);
725 char fmt_string[FMT_STRING_LEN_MAX + 1];
726 fmt_to_string (var_get_print_format (var), fmt_string);
728 tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
729 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
730 tab_text (t, 3, row, TAB_LEFT, fmt_string);
733 tab_hline (t, TAL_1, 0, 3, row);