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