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
27 #include "dictionary.h"
29 #include "file-handle-def.h"
35 #include "sys-file-reader.h"
38 #include "value-labels.h"
42 #define _(msgid) gettext (msgid)
44 /* Constants for DISPLAY utility. */
56 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
58 /* Sets the widths of all the columns and heights of all the rows in
59 table T for driver D. */
61 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
63 static const int max[] = {20, 5, 35, 3, 0};
67 for (p = max; *p; p++)
68 t->w[p - max] = min (tab_natural_width (t, d, p - max),
69 *p * d->prop_em_width);
70 for (i = 0; i < t->nr; i++)
71 t->h[i] = tab_natural_height (t, d, i);
74 /* SYSFILE INFO utility. */
76 cmd_sysfile_info (void)
78 struct file_handle *h;
81 struct sfm_reader *reader;
82 struct sfm_read_info info;
86 lex_match_id ("FILE");
89 h = fh_parse (FH_REF_FILE);
93 reader = sfm_open_reader (h, &d, &info);
96 sfm_close_reader (reader);
98 t = tab_create (2, 9, 0);
99 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
100 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
101 tab_text (t, 1, 0, TAB_LEFT, fh_get_filename (h));
102 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
104 const char *label = dict_get_label (d);
106 label = _("No label.");
107 tab_text (t, 1, 1, TAB_LEFT, label);
109 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
110 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
111 info.creation_date, info.creation_time, info.product);
112 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
113 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
114 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
115 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
116 dict_get_var_cnt (d));
117 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
118 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
119 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
120 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
121 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
122 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
124 struct variable *weight_var = dict_get_weight (d);
125 tab_text (t, 1, 7, TAB_LEFT,
126 weight_var != NULL ? weight_var->name : _("Not weighted."));
128 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
129 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
130 _("Compression %s."), info.compressed ? _("on") : _("off"));
131 tab_dim (t, tab_natural_dimensions);
134 nr = 1 + 2 * dict_get_var_cnt (d);
136 t = tab_create (4, nr, 1);
137 tab_dim (t, sysfile_info_dim);
138 tab_headers (t, 0, 0, 1, 0);
139 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
140 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
141 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
142 tab_hline (t, TAL_2, 0, 3, 1);
143 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
145 struct variable *v = dict_get_var (d, i);
146 const int nvl = val_labs_count (v->val_labs);
148 if (r + 10 + nvl > nr)
150 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
152 tab_realloc (t, 4, nr);
155 r = describe_variable (v, t, r, AS_DICTIONARY);
158 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
159 tab_vline (t, TAL_1, 1, 0, r);
160 tab_vline (t, TAL_1, 3, 0, r);
162 tab_resize (t, -1, r);
163 tab_flags (t, SOMF_NO_TITLE);
168 return lex_end_of_command ();
171 /* DISPLAY utility. */
173 static void display_macros (void);
174 static void display_documents (void);
175 static void display_variables (struct variable **, size_t, int);
176 static void display_vectors (int sorted);
181 /* Whether to sort the list of variables alphabetically. */
184 /* Variables to display. */
186 struct variable **vl;
188 if (lex_match_id ("MACROS"))
190 else if (lex_match_id ("DOCUMENTS"))
191 display_documents ();
192 else if (lex_match_id ("FILE"))
195 if (!lex_force_match_id ("LABEL"))
197 if (dict_get_label (default_dict) == NULL)
198 tab_output_text (TAB_LEFT,
199 _("The active file does not have a file label."));
202 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
203 tab_output_text (TAB_LEFT | TAT_FIX, dict_get_label (default_dict));
208 static const char *sbc[] =
209 {"NAMES", "INDEX", "VARIABLES", "LABELS",
210 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
214 sorted = lex_match_id ("SORTED");
216 for (cp = sbc; *cp; cp++)
217 if (token == T_ID && lex_id_match (*cp, tokid))
229 display_vectors (sorted);
234 lex_match_id ("VARIABLES");
239 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
247 dict_get_vars (default_dict, &vl, &n, 0);
249 if (as == AS_SCRATCH)
252 for (i = 0, m = n; i < n; i++)
253 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
264 msg (SW, _("No variables to display."));
269 sort (vl, n, sizeof *vl, compare_var_names, NULL);
271 display_variables (vl, n, as);
276 return lex_end_of_command ();
280 display_macros (void)
283 tab_output_text (TAB_LEFT, _("Macros not supported."));
287 display_documents (void)
289 const char *documents = dict_get_documents (default_dict);
292 if (documents == NULL)
293 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
294 "contain any documents."));
297 size_t n_lines = strlen (documents) / 80;
301 tab_output_text (TAB_LEFT | TAT_TITLE,
302 _("Documents in the active file:"));
305 for (i = 0; i < n_lines; i++)
309 memcpy (buf, &documents[i * 80], 80);
310 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
314 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
321 /* Sets the widths of all the columns and heights of all the rows in
322 table T for driver D. */
324 variables_dim (struct tab_table *t, struct outp_driver *d)
329 t->w[0] = tab_natural_width (t, d, 0);
330 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
332 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
333 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
338 t->w[pc] = tab_natural_width (t, d, pc);
340 for (i = 0; i < t->nr; i++)
341 t->h[i] = tab_natural_height (t, d, i);
345 display_variables (struct variable **vl, size_t n, int as)
347 struct variable **vp = vl; /* Variable pointer. */
349 int nc; /* Number of columns. */
350 int nr; /* Number of rows. */
351 int pc; /* `Position column' */
352 int r; /* Current row. */
369 t = tab_create (nc, n + 5, 1);
370 tab_headers (t, 0, 0, 1, 0);
372 tab_hline (t, TAL_2, 0, nc - 1, 1);
373 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
374 pc = (as == AS_INDEX ? 1 : 3);
376 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
377 if (as == AS_DICTIONARY || as == AS_VARIABLES)
378 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
379 else if (as == AS_LABELS)
380 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
381 tab_dim (t, variables_dim);
383 for (i = r = 1; i <= n; i++)
391 if (as == AS_DICTIONARY || as == AS_VARIABLES)
393 int nvl = val_labs_count (v->val_labs);
395 if (r + 10 + nvl > nr)
397 nr = max (nr * n / (i + 1), nr);
399 tab_realloc (t, nc, nr);
402 r = describe_variable (v, t, r, as);
404 tab_text (t, 0, r, TAB_LEFT, v->name);
406 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
407 v->label == NULL ? "(no label)" : v->label);
410 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
411 tab_hline (t, TAL_1, 0, nc - 1, r);
416 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
419 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
420 tab_vline (t, TAL_1, 1, 0, r - 1);
423 tab_flags (t, SOMF_NO_TITLE);
424 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
425 tab_vline (t, TAL_1, 3, 0, r - 1);
426 tab_resize (t, -1, r);
427 tab_columns (t, TAB_COL_DOWN, 1);
431 /* Puts a description of variable V into table T starting at row R.
432 The variable will be described in the format AS. Returns the next
433 row available for use in the table. */
435 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
437 /* Put the name, var label, and position into the first row. */
438 tab_text (t, 0, r, TAB_LEFT, v->name);
439 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
441 if (as == AS_DICTIONARY && v->label)
443 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
447 /* Print/write format, or print and write formats. */
448 if (v->print.type == v->write.type
449 && v->print.w == v->write.w
450 && v->print.d == v->write.d)
452 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
453 fmt_to_string (&v->print));
458 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
459 _("Print Format: %s"), fmt_to_string (&v->print));
461 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
462 _("Write Format: %s"), fmt_to_string (&v->write));
466 /* Missing values if any. */
467 if (!mv_is_empty (&v->miss))
471 struct missing_values mv;
474 cp = stpcpy (buf, _("Missing Values: "));
475 mv_copy (&mv, &v->miss);
476 if (mv_has_range (&mv))
479 mv_pop_range (&mv, &x, &y);
481 cp += nsprintf (cp, "LOWEST THRU %g", y);
482 else if (y == HIGHEST)
483 cp += nsprintf (cp, "%g THRU HIGHEST", x);
485 cp += nsprintf (cp, "%g THRU %g", x, y);
488 while (mv_has_value (&mv))
491 mv_pop_value (&mv, &value);
493 cp += nsprintf (cp, "; ");
494 if (v->type == NUMERIC)
495 cp += nsprintf (cp, "%g", value.f);
499 memcpy (cp, value.s, v->width);
506 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
511 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
513 struct val_labs_iterator *i;
518 tab_text (t, 1, r, TAB_LEFT, _("Value"));
519 tab_text (t, 2, r, TAB_LEFT, _("Label"));
523 tab_hline (t, TAL_1, 1, 2, r);
524 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
525 vl = val_labs_next (v->val_labs, &i))
529 if (v->type == ALPHA)
531 memcpy (buf, vl->value.s, v->width);
535 sprintf (buf, "%g", vl->value.f);
537 tab_text (t, 1, r, TAB_NONE, buf);
538 tab_text (t, 2, r, TAB_LEFT, vl->label);
542 tab_vline (t, TAL_1, 2, orig_r, r - 1);
545 /* Draw a line below the last row of information on this variable. */
546 tab_hline (t, TAL_1, 0, 3, r);
552 compare_vectors_by_name (const void *a_, const void *b_)
554 struct vector *const *pa = a_;
555 struct vector *const *pb = b_;
556 struct vector *a = *pa;
557 struct vector *b = *pb;
559 return strcasecmp (a->name, b->name);
562 /* Display a list of vectors. If SORTED is nonzero then they are
563 sorted alphabetically. */
565 display_vectors (int sorted)
567 const struct vector **vl;
572 nvec = dict_get_vector_cnt (default_dict);
575 msg (SW, _("No vectors defined."));
579 vl = xnmalloc (nvec, sizeof *vl);
580 for (i = 0; i < nvec; i++)
581 vl[i] = dict_get_vector (default_dict, i);
583 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
585 t = tab_create (1, nvec + 1, 0);
586 tab_headers (t, 0, 0, 1, 0);
587 tab_columns (t, TAB_COL_DOWN, 1);
588 tab_dim (t, tab_natural_dimensions);
589 tab_hline (t, TAL_1, 0, 0, 1);
590 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
591 tab_flags (t, SOMF_NO_TITLE);
592 for (i = 0; i < nvec; i++)
593 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);