Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / language / dictionary / sys-file-info.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <stdlib.h>
21
22 #include <data/casereader.h>
23 #include <data/dictionary.h>
24 #include <data/file-handle-def.h>
25 #include <data/format.h>
26 #include <data/missing-values.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 <data/vector.h>
32 #include <language/command.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/array.h>
37 #include <libpspp/hash.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>
44
45 #include "minmax.h"
46 #include "xalloc.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext (msgid)
50
51 /* Constants for DISPLAY utility. */
52 enum
53   {
54     AS_NAMES = 0,
55     AS_INDEX,
56     AS_VARIABLES,
57     AS_LABELS,
58     AS_DICTIONARY,
59     AS_SCRATCH,
60     AS_VECTOR
61   };
62
63 static int describe_variable (const struct variable *v, struct tab_table *t, int r, int as);
64
65 /* Sets the widths of all the columns and heights of all the rows in
66    table T for driver D. */
67 static void
68 sysfile_info_dim (struct tab_table *t, struct outp_driver *d)
69 {
70   static const int max[] = {20, 5, 35, 3, 0};
71   const int *p;
72   int i;
73
74   for (p = max; *p; p++)
75     t->w[p - max] = MIN (tab_natural_width (t, d, p - max),
76                          *p * d->prop_em_width);
77   for (i = 0; i < t->nr; i++)
78     t->h[i] = tab_natural_height (t, d, i);
79 }
80
81 /* SYSFILE INFO utility. */
82 int
83 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
84 {
85   struct file_handle *h;
86   struct dictionary *d;
87   struct tab_table *t;
88   struct casereader *reader;
89   struct sfm_read_info info;
90   int r, nr;
91   int i;
92
93   lex_match_id (lexer, "FILE");
94   lex_match (lexer, '=');
95
96   h = fh_parse (lexer, FH_REF_FILE);
97   if (!h)
98     return CMD_FAILURE;
99
100   reader = sfm_open_reader (h, &d, &info);
101   if (!reader)
102     {
103       fh_unref (h);
104       return CMD_FAILURE;
105     }
106   casereader_destroy (reader);
107
108   t = tab_create (2, 10, 0);
109   tab_vline (t, TAL_GAP, 1, 0, 8);
110   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
111   tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
112   tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
113   {
114     const char *label = dict_get_label (d);
115     if (label == NULL)
116       label = _("No label.");
117     tab_text (t, 1, 1, TAB_LEFT, label);
118   }
119   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
120   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
121                 info.creation_date, info.creation_time, info.product);
122   tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
123   tab_text (t, 1, 3, TAB_LEFT,
124             info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
125             : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
126             : _("Unknown."));
127   tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
128   tab_text (t, 1, 4, TAB_LEFT,
129             info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
130             : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
131             : info.float_format == FLOAT_VAX_D ? _("VAX D.")
132             : info.float_format == FLOAT_VAX_G ? _("VAX G.")
133             : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
134             : _("Unknown."));
135   tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
136   tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%zu", dict_get_var_cnt (d));
137   tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
138   tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
139             info.case_cnt == -1 ? _("Unknown") : "%ld",
140             (long int) info.case_cnt);
141   tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
142   tab_text (t, 1, 7, TAB_LEFT, _("System File."));
143   tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
144   {
145     struct variable *weight_var = dict_get_weight (d);
146     tab_text (t, 1, 8, TAB_LEFT,
147               (weight_var != NULL
148                ? var_get_name (weight_var) : _("Not weighted.")));
149   }
150   tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
151   tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
152                 _("Compression %s."), info.compressed ? _("on") : _("off"));
153   tab_dim (t, tab_natural_dimensions);
154   tab_submit (t);
155
156   nr = 1 + 2 * dict_get_var_cnt (d);
157
158   t = tab_create (4, nr, 1);
159   tab_dim (t, sysfile_info_dim);
160   tab_headers (t, 0, 0, 1, 0);
161   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
162   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
163   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
164   tab_hline (t, TAL_2, 0, 3, 1);
165   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
166     {
167       struct variable *v = dict_get_var (d, i);
168       const int nvl = val_labs_count (var_get_value_labels (v));
169
170       if (r + 13 + nvl > nr)
171         {
172           nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
173           nr += 10 + nvl;
174           tab_realloc (t, 4, nr);
175         }
176
177       r = describe_variable (v, t, r, AS_DICTIONARY);
178     }
179
180   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
181   tab_vline (t, TAL_1, 1, 0, r);
182   tab_vline (t, TAL_1, 3, 0, r);
183
184   tab_resize (t, -1, r);
185   tab_flags (t, SOMF_NO_TITLE);
186   tab_submit (t);
187
188   dict_destroy (d);
189
190   fh_unref (h);
191   return lex_end_of_command (lexer);
192 }
193 \f
194 /* DISPLAY utility. */
195
196 static void display_macros (void);
197 static void display_documents (const struct dictionary *dict);
198 static void display_variables (const struct variable **, size_t, int);
199 static void display_vectors (const struct dictionary *dict, int sorted);
200
201 int
202 cmd_display (struct lexer *lexer, struct dataset *ds)
203 {
204   /* Whether to sort the list of variables alphabetically. */
205   int sorted;
206
207   /* Variables to display. */
208   size_t n;
209   const struct variable **vl;
210
211   if (lex_match_id (lexer, "MACROS"))
212     display_macros ();
213   else if (lex_match_id (lexer, "DOCUMENTS"))
214     display_documents (dataset_dict (ds));
215   else if (lex_match_id (lexer, "FILE"))
216     {
217       som_blank_line ();
218       if (!lex_force_match_id (lexer, "LABEL"))
219         return CMD_FAILURE;
220       if (dict_get_label (dataset_dict (ds)) == NULL)
221         tab_output_text (TAB_LEFT,
222                          _("The active file does not have a file label."));
223       else
224         {
225           tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
226           tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
227         }
228     }
229   else
230     {
231       static const char *sbc[] =
232         {"NAMES", "INDEX", "VARIABLES", "LABELS",
233          "DICTIONARY", "SCRATCH", "VECTORS", NULL};
234       const char **cp;
235       int as;
236
237       sorted = lex_match_id (lexer, "SORTED");
238
239       for (cp = sbc; *cp; cp++)
240         if (lex_token (lexer) == T_ID
241             && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
242           {
243             lex_get (lexer);
244             break;
245           }
246       as = cp - sbc;
247
248       if (*cp == NULL)
249         as = AS_NAMES;
250
251       if (as == AS_VECTOR)
252         {
253           display_vectors (dataset_dict(ds), sorted);
254           return CMD_SUCCESS;
255         }
256
257       lex_match (lexer, '/');
258       lex_match_id (lexer, "VARIABLES");
259       lex_match (lexer, '=');
260
261       if (lex_token (lexer) != '.')
262         {
263           if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
264             {
265               free (vl);
266               return CMD_FAILURE;
267             }
268           as = AS_DICTIONARY;
269         }
270       else
271         dict_get_vars (dataset_dict (ds), &vl, &n, 0);
272
273       if (as == AS_SCRATCH)
274         {
275           size_t i, m;
276           for (i = 0, m = n; i < n; i++)
277             if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
278               {
279                 vl[i] = NULL;
280                 m--;
281               }
282           as = AS_NAMES;
283           n = m;
284         }
285
286       if (n == 0)
287         {
288           msg (SW, _("No variables to display."));
289           return CMD_FAILURE;
290         }
291
292       if (sorted)
293         sort (vl, n, sizeof *vl, compare_var_ptrs_by_name, NULL);
294
295       display_variables (vl, n, as);
296
297       free (vl);
298     }
299
300   return lex_end_of_command (lexer);
301 }
302
303 static void
304 display_macros (void)
305 {
306   som_blank_line ();
307   tab_output_text (TAB_LEFT, _("Macros not supported."));
308 }
309
310 static void
311 display_documents (const struct dictionary *dict)
312 {
313   const char *documents = dict_get_documents (dict);
314
315   som_blank_line ();
316   if (documents == NULL)
317     tab_output_text (TAB_LEFT, _("The active file dictionary does not "
318                                  "contain any documents."));
319   else
320     {
321       struct string line = DS_EMPTY_INITIALIZER;
322       size_t i;
323
324       tab_output_text (TAB_LEFT | TAT_TITLE,
325                        _("Documents in the active file:"));
326       som_blank_line ();
327       for (i = 0; i < dict_get_document_line_cnt (dict); i++)
328         {
329           dict_get_document_line (dict, i, &line);
330           tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
331         }
332       ds_destroy (&line);
333     }
334 }
335
336 static int _as;
337
338 /* Sets the widths of all the columns and heights of all the rows in
339    table T for driver D. */
340 static void
341 variables_dim (struct tab_table *t, struct outp_driver *d)
342 {
343   int pc;
344   int i;
345
346   t->w[0] = tab_natural_width (t, d, 0);
347   if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
348     {
349       t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
350       t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
351       pc = 3;
352     }
353   else pc = 1;
354   if (_as != AS_NAMES)
355     t->w[pc] = tab_natural_width (t, d, pc);
356
357   for (i = 0; i < t->nr; i++)
358     t->h[i] = tab_natural_height (t, d, i);
359 }
360
361 static void
362 display_variables (const struct variable **vl, size_t n, int as)
363 {
364   const struct variable **vp = vl;      /* Variable pointer. */
365   struct tab_table *t;
366   int nc;                       /* Number of columns. */
367   int nr;                       /* Number of rows. */
368   int pc;                       /* `Position column' */
369   int r;                        /* Current row. */
370   size_t i;
371
372   _as = as;
373   switch (as)
374     {
375     case AS_INDEX:
376       nc = 2;
377       break;
378     case AS_NAMES:
379       nc = 1;
380       break;
381     default:
382       nc = 4;
383       break;
384     }
385
386   t = tab_create (nc, n + 5, 1);
387   tab_headers (t, 0, 0, 1, 0);
388   nr = n + 5;
389   tab_hline (t, TAL_2, 0, nc - 1, 1);
390   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
391   pc = (as == AS_INDEX ? 1 : 3);
392   if (as != AS_NAMES)
393     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
394   if (as == AS_DICTIONARY || as == AS_VARIABLES)
395     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
396   else if (as == AS_LABELS)
397     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
398   tab_dim (t, variables_dim);
399
400   for (i = r = 1; i <= n; i++)
401     {
402       const struct variable *v;
403
404       while (*vp == NULL)
405         vp++;
406       v = *vp++;
407
408       if (as == AS_DICTIONARY || as == AS_VARIABLES)
409         {
410           int nvl = val_labs_count (var_get_value_labels (v));
411
412           if (r + 13 + nvl > nr)
413             {
414               nr = MAX (nr * n / (i + 1), nr);
415               nr += 10 + nvl;
416               tab_realloc (t, nc, nr);
417             }
418
419           r = describe_variable (v, t, r, as);
420         } else {
421           tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
422           if (as == AS_LABELS)
423             {
424               const char *label = var_get_label (v);
425               tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
426                               label == NULL ? "(no label)" : label);
427             }
428           if (as != AS_NAMES)
429             {
430               tab_text (t, pc, r, TAT_PRINTF, "%zu",
431                         var_get_dict_index (v) + 1);
432               tab_hline (t, TAL_1, 0, nc - 1, r);
433             }
434           r++;
435         }
436     }
437   tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
438   if (as != AS_NAMES)
439     {
440       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
441       tab_vline (t, TAL_1, 1, 0, r - 1);
442     }
443   else
444     tab_flags (t, SOMF_NO_TITLE);
445   if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
446     tab_vline (t, TAL_1, 3, 0, r - 1);
447   tab_resize (t, -1, r);
448   tab_columns (t, TAB_COL_DOWN, 1);
449   tab_submit (t);
450 }
451 \f
452 /* Puts a description of variable V into table T starting at row R.
453    The variable will be described in the format AS.  Returns the next
454    row available for use in the table. */
455 static int
456 describe_variable (const struct variable *v, struct tab_table *t, int r, int as)
457 {
458   const struct fmt_spec *print = var_get_print_format (v);
459   const struct fmt_spec *write = var_get_write_format (v);
460   enum measure m = var_get_measure (v);
461   enum alignment a = var_get_alignment (v);
462
463   /* Put the name, var label, and position into the first row. */
464   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
465   tab_text (t, 3, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
466
467   if (as == AS_DICTIONARY && var_has_label (v))
468     {
469       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
470       r++;
471     }
472
473   /* Print/write format, or print and write formats. */
474   if (fmt_equal (print, write))
475     {
476       char str[FMT_STRING_LEN_MAX + 1];
477       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
478                       fmt_to_string (print, str));
479       r++;
480     }
481   else
482     {
483       char str[FMT_STRING_LEN_MAX + 1];
484       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
485                       _("Print Format: %s"), fmt_to_string (print, str));
486       r++;
487       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
488                       _("Write Format: %s"), fmt_to_string (write, str));
489       r++;
490     }
491
492   /* Measurement level, display width, alignment. */
493   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
494                   _("Measure: %s"),
495                   m == MEASURE_NOMINAL ? _("Nominal")
496                   : m == MEASURE_ORDINAL ? _("Ordinal")
497                   : _("Scale"));
498   r++;
499   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
500                   _("Display Alignment: %s"),
501                   a == ALIGN_LEFT ? _("Left")
502                   : a == ALIGN_CENTRE ? _("Center")
503                   : _("Right"));
504   r++;
505   tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
506                   _("Display Width: %d"), var_get_display_width (v));
507   r++;
508
509   /* Missing values if any. */
510   if (var_has_missing_values (v))
511     {
512       char buf[128];
513       char *cp;
514       struct missing_values mv;
515       int cnt = 0;
516
517       cp = stpcpy (buf, _("Missing Values: "));
518
519       mv_copy (&mv, var_get_missing_values (v));
520       if (mv_has_range (&mv))
521         {
522           double x, y;
523           mv_pop_range (&mv, &x, &y);
524           if (x == LOWEST)
525             cp += sprintf (cp, "LOWEST THRU %g", y);
526           else if (y == HIGHEST)
527             cp += sprintf (cp, "%g THRU HIGHEST", x);
528           else
529             cp += sprintf (cp, "%g THRU %g", x, y);
530           cnt++;
531         }
532       while (mv_has_value (&mv))
533         {
534           union value value;
535           mv_pop_value (&mv, &value);
536           if (cnt++ > 0)
537             cp += sprintf (cp, "; ");
538           if (var_is_numeric (v))
539             cp += sprintf (cp, "%g", value.f);
540           else
541             {
542               *cp++ = '"';
543               memcpy (cp, value.s, var_get_width (v));
544               cp += var_get_width (v);
545               *cp++ = '"';
546               *cp = '\0';
547             }
548         }
549
550       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
551       r++;
552     }
553
554   /* Value labels. */
555   if (as == AS_DICTIONARY && var_has_value_labels (v))
556     {
557       const struct val_labs *val_labs = var_get_value_labels (v);
558       struct val_labs_iterator *i;
559       struct val_lab *vl;
560       int orig_r = r;
561
562 #if 0
563       tab_text (t, 1, r, TAB_LEFT, _("Value"));
564       tab_text (t, 2, r, TAB_LEFT, _("Label"));
565       r++;
566 #endif
567
568       tab_hline (t, TAL_1, 1, 2, r);
569       for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
570            vl = val_labs_next (val_labs, &i))
571         {
572           char buf[128];
573
574           if (var_is_alpha (v))
575             {
576               memcpy (buf, vl->value.s, var_get_width (v));
577               buf[var_get_width (v)] = 0;
578             }
579           else
580             sprintf (buf, "%g", vl->value.f);
581
582           tab_text (t, 1, r, TAB_NONE, buf);
583           tab_text (t, 2, r, TAB_LEFT, vl->label);
584           r++;
585         }
586
587       tab_vline (t, TAL_1, 2, orig_r, r - 1);
588     }
589
590   /* Draw a line below the last row of information on this variable. */
591   tab_hline (t, TAL_1, 0, 3, r);
592
593   return r;
594 }
595
596 /* Display a list of vectors.  If SORTED is nonzero then they are
597    sorted alphabetically. */
598 static void
599 display_vectors (const struct dictionary *dict, int sorted)
600 {
601   const struct vector **vl;
602   int i;
603   struct tab_table *t;
604   size_t nvec;
605   size_t nrow;
606   size_t row;
607
608   nvec = dict_get_vector_cnt (dict);
609   if (nvec == 0)
610     {
611       msg (SW, _("No vectors defined."));
612       return;
613     }
614
615   vl = xnmalloc (nvec, sizeof *vl);
616   nrow = 0;
617   for (i = 0; i < nvec; i++)
618     {
619       vl[i] = dict_get_vector (dict, i);
620       nrow += vector_get_var_cnt (vl[i]);
621     }
622   if (sorted)
623     qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
624
625   t = tab_create (4, nrow + 1, 0);
626   tab_headers (t, 0, 0, 1, 0);
627   tab_columns (t, TAB_COL_DOWN, 1);
628   tab_dim (t, tab_natural_dimensions);
629   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
630   tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
631   tab_hline (t, TAL_2, 0, 3, 1);
632   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
633   tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
634   tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
635   tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
636   tab_flags (t, SOMF_NO_TITLE);
637
638   row = 1;
639   for (i = 0; i < nvec; i++)
640     {
641       const struct vector *vec = vl[i];
642       size_t j;
643
644       tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
645                       TAB_LEFT, vector_get_name (vl[i]));
646
647       for (j = 0; j < vector_get_var_cnt (vec); j++)
648         {
649           struct variable *var = vector_get_var (vec, j);
650           char fmt_string[FMT_STRING_LEN_MAX + 1];
651           fmt_to_string (var_get_print_format (var), fmt_string);
652
653           tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
654           tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
655           tab_text (t, 3, row, TAB_LEFT, fmt_string);
656           row++;
657         }
658       tab_hline (t, TAL_1, 0, 3, row);
659     }
660
661   tab_submit (t);
662
663   free (vl);
664 }