1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <data/dictionary.h>
26 #include <data/file-handle-def.h>
27 #include <data/procedure.h>
28 #include <data/sys-file-reader.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/file-handle.h>
33 #include <language/lexer/lexer.h>
34 #include <language/lexer/variable-parser.h>
35 #include <libpspp/alloc.h>
36 #include <libpspp/array.h>
37 #include <libpspp/hash.h>
38 #include <libpspp/magic.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>
49 #define _(msgid) gettext (msgid)
51 /* Constants for DISPLAY utility. */
63 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
65 /* Sets the widths of all the columns and heights of all the rows in
66 table T for driver D. */
68 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
70 static const int max[] = {20, 5, 35, 3, 0};
74 for (p = max; *p; p++)
75 t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
76 *p * d->prop_em_width);
77 for (i = 0; i < t->nr; i++)
78 t->h[i] = tab_natural_height (t, d, i);
81 /* SYSFILE INFO utility. */
83 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
85 struct file_handle *h;
88 struct sfm_reader *reader;
89 struct sfm_read_info info;
93 lex_match_id (lexer, "FILE");
94 lex_match (lexer, '=');
96 h = fh_parse (lexer, FH_REF_FILE);
100 reader = sfm_open_reader (h, &d, &info);
103 sfm_close_reader (reader);
105 t = tab_create (2, 9, 0);
106 tab_vline (t, TAL_GAP, 1, 0, 8);
107 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
108 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
109 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
111 const char *label = dict_get_label (d);
113 label = _("No label.");
114 tab_text (t, 1, 1, TAB_LEFT, label);
116 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
117 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
118 info.creation_date, info.creation_time, info.product);
119 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
120 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
121 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
122 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
123 dict_get_var_cnt (d));
124 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
125 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
126 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
127 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
128 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
129 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
131 struct variable *weight_var = dict_get_weight (d);
132 tab_text (t, 1, 7, TAB_LEFT,
134 ? var_get_name (weight_var) : _("Not weighted.")));
136 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
137 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
138 _("Compression %s."), info.compressed ? _("on") : _("off"));
139 tab_dim (t, tab_natural_dimensions);
142 nr = 1 + 2 * dict_get_var_cnt (d);
144 t = tab_create (4, nr, 1);
145 tab_dim (t, sysfile_info_dim);
146 tab_headers (t, 0, 0, 1, 0);
147 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
148 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
149 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
150 tab_hline (t, TAL_2, 0, 3, 1);
151 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
153 struct variable *v = dict_get_var (d, i);
154 const int nvl = val_labs_count (v->val_labs);
156 if (r + 10 + nvl > nr)
158 nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
160 tab_realloc (t, 4, nr);
163 r = describe_variable (v, t, r, AS_DICTIONARY);
166 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
167 tab_vline (t, TAL_1, 1, 0, r);
168 tab_vline (t, TAL_1, 3, 0, r);
170 tab_resize (t, -1, r);
171 tab_flags (t, SOMF_NO_TITLE);
176 return lex_end_of_command (lexer);
179 /* DISPLAY utility. */
181 static void display_macros (void);
182 static void display_documents (const struct dictionary *dict);
183 static void display_variables (struct variable **, size_t, int);
184 static void display_vectors (const struct dictionary *dict, int sorted);
187 cmd_display (struct lexer *lexer, struct dataset *ds)
189 /* Whether to sort the list of variables alphabetically. */
192 /* Variables to display. */
194 struct variable **vl;
196 if (lex_match_id (lexer, "MACROS"))
198 else if (lex_match_id (lexer, "DOCUMENTS"))
199 display_documents (dataset_dict (ds));
200 else if (lex_match_id (lexer, "FILE"))
203 if (!lex_force_match_id (lexer, "LABEL"))
205 if (dict_get_label (dataset_dict (ds)) == NULL)
206 tab_output_text (TAB_LEFT,
207 _("The active file does not have a file label."));
210 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
211 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
216 static const char *sbc[] =
217 {"NAMES", "INDEX", "VARIABLES", "LABELS",
218 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
222 sorted = lex_match_id (lexer, "SORTED");
224 for (cp = sbc; *cp; cp++)
225 if (lex_token (lexer) == T_ID
226 && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
238 display_vectors (dataset_dict(ds), sorted);
242 lex_match (lexer, '/');
243 lex_match_id (lexer, "VARIABLES");
244 lex_match (lexer, '=');
246 if (lex_token (lexer) != '.')
248 if (!parse_variables (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
256 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
258 if (as == AS_SCRATCH)
261 for (i = 0, m = n; i < n; i++)
262 if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
273 msg (SW, _("No variables to display."));
278 sort (vl, n, sizeof *vl, compare_var_ptr_names, NULL);
280 display_variables (vl, n, as);
285 return lex_end_of_command (lexer);
289 display_macros (void)
292 tab_output_text (TAB_LEFT, _("Macros not supported."));
296 display_documents (const struct dictionary *dict)
298 const char *documents = dict_get_documents (dict);
301 if (documents == NULL)
302 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
303 "contain any documents."));
306 size_t n_lines = strlen (documents) / 80;
310 tab_output_text (TAB_LEFT | TAT_TITLE,
311 _("Documents in the active file:"));
314 for (i = 0; i < n_lines; i++)
318 memcpy (buf, &documents[i * 80], 80);
319 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
323 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
330 /* Sets the widths of all the columns and heights of all the rows in
331 table T for driver D. */
333 variables_dim (struct tab_table *t, struct outp_driver *d)
338 t->w[0] = tab_natural_width (t, d, 0);
339 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
341 t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
342 t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
347 t->w[pc] = tab_natural_width (t, d, pc);
349 for (i = 0; i < t->nr; i++)
350 t->h[i] = tab_natural_height (t, d, i);
354 display_variables (struct variable **vl, size_t n, int as)
356 struct variable **vp = vl; /* Variable pointer. */
358 int nc; /* Number of columns. */
359 int nr; /* Number of rows. */
360 int pc; /* `Position column' */
361 int r; /* Current row. */
378 t = tab_create (nc, n + 5, 1);
379 tab_headers (t, 0, 0, 1, 0);
381 tab_hline (t, TAL_2, 0, nc - 1, 1);
382 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
383 pc = (as == AS_INDEX ? 1 : 3);
385 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
386 if (as == AS_DICTIONARY || as == AS_VARIABLES)
387 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
388 else if (as == AS_LABELS)
389 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
390 tab_dim (t, variables_dim);
392 for (i = r = 1; i <= n; i++)
400 if (as == AS_DICTIONARY || as == AS_VARIABLES)
402 int nvl = val_labs_count (v->val_labs);
404 if (r + 10 + nvl > nr)
406 nr = MAX (nr * n / (i + 1), nr);
408 tab_realloc (t, nc, nr);
411 r = describe_variable (v, t, r, as);
413 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
416 const char *label = var_get_label (v);
417 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
418 label != NULL ? "(no label)" : label);
422 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
423 tab_hline (t, TAL_1, 0, nc - 1, r);
428 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
431 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
432 tab_vline (t, TAL_1, 1, 0, r - 1);
435 tab_flags (t, SOMF_NO_TITLE);
436 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
437 tab_vline (t, TAL_1, 3, 0, r - 1);
438 tab_resize (t, -1, r);
439 tab_columns (t, TAB_COL_DOWN, 1);
443 /* Puts a description of variable V into table T starting at row R.
444 The variable will be described in the format AS. Returns the next
445 row available for use in the table. */
447 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
449 const struct fmt_spec *print = var_get_print_format (v);
450 const struct fmt_spec *write = var_get_write_format (v);
452 /* Put the name, var label, and position into the first row. */
453 tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
454 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
456 if (as == AS_DICTIONARY && var_has_label (v))
458 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
462 /* Print/write format, or print and write formats. */
463 if (fmt_equal (print, write))
465 char str[FMT_STRING_LEN_MAX + 1];
466 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
467 fmt_to_string (print, str));
472 char str[FMT_STRING_LEN_MAX + 1];
473 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
474 _("Print Format: %s"), fmt_to_string (print, str));
476 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
477 _("Write Format: %s"), fmt_to_string (write, str));
481 /* Missing values if any. */
482 if (var_has_missing_values (v))
486 struct missing_values mv;
489 cp = stpcpy (buf, _("Missing Values: "));
491 mv_copy (&mv, var_get_missing_values (v));
492 if (mv_has_range (&mv))
495 mv_pop_range (&mv, &x, &y);
497 cp += sprintf (cp, "LOWEST THRU %g", y);
498 else if (y == HIGHEST)
499 cp += sprintf (cp, "%g THRU HIGHEST", x);
501 cp += sprintf (cp, "%g THRU %g", x, y);
504 while (mv_has_value (&mv))
507 mv_pop_value (&mv, &value);
509 cp += sprintf (cp, "; ");
510 if (var_is_numeric (v))
511 cp += sprintf (cp, "%g", value.f);
515 memcpy (cp, value.s, var_get_width (v));
516 cp += var_get_width (v);
522 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
527 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
529 struct val_labs_iterator *i;
534 tab_text (t, 1, r, TAB_LEFT, _("Value"));
535 tab_text (t, 2, r, TAB_LEFT, _("Label"));
539 tab_hline (t, TAL_1, 1, 2, r);
540 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
541 vl = val_labs_next (v->val_labs, &i))
545 if (var_is_alpha (v))
547 memcpy (buf, vl->value.s, var_get_width (v));
548 buf[var_get_width (v)] = 0;
551 sprintf (buf, "%g", vl->value.f);
553 tab_text (t, 1, r, TAB_NONE, buf);
554 tab_text (t, 2, r, TAB_LEFT, vl->label);
558 tab_vline (t, TAL_1, 2, orig_r, r - 1);
561 /* Draw a line below the last row of information on this variable. */
562 tab_hline (t, TAL_1, 0, 3, r);
568 compare_vectors_by_name (const void *a_, const void *b_)
570 struct vector *const *pa = a_;
571 struct vector *const *pb = b_;
572 struct vector *a = *pa;
573 struct vector *b = *pb;
575 return strcasecmp (a->name, b->name);
578 /* Display a list of vectors. If SORTED is nonzero then they are
579 sorted alphabetically. */
581 display_vectors (const struct dictionary *dict, int sorted)
583 const struct vector **vl;
588 nvec = dict_get_vector_cnt (dict);
591 msg (SW, _("No vectors defined."));
595 vl = xnmalloc (nvec, sizeof *vl);
596 for (i = 0; i < nvec; i++)
597 vl[i] = dict_get_vector (dict, i);
599 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
601 t = tab_create (1, nvec + 1, 0);
602 tab_headers (t, 0, 0, 1, 0);
603 tab_columns (t, TAB_COL_DOWN, 1);
604 tab_dim (t, tab_natural_dimensions);
605 tab_hline (t, TAL_1, 0, 0, 1);
606 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
607 tab_flags (t, SOMF_NO_TITLE);
608 for (i = 0; i < nvec; i++)
609 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);