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