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