1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010 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/message.h>
39 #include <libpspp/misc.h>
40 #include <output/tab.h>
46 #define _(msgid) gettext (msgid)
48 /* Information to include in displaying a dictionary. */
51 DF_DICT_INDEX = 1 << 0,
53 DF_VALUE_LABELS = 1 << 2,
54 DF_VARIABLE_LABELS = 1 << 3,
55 DF_MISSING_VALUES = 1 << 4,
56 DF_AT_ATTRIBUTES = 1 << 5, /* Attributes whose names begin with @. */
57 DF_ATTRIBUTES = 1 << 6, /* All other attributes. */
62 static int describe_variable (const struct variable *v, struct tab_table *t,
63 int r, int pc, int flags);
65 /* SYSFILE INFO utility. */
67 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
69 struct file_handle *h;
72 struct casereader *reader;
73 struct sfm_read_info info;
76 lex_match_id (lexer, "FILE");
77 lex_match (lexer, '=');
79 h = fh_parse (lexer, FH_REF_FILE);
83 reader = sfm_open_reader (h, &d, &info);
89 casereader_destroy (reader);
91 t = tab_create (2, 11);
92 tab_vline (t, TAL_GAP, 1, 0, 8);
93 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
94 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
95 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
97 const char *label = dict_get_label (d);
99 label = _("No label.");
100 tab_text (t, 1, 1, TAB_LEFT, label);
102 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
103 tab_text_format (t, 1, 2, TAB_LEFT, "%s %s by %s",
104 info.creation_date, info.creation_time, info.product);
105 tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
106 tab_text (t, 1, 3, TAB_LEFT,
107 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
108 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
110 tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
111 tab_text (t, 1, 4, TAB_LEFT,
112 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
113 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
114 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
115 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
116 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
118 tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
119 tab_text_format (t, 1, 5, TAB_LEFT, "%zu", dict_get_var_cnt (d));
120 tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
121 if (info.case_cnt == -1)
122 tab_text (t, 1, 6, TAB_LEFT, _("Unknown"));
124 tab_text_format (t, 1, 6, TAB_LEFT, "%ld", (long int) info.case_cnt);
125 tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
126 tab_text (t, 1, 7, TAB_LEFT, _("System File"));
127 tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
129 struct variable *weight_var = dict_get_weight (d);
130 tab_text (t, 1, 8, TAB_LEFT,
132 ? var_get_name (weight_var) : _("Not weighted.")));
134 tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
135 tab_text_format (t, 1, 9, TAB_LEFT,
136 _("Compression %s."), info.compressed ? _("on") : _("off"));
139 tab_text (t, 0, 10, TAB_LEFT, _("Charset:"));
140 tab_text (t, 1, 10, TAB_LEFT,
141 dict_get_encoding(d) ? dict_get_encoding(d) : _("Unknown"));
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 return lex_end_of_command (lexer);
169 /* DISPLAY utility. */
171 static void display_macros (void);
172 static void display_documents (const struct dictionary *dict);
173 static void display_variables (const struct variable **, size_t, int);
174 static void display_vectors (const struct dictionary *dict, int sorted);
175 static void display_data_file_attributes (struct attrset *, int flags);
178 cmd_display (struct lexer *lexer, struct dataset *ds)
180 /* Whether to sort the list of variables alphabetically. */
183 /* Variables to display. */
185 const struct variable **vl;
187 if (lex_match_id (lexer, "MACROS"))
189 else if (lex_match_id (lexer, "DOCUMENTS"))
190 display_documents (dataset_dict (ds));
191 else if (lex_match_id (lexer, "FILE"))
193 if (!lex_force_match_id (lexer, "LABEL"))
195 if (dict_get_label (dataset_dict (ds)) == NULL)
196 tab_output_text (TAB_LEFT,
197 _("The active file does not have a file label."));
200 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
201 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
208 sorted = lex_match_id (lexer, "SORTED");
210 if (lex_match_id (lexer, "VECTORS"))
212 display_vectors (dataset_dict(ds), sorted);
213 return lex_end_of_command (lexer);
215 else if (lex_match_id (lexer, "SCRATCH"))
217 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
227 static const struct subcommand subcommands[] =
229 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
230 {"ATTRIBUTES", DF_ATTRIBUTES},
231 {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
232 {"INDEX", DF_DICT_INDEX},
233 {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
236 DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
239 const struct subcommand *sbc;
242 for (sbc = subcommands; sbc->name != NULL; sbc++)
243 if (lex_match_id (lexer, sbc->name))
249 lex_match (lexer, '/');
250 lex_match_id (lexer, "VARIABLES");
251 lex_match (lexer, '=');
253 if (lex_token (lexer) != '.')
255 if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
263 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
268 sort (vl, n, sizeof *vl,
270 ? compare_var_ptrs_by_name
271 : compare_var_ptrs_by_dict_index), NULL);
272 display_variables (vl, n, flags);
275 msg (SW, _("No variables to display."));
278 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
279 display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
283 return lex_end_of_command (lexer);
287 display_macros (void)
289 tab_output_text (TAB_LEFT, _("Macros not supported."));
293 display_documents (const struct dictionary *dict)
295 const char *documents = dict_get_documents (dict);
297 if (documents == NULL)
298 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
299 "contain any documents."));
302 struct string line = DS_EMPTY_INITIALIZER;
305 tab_output_text (TAB_LEFT | TAT_TITLE,
306 _("Documents in the active file:"));
307 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
309 dict_get_document_line (dict, i, &line);
310 tab_output_text (TAB_LEFT | TAB_FIX, ds_cstr (&line));
317 display_variables (const struct variable **vl, size_t n, int flags)
320 int nc; /* Number of columns. */
321 int pc; /* `Position column' */
322 int r; /* Current row. */
325 /* One column for the name,
326 two columns for general description,
327 one column for dictionary index. */
329 if (flags & ~DF_DICT_INDEX)
332 if (flags & DF_DICT_INDEX)
335 t = tab_create (nc, n + 5);
336 tab_headers (t, 0, 0, 1, 0);
337 tab_hline (t, TAL_2, 0, nc - 1, 1);
338 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
339 if (flags & ~DF_DICT_INDEX)
340 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
341 (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
342 ? _("Description") : _("Label")));
343 if (flags & DF_DICT_INDEX)
344 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
347 for (i = 0; i < n; i++)
348 r = describe_variable (vl[i], t, r, pc, flags);
349 tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
352 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
353 tab_vline (t, TAL_1, 1, 0, r - 1);
355 if (flags & ~DF_DICT_INDEX)
356 tab_vline (t, TAL_1, nc - 1, 0, r - 1);
357 tab_resize (t, -1, r);
362 is_at_name (const char *name)
364 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
368 count_attributes (const struct attrset *set, int flags)
370 struct attrset_iterator i;
371 struct attribute *attr;
375 for (attr = attrset_first (set, &i); attr != NULL;
376 attr = attrset_next (set, &i))
377 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
378 n_attrs += attribute_get_n_values (attr);
383 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
386 struct attrset_iterator i;
387 struct attribute *attr;
389 for (attr = attrset_first (set, &i); attr != NULL;
390 attr = attrset_next (set, &i))
392 const char *name = attribute_get_name (attr);
396 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
399 n_values = attribute_get_n_values (attr);
400 for (i = 0; i < n_values; i++)
403 tab_text_format (t, c, r, TAB_LEFT, "%s[%d]", name, i + 1);
405 tab_text (t, c, r, TAB_LEFT, name);
406 tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
413 display_data_file_attributes (struct attrset *set, int flags)
418 n_attrs = count_attributes (set, flags);
422 t = tab_create (2, n_attrs + 1);
423 tab_headers (t, 0, 0, 1, 0);
424 tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
425 tab_hline (t, TAL_2, 0, 1, 1);
426 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
427 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
428 display_attributes (t, set, flags, 0, 1);
429 tab_title (t, "Custom data file attributes.");
433 /* Puts a description of variable V into table T starting at row
434 R. The variable will be described in the format given by
435 FLAGS. Returns the next row available for use in the
438 describe_variable (const struct variable *v, struct tab_table *t, int r,
444 /* Make sure that enough rows are allocated. */
446 if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
448 if (flags & DF_VALUE_LABELS)
449 need_rows += val_labs_count (var_get_value_labels (v));
450 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
452 n_attrs = count_attributes (var_get_attributes (v), flags);
453 need_rows += n_attrs;
455 if (r + need_rows > tab_nr (t))
457 int nr = MAX (r + need_rows, tab_nr (t) * 2);
458 tab_realloc (t, -1, nr);
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 if (flags & DF_DICT_INDEX)
464 tab_text_format (t, pc, r, 0, "%zu", var_get_dict_index (v) + 1);
466 if (flags & DF_VARIABLE_LABELS && var_has_label (v))
468 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
472 /* Print/write format, or print and write formats. */
473 if (flags & DF_FORMATS)
475 const struct fmt_spec *print = var_get_print_format (v);
476 const struct fmt_spec *write = var_get_write_format (v);
478 if (fmt_equal (print, write))
480 char str[FMT_STRING_LEN_MAX + 1];
481 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
482 _("Format: %s"), fmt_to_string (print, str));
487 char str[FMT_STRING_LEN_MAX + 1];
488 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
489 _("Print Format: %s"),
490 fmt_to_string (print, str));
492 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
493 _("Write Format: %s"),
494 fmt_to_string (write, str));
499 /* Measurement level, display width, alignment. */
502 enum measure m = var_get_measure (v);
503 enum alignment a = var_get_alignment (v);
505 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
507 m == MEASURE_NOMINAL ? _("Nominal")
508 : m == MEASURE_ORDINAL ? _("Ordinal")
511 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
512 _("Display Alignment: %s"),
513 a == ALIGN_LEFT ? _("Left")
514 : a == ALIGN_CENTRE ? _("Center")
517 tab_joint_text_format (t, 1, r, 2, r, TAB_LEFT,
518 _("Display Width: %d"),
519 var_get_display_width (v));
523 /* Missing values if any. */
524 if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
526 const struct missing_values *mv = var_get_missing_values (v);
532 cp = stpcpy (buf, _("Missing Values: "));
534 if (mv_has_range (mv))
537 mv_get_range (mv, &x, &y);
539 cp += sprintf (cp, "LOWEST THRU %g", y);
540 else if (y == HIGHEST)
541 cp += sprintf (cp, "%g THRU HIGHEST", x);
543 cp += sprintf (cp, "%g THRU %g", x, y);
546 for (i = 0; i < mv_n_values (mv); i++)
548 const union value *value = mv_get_value (mv, i);
550 cp += sprintf (cp, "; ");
551 if (var_is_numeric (v))
552 cp += sprintf (cp, "%g", value->f);
555 int width = var_get_width (v);
556 int mv_width = MIN (width, MV_MAX_STRING);
559 memcpy (cp, value_str (value, width), mv_width);
566 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
571 if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
573 const struct val_labs *val_labs = var_get_value_labels (v);
574 size_t n_labels = val_labs_count (val_labs);
575 const struct val_lab **labels;
580 tab_text (t, 1, r, TAB_LEFT, _("Value"));
581 tab_text (t, 2, r, TAB_LEFT, _("Label"));
585 tab_hline (t, TAL_1, 1, 2, r);
587 labels = val_labs_sorted (val_labs);
588 for (i = 0; i < n_labels; i++)
590 const struct val_lab *vl = labels[i];
591 char buf[MAX_STRING + 1];
593 if (var_is_alpha (v))
595 int width = var_get_width (v);
596 memcpy (buf, value_str (&vl->value, width), width);
600 sprintf (buf, "%g", vl->value.f);
602 tab_text (t, 1, r, TAB_NONE, buf);
603 tab_text (t, 2, r, TAB_LEFT, val_lab_get_label (vl));
608 tab_vline (t, TAL_1, 2, orig_r, r - 1);
611 if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
613 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
616 display_attributes (t, var_get_attributes (v), flags, 1, r);
620 /* Draw a line below the last row of information on this variable. */
621 tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
626 /* Display a list of vectors. If SORTED is nonzero then they are
627 sorted alphabetically. */
629 display_vectors (const struct dictionary *dict, int sorted)
631 const struct vector **vl;
638 nvec = dict_get_vector_cnt (dict);
641 msg (SW, _("No vectors defined."));
645 vl = xnmalloc (nvec, sizeof *vl);
647 for (i = 0; i < nvec; i++)
649 vl[i] = dict_get_vector (dict, i);
650 nrow += vector_get_var_cnt (vl[i]);
653 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
655 t = tab_create (4, nrow + 1);
656 tab_headers (t, 0, 0, 1, 0);
657 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
658 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
659 tab_hline (t, TAL_2, 0, 3, 1);
660 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
661 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
662 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
663 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
666 for (i = 0; i < nvec; i++)
668 const struct vector *vec = vl[i];
671 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
672 TAB_LEFT, vector_get_name (vl[i]));
674 for (j = 0; j < vector_get_var_cnt (vec); j++)
676 struct variable *var = vector_get_var (vec, j);
677 char fmt_string[FMT_STRING_LEN_MAX + 1];
678 fmt_to_string (var_get_print_format (var), fmt_string);
680 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
681 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
682 tab_text (t, 3, row, TAB_LEFT, fmt_string);
685 tab_hline (t, TAL_1, 0, 3, row);