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