First step in making struct variable opaque: the boring mechanical
[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
134                ? var_get_name (weight_var) : _("Not weighted."))); 
135   }
136   tab_text (t, 0, 8, TAB_LEFT, _("Mode:"));
137   tab_text (t, 1, 8, TAB_LEFT | TAT_PRINTF,
138                 _("Compression %s."), info.compressed ? _("on") : _("off"));
139   tab_dim (t, tab_natural_dimensions);
140   tab_submit (t);
141
142   nr = 1 + 2 * dict_get_var_cnt (d);
143
144   t = tab_create (4, nr, 1);
145   tab_dim (t, sysfile_info_dim);
146   tab_headers (t, 0, 0, 1, 0);
147   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
148   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
149   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
150   tab_hline (t, TAL_2, 0, 3, 1);
151   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
152     {
153       struct variable *v = dict_get_var (d, i);
154       const int nvl = val_labs_count (v->val_labs);
155       
156       if (r + 10 + nvl > nr)
157         {
158           nr = MAX (nr * dict_get_var_cnt (d) / (i + 1), nr);
159           nr += 10 + nvl;
160           tab_realloc (t, 4, nr);
161         }
162
163       r = describe_variable (v, t, r, AS_DICTIONARY);
164     }
165
166   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
167   tab_vline (t, TAL_1, 1, 0, r);
168   tab_vline (t, TAL_1, 3, 0, r);
169
170   tab_resize (t, -1, r);
171   tab_flags (t, SOMF_NO_TITLE);
172   tab_submit (t);
173
174   dict_destroy (d);
175   
176   return lex_end_of_command (lexer);
177 }
178 \f
179 /* DISPLAY utility. */
180
181 static void display_macros (void);
182 static void display_documents (const struct dictionary *dict);
183 static void display_variables (struct variable **, size_t, int);
184 static void display_vectors (const struct dictionary *dict, int sorted);
185
186 int
187 cmd_display (struct lexer *lexer, struct dataset *ds)
188 {
189   /* Whether to sort the list of variables alphabetically. */
190   int sorted;
191
192   /* Variables to display. */
193   size_t n;
194   struct variable **vl;
195
196   if (lex_match_id (lexer, "MACROS"))
197     display_macros ();
198   else if (lex_match_id (lexer, "DOCUMENTS"))
199     display_documents (dataset_dict (ds));
200   else if (lex_match_id (lexer, "FILE"))
201     {
202       som_blank_line ();
203       if (!lex_force_match_id (lexer, "LABEL"))
204         return CMD_FAILURE;
205       if (dict_get_label (dataset_dict (ds)) == NULL)
206         tab_output_text (TAB_LEFT,
207                          _("The active file does not have a file label."));
208       else
209         {
210           tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
211           tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
212         }
213     }
214   else
215     {
216       static const char *sbc[] =
217         {"NAMES", "INDEX", "VARIABLES", "LABELS",
218          "DICTIONARY", "SCRATCH", "VECTORS", NULL};
219       const char **cp;
220       int as;
221
222       sorted = lex_match_id (lexer, "SORTED");
223
224       for (cp = sbc; *cp; cp++)
225         if (lex_token (lexer) == T_ID
226             && lex_id_match (ss_cstr (*cp), ss_cstr (lex_tokid (lexer))))
227           {
228             lex_get (lexer);
229             break;
230           }
231       as = cp - sbc;
232
233       if (*cp == NULL)
234         as = AS_NAMES;
235
236       if (as == AS_VECTOR)
237         {
238           display_vectors (dataset_dict(ds), sorted);
239           return CMD_SUCCESS;
240         }
241
242       lex_match (lexer, '/');
243       lex_match_id (lexer, "VARIABLES");
244       lex_match (lexer, '=');
245
246       if (lex_token (lexer) != '.')
247         {
248           if (!parse_variables (lexer, dataset_dict (ds), &vl, &n, PV_NONE))
249             {
250               free (vl);
251               return CMD_FAILURE;
252             }
253           as = AS_DICTIONARY;
254         }
255       else
256         dict_get_vars (dataset_dict (ds), &vl, &n, 0);
257
258       if (as == AS_SCRATCH)
259         {
260           size_t i, m;
261           for (i = 0, m = n; i < n; i++)
262             if (dict_class_from_id (var_get_name (vl[i])) != DC_SCRATCH)
263               {
264                 vl[i] = NULL;
265                 m--;
266               }
267           as = AS_NAMES;
268           n = m;
269         }
270
271       if (n == 0)
272         {
273           msg (SW, _("No variables to display."));
274           return CMD_FAILURE;
275         }
276
277       if (sorted)
278         sort (vl, n, sizeof *vl, compare_var_ptr_names, NULL);
279
280       display_variables (vl, n, as);
281
282       free (vl);
283     }
284
285   return lex_end_of_command (lexer);
286 }
287
288 static void
289 display_macros (void)
290 {
291   som_blank_line ();
292   tab_output_text (TAB_LEFT, _("Macros not supported."));
293 }
294
295 static void
296 display_documents (const struct dictionary *dict)
297 {
298   const char *documents = dict_get_documents (dict);
299
300   som_blank_line ();
301   if (documents == NULL)
302     tab_output_text (TAB_LEFT, _("The active file dictionary does not "
303                                  "contain any documents."));
304   else
305     {
306       size_t n_lines = strlen (documents) / 80;
307       char buf[81];
308       size_t i;
309
310       tab_output_text (TAB_LEFT | TAT_TITLE,
311                        _("Documents in the active file:"));
312       som_blank_line ();
313       buf[80] = 0;
314       for (i = 0; i < n_lines; i++)
315         {
316           int len = 79;
317
318           memcpy (buf, &documents[i * 80], 80);
319           while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
320                  && len > 0)
321             len--;
322           buf[len + 1] = 0;
323           tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
324         }
325     }
326 }
327
328 static int _as;
329
330 /* Sets the widths of all the columns and heights of all the rows in
331    table T for driver D. */
332 static void
333 variables_dim (struct tab_table *t, struct outp_driver *d)
334 {
335   int pc;
336   int i;
337   
338   t->w[0] = tab_natural_width (t, d, 0);
339   if (_as == AS_DICTIONARY || _as == AS_VARIABLES || _as == AS_LABELS)
340     {
341       t->w[1] = MAX (tab_natural_width (t, d, 1), d->prop_em_width * 5);
342       t->w[2] = MAX (tab_natural_width (t, d, 2), d->prop_em_width * 35);
343       pc = 3;
344     }
345   else pc = 1;
346   if (_as != AS_NAMES)
347     t->w[pc] = tab_natural_width (t, d, pc);
348
349   for (i = 0; i < t->nr; i++)
350     t->h[i] = tab_natural_height (t, d, i);
351 }
352   
353 static void
354 display_variables (struct variable **vl, size_t n, int as)
355 {
356   struct variable **vp = vl;            /* Variable pointer. */
357   struct tab_table *t;
358   int nc;                       /* Number of columns. */
359   int nr;                       /* Number of rows. */
360   int pc;                       /* `Position column' */
361   int r;                        /* Current row. */
362   size_t i;
363
364   _as = as;
365   switch (as)
366     {
367     case AS_INDEX:
368       nc = 2;
369       break;
370     case AS_NAMES:
371       nc = 1;
372       break;
373     default:
374       nc = 4;
375       break;
376     }
377
378   t = tab_create (nc, n + 5, 1);
379   tab_headers (t, 0, 0, 1, 0);
380   nr = n + 5;
381   tab_hline (t, TAL_2, 0, nc - 1, 1);
382   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
383   pc = (as == AS_INDEX ? 1 : 3);
384   if (as != AS_NAMES)
385     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
386   if (as == AS_DICTIONARY || as == AS_VARIABLES)
387     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
388   else if (as == AS_LABELS)
389     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
390   tab_dim (t, variables_dim);
391     
392   for (i = r = 1; i <= n; i++)
393     {
394       struct variable *v;
395
396       while (*vp == NULL)
397         vp++;
398       v = *vp++;
399
400       if (as == AS_DICTIONARY || as == AS_VARIABLES)
401         {
402           int nvl = val_labs_count (v->val_labs);
403       
404           if (r + 10 + nvl > nr)
405             {
406               nr = MAX (nr * n / (i + 1), nr);
407               nr += 10 + nvl;
408               tab_realloc (t, nc, nr);
409             }
410
411           r = describe_variable (v, t, r, as);
412         } else {
413           tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
414           if (as == AS_LABELS) 
415             {
416               const char *label = var_get_label (v);
417               tab_joint_text (t, 1, r, 2, r, TAB_LEFT,
418                               label != NULL ? "(no label)" : label); 
419             }
420           if (as != AS_NAMES)
421             {
422               tab_text (t, pc, r, TAT_PRINTF, "%d", v->index + 1);
423               tab_hline (t, TAL_1, 0, nc - 1, r);
424             }
425           r++;
426         }
427     }
428   tab_hline (t, as == AS_NAMES ? TAL_1 : TAL_2, 0, nc - 1, 1);
429   if (as != AS_NAMES)
430     {
431       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
432       tab_vline (t, TAL_1, 1, 0, r - 1);
433     }
434   else
435     tab_flags (t, SOMF_NO_TITLE);
436   if (as == AS_DICTIONARY || as == AS_VARIABLES || as == AS_LABELS)
437     tab_vline (t, TAL_1, 3, 0, r - 1);
438   tab_resize (t, -1, r);
439   tab_columns (t, TAB_COL_DOWN, 1);
440   tab_submit (t);
441 }
442 \f
443 /* Puts a description of variable V into table T starting at row R.
444    The variable will be described in the format AS.  Returns the next
445    row available for use in the table. */
446 static int 
447 describe_variable (struct variable *v, struct tab_table *t, int r, int as)
448 {
449   const struct fmt_spec *print = var_get_print_format (v);
450   const struct fmt_spec *write = var_get_write_format (v);
451
452   /* Put the name, var label, and position into the first row. */
453   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
454   tab_text (t, 3, r, TAT_PRINTF, "%d", v->index + 1);
455
456   if (as == AS_DICTIONARY && var_has_label (v)) 
457     {
458       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
459       r++;
460     }
461   
462   /* Print/write format, or print and write formats. */
463   if (fmt_equal (print, write))
464     {
465       char str[FMT_STRING_LEN_MAX + 1];
466       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF, _("Format: %s"),
467                       fmt_to_string (print, str));
468       r++;
469     }
470   else
471     {
472       char str[FMT_STRING_LEN_MAX + 1];
473       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
474                       _("Print Format: %s"), fmt_to_string (print, str));
475       r++;
476       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
477                       _("Write Format: %s"), fmt_to_string (write, str));
478       r++;
479     }
480
481   /* Missing values if any. */
482   if (var_has_missing_values (v))
483     {
484       char buf[128];
485       char *cp;
486       struct missing_values mv;
487       int cnt = 0;
488       
489       cp = stpcpy (buf, _("Missing Values: "));
490       
491       mv_copy (&mv, var_get_missing_values (v));
492       if (mv_has_range (&mv)) 
493         {
494           double x, y;
495           mv_pop_range (&mv, &x, &y);
496           if (x == LOWEST)
497             cp += sprintf (cp, "LOWEST THRU %g", y);
498           else if (y == HIGHEST)
499             cp += sprintf (cp, "%g THRU HIGHEST", x);
500           else
501             cp += sprintf (cp, "%g THRU %g", x, y);
502           cnt++;
503         }
504       while (mv_has_value (&mv)) 
505         {
506           union value value;
507           mv_pop_value (&mv, &value);
508           if (cnt++ > 0)
509             cp += sprintf (cp, "; ");
510           if (var_is_numeric (v))
511             cp += sprintf (cp, "%g", value.f);
512           else 
513             {
514               *cp++ = '"';
515               memcpy (cp, value.s, var_get_width (v));
516               cp += var_get_width (v);
517               *cp++ = '"';
518               *cp = '\0';
519             }
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 (var_is_alpha (v))
546             {
547               memcpy (buf, vl->value.s, var_get_width (v));
548               buf[var_get_width (v)] = 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 strcasecmp (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 (const struct dictionary *dict, 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 (dict);
589   if (nvec == 0)
590     {
591       msg (SW, _("No vectors defined."));
592       return;
593     }
594
595   vl = xnmalloc (nvec, sizeof *vl);
596   for (i = 0; i < nvec; i++)
597     vl[i] = dict_get_vector (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 }
614
615
616
617
618
619
620
621
622
623
624