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