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