1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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 <libpspp/alloc.h>
35 #include <libpspp/array.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/magic.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <output/manager.h>
42 #include <output/output.h>
43 #include <output/table.h>
46 #define _(msgid) gettext (msgid)
48 /* Constants for DISPLAY utility. */
60 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
62 /* Sets the widths of all the columns and heights of all the rows in
63 table T for driver D. */
65 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
67 static const int max[] = {20, 5, 35, 3, 0};
71 for (p = max; *p; p++)
72 t->w[p - max] = min (tab_natural_width (t, d, p - max),
73 *p * d->prop_em_width);
74 for (i = 0; i < t->nr; i++)
75 t->h[i] = tab_natural_height (t, d, i);
78 /* SYSFILE INFO utility. */
80 cmd_sysfile_info (void)
82 struct file_handle *h;
85 struct sfm_reader *reader;
86 struct sfm_read_info info;
90 lex_match_id ("FILE");
93 h = fh_parse (FH_REF_FILE);
97 reader = sfm_open_reader (h, &d, &info);
100 sfm_close_reader (reader);
102 t = tab_create (2, 9, 0);
103 tab_vline (t, TAL_GAP, 1, 0, 8);
104 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
105 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
106 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
108 const char *label = dict_get_label (d);
110 label = _("No label.");
111 tab_text (t, 1, 1, TAB_LEFT, label);
113 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
114 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
115 info.creation_date, info.creation_time, info.product);
116 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
117 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
118 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
119 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
120 dict_get_var_cnt (d));
121 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
122 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
123 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
124 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
125 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
126 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
128 struct variable *weight_var = dict_get_weight (d);
129 tab_text (t, 1, 7, TAB_LEFT,
130 weight_var != NULL ? weight_var->name : _("Not weighted."));
132 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
133 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
134 _("Compression %s."), info.compressed ? _("on") : _("off"));
135 tab_dim (t, tab_natural_dimensions);
138 nr = 1 + 2 * dict_get_var_cnt (d);
140 t = tab_create (4, nr, 1);
141 tab_dim (t, sysfile_info_dim);
142 tab_headers (t, 0, 0, 1, 0);
143 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
144 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
145 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
146 tab_hline (t, TAL_2, 0, 3, 1);
147 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
149 struct variable *v = dict_get_var (d, i);
150 const int nvl = val_labs_count (v->val_labs);
152 if (r + 10 + nvl > nr)
154 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
156 tab_realloc (t, 4, nr);
159 r = describe_variable (v, t, r, AS_DICTIONARY);
162 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
163 tab_vline (t, TAL_1, 1, 0, r);
164 tab_vline (t, TAL_1, 3, 0, r);
166 tab_resize (t, -1, r);
167 tab_flags (t, SOMF_NO_TITLE);
172 return lex_end_of_command ();
175 /* DISPLAY utility. */
177 static void display_macros (void);
178 static void display_documents (void);
179 static void display_variables (struct variable **, size_t, int);
180 static void display_vectors (int sorted);
185 /* Whether to sort the list of variables alphabetically. */
188 /* Variables to display. */
190 struct variable **vl;
192 if (lex_match_id ("MACROS"))
194 else if (lex_match_id ("DOCUMENTS"))
195 display_documents ();
196 else if (lex_match_id ("FILE"))
199 if (!lex_force_match_id ("LABEL"))
201 if (dict_get_label (default_dict) == NULL)
202 tab_output_text (TAB_LEFT,
203 _("The active file does not have a file label."));
206 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
207 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (default_dict));
212 static const char *sbc[] =
213 {"NAMES", "INDEX", "VARIABLES", "LABELS",
214 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
218 sorted = lex_match_id ("SORTED");
220 for (cp = sbc; *cp; cp++)
221 if (token == T_ID && lex_id_match (*cp, tokid))
233 display_vectors (sorted);
238 lex_match_id ("VARIABLES");
243 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
251 dict_get_vars (default_dict, &vl, &n, 0);
253 if (as == AS_SCRATCH)
256 for (i = 0, m = n; i < n; i++)
257 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
268 msg (SW, _("No variables to display."));
273 sort (vl, n, sizeof *vl, compare_var_names, NULL);
275 display_variables (vl, n, as);
280 return lex_end_of_command ();
284 display_macros (void)
287 tab_output_text (TAB_LEFT, _("Macros not supported."));
291 display_documents (void)
293 const char *documents = dict_get_documents (default_dict);
296 if (documents == NULL)
297 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
298 "contain any documents."));
301 size_t n_lines = strlen (documents) / 80;
305 tab_output_text (TAB_LEFT | TAT_TITLE,
306 _("Documents in the active file:"));
309 for (i = 0; i < n_lines; i++)
313 memcpy (buf, &documents[i * 80], 80);
314 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
318 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
325 /* Sets the widths of all the columns and heights of all the rows in
326 table T for driver D. */
328 variables_dim (struct tab_table *t, struct outp_driver *d)
333 t->w[0] = tab_natural_width (t, d, 0);
334 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
336 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
337 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
342 t->w[pc] = tab_natural_width (t, d, pc);
344 for (i = 0; i < t->nr; i++)
345 t->h[i] = tab_natural_height (t, d, i);
349 display_variables (struct variable **vl, size_t n, int as)
351 struct variable **vp = vl; /* Variable pointer. */
353 int nc; /* Number of columns. */
354 int nr; /* Number of rows. */
355 int pc; /* `Position column' */
356 int r; /* Current row. */
373 t = tab_create (nc, n + 5, 1);
374 tab_headers (t, 0, 0, 1, 0);
376 tab_hline (t, TAL_2, 0, nc - 1, 1);
377 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
378 pc = (as == AS_INDEX ? 1 : 3);
380 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
381 if (as == AS_DICTIONARY || as == AS_VARIABLES)
382 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
383 else if (as == AS_LABELS)
384 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
385 tab_dim (t, variables_dim);
387 for (i = r = 1; i <= n; i++)
395 if (as == AS_DICTIONARY || as == AS_VARIABLES)
397 int nvl = val_labs_count (v->val_labs);
399 if (r + 10 + nvl > nr)
401 nr = max (nr * n / (i + 1), nr);
403 tab_realloc (t, nc, nr);
406 r = describe_variable (v, t, r, as);
408 tab_text (t, 0, r, TAB_LEFT, v->name);
410 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
411 v->label == NULL ? "(no label)" : v->label);
414 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
415 tab_hline (t, TAL_1, 0, nc - 1, r);
420 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
423 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
424 tab_vline (t, TAL_1, 1, 0, r - 1);
427 tab_flags (t, SOMF_NO_TITLE);
428 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
429 tab_vline (t, TAL_1, 3, 0, r - 1);
430 tab_resize (t, -1, r);
431 tab_columns (t, TAB_COL_DOWN, 1);
435 /* Puts a description of variable V into table T starting at row R.
436 The variable will be described in the format AS. Returns the next
437 row available for use in the table. */
439 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
441 /* Put the name, var label, and position into the first row. */
442 tab_text (t, 0, r, TAB_LEFT, v->name);
443 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
445 if (as == AS_DICTIONARY && v->label)
447 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
451 /* Print/write format, or print and write formats. */
452 if (v->print.type == v->write.type
453 && v->print.w == v->write.w
454 && v->print.d == v->write.d)
456 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
457 fmt_to_string (&v->print));
462 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
463 _("Print Format: %s"), fmt_to_string (&v->print));
465 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
466 _("Write Format: %s"), fmt_to_string (&v->write));
470 /* Missing values if any. */
471 if (!mv_is_empty (&v->miss))
475 struct missing_values mv;
478 cp = stpcpy (buf, _("Missing Values: "));
479 mv_copy (&mv, &v->miss);
480 if (mv_has_range (&mv))
483 mv_pop_range (&mv, &x, &y);
485 cp += nsprintf (cp, "LOWEST THRU %g", y);
486 else if (y == HIGHEST)
487 cp += nsprintf (cp, "%g THRU HIGHEST", x);
489 cp += nsprintf (cp, "%g THRU %g", x, y);
492 while (mv_has_value (&mv))
495 mv_pop_value (&mv, &value);
497 cp += nsprintf (cp, "; ");
498 if (v->type == NUMERIC)
499 cp += nsprintf (cp, "%g", value.f);
503 memcpy (cp, value.s, v->width);
510 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
515 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
517 struct val_labs_iterator *i;
522 tab_text (t, 1, r, TAB_LEFT, _("Value"));
523 tab_text (t, 2, r, TAB_LEFT, _("Label"));
527 tab_hline (t, TAL_1, 1, 2, r);
528 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
529 vl = val_labs_next (v->val_labs, &i))
533 if (v->type == ALPHA)
535 memcpy (buf, vl->value.s, v->width);
539 sprintf (buf, "%g", vl->value.f);
541 tab_text (t, 1, r, TAB_NONE, buf);
542 tab_text (t, 2, r, TAB_LEFT, vl->label);
546 tab_vline (t, TAL_1, 2, orig_r, r - 1);
549 /* Draw a line below the last row of information on this variable. */
550 tab_hline (t, TAL_1, 0, 3, r);
556 compare_vectors_by_name (const void *a_, const void *b_)
558 struct vector *const *pa = a_;
559 struct vector *const *pb = b_;
560 struct vector *a = *pa;
561 struct vector *b = *pb;
563 return strcasecmp (a->name, b->name);
566 /* Display a list of vectors. If SORTED is nonzero then they are
567 sorted alphabetically. */
569 display_vectors (int sorted)
571 const struct vector **vl;
576 nvec = dict_get_vector_cnt (default_dict);
579 msg (SW, _("No vectors defined."));
583 vl = xnmalloc (nvec, sizeof *vl);
584 for (i = 0; i < nvec; i++)
585 vl[i] = dict_get_vector (default_dict, i);
587 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
589 t = tab_create (1, nvec + 1, 0);
590 tab_headers (t, 0, 0, 1, 0);
591 tab_columns (t, TAB_COL_DOWN, 1);
592 tab_dim (t, tab_natural_dimensions);
593 tab_hline (t, TAL_1, 0, 0, 1);
594 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
595 tab_flags (t, SOMF_NO_TITLE);
596 for (i = 0; i < nvec; i++)
597 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);