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., 59 Temple Place - Suite 330, Boston, MA
24 #include "algorithm.h"
28 #include "file-handle.h"
36 #include "value-labels.h"
39 /* Constants for DISPLAY utility. */
51 int describe_variable (struct variable *v, struct tab_table *t, int r, int as);
53 /* Sets the widths of all the columns and heights of all the rows in
54 table T for driver D. */
56 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
58 static const int max[] = {20, 5, 35, 3, 0};
62 for (p = max; *p; p++)
63 t->w[p - max] = min (tab_natural_width (t, d, p - max),
64 *p * d->prop_em_width);
65 for (i = 0; i < t->nr; i++)
66 t->h[i] = tab_natural_height (t, d, i);
69 /* SYSFILE INFO utility. */
71 cmd_sysfile_info (void)
73 struct file_handle *h;
76 struct sfm_read_info inf;
80 lex_match_id ("SYSFILE");
81 lex_match_id ("INFO");
83 lex_match_id ("FILE");
86 h = fh_parse_file_handle ();
90 d = sfm_read_dictionary (h, &inf);
95 t = tab_create (2, 9, 0);
96 tab_vline (t, TAL_1 | TAL_SPACING, 1, 0, 8);
97 tab_text (t, 0, 0, TAB_LEFT, _("File:"));
98 tab_text (t, 1, 0, TAB_LEFT, fh_handle_filename (h));
99 tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
101 const char *label = dict_get_label (d);
103 label = _("No label.");
104 tab_text (t, 1, 1, TAB_LEFT, label);
106 tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
107 tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
108 inf.creation_date, inf.creation_time, inf.product);
109 tab_text (t, 0, 3, TAB_LEFT, _("Endian:"));
110 tab_text (t, 1, 3, TAB_LEFT, inf.bigendian ? _("Big.") : _("Little."));
111 tab_text (t, 0, 4, TAB_LEFT, _("Variables:"));
112 tab_text (t, 1, 4, TAB_LEFT | TAT_PRINTF, "%d",
113 dict_get_var_cnt (d));
114 tab_text (t, 0, 5, TAB_LEFT, _("Cases:"));
115 tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF,
116 inf.ncases == -1 ? _("Unknown") : "%d", inf.ncases);
117 tab_text (t, 0, 6, TAB_LEFT, _("Type:"));
118 tab_text (t, 1, 6, TAB_LEFT, _("System File."));
119 tab_text (t, 0, 7, TAB_LEFT, _("Weight:"));
121 struct variable *weight_var = dict_get_weight (d);
122 tab_text (t, 1, 7, TAB_LEFT,
123 weight_var != NULL ? weight_var->name : _("Not weighted."));
125 tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
126 tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
127 _("Compression %s."), inf.compressed ? _("on") : _("off"));
128 tab_dim (t, tab_natural_dimensions);
131 nr = 1 + 2 * dict_get_var_cnt (d);
132 t = tab_create (4, nr, 1);
133 tab_dim (t, sysfile_info_dim);
134 tab_headers (t, 0, 0, 1, 0);
135 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
136 tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
137 tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
138 tab_hline (t, TAL_2, 0, 3, 1);
139 for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
141 struct variable *v = dict_get_var (d, i);
142 int nvl = val_labs_count (v->val_labs);
144 if (r + 10 + nvl > nr)
146 nr = max (nr * dict_get_var_cnt (d) / (i + 1), nr);
148 tab_realloc (t, 4, nr);
151 r = describe_variable (v, t, r, AS_DICTIONARY);
153 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
154 tab_vline (t, TAL_1, 0, 0, r);
155 tab_vline (t, TAL_1, 1, 0, r);
156 tab_vline (t, TAL_1, 3, 0, r);
157 tab_resize (t, -1, r);
158 tab_flags (t, SOMF_NO_TITLE);
163 return lex_end_of_command ();
166 /* DISPLAY utility. */
168 static void display_macros (void);
169 static void display_documents (void);
170 static void display_variables (struct variable **, int, int);
171 static void display_vectors (int sorted);
176 /* Whether to sort the list of variables alphabetically. */
179 /* Variables to display. */
181 struct variable **vl;
183 lex_match_id ("DISPLAY");
185 if (lex_match_id ("MACROS"))
187 else if (lex_match_id ("DOCUMENTS"))
188 display_documents ();
189 else if (lex_match_id ("FILE"))
192 if (!lex_force_match_id ("LABEL"))
194 if (dict_get_label (default_dict) == NULL)
195 tab_output_text (TAB_LEFT,
196 _("The active file does not have a file label."));
199 tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
200 tab_output_text (TAB_LEFT | TAT_FIX, dict_get_label (default_dict));
205 static const char *sbc[] =
206 {"NAMES", "INDEX", "VARIABLES", "LABELS",
207 "DICTIONARY", "SCRATCH", "VECTORS", NULL};
211 sorted = lex_match_id ("SORTED");
213 for (cp = sbc; *cp; cp++)
214 if (token == T_ID && lex_id_match (*cp, tokid))
226 display_vectors (sorted);
231 lex_match_id ("VARIABLES");
236 if (!parse_variables (default_dict, &vl, &n, PV_NONE))
244 dict_get_vars (default_dict, &vl, &n, 0);
246 if (as == AS_SCRATCH)
249 for (i = 0, m = n; i < n; i++)
250 if (vl[i]->name[0] != '#')
261 msg (SW, _("No variables to display."));
266 sort (vl, n, sizeof *vl, compare_variables, NULL);
268 display_variables (vl, n, as);
273 return lex_end_of_command ();
277 display_macros (void)
280 tab_output_text (TAB_LEFT, _("Macros not supported."));
284 display_documents (void)
286 const char *documents = dict_get_documents (default_dict);
289 if (documents == NULL)
290 tab_output_text (TAB_LEFT, _("The active file dictionary does not "
291 "contain any documents."));
294 size_t n_lines = strlen (documents) / 80;
298 tab_output_text (TAB_LEFT | TAT_TITLE,
299 _("Documents in the active file:"));
302 for (i = 0; i < n_lines; i++)
306 memcpy (buf, &documents[i * 80], 80);
307 while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
311 tab_output_text (TAB_LEFT | TAT_FIX | TAT_NOWRAP, buf);
318 /* Sets the widths of all the columns and heights of all the rows in
319 table T for driver D. */
321 variables_dim (struct tab_table *t, struct outp_driver *d)
326 t->w[0] = tab_natural_width (t, d, 0);
327 if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
329 t->w[1] = max (tab_natural_width (t, d, 1), d->prop_em_width * 5);
330 t->w[2] = max (tab_natural_width (t, d, 2), d->prop_em_width * 35);
335 t->w[pc] = tab_natural_width (t, d, pc);
337 for (i = 0; i < t->nr; i++)
338 t->h[i] = tab_natural_height (t, d, i);
342 display_variables (struct variable **vl, int n, int as)
344 struct variable **vp = vl; /* Variable pointer. */
346 int nc; /* Number of columns. */
347 int nr; /* Number of rows. */
348 int pc; /* `Position column' */
349 int r; /* Current row. */
366 t = tab_create (nc, n + 5, 1);
367 tab_headers (t, 0, 0, 1, 0);
369 tab_hline (t, TAL_2, 0, nc - 1, 1);
370 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
373 pc = (as == AS_INDEX ? 1 : 3);
374 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 (v->miss_type != MISSING_NONE)
469 char *cp = stpcpy (buf, _("Missing Values: "));
471 if (v->type == NUMERIC)
472 switch (v->miss_type)
475 sprintf (cp, "%g", v->missing[0].f);
478 sprintf (cp, "%g; %g", v->missing[0].f, v->missing[1].f);
481 sprintf (cp, "%g; %g; %g", v->missing[0].f,
482 v->missing[1].f, v->missing[2].f);
485 sprintf (cp, "%g THRU %g", v->missing[0].f, v->missing[1].f);
488 sprintf (cp, "LOWEST THRU %g", v->missing[0].f);
491 sprintf (cp, "%g THRU HIGHEST", v->missing[0].f);
493 case MISSING_RANGE_1:
494 sprintf (cp, "%g THRU %g; %g",
495 v->missing[0].f, v->missing[1].f, v->missing[2].f);
498 sprintf (cp, "LOWEST THRU %g; %g",
499 v->missing[0].f, v->missing[1].f);
502 sprintf (cp, "%g THRU HIGHEST; %g",
503 v->missing[0].f, v->missing[1].f);
512 for (i = 0; i < v->miss_type; i++)
515 cp = stpcpy (cp, "; ");
517 memcpy (cp, v->missing[i].s, v->width);
524 tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
529 if (as == AS_DICTIONARY && val_labs_count (v->val_labs))
531 struct val_labs_iterator *i;
536 tab_text (t, 1, r, TAB_LEFT, _("Value"));
537 tab_text (t, 2, r, TAB_LEFT, _("Label"));
541 tab_hline (t, TAL_1, 1, 2, r);
542 for (vl = val_labs_first_sorted (v->val_labs, &i); vl != NULL;
543 vl = val_labs_next (v->val_labs, &i))
547 if (v->type == ALPHA)
549 memcpy (buf, vl->value.s, v->width);
553 sprintf (buf, "%g", vl->value.f);
555 tab_text (t, 1, r, TAB_NONE, buf);
556 tab_text (t, 2, r, TAB_LEFT, vl->label);
560 tab_vline (t, TAL_1, 2, orig_r, r - 1);
563 /* Draw a line below the last row of information on this variable. */
564 tab_hline (t, TAL_1, 0, 3, r);
570 compare_vectors_by_name (const void *a_, const void *b_)
572 struct vector *const *pa = a_;
573 struct vector *const *pb = b_;
574 struct vector *a = *pa;
575 struct vector *b = *pb;
577 return strcmp (a->name, b->name);
580 /* Display a list of vectors. If SORTED is nonzero then they are
581 sorted alphabetically. */
583 display_vectors (int sorted)
585 const struct vector **vl;
590 nvec = dict_get_vector_cnt (default_dict);
593 msg (SW, _("No vectors defined."));
597 vl = xmalloc (sizeof *vl * nvec);
598 for (i = 0; i < nvec; i++)
599 vl[i] = dict_get_vector (default_dict, i);
601 qsort (vl, nvec, sizeof *vl, compare_vectors_by_name);
603 t = tab_create (1, nvec + 1, 0);
604 tab_headers (t, 0, 0, 1, 0);
605 tab_columns (t, TAB_COL_DOWN, 1);
606 tab_dim (t, tab_natural_dimensions);
607 tab_hline (t, TAL_1, 0, 0, 1);
608 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
609 tab_flags (t, SOMF_NO_TITLE);
610 for (i = 0; i < nvec; i++)
611 tab_text (t, 0, i + 1, TAB_LEFT, vl[i]->name);