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,
133 weight_var != NULL ? weight_var->name : _("Not weighted."));
135 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
136 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
137 _("Compression %s."), info.compressed ? _("on") : _("off"));
138 tab_dim (t, tab_natural_dimensions);
141 nr = 1 + 2 * dict_get_var_cnt (d);
143 t = tab_create (4, nr, 1);
144 tab_dim (t, sysfile_info_dim);
145 tab_headers (t, 0, 0, 1, 0);
146 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
147 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
148 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
149 tab_hline (t, TAL_2, 0, 3, 1);
150 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
152 struct variable *v = dict_get_var (d, i);
153 const int nvl = val_labs_count (v->val_labs);
155 if (r + 10 + nvl > nr)
157 nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
159 tab_realloc (t, 4, nr);
162 r = describe_variable (v, t, r, AS_DICTIONARY);
165 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
166 tab_vline (t, TAL_1, 1, 0, r);
167 tab_vline (t, TAL_1, 3, 0, r);
169 tab_resize (t, -1, r);
170 tab_flags (t, SOMF_NO_TITLE);
175 return lex_end_of_command (lexer);
178 /* DISPLAY utility. */
180 static void display_macros (void);
181 static void display_documents (const struct dictionary *dict);
182 static void display_variables (struct variable **, size_t, int);
183 static void display_vectors (const struct dictionary *dict, int sorted);
186 cmd_display (struct lexer *lexer, struct dataset *ds)
188 /* Whether to sort the list of variables alphabetically. */
191 /* Variables to display. */
193 struct variable **vl;
195 if (lex_match_id (lexer, "MACROS"))
197 else if (lex_match_id (lexer, "DOCUMENTS"))
198 display_documents (dataset_dict (ds));
199 else if (lex_match_id (lexer, "FILE"))
202 if (!lex_force_match_id (lexer, "LABEL"))
204 if (dict_get_label (dataset_dict (ds)) == NULL)
205 tab_output_text (TAB_LEFT,
206 _("The active file does not have a file label."));
209 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
210 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
215 static const char *sbc[] =
216 {"NAMES", "INDEX", "VARIABLES", "LABELS",
217 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
221 sorted = lex_match_id (lexer, "SORTED");
223 for (cp = sbc; *cp; cp++)
224 if (lex_token (lexer) == T_ID
225 && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
237 display_vectors (dataset_dict(ds), sorted);
241 lex_match (lexer, '/');
242 lex_match_id (lexer, "VARIABLES");
243 lex_match (lexer, '=');
245 if (lex_token (lexer) != '.')
247 if (!parse_variables (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
255 dict_get_vars (dataset_dict (ds), &vl, &n, 0);
257 if (as == AS_SCRATCH)
260 for (i = 0, m = n; i < n; i++)
261 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
272 msg (SW, _("No variables to display."));
277 sort (vl, n, sizeof *vl, compare_var_ptr_names, NULL);
279 display_variables (vl, n, as);
284 return lex_end_of_command (lexer);
288 display_macros (void)
291 tab_output_text (TAB_LEFT, _("Macros not supported."));
295 display_documents (const struct dictionary *dict)
297 const char *documents = dict_get_documents (dict);
300 if (documents == NULL)
301 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
302 "contain any documents."));
305 size_t n_lines = strlen (documents) / 80;
309 tab_output_text (TAB_LEFT | TAT_TITLE,
310 _("Documents in the active file:"));
313 for (i = 0; i < n_lines; i++)
317 memcpy (buf, &documents[i * 80], 80);
318 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
322 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
329 /* Sets the widths of all the columns and heights of all the rows in
330 table T for driver D. */
332 variables_dim (struct tab_table *t, struct outp_driver *d)
337 t->w[0] = tab_natural_width (t, d, 0);
338 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
340 t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
341 t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
346 t->w[pc] = tab_natural_width (t, d, pc);
348 for (i = 0; i < t->nr; i++)
349 t->h[i] = tab_natural_height (t, d, i);
353 display_variables (struct variable **vl, size_t n, int as)
355 struct variable **vp = vl; /* Variable pointer. */
357 int nc; /* Number of columns. */
358 int nr; /* Number of rows. */
359 int pc; /* `Position column' */
360 int r; /* Current row. */
377 t = tab_create (nc, n + 5, 1);
378 tab_headers (t, 0, 0, 1, 0);
380 tab_hline (t, TAL_2, 0, nc - 1, 1);
381 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
382 pc = (as == AS_INDEX ? 1 : 3);
384 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
385 if (as == AS_DICTIONARY || as == AS_VARIABLES)
386 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
387 else if (as == AS_LABELS)
388 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
389 tab_dim (t, variables_dim);
391 for (i = r = 1; i <= n; i++)
399 if (as == AS_DICTIONARY || as == AS_VARIABLES)
401 int nvl = val_labs_count (v->val_labs);
403 if (r + 10 + nvl > nr)
405 nr = MAX (nr * n / (i + 1), nr);
407 tab_realloc (t, nc, nr);
410 r = describe_variable (v, t, r, as);
412 tab_text (t, 0, r, TAB_LEFT, v->name);
414 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
415 v->label == NULL ? "(no label)" : v->label);
418 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
419 tab_hline (t, TAL_1, 0, nc - 1, r);
424 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
427 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
428 tab_vline (t, TAL_1, 1, 0, r - 1);
431 tab_flags (t, SOMF_NO_TITLE);
432 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
433 tab_vline (t, TAL_1, 3, 0, r - 1);
434 tab_resize (t, -1, r);
435 tab_columns (t, TAB_COL_DOWN, 1);
439 /* Puts a description of variable V into table T starting at row R.
440 The variable will be described in the format AS. Returns the next
441 row available for use in the table. */
443 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
445 /* Put the name, var label, and position into the first row. */
446 tab_text (t, 0, r, TAB_LEFT, v->name);
447 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
449 if (as == AS_DICTIONARY && v->label)
451 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
455 /* Print/write format, or print and write formats. */
456 if (v->print.type == v->write.type
457 && v->print.w == v->write.w
458 && v->print.d == v->write.d)
460 char str[FMT_STRING_LEN_MAX + 1];
461 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
462 fmt_to_string (&v->print, str));
467 char str[FMT_STRING_LEN_MAX + 1];
468 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
469 _("Print Format: %s"), fmt_to_string (&v->print, str));
471 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
472 _("Write Format: %s"), fmt_to_string (&v->write, str));
476 /* Missing values if any. */
477 if (!mv_is_empty (&v->miss))
481 struct missing_values mv;
484 cp = stpcpy (buf, _("Missing Values: "));
485 mv_copy (&mv, &v->miss);
486 if (mv_has_range (&mv))
489 mv_pop_range (&mv, &x, &y);
491 cp += sprintf (cp, "LOWEST THRU %g", y);
492 else if (y == HIGHEST)
493 cp += sprintf (cp, "%g THRU HIGHEST", x);
495 cp += sprintf (cp, "%g THRU %g", x, y);
498 while (mv_has_value (&mv))
501 mv_pop_value (&mv, &value);
503 cp += sprintf (cp, "; ");
504 if (v->type == NUMERIC)
505 cp += sprintf (cp, "%g", value.f);
509 memcpy (cp, value.s, v->width);
516 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
521 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
523 struct val_labs_iterator *i;
528 tab_text (t, 1, r, TAB_LEFT, _("Value"));
529 tab_text (t, 2, r, TAB_LEFT, _("Label"));
533 tab_hline (t, TAL_1, 1, 2, r);
534 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
535 vl = val_labs_next (v->val_labs, &i))
539 if (v->type == ALPHA)
541 memcpy (buf, vl->value.s, v->width);
545 sprintf (buf, "%g", vl->value.f);
547 tab_text (t, 1, r, TAB_NONE, buf);
548 tab_text (t, 2, r, TAB_LEFT, vl->label);
552 tab_vline (t, TAL_1, 2, orig_r, r - 1);
555 /* Draw a line below the last row of information on this variable. */
556 tab_hline (t, TAL_1, 0, 3, r);
562 compare_vectors_by_name (const void *a_, const void *b_)
564 struct vector *const *pa = a_;
565 struct vector *const *pb = b_;
566 struct vector *a = *pa;
567 struct vector *b = *pb;
569 return strcasecmp (a->name, b->name);
572 /* Display a list of vectors. If SORTED is nonzero then they are
573 sorted alphabetically. */
575 display_vectors (const struct dictionary *dict, int sorted)
577 const struct vector **vl;
582 nvec = dict_get_vector_cnt (dict);
585 msg (SW, _("No vectors defined."));
589 vl = xnmalloc (nvec, sizeof *vl);
590 for (i = 0; i < nvec; i++)
591 vl[i] = dict_get_vector (dict, i);
593 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
595 t = tab_create (1, nvec + 1, 0);
596 tab_headers (t, 0, 0, 1, 0);
597 tab_columns (t, TAB_COL_DOWN, 1);
598 tab_dim (t, tab_natural_dimensions);
599 tab_hline (t, TAL_1, 0, 0, 1);
600 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
601 tab_flags (t, SOMF_NO_TITLE);
602 for (i = 0; i < nvec; i++)
603 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);