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