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