Get rid of most global variables in outputting tables.
[pspp-builds.git] / src / language / dictionary / sys-file-info.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <stdlib.h>
21
22 #include <data/attributes.h>
23 #include <data/casereader.h>
24 #include <data/dictionary.h>
25 #include <data/file-handle-def.h>
26 #include <data/format.h>
27 #include <data/missing-values.h>
28 #include <data/procedure.h>
29 #include <data/sys-file-reader.h>
30 #include <data/value-labels.h>
31 #include <data/variable.h>
32 #include <data/vector.h>
33 #include <language/command.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/lexer/lexer.h>
36 #include <language/lexer/variable-parser.h>
37 #include <libpspp/array.h>
38 #include <libpspp/hash.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 #include "xalloc.h"
48
49 #include "gettext.h"
50 #define _(msgid) gettext (msgid)
51
52 /* Information to include in displaying a dictionary. */
53 enum 
54   {
55     DF_DICT_INDEX       = 1 << 0,
56     DF_FORMATS          = 1 << 1,
57     DF_VALUE_LABELS     = 1 << 2,
58     DF_VARIABLE_LABELS  = 1 << 3,
59     DF_MISSING_VALUES   = 1 << 4,
60     DF_AT_ATTRIBUTES    = 1 << 5, /* Attributes whose names begin with @. */
61     DF_ATTRIBUTES       = 1 << 6, /* All other attributes. */
62     DF_MISC             = 1 << 7,
63     DF_ALL              = (1 << 8) - 1
64   };
65
66 static int describe_variable (const struct variable *v, struct tab_table *t,
67                               int r, int pc, int flags);
68
69 /* Sets the widths of all the columns and heights of all the rows in
70    table T for driver D. */
71 static void
72 sysfile_info_dim (struct tab_rendering *r, void *aux UNUSED)
73 {
74   const struct tab_table *t = r->table;
75   static const int max[] = {20, 5, 35, 3, 0};
76   const int *p;
77   int i;
78
79   for (p = max; *p; p++)
80     r->w[p - max] = MIN (tab_natural_width (r, p - max),
81                          *p * r->driver->prop_em_width);
82   for (i = 0; i < t->nr; i++)
83     r->h[i] = tab_natural_height (r, i);
84 }
85
86 /* SYSFILE INFO utility. */
87 int
88 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
89 {
90   struct file_handle *h;
91   struct dictionary *d;
92   struct tab_table *t;
93   struct casereader *reader;
94   struct sfm_read_info info;
95   int r, i;
96
97   lex_match_id (lexer, "FILE");
98   lex_match (lexer, '=');
99
100   h = fh_parse (lexer, FH_REF_FILE);
101   if (!h)
102     return CMD_FAILURE;
103
104   reader = sfm_open_reader (h, &d, &info);
105   if (!reader)
106     {
107       fh_unref (h);
108       return CMD_FAILURE;
109     }
110   casereader_destroy (reader);
111
112   t = tab_create (2, 11, 0);
113   tab_vline (t, TAL_GAP, 1, 0, 8);
114   tab_text (t, 0, 0, TAB_LEFT, _("File:"));
115   tab_text (t, 1, 0, TAB_LEFT, fh_get_file_name (h));
116   tab_text (t, 0, 1, TAB_LEFT, _("Label:"));
117   {
118     const char *label = dict_get_label (d);
119     if (label == NULL)
120       label = _("No label.");
121     tab_text (t, 1, 1, TAB_LEFT, label);
122   }
123   tab_text (t, 0, 2, TAB_LEFT, _("Created:"));
124   tab_text (t, 1, 2, TAB_LEFT | TAT_PRINTF, "%s %s by %s",
125                 info.creation_date, info.creation_time, info.product);
126   tab_text (t, 0, 3, TAB_LEFT, _("Integer Format:"));
127   tab_text (t, 1, 3, TAB_LEFT,
128             info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian.")
129             : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian.")
130             : _("Unknown."));
131   tab_text (t, 0, 4, TAB_LEFT, _("Real Format:"));
132   tab_text (t, 1, 4, TAB_LEFT,
133             info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
134             : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
135             : info.float_format == FLOAT_VAX_D ? _("VAX D.")
136             : info.float_format == FLOAT_VAX_G ? _("VAX G.")
137             : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
138             : _("Unknown."));
139   tab_text (t, 0, 5, TAB_LEFT, _("Variables:"));
140   tab_text (t, 1, 5, TAB_LEFT | TAT_PRINTF, "%zu", dict_get_var_cnt (d));
141   tab_text (t, 0, 6, TAB_LEFT, _("Cases:"));
142   tab_text (t, 1, 6, TAB_LEFT | TAT_PRINTF,
143             info.case_cnt == -1 ? _("Unknown") : "%ld",
144             (long int) info.case_cnt);
145   tab_text (t, 0, 7, TAB_LEFT, _("Type:"));
146   tab_text (t, 1, 7, TAB_LEFT, _("System File."));
147   tab_text (t, 0, 8, TAB_LEFT, _("Weight:"));
148   {
149     struct variable *weight_var = dict_get_weight (d);
150     tab_text (t, 1, 8, TAB_LEFT,
151               (weight_var != NULL
152                ? var_get_name (weight_var) : _("Not weighted.")));
153   }
154   tab_text (t, 0, 9, TAB_LEFT, _("Mode:"));
155   tab_text (t, 1, 9, TAB_LEFT | TAT_PRINTF,
156                 _("Compression %s."), info.compressed ? _("on") : _("off"));
157
158
159   tab_text (t, 0, 10, TAB_LEFT, _("Charset:"));
160   tab_text (t, 1, 10, TAB_LEFT | TAT_PRINTF,
161             dict_get_encoding(d) ? dict_get_encoding(d) : _("Unknown"));
162
163
164   tab_dim (t, tab_natural_dimensions, NULL);
165   tab_submit (t);
166
167   t = tab_create (4, 1 + 2 * dict_get_var_cnt (d), 1);
168   tab_dim (t, sysfile_info_dim, NULL);
169   tab_headers (t, 0, 0, 1, 0);
170   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
171   tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE, _("Description"));
172   tab_text (t, 3, 0, TAB_LEFT | TAT_TITLE, _("Position"));
173   tab_hline (t, TAL_2, 0, 3, 1);
174   for (r = 1, i = 0; i < dict_get_var_cnt (d); i++)
175     r = describe_variable (dict_get_var (d, i), t, r, 3,
176                            DF_ALL & ~DF_AT_ATTRIBUTES);
177
178   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, r);
179   tab_vline (t, TAL_1, 1, 0, r);
180   tab_vline (t, TAL_1, 3, 0, r);
181
182   tab_resize (t, -1, r);
183   tab_flags (t, SOMF_NO_TITLE);
184   tab_submit (t);
185
186   dict_destroy (d);
187
188   fh_unref (h);
189   return lex_end_of_command (lexer);
190 }
191 \f
192 /* DISPLAY utility. */
193
194 static void display_macros (void);
195 static void display_documents (const struct dictionary *dict);
196 static void display_variables (const struct variable **, size_t, int);
197 static void display_vectors (const struct dictionary *dict, int sorted);
198 static void display_data_file_attributes (struct attrset *, int flags);
199
200 int
201 cmd_display (struct lexer *lexer, struct dataset *ds)
202 {
203   /* Whether to sort the list of variables alphabetically. */
204   int sorted;
205
206   /* Variables to display. */
207   size_t n;
208   const struct variable **vl;
209
210   if (lex_match_id (lexer, "MACROS"))
211     display_macros ();
212   else if (lex_match_id (lexer, "DOCUMENTS"))
213     display_documents (dataset_dict (ds));
214   else if (lex_match_id (lexer, "FILE"))
215     {
216       som_blank_line ();
217       if (!lex_force_match_id (lexer, "LABEL"))
218         return CMD_FAILURE;
219       if (dict_get_label (dataset_dict (ds)) == NULL)
220         tab_output_text (TAB_LEFT,
221                          _("The active file does not have a file label."));
222       else
223         {
224           tab_output_text (TAB_LEFT | TAT_TITLE, _("File label:"));
225           tab_output_text (TAB_LEFT | TAB_FIX, dict_get_label (dataset_dict (ds)));
226         }
227     }
228   else
229     {
230       int flags;
231
232       sorted = lex_match_id (lexer, "SORTED");
233
234       if (lex_match_id (lexer, "VECTORS"))
235         {
236           display_vectors (dataset_dict(ds), sorted);
237           return lex_end_of_command (lexer);
238         }
239       else if (lex_match_id (lexer, "SCRATCH")) 
240         {
241           dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
242           flags = 0;
243         }
244       else 
245         {
246           struct subcommand 
247             {
248               const char *name;
249               int flags;
250             };
251           static const struct subcommand subcommands[] = 
252             {
253               {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
254               {"ATTRIBUTES", DF_ATTRIBUTES},
255               {"DICTIONARY", DF_ALL & ~DF_AT_ATTRIBUTES},
256               {"INDEX", DF_DICT_INDEX},
257               {"LABELS", DF_DICT_INDEX | DF_VARIABLE_LABELS},
258               {"NAMES", 0},
259               {"VARIABLES",
260                DF_DICT_INDEX | DF_FORMATS | DF_MISSING_VALUES | DF_MISC},
261               {NULL, 0},
262             };
263           const struct subcommand *sbc;
264
265           flags = 0;
266           for (sbc = subcommands; sbc->name != NULL; sbc++)
267             if (lex_match_id (lexer, sbc->name))
268               {
269                 flags = sbc->flags;
270                 break;
271               }
272
273           lex_match (lexer, '/');
274           lex_match_id (lexer, "VARIABLES");
275           lex_match (lexer, '=');
276
277           if (lex_token (lexer) != '.')
278             {
279               if (!parse_variables_const (lexer, dataset_dict (ds), &vl, &n,
280                                           PV_NONE))
281                 {
282                   free (vl);
283                   return CMD_FAILURE;
284                 }
285             }
286           else
287             dict_get_vars (dataset_dict (ds), &vl, &n, 0);
288         }
289
290       if (n > 0) 
291         {
292           sort (vl, n, sizeof *vl,
293                 (sorted
294                  ? compare_var_ptrs_by_name
295                  : compare_var_ptrs_by_dict_index), NULL);
296           display_variables (vl, n, flags);
297         }
298       else
299         msg (SW, _("No variables to display."));
300       free (vl);
301
302       if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
303         display_data_file_attributes (dict_get_attributes (dataset_dict (ds)),
304                                       flags);
305     }
306
307   return lex_end_of_command (lexer);
308 }
309
310 static void
311 display_macros (void)
312 {
313   som_blank_line ();
314   tab_output_text (TAB_LEFT, _("Macros not supported."));
315 }
316
317 static void
318 display_documents (const struct dictionary *dict)
319 {
320   const char *documents = dict_get_documents (dict);
321
322   som_blank_line ();
323   if (documents == NULL)
324     tab_output_text (TAB_LEFT, _("The active file dictionary does not "
325                                  "contain any documents."));
326   else
327     {
328       struct string line = DS_EMPTY_INITIALIZER;
329       size_t i;
330
331       tab_output_text (TAB_LEFT | TAT_TITLE,
332                        _("Documents in the active file:"));
333       som_blank_line ();
334       for (i = 0; i < dict_get_document_line_cnt (dict); i++)
335         {
336           dict_get_document_line (dict, i, &line);
337           tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
338         }
339       ds_destroy (&line);
340     }
341 }
342
343 static int _flags;
344
345 /* Sets the widths of all the columns and heights of all the rows in
346    table T for driver D. */
347 static void
348 variables_dim (struct tab_rendering *r, void *aux UNUSED)
349 {
350   const struct outp_driver *d = r->driver;
351
352   tab_natural_dimensions (r, NULL);
353   if (_flags & (DF_VALUE_LABELS | DF_VARIABLE_LABELS | DF_MISSING_VALUES
354                 | DF_AT_ATTRIBUTES | DF_ATTRIBUTES))
355     {
356       r->w[1] = MAX (r->w[1], d->prop_em_width * 5);
357       r->w[2] = MAX (r->w[2], d->prop_em_width * 35);
358     }
359 }
360
361 static void
362 display_variables (const struct variable **vl, size_t n, int flags)
363 {
364   struct tab_table *t;
365   int nc;                       /* Number of columns. */
366   int pc;                       /* `Position column' */
367   int r;                        /* Current row. */
368   size_t i;
369
370   _flags = flags;
371
372   /* One column for the name,
373      two columns for general description,
374      one column for dictionary index. */
375   nc = 1;
376   if (flags & ~DF_DICT_INDEX)
377     nc += 2;
378   pc = nc;
379   if (flags & DF_DICT_INDEX)
380     nc++;
381
382   t = tab_create (nc, n + 5, 1);
383   tab_headers (t, 0, 0, 1, 0);
384   tab_hline (t, TAL_2, 0, nc - 1, 1);
385   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
386   if (flags & ~DF_DICT_INDEX) 
387     tab_joint_text (t, 1, 0, 2, 0, TAB_LEFT | TAT_TITLE,
388                     (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS)
389                      ? _("Description") : _("Label")));
390   if (flags & DF_DICT_INDEX)
391     tab_text (t, pc, 0, TAB_LEFT | TAT_TITLE, _("Position"));
392   tab_dim (t, variables_dim, NULL);
393
394   r = 1;
395   for (i = 0; i < n; i++)
396     r = describe_variable (vl[i], t, r, pc, flags);
397   tab_hline (t, flags & ~DF_DICT_INDEX ? TAL_2 : TAL_1, 0, nc - 1, 1);
398   if (flags)
399     {
400       tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, nc - 1, r - 1);
401       tab_vline (t, TAL_1, 1, 0, r - 1);
402     }
403   else
404     tab_flags (t, SOMF_NO_TITLE);
405   if (flags & ~DF_DICT_INDEX)
406     tab_vline (t, TAL_1, nc - 1, 0, r - 1);
407   tab_resize (t, -1, r);
408   tab_columns (t, TAB_COL_DOWN, 1);
409   tab_submit (t);
410 }
411 \f
412 static bool
413 is_at_name (const char *name) 
414 {
415   return name[0] == '@' || (name[0] == '$' && name[1] == '@');
416 }
417
418 static size_t
419 count_attributes (const struct attrset *set, int flags) 
420 {
421   struct attrset_iterator i;
422   struct attribute *attr;
423   size_t n_attrs;
424   
425   n_attrs = 0;
426   for (attr = attrset_first (set, &i); attr != NULL;
427        attr = attrset_next (set, &i)) 
428     if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
429       n_attrs += attribute_get_n_values (attr);
430   return n_attrs;
431 }
432
433 static void
434 display_attributes (struct tab_table *t, const struct attrset *set, int flags,
435                     int c, int r)
436 {
437   struct attrset_iterator i;
438   struct attribute *attr;
439
440   for (attr = attrset_first (set, &i); attr != NULL;
441        attr = attrset_next (set, &i)) 
442     {
443       const char *name = attribute_get_name (attr);
444       size_t n_values;
445       size_t i;
446
447       if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
448         continue;
449
450       n_values = attribute_get_n_values (attr);
451       for (i = 0; i < n_values; i++)
452         {
453           if (n_values > 1)
454             tab_text (t, c, r, TAB_LEFT | TAT_PRINTF, "%s[%d]",
455                       name, i + 1);
456           else
457             tab_text (t, c, r, TAB_LEFT, name);
458           tab_text (t, c + 1, r, TAB_LEFT, attribute_get_value (attr, i));
459           r++;
460         }
461     }
462 }
463
464 static void
465 display_data_file_attributes (struct attrset *set, int flags) 
466 {
467   struct tab_table *t;
468   size_t n_attrs;
469
470   n_attrs = count_attributes (set, flags);
471   if (!n_attrs)
472     return;
473
474   t = tab_create (2, n_attrs + 1, 0);
475   tab_headers (t, 0, 0, 1, 0);
476   tab_box (t, TAL_1, TAL_1, -1, TAL_1, 0, 0, tab_nc (t) - 1, tab_nr (t) - 1);
477   tab_hline (t, TAL_2, 0, 1, 1); 
478   tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Attribute"));
479   tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
480   display_attributes (t, set, flags, 0, 1);
481   tab_columns (t, TAB_COL_DOWN, 1);
482   tab_dim (t, tab_natural_dimensions, NULL);
483   tab_title (t, "Custom data file attributes.");
484   tab_submit (t);
485 }
486
487 /* Puts a description of variable V into table T starting at row
488    R.  The variable will be described in the format given by
489    FLAGS.  Returns the next row available for use in the
490    table. */
491 static int
492 describe_variable (const struct variable *v, struct tab_table *t, int r,
493                    int pc, int flags)
494 {
495   size_t n_attrs = 0;
496   int need_rows;
497
498   /* Make sure that enough rows are allocated. */
499   need_rows = 1;
500   if (flags & ~(DF_DICT_INDEX | DF_VARIABLE_LABELS))
501     need_rows += 15;
502   if (flags & DF_VALUE_LABELS)
503     need_rows += val_labs_count (var_get_value_labels (v));
504   if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES))
505     {
506       n_attrs = count_attributes (var_get_attributes (v), flags);
507       need_rows += n_attrs; 
508     }
509   if (r + need_rows > tab_nr (t))
510     {
511       int nr = MAX (r + need_rows, tab_nr (t) * 2);
512       tab_realloc (t, -1, nr);
513     }
514
515   /* Put the name, var label, and position into the first row. */
516   tab_text (t, 0, r, TAB_LEFT, var_get_name (v));
517   if (flags & DF_DICT_INDEX)
518     tab_text (t, pc, r, TAT_PRINTF, "%zu", var_get_dict_index (v) + 1);
519
520   if (flags & DF_VARIABLE_LABELS && var_has_label (v))
521     {
522       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, var_get_label (v));
523       r++;
524     }
525
526   /* Print/write format, or print and write formats. */
527   if (flags & DF_FORMATS) 
528     {
529       const struct fmt_spec *print = var_get_print_format (v);
530       const struct fmt_spec *write = var_get_write_format (v);
531
532       if (fmt_equal (print, write))
533         {
534           char str[FMT_STRING_LEN_MAX + 1];
535           tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
536                           _("Format: %s"), fmt_to_string (print, str));
537           r++;
538         }
539       else
540         {
541           char str[FMT_STRING_LEN_MAX + 1];
542           tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
543                           _("Print Format: %s"), fmt_to_string (print, str));
544           r++;
545           tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
546                           _("Write Format: %s"), fmt_to_string (write, str));
547           r++;
548         }
549     }
550   
551   /* Measurement level, display width, alignment. */
552   if (flags & DF_MISC) 
553     {
554       enum measure m = var_get_measure (v);
555       enum alignment a = var_get_alignment (v);
556
557       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
558                       _("Measure: %s"),
559                       m == MEASURE_NOMINAL ? _("Nominal")
560                       : m == MEASURE_ORDINAL ? _("Ordinal")
561                       : _("Scale"));
562       r++;
563       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
564                       _("Display Alignment: %s"),
565                       a == ALIGN_LEFT ? _("Left")
566                       : a == ALIGN_CENTRE ? _("Center")
567                       : _("Right"));
568       r++;
569       tab_joint_text (t, 1, r, 2, r, TAB_LEFT | TAT_PRINTF,
570                       _("Display Width: %d"), var_get_display_width (v));
571       r++;
572     }
573   
574   /* Missing values if any. */
575   if (flags & DF_MISSING_VALUES && var_has_missing_values (v))
576     {
577       const struct missing_values *mv = var_get_missing_values (v);
578       char buf[128];
579       char *cp;
580       int cnt = 0;
581       int i;
582
583       cp = stpcpy (buf, _("Missing Values: "));
584
585       if (mv_has_range (mv))
586         {
587           double x, y;
588           mv_get_range (mv, &x, &y);
589           if (x == LOWEST)
590             cp += sprintf (cp, "LOWEST THRU %g", y);
591           else if (y == HIGHEST)
592             cp += sprintf (cp, "%g THRU HIGHEST", x);
593           else
594             cp += sprintf (cp, "%g THRU %g", x, y);
595           cnt++;
596         }
597       for (i = 0; i < mv_n_values (mv); i++)
598         {
599           const union value *value = mv_get_value (mv, i);
600           if (cnt++ > 0)
601             cp += sprintf (cp, "; ");
602           if (var_is_numeric (v))
603             cp += sprintf (cp, "%g", value->f);
604           else
605             {
606               int width = var_get_width (v);
607               int mv_width = MIN (width, MV_MAX_STRING);
608
609               *cp++ = '"';
610               memcpy (cp, value_str (value, width), mv_width);
611               cp += mv_width;
612               *cp++ = '"';
613               *cp = '\0';
614             }
615         }
616
617       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, buf);
618       r++;
619     }
620
621   /* Value labels. */
622   if (flags & DF_VALUE_LABELS && var_has_value_labels (v))
623     {
624       const struct val_labs *val_labs = var_get_value_labels (v);
625       size_t n_labels = val_labs_count (val_labs);
626       const struct val_lab **labels;
627       int orig_r = r;
628       size_t i;
629
630 #if 0
631       tab_text (t, 1, r, TAB_LEFT, _("Value"));
632       tab_text (t, 2, r, TAB_LEFT, _("Label"));
633       r++;
634 #endif
635
636       tab_hline (t, TAL_1, 1, 2, r);
637
638       labels = val_labs_sorted (val_labs);
639       for (i = 0; i < n_labels; i++)
640         {
641           const struct val_lab *vl = labels[i];
642           char buf[MAX_STRING + 1];
643
644           if (var_is_alpha (v))
645             {
646               int width = var_get_width (v);
647               memcpy (buf, value_str (&vl->value, width), width);
648               buf[width] = 0;
649             }
650           else
651             sprintf (buf, "%g", vl->value.f);
652
653           tab_text (t, 1, r, TAB_NONE, buf);
654           tab_text (t, 2, r, TAB_LEFT, val_lab_get_label (vl));
655           r++;
656         }
657       free (labels);
658
659       tab_vline (t, TAL_1, 2, orig_r, r - 1);
660     }
661
662   if (flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES) && n_attrs)
663     {
664       tab_joint_text (t, 1, r, 2, r, TAB_LEFT, "Custom attributes:");
665       r++;
666
667       display_attributes (t, var_get_attributes (v), flags, 1, r);
668       r += n_attrs;
669     }
670
671   /* Draw a line below the last row of information on this variable. */
672   tab_hline (t, TAL_1, 0, tab_nc (t) - 1, r);
673
674   return r;
675 }
676
677 /* Display a list of vectors.  If SORTED is nonzero then they are
678    sorted alphabetically. */
679 static void
680 display_vectors (const struct dictionary *dict, int sorted)
681 {
682   const struct vector **vl;
683   int i;
684   struct tab_table *t;
685   size_t nvec;
686   size_t nrow;
687   size_t row;
688
689   nvec = dict_get_vector_cnt (dict);
690   if (nvec == 0)
691     {
692       msg (SW, _("No vectors defined."));
693       return;
694     }
695
696   vl = xnmalloc (nvec, sizeof *vl);
697   nrow = 0;
698   for (i = 0; i < nvec; i++)
699     {
700       vl[i] = dict_get_vector (dict, i);
701       nrow += vector_get_var_cnt (vl[i]);
702     }
703   if (sorted)
704     qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
705
706   t = tab_create (4, nrow + 1, 0);
707   tab_headers (t, 0, 0, 1, 0);
708   tab_columns (t, TAB_COL_DOWN, 1);
709   tab_dim (t, tab_natural_dimensions, NULL);
710   tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
711   tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
712   tab_hline (t, TAL_2, 0, 3, 1);
713   tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
714   tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
715   tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
716   tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
717   tab_flags (t, SOMF_NO_TITLE);
718
719   row = 1;
720   for (i = 0; i < nvec; i++)
721     {
722       const struct vector *vec = vl[i];
723       size_t j;
724
725       tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
726                       TAB_LEFT, vector_get_name (vl[i]));
727
728       for (j = 0; j < vector_get_var_cnt (vec); j++)
729         {
730           struct variable *var = vector_get_var (vec, j);
731           char fmt_string[FMT_STRING_LEN_MAX + 1];
732           fmt_to_string (var_get_print_format (var), fmt_string);
733
734           tab_text (t, 1, row, TAB_RIGHT | TAT_PRINTF, "%zu", j + 1);
735           tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
736           tab_text (t, 3, row, TAB_LEFT, fmt_string);
737           row++;
738         }
739       tab_hline (t, TAL_1, 0, 3, row);
740     }
741
742   tab_submit (t);
743
744   free (vl);
745 }