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
24 #include "algorithm.h"
27 #include "dictionary.h"
29 #include "file-handle.h"
37 #include "value-labels.h"
41 #define _(msgid) gettext (msgid)
43 /* Constants for DISPLAY utility. */
55 static int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
57 /* Sets the widths of all the columns and heights of all the rows in
58 table T for driver D. */
60 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
62 static const int max[] = {20, 5, 35, 3, 0};
66 for (p = max; *p; p++)
67 t->w[p - max] = min (tab_natural_width (t, d, p - max),
68 *p * d->prop_em_width);
69 for (i = 0; i < t->nr; i++)
70 t->h[i] = tab_natural_height (t, d, i);
73 /* SYSFILE INFO utility. */
75 cmd_sysfile_info (void)
77 struct file_handle *h;
80 struct sfm_reader *reader;
81 struct sfm_read_info info;
85 lex_match_id ("FILE");
92 reader = sfm_open_reader (h, &d, &info);
95 sfm_close_reader (reader);
97 t = tab_create (2, 9, 0);
98 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
99 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
100 tab_text (t, 1, 0, TAB_LEFT, handle_get_filename (h));
101 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
103 const char *label = dict_get_label (d);
105 label = _("No label.");
106 tab_text (t, 1, 1, TAB_LEFT, label);
108 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
109 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
110 info.creation_date, info.creation_time, info.product);
111 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
112 tab_text (t, 1, 3, TAB_LEFT, info.big_endian ? _("Big.") : _("Little."));
113 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
114 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
115 dict_get_var_cnt (d));
116 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
117 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
118 info.case_cnt == -1 ? _("Unknown") : "%d", info.case_cnt);
119 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
120 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
121 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
123 struct variable *weight_var = dict_get_weight (d);
124 tab_text (t, 1, 7, TAB_LEFT,
125 weight_var != NULL ? weight_var->name : _("Not weighted."));
127 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
128 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
129 _("Compression %s."), info.compressed ? _("on") : _("off"));
130 tab_dim (t, tab_natural_dimensions);
133 nr = 1 + 2 * dict_get_var_cnt (d);
135 t = tab_create (4, nr, 1);
136 tab_dim (t, sysfile_info_dim);
137 tab_headers (t, 0, 0, 1, 0);
138 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
139 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
140 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
141 tab_hline (t, TAL_2, 0, 3, 1);
142 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
144 struct variable *v = dict_get_var (d, i);
145 const int nvl = val_labs_count (v->val_labs);
147 if (r + 10 + nvl > nr)
149 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
151 tab_realloc (t, 4, nr);
154 r = describe_variable (v, t, r, AS_DICTIONARY);
157 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
158 tab_vline (t, TAL_1, 1, 0, r);
159 tab_vline (t, TAL_1, 3, 0, r);
161 tab_resize (t, -1, r);
162 tab_flags (t, SOMF_NO_TITLE);
167 return lex_end_of_command ();
170 /* DISPLAY utility. */
172 static void display_macros (void);
173 static void display_documents (void);
174 static void display_variables (struct variable **, int, int);
175 static void display_vectors (int sorted);
180 /* Whether to sort the list of variables alphabetically. */
183 /* Variables to display. */
185 struct variable **vl;
187 if (lex_match_id ("MACROS"))
189 else if (lex_match_id ("DOCUMENTS"))
190 display_documents ();
191 else if (lex_match_id ("FILE"))
194 if (!lex_force_match_id ("LABEL"))
196 if (dict_get_label (default_dict) == NULL)
197 tab_output_text (TAB_LEFT,
198 _("The active file does not have a file label."));
201 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
202 tab_output_text (TAB_LEFT | TAT_FIX, dict_get_label (default_dict));
207 static const char *sbc[] =
208 {"NAMES", "INDEX", "VARIABLES", "LABELS",
209 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
213 sorted = lex_match_id ("SORTED");
215 for (cp = sbc; *cp; cp++)
216 if (token == T_ID && lex_id_match (*cp, tokid))
228 display_vectors (sorted);
233 lex_match_id ("VARIABLES");
238 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
246 dict_get_vars (default_dict, &vl, &n, 0);
248 if (as == AS_SCRATCH)
251 for (i = 0, m = n; i < n; i++)
252 if (dict_class_from_id (vl[i]->name) != DC_SCRATCH)
263 msg (SW, _("No variables to display."));
268 sort (vl, n, sizeof *vl, compare_var_names, NULL);
270 display_variables (vl, n, as);
275 return lex_end_of_command ();
279 display_macros (void)
282 tab_output_text (TAB_LEFT, _("Macros not supported."));
286 display_documents (void)
288 const char *documents = dict_get_documents (default_dict);
291 if (documents == NULL)
292 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
293 "contain any documents."));
296 size_t n_lines = strlen (documents) / 80;
300 tab_output_text (TAB_LEFT | TAT_TITLE,
301 _("Documents in the active file:"));
304 for (i = 0; i < n_lines; i++)
308 memcpy (buf, &documents[i * 80], 80);
309 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
313 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
320 /* Sets the widths of all the columns and heights of all the rows in
321 table T for driver D. */
323 variables_dim (struct tab_table *t, struct outp_driver *d)
328 t->w[0] = tab_natural_width (t, d, 0);
329 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
331 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
332 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
337 t->w[pc] = tab_natural_width (t, d, pc);
339 for (i = 0; i < t->nr; i++)
340 t->h[i] = tab_natural_height (t, d, i);
344 display_variables (struct variable **vl, int n, int as)
346 struct variable **vp = vl; /* Variable pointer. */
348 int nc; /* Number of columns. */
349 int nr; /* Number of rows. */
350 int pc; /* `Position column' */
351 int r; /* Current row. */
368 t = tab_create (nc, n + 5, 1);
369 tab_headers (t, 0, 0, 1, 0);
371 tab_hline (t, TAL_2, 0, nc - 1, 1);
372 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
373 pc = (as == AS_INDEX ? 1 : 3);
375 tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
376 if (as == AS_DICTIONARY || as == AS_VARIABLES)
377 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
378 else if (as == AS_LABELS)
379 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
380 tab_dim (t, variables_dim);
382 for (i = r = 1; i <= n; i++)
390 if (as == AS_DICTIONARY || as == AS_VARIABLES)
392 int nvl = val_labs_count (v->val_labs);
394 if (r + 10 + nvl > nr)
396 nr = max (nr * n / (i + 1), nr);
398 tab_realloc (t, nc, nr);
401 r = describe_variable (v, t, r, as);
403 tab_text (t, 0, r, TAB_LEFT, v->name);
405 tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
406 v->label == NULL ? "(no label)" : v->label);
409 tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
410 tab_hline (t, TAL_1, 0, nc - 1, r);
415 tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
418 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
419 tab_vline (t, TAL_1, 1, 0, r - 1);
422 tab_flags (t, SOMF_NO_TITLE);
423 if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
424 tab_vline (t, TAL_1, 3, 0, r - 1);
425 tab_resize (t, -1, r);
426 tab_columns (t, TAB_COL_DOWN, 1);
430 /* Puts a description of variable V into table T starting at row R.
431 The variable will be described in the format AS. Returns the next
432 row available for use in the table. */
434 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
436 /* Put the name, var label, and position into the first row. */
437 tab_text (t, 0, r, TAB_LEFT, v->name);
438 tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
440 if (as == AS_DICTIONARY && v->label)
442 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, v->label);
446 /* Print/write format, or print and write formats. */
447 if (v->print.type == v->write.type
448 && v->print.w == v->write.w
449 && v->print.d == v->write.d)
451 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
452 fmt_to_string (&v->print));
457 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
458 _("Print Format: %s"), fmt_to_string (&v->print));
460 tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
461 _("Write Format: %s"), fmt_to_string (&v->write));
465 /* Missing values if any. */
466 if (!mv_is_empty (&v->miss))
470 struct missing_values mv;
473 cp = stpcpy (buf, _("Missing Values: "));
474 mv_copy (&mv, &v->miss);
475 if (mv_has_range (&mv))
478 mv_pop_range (&mv, &x, &y);
480 cp += nsprintf (cp, "LOWEST THRU %g", y);
481 else if (y == HIGHEST)
482 cp += nsprintf (cp, "%g THRU HIGHEST", x);
484 cp += nsprintf (cp, "%g THRU %g", x, y);
487 while (mv_has_value (&mv))
490 mv_pop_value (&mv, &value);
492 cp += nsprintf (cp, "; ");
493 if (v->type == NUMERIC)
494 cp += nsprintf (cp, "%g", value.f);
498 memcpy (cp, value.s, v->width);
505 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
510 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
512 struct val_labs_iterator *i;
517 tab_text (t, 1, r, TAB_LEFT, _("Value"));
518 tab_text (t, 2, r, TAB_LEFT, _("Label"));
522 tab_hline (t, TAL_1, 1, 2, r);
523 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
524 vl = val_labs_next (v->val_labs, &i))
528 if (v->type == ALPHA)
530 memcpy (buf, vl->value.s, v->width);
534 sprintf (buf, "%g", vl->value.f);
536 tab_text (t, 1, r, TAB_NONE, buf);
537 tab_text (t, 2, r, TAB_LEFT, vl->label);
541 tab_vline (t, TAL_1, 2, orig_r, r - 1);
544 /* Draw a line below the last row of information on this variable. */
545 tab_hline (t, TAL_1, 0, 3, r);
551 compare_vectors_by_name (const void *a_, const void *b_)
553 struct vector *const *pa = a_;
554 struct vector *const *pb = b_;
555 struct vector *a = *pa;
556 struct vector *b = *pb;
558 return strcasecmp (a->name, b->name);
561 /* Display a list of vectors. If SORTED is nonzero then they are
562 sorted alphabetically. */
564 display_vectors (int sorted)
566 const struct vector **vl;
571 nvec = dict_get_vector_cnt (default_dict);
574 msg (SW, _("No vectors defined."));
578 vl = xmalloc (sizeof *vl * nvec);
579 for (i = 0; i < nvec; i++)
580 vl[i] = dict_get_vector (default_dict, i);
582 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
584 t = tab_create (1, nvec + 1, 0);
585 tab_headers (t, 0, 0, 1, 0);
586 tab_columns (t, TAB_COL_DOWN, 1);
587 tab_dim (t, tab_natural_dimensions);
588 tab_hline (t, TAL_1, 0, 0, 1);
589 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
590 tab_flags (t, SOMF_NO_TITLE);
591 for (i = 0; i < nvec; i++)
592 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);