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
21 #include <libpspp/message.h>
24 #include <libpspp/array.h>
25 #include <libpspp/alloc.h>
26 #include <language/command.h>
27 #include <data/dictionary.h>
28 #include <libpspp/message.h>
29 #include <data/file-handle-def.h>
30 #include <language/data-io/file-handle.h>
31 #include <libpspp/hash.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/magic.h>
34 #include <libpspp/misc.h>
35 #include <output/output.h>
36 #include <data/sys-file-reader.h>
37 #include <output/manager.h>
38 #include <output/table.h>
39 #include <data/value-labels.h>
40 #include <data/variable.h>
43 #define _(msgid) gettext (msgid)
45 /* Constants for DISPLAY utility. */
57 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
59 /* Sets the widths of all the columns and heights of all the rows in
60 table T for driver D. */
62 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
64 static const int max[] = {20, 5, 35, 3, 0};
68 for (p = max; *p; p++)
69 t->w[p - max] = min (tab_natural_width (t, d, p - max),
70 *p * d->prop_em_width);
71 for (i = 0; i < t->nr; i++)
72 t->h[i] = tab_natural_height (t, d, i);
75 /* SYSFILE INFO utility. */
77 cmd_sysfile_info (void)
79 struct file_handle *h;
82 struct sfm_reader *reader;
83 struct sfm_read_info info;
87 lex_match_id ("FILE");
90 h = fh_parse (FH_REF_FILE);
94 reader = sfm_open_reader (h, &d, &info);
97 sfm_close_reader (reader);
99 t = tab_create (2, 9, 0);
100 tab_vline (t, TAL_GAP, 1, 0, 8);
101 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
102 tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
103 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
105 const char *label = dict_get_label (d);
107 label = _("No label.");
108 tab_text (t, 1, 1, TAB_LEFT, label);
110 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
111 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
112 info.creation_date, info.creation_time, info.product);
113 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
114 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
115 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
116 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
117 dict_get_var_cnt (d));
118 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
119 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
120 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
121 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
122 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
123 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
125 struct variable *weight_var = dict_get_weight (d);
126 tab_text (t, 1, 7, TAB_LEFT,
127 weight_var != NULL ? weight_var->name : _("Not weighted."));
129 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
130 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
131 _("Compression %s."), info.compressed ? _("on") : _("off"));
132 tab_dim (t, tab_natural_dimensions);
135 nr = 1 + 2 * dict_get_var_cnt (d);
137 t = tab_create (4, nr, 1);
138 tab_dim (t, sysfile_info_dim);
139 tab_headers (t, 0, 0, 1, 0);
140 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
141 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
142 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
143 tab_hline (t, TAL_2, 0, 3, 1);
144 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
146 struct variable *v = dict_get_var (d, i);
147 const int nvl = val_labs_count (v->val_labs);
149 if (r + 10 + nvl > nr)
151 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
153 tab_realloc (t, 4, nr);
156 r = describe_variable (v, t, r, AS_DICTIONARY);
159 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
160 tab_vline (t, TAL_1, 1, 0, r);
161 tab_vline (t, TAL_1, 3, 0, r);
163 tab_resize (t, -1, r);
164 tab_flags (t, SOMF_NO_TITLE);
169 return lex_end_of_command ();
172 /* DISPLAY utility. */
174 static void display_macros (void);
175 static void display_documents (void);
176 static void display_variables (struct variable **, size_t, int);
177 static void display_vectors (int sorted);
182 /* Whether to sort the list of variables alphabetically. */
185 /* Variables to display. */
187 struct variable **vl;
189 if (lex_match_id ("MACROS"))
191 else if (lex_match_id ("DOCUMENTS"))
192 display_documents ();
193 else if (lex_match_id ("FILE"))
196 if (!lex_force_match_id ("LABEL"))
198 if (dict_get_label (default_dict) == NULL)
199 tab_output_text (TAB_LEFT,
200 _("The active file does not have a file label."));
203 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
204 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (default_dict));
209 static const char *sbc[] =
210 {"NAMES", "INDEX", "VARIABLES", "LABELS",
211 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
215 sorted = lex_match_id ("SORTED");
217 for (cp = sbc; *cp; cp++)
218 if (token == T_ID && lex_id_match (*cp, tokid))
230 display_vectors (sorted);
235 lex_match_id ("VARIABLES");
240 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
248 dict_get_vars (default_dict, &vl, &n, 0);
250 if (as == AS_SCRATCH)
253 for (i = 0, m = n; i < n; i++)
254 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
265 msg (SW, _("No variables to display."));
270 sort (vl, n, sizeof *vl, compare_var_names, NULL);
272 display_variables (vl, n, as);
277 return lex_end_of_command ();
281 display_macros (void)
284 tab_output_text (TAB_LEFT, _("Macros not supported."));
288 display_documents (void)
290 const char *documents = dict_get_documents (default_dict);
293 if (documents == NULL)
294 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
295 "contain any documents."));
298 size_t n_lines = strlen (documents) / 80;
302 tab_output_text (TAB_LEFT | TAT_TITLE,
303 _("Documents in the active file:"));
306 for (i = 0; i < n_lines; i++)
310 memcpy (buf, &documents[i * 80], 80);
311 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
315 tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
322 /* Sets the widths of all the columns and heights of all the rows in
323 table T for driver D. */
325 variables_dim (struct tab_table *t, struct outp_driver *d)
330 t->w[0] = tab_natural_width (t, d, 0);
331 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
333 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
334 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
339 t->w[pc] = tab_natural_width (t, d, pc);
341 for (i = 0; i < t->nr; i++)
342 t->h[i] = tab_natural_height (t, d, i);
346 display_variables (struct variable **vl, size_t n, int as)
348 struct variable **vp = vl; /* Variable pointer. */
350 int nc; /* Number of columns. */
351 int nr; /* Number of rows. */
352 int pc; /* `Position column' */
353 int r; /* Current row. */
370 t = tab_create (nc, n + 5, 1);
371 tab_headers (t, 0, 0, 1, 0);
373 tab_hline (t, TAL_2, 0, nc - 1, 1);
374 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
375 pc = (as == AS_INDEX ? 1 : 3);
377 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
378 if (as == AS_DICTIONARY || as == AS_VARIABLES)
379 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
380 else if (as == AS_LABELS)
381 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
382 tab_dim (t, variables_dim);
384 for (i = r = 1; i <= n; i++)
392 if (as == AS_DICTIONARY || as == AS_VARIABLES)
394 int nvl = val_labs_count (v->val_labs);
396 if (r + 10 + nvl > nr)
398 nr = max (nr * n / (i + 1), nr);
400 tab_realloc (t, nc, nr);
403 r = describe_variable (v, t, r, as);
405 tab_text (t, 0, r, TAB_LEFT, v->name);
407 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
408 v->label == NULL ? "(no label)" : v->label);
411 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
412 tab_hline (t, TAL_1, 0, nc - 1, r);
417 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
420 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
421 tab_vline (t, TAL_1, 1, 0, r - 1);
424 tab_flags (t, SOMF_NO_TITLE);
425 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
426 tab_vline (t, TAL_1, 3, 0, r - 1);
427 tab_resize (t, -1, r);
428 tab_columns (t, TAB_COL_DOWN, 1);
432 /* Puts a description of variable V into table T starting at row R.
433 The variable will be described in the format AS. Returns the next
434 row available for use in the table. */
436 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
438 /* Put the name, var label, and position into the first row. */
439 tab_text (t, 0, r, TAB_LEFT, v->name);
440 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
442 if (as == AS_DICTIONARY && v->label)
444 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
448 /* Print/write format, or print and write formats. */
449 if (v->print.type == v->write.type
450 && v->print.w == v->write.w
451 && v->print.d == v->write.d)
453 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
454 fmt_to_string (&v->print));
459 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
460 _("Print Format: %s"), fmt_to_string (&v->print));
462 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
463 _("Write Format: %s"), fmt_to_string (&v->write));
467 /* Missing values if any. */
468 if (!mv_is_empty (&v->miss))
472 struct missing_values mv;
475 cp = stpcpy (buf, _("Missing Values: "));
476 mv_copy (&mv, &v->miss);
477 if (mv_has_range (&mv))
480 mv_pop_range (&mv, &x, &y);
482 cp += nsprintf (cp, "LOWEST THRU %g", y);
483 else if (y == HIGHEST)
484 cp += nsprintf (cp, "%g THRU HIGHEST", x);
486 cp += nsprintf (cp, "%g THRU %g", x, y);
489 while (mv_has_value (&mv))
492 mv_pop_value (&mv, &value);
494 cp += nsprintf (cp, "; ");
495 if (v->type == NUMERIC)
496 cp += nsprintf (cp, "%g", value.f);
500 memcpy (cp, value.s, v->width);
507 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
512 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
514 struct val_labs_iterator *i;
519 tab_text (t, 1, r, TAB_LEFT, _("Value"));
520 tab_text (t, 2, r, TAB_LEFT, _("Label"));
524 tab_hline (t, TAL_1, 1, 2, r);
525 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
526 vl = val_labs_next (v->val_labs, &i))
530 if (v->type == ALPHA)
532 memcpy (buf, vl->value.s, v->width);
536 sprintf (buf, "%g", vl->value.f);
538 tab_text (t, 1, r, TAB_NONE, buf);
539 tab_text (t, 2, r, TAB_LEFT, vl->label);
543 tab_vline (t, TAL_1, 2, orig_r, r - 1);
546 /* Draw a line below the last row of information on this variable. */
547 tab_hline (t, TAL_1, 0, 3, r);
553 compare_vectors_by_name (const void *a_, const void *b_)
555 struct vector *const *pa = a_;
556 struct vector *const *pb = b_;
557 struct vector *a = *pa;
558 struct vector *b = *pb;
560 return strcasecmp (a->name, b->name);
563 /* Display a list of vectors. If SORTED is nonzero then they are
564 sorted alphabetically. */
566 display_vectors (int sorted)
568 const struct vector **vl;
573 nvec = dict_get_vector_cnt (default_dict);
576 msg (SW, _("No vectors defined."));
580 vl = xnmalloc (nvec, sizeof *vl);
581 for (i = 0; i < nvec; i++)
582 vl[i] = dict_get_vector (default_dict, i);
584 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
586 t = tab_create (1, nvec + 1, 0);
587 tab_headers (t, 0, 0, 1, 0);
588 tab_columns (t, TAB_COL_DOWN, 1);
589 tab_dim (t, tab_natural_dimensions);
590 tab_hline (t, TAL_1, 0, 0, 1);
591 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
592 tab_flags (t, SOMF_NO_TITLE);
593 for (i = 0; i < nvec; i++)
594 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);