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