1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #include "data/any-reader.h"
24 #include "data/attributes.h"
25 #include "data/casereader.h"
26 #include "data/dataset.h"
27 #include "data/dictionary.h"
28 #include "data/file-handle-def.h"
29 #include "data/format.h"
30 #include "data/missing-values.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/array.h"
39 #include "libpspp/hash-functions.h"
40 #include "libpspp/i18n.h"
41 #include "libpspp/message.h"
42 #include "libpspp/misc.h"
43 #include "libpspp/pool.h"
44 #include "libpspp/string-array.h"
45 #include "output/tab.h"
46 #include "output/text-item.h"
47 #include "output/table-item.h"
49 #include "gl/count-one-bits.h"
50 #include "gl/localcharset.h"
51 #include "gl/intprops.h"
52 #include "gl/minmax.h"
53 #include "gl/xalloc.h"
56 #define _(msgid) gettext (msgid)
57 #define N_(msgid) (msgid)
59 /* Information to include in displaying a dictionary. */
66 DF_MEASUREMENT_LEVEL = 1 << 3,
69 DF_ALIGNMENT = 1 << 6,
70 DF_PRINT_FORMAT = 1 << 7,
71 DF_WRITE_FORMAT = 1 << 8,
72 DF_MISSING_VALUES = 1 << 9,
73 #define DF_ALL_VARIABLE ((1 << 10) - 1)
75 /* Value labels table. */
76 DF_VALUE_LABELS = 1 << 10,
78 /* Attribute table. */
79 DF_AT_ATTRIBUTES = 1 << 11, /* Attributes whose names begin with @. */
80 DF_ATTRIBUTES = 1 << 12, /* All other attributes. */
83 static void display_variables (const struct variable **, size_t, int flags);
84 static void display_value_labels (const struct variable **, size_t);
85 static void display_attributes (const struct attrset *,
86 const struct variable **, size_t, int flags);
88 static void report_encodings (const struct file_handle *, struct pool *,
89 char **titles, bool *ids,
90 char **strings, size_t n_strings);
92 /* SYSFILE INFO utility. */
94 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED)
96 struct any_reader *any_reader;
97 struct file_handle *h;
100 struct casereader *reader;
101 struct any_read_info info;
109 lex_match (lexer, T_SLASH);
111 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
113 lex_match (lexer, T_EQUALS);
116 h = fh_parse (lexer, FH_REF_FILE, NULL);
120 else if (lex_match_id (lexer, "ENCODING"))
122 lex_match (lexer, T_EQUALS);
124 if (!lex_force_string (lexer))
128 encoding = ss_xstrdup (lex_tokss (lexer));
138 lex_sbc_missing ("FILE");
142 any_reader = any_reader_open (h);
149 if (encoding && !strcasecmp (encoding, "detect"))
151 char **titles, **strings;
156 pool = pool_create ();
157 n_strings = any_reader_get_strings (any_reader, pool,
158 &titles, &ids, &strings);
159 any_reader_close (any_reader);
161 report_encodings (h, pool, titles, ids, strings, n_strings);
169 reader = any_reader_decode (any_reader, encoding, &d, &info);
172 casereader_destroy (reader);
174 t = tab_create (2, 11 + (info.product_ext != NULL));
176 tab_vline (t, TAL_GAP, 1, 0, 8);
178 tab_text (t, 0, r, TAB_LEFT, _("File:"));
179 tab_text (t, 1, r++, TAB_LEFT, fh_get_file_name (h));
181 tab_text (t, 0, r, TAB_LEFT, _("Label:"));
183 const char *label = dict_get_label (d);
185 label = _("No label.");
186 tab_text (t, 1, r++, TAB_LEFT, label);
189 tab_text (t, 0, r, TAB_LEFT, _("Created:"));
190 tab_text_format (t, 1, r++, TAB_LEFT, "%s %s by %s",
191 info.creation_date, info.creation_time, info.product);
193 if (info.product_ext)
195 tab_text (t, 0, r, TAB_LEFT, _("Product:"));
196 tab_text (t, 1, r++, TAB_LEFT, info.product_ext);
199 tab_text (t, 0, r, TAB_LEFT, _("Integer Format:"));
200 tab_text (t, 1, r++, TAB_LEFT,
201 info.integer_format == INTEGER_MSB_FIRST ? _("Big Endian")
202 : info.integer_format == INTEGER_LSB_FIRST ? _("Little Endian")
205 tab_text (t, 0, r, TAB_LEFT, _("Real Format:"));
206 tab_text (t, 1, r++, TAB_LEFT,
207 info.float_format == FLOAT_IEEE_DOUBLE_LE ? _("IEEE 754 LE.")
208 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? _("IEEE 754 BE.")
209 : info.float_format == FLOAT_VAX_D ? _("VAX D.")
210 : info.float_format == FLOAT_VAX_G ? _("VAX G.")
211 : info.float_format == FLOAT_Z_LONG ? _("IBM 390 Hex Long.")
214 tab_text (t, 0, r, TAB_LEFT, _("Variables:"));
215 tab_text_format (t, 1, r++, TAB_LEFT, "%zu", dict_get_var_cnt (d));
217 tab_text (t, 0, r, TAB_LEFT, _("Cases:"));
218 if (info.case_cnt == -1)
219 tab_text (t, 1, r, TAB_LEFT, _("Unknown"));
221 tab_text_format (t, 1, r, TAB_LEFT, "%ld", (long int) info.case_cnt);
224 tab_text (t, 0, r, TAB_LEFT, _("Type:"));
225 tab_text (t, 1, r++, TAB_LEFT, gettext (info.klass->name));
227 tab_text (t, 0, r, TAB_LEFT, _("Weight:"));
229 struct variable *weight_var = dict_get_weight (d);
230 tab_text (t, 1, r++, TAB_LEFT,
232 ? var_get_name (weight_var) : _("Not weighted.")));
235 tab_text (t, 0, r, TAB_LEFT, _("Compression:"));
236 tab_text_format (t, 1, r++, TAB_LEFT,
237 info.compression == ANY_COMP_NONE ? _("None")
238 : info.compression == ANY_COMP_SIMPLE ? "SAV"
241 tab_text (t, 0, r, TAB_LEFT, _("Encoding:"));
242 tab_text (t, 1, r++, TAB_LEFT, dict_get_encoding (d));
246 size_t n_vars = dict_get_var_cnt (d);
247 const struct variable **vars = xnmalloc (n_vars, sizeof *vars);
248 for (size_t i = 0; i < dict_get_var_cnt (d); i++)
249 vars[i] = dict_get_var (d, i);
250 display_variables (vars, n_vars, DF_ALL_VARIABLE);
251 display_value_labels (vars, n_vars);
252 display_attributes (dict_get_attributes (dataset_dict (ds)),
253 vars, n_vars, DF_ATTRIBUTES);
259 any_read_info_destroy (&info);
268 /* DISPLAY utility. */
270 static void display_macros (void);
271 static void display_documents (const struct dictionary *dict);
272 static void display_vectors (const struct dictionary *dict, int sorted);
275 cmd_display (struct lexer *lexer, struct dataset *ds)
277 /* Whether to sort the list of variables alphabetically. */
280 /* Variables to display. */
282 const struct variable **vl;
284 if (lex_match_id (lexer, "MACROS"))
286 else if (lex_match_id (lexer, "DOCUMENTS"))
287 display_documents (dataset_dict (ds));
288 else if (lex_match_id (lexer, "FILE"))
290 if (!lex_force_match_id (lexer, "LABEL"))
292 if (dict_get_label (dataset_dict (ds)) == NULL)
293 tab_output_text (TAB_LEFT,
294 _("The active dataset does not have a file label."));
296 tab_output_text_format (TAB_LEFT, _("File label: %s"),
297 dict_get_label (dataset_dict (ds)));
303 sorted = lex_match_id (lexer, "SORTED");
305 if (lex_match_id (lexer, "VECTORS"))
307 display_vectors (dataset_dict(ds), sorted);
310 else if (lex_match_id (lexer, "SCRATCH"))
312 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
322 static const struct subcommand subcommands[] =
324 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
325 {"ATTRIBUTES", DF_ATTRIBUTES},
326 {"DICTIONARY", (DF_NAME | DF_POSITION | DF_LABEL
327 | DF_MEASUREMENT_LEVEL | DF_ROLE | DF_WIDTH
328 | DF_ALIGNMENT | DF_PRINT_FORMAT
329 | DF_WRITE_FORMAT | DF_MISSING_VALUES
331 {"INDEX", DF_NAME | DF_POSITION},
332 {"LABELS", DF_NAME | DF_POSITION | DF_LABEL},
334 {"VARIABLES", (DF_NAME | DF_POSITION | DF_PRINT_FORMAT
335 | DF_WRITE_FORMAT | DF_MISSING_VALUES)},
338 const struct subcommand *sbc;
339 struct dictionary *dict = dataset_dict (ds);
342 for (sbc = subcommands; sbc->name != NULL; sbc++)
343 if (lex_match_id (lexer, sbc->name))
349 lex_match (lexer, T_SLASH);
350 lex_match_id (lexer, "VARIABLES");
351 lex_match (lexer, T_EQUALS);
353 if (lex_token (lexer) != T_ENDCMD)
355 if (!parse_variables_const (lexer, dict, &vl, &n, PV_NONE))
362 dict_get_vars (dict, &vl, &n, 0);
367 sort (vl, n, sizeof *vl, (sorted
368 ? compare_var_ptrs_by_name
369 : compare_var_ptrs_by_dict_index), NULL);
371 int variable_flags = flags & DF_ALL_VARIABLE;
373 display_variables (vl, n, variable_flags);
375 if (flags & DF_VALUE_LABELS)
376 display_value_labels (vl, n);
378 int attribute_flags = flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES);
380 display_attributes (dict_get_attributes (dataset_dict (ds)),
381 vl, n, attribute_flags);
384 msg (SW, _("No variables to display."));
393 display_macros (void)
395 tab_output_text (TAB_LEFT, _("Macros not supported."));
399 display_documents (const struct dictionary *dict)
401 const struct string_array *documents = dict_get_documents (dict);
403 if (string_array_is_empty (documents))
404 tab_output_text (TAB_LEFT, _("The active dataset dictionary does not "
405 "contain any documents."));
410 tab_output_text (TAB_LEFT | TAT_TITLE,
411 _("Documents in the active dataset:"));
412 for (i = 0; i < dict_get_document_line_cnt (dict); i++)
413 tab_output_text (TAB_LEFT | TAB_FIX, dict_get_document_line (dict, i));
418 display_variables (const struct variable **vl, size_t n, int flags)
420 int nc = count_one_bits (flags);
421 struct tab_table *t = tab_create (nc, n + 1);
422 tab_title (t, "%s", _("Variables"));
423 tab_headers (t, 0, 0, 1, 0);
424 tab_hline (t, TAL_2, 0, nc - 1, 1);
431 static const struct heading headings[] = {
432 { DF_NAME, N_("Name") },
433 { DF_POSITION, N_("Position") },
434 { DF_LABEL, N_("Label") },
435 { DF_MEASUREMENT_LEVEL, N_("Measurement Level") },
436 { DF_ROLE, N_("Role") },
437 { DF_WIDTH, N_("Width") },
438 { DF_ALIGNMENT, N_("Alignment") },
439 { DF_PRINT_FORMAT, N_("Print Format") },
440 { DF_WRITE_FORMAT, N_("Write Format") },
441 { DF_MISSING_VALUES, N_("Missing Values") },
443 for (size_t i = 0, x = 0; i < sizeof headings / sizeof *headings; i++)
444 if (flags & headings[i].flag)
445 tab_text (t, x++, 0, TAB_LEFT | TAT_TITLE, gettext (headings[i].title));
447 for (size_t i = 0; i < n; i++)
449 const struct variable *v = vl[i];
453 tab_text (t, x++, y, TAB_LEFT, var_get_name (v));
454 if (flags & DF_POSITION)
456 char s[INT_BUFSIZE_BOUND (size_t)];
458 sprintf (s, "%zu", var_get_dict_index (v) + 1);
459 tab_text (t, x++, y, TAB_LEFT, s);
461 if (flags & DF_LABEL)
463 const char *label = var_get_label (v);
465 tab_text (t, x, y, TAB_LEFT, label);
468 if (flags & DF_MEASUREMENT_LEVEL)
469 tab_text (t, x++, y, TAB_LEFT,
470 measure_to_string (var_get_measure (v)));
472 tab_text (t, x++, y, TAB_LEFT,
473 var_role_to_string (var_get_role (v)));
474 if (flags & DF_WIDTH)
476 char s[INT_BUFSIZE_BOUND (int)];
477 sprintf (s, "%d", var_get_display_width (v));
478 tab_text (t, x++, y, TAB_RIGHT, s);
480 if (flags & DF_ALIGNMENT)
481 tab_text (t, x++, y, TAB_LEFT,
482 alignment_to_string (var_get_alignment (v)));
483 if (flags & DF_PRINT_FORMAT)
485 const struct fmt_spec *print = var_get_print_format (v);
486 char s[FMT_STRING_LEN_MAX + 1];
488 tab_text (t, x++, y, TAB_LEFT, fmt_to_string (print, s));
490 if (flags & DF_WRITE_FORMAT)
492 const struct fmt_spec *write = var_get_write_format (v);
493 char s[FMT_STRING_LEN_MAX + 1];
495 tab_text (t, x++, y, TAB_LEFT, fmt_to_string (write, s));
497 if (flags & DF_MISSING_VALUES)
499 const struct missing_values *mv = var_get_missing_values (v);
501 struct string s = DS_EMPTY_INITIALIZER;
502 if (mv_has_range (mv))
505 mv_get_range (mv, &x, &y);
507 ds_put_format (&s, "LOWEST THRU %.*g", DBL_DIG + 1, y);
508 else if (y == HIGHEST)
509 ds_put_format (&s, "%.*g THRU HIGHEST", DBL_DIG + 1, x);
511 ds_put_format (&s, "%.*g THRU %.*g",
515 for (size_t j = 0; j < mv_n_values (mv); j++)
517 const union value *value = mv_get_value (mv, j);
518 if (!ds_is_empty (&s))
519 ds_put_cstr (&s, "; ");
520 if (var_is_numeric (v))
521 ds_put_format (&s, "%.*g", DBL_DIG + 1, value->f);
524 int width = var_get_width (v);
525 int mv_width = MIN (width, MV_MAX_STRING);
527 ds_put_byte (&s, '"');
528 memcpy (ds_put_uninit (&s, mv_width),
529 value_str (value, width), mv_width);
530 ds_put_byte (&s, '"');
533 if (!ds_is_empty (&s))
534 tab_text (t, x, y, TAB_LEFT, ds_cstr (&s));
546 display_value_labels (const struct variable **vars, size_t n_vars)
548 size_t n_value_labels = 0;
549 for (size_t i = 0; i < n_vars; i++)
550 n_value_labels += val_labs_count (var_get_value_labels (vars[i]));
554 struct tab_table *t = tab_create (3, n_value_labels + 1);
555 tab_title (t, "%s", _("Value Labels"));
556 tab_headers (t, 0, 0, 1, 0);
557 tab_hline (t, TAL_2, 0, 2, 1);
558 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
559 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Value"));
560 tab_text (t, 2, 0, TAB_LEFT | TAT_TITLE, _("Label"));
563 for (size_t i = 0; i < n_vars; i++)
565 const struct val_labs *val_labs = var_get_value_labels (vars[i]);
566 size_t n_labels = val_labs_count (val_labs);
570 tab_joint_text (t, 0, y, 0, y + (n_labels - 1), TAB_LEFT,
571 var_get_name (vars[i]));
573 const struct val_lab **labels = val_labs_sorted (val_labs);
574 for (size_t j = 0; j < n_labels; j++)
576 const struct val_lab *vl = labels[j];
578 tab_value (t, 1, y, TAB_NONE, &vl->value, vars[i], NULL);
579 tab_text (t, 2, y, TAB_LEFT, val_lab_get_escaped_label (vl));
588 is_at_name (const char *name)
590 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
594 count_attributes (const struct attrset *set, int flags)
596 struct attrset_iterator i;
597 struct attribute *attr;
601 for (attr = attrset_first (set, &i); attr != NULL;
602 attr = attrset_next (set, &i))
603 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
604 n_attrs += attribute_get_n_values (attr);
609 display_attrset (const char *name, const struct attrset *set, int flags,
610 struct tab_table *t, int y)
612 size_t n_total = count_attributes (set, flags);
616 tab_joint_text (t, 0, y, 0, y + (n_total - 1), TAB_LEFT, name);
618 size_t n_attrs = attrset_count (set);
619 struct attribute **attrs = attrset_sorted (set);
620 for (size_t i = 0; i < n_attrs; i++)
622 const struct attribute *attr = attrs[i];
623 const char *name = attribute_get_name (attr);
625 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
628 size_t n_values = attribute_get_n_values (attr);
629 for (size_t j = 0; j < n_values; j++)
632 tab_text_format (t, 1, y, TAB_LEFT, "%s[%zu]", name, j + 1);
634 tab_text (t, 1, y, TAB_LEFT, name);
635 tab_text (t, 2, y, TAB_LEFT, attribute_get_value (attr, j));
645 display_attributes (const struct attrset *dict_attrset,
646 const struct variable **vars, size_t n_vars, int flags)
648 size_t n_attributes = count_attributes (dict_attrset, flags);
649 for (size_t i = 0; i < n_vars; i++)
650 n_attributes += count_attributes (var_get_attributes (vars[i]), flags);
654 struct tab_table *t = tab_create (3, n_attributes + 1);
655 tab_title (t, "%s", _("Variable and Dataset Attributes"));
656 tab_headers (t, 0, 0, 1, 0);
657 tab_hline (t, TAL_2, 0, 2, 1);
658 tab_text (t, 0, 0, TAB_LEFT | TAT_TITLE, _("Variable"));
659 tab_text (t, 1, 0, TAB_LEFT | TAT_TITLE, _("Name"));
660 tab_text (t, 2, 0, TAB_LEFT | TAT_TITLE, _("Value"));
662 int y = display_attrset (_("(dataset)"), dict_attrset, flags, t, 1);
663 for (size_t i = 0; i < n_vars; i++)
664 y = display_attrset (var_get_name (vars[i]), var_get_attributes (vars[i]),
670 /* Display a list of vectors. If SORTED is nonzero then they are
671 sorted alphabetically. */
673 display_vectors (const struct dictionary *dict, int sorted)
675 const struct vector **vl;
682 nvec = dict_get_vector_cnt (dict);
685 msg (SW, _("No vectors defined."));
689 vl = xnmalloc (nvec, sizeof *vl);
691 for (i = 0; i < nvec; i++)
693 vl[i] = dict_get_vector (dict, i);
694 nrow += vector_get_var_cnt (vl[i]);
697 qsort (vl, nvec, sizeof *vl, compare_vector_ptrs_by_name);
699 t = tab_create (4, nrow + 1);
700 tab_headers (t, 0, 0, 1, 0);
701 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 3, nrow);
702 tab_box (t, -1, -1, -1, TAL_1, 0, 0, 3, nrow);
703 tab_hline (t, TAL_2, 0, 3, 1);
704 tab_text (t, 0, 0, TAT_TITLE | TAB_LEFT, _("Vector"));
705 tab_text (t, 1, 0, TAT_TITLE | TAB_LEFT, _("Position"));
706 tab_text (t, 2, 0, TAT_TITLE | TAB_LEFT, _("Variable"));
707 tab_text (t, 3, 0, TAT_TITLE | TAB_LEFT, _("Print Format"));
710 for (i = 0; i < nvec; i++)
712 const struct vector *vec = vl[i];
715 tab_joint_text (t, 0, row, 0, row + vector_get_var_cnt (vec) - 1,
716 TAB_LEFT, vector_get_name (vl[i]));
718 for (j = 0; j < vector_get_var_cnt (vec); j++)
720 struct variable *var = vector_get_var (vec, j);
721 char fmt_string[FMT_STRING_LEN_MAX + 1];
722 fmt_to_string (var_get_print_format (var), fmt_string);
724 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
725 tab_text (t, 2, row, TAB_LEFT, var_get_name (var));
726 tab_text (t, 3, row, TAB_LEFT, fmt_string);
729 tab_hline (t, TAL_1, 0, 3, row);
737 /* Encoding analysis. */
739 static const char *encoding_names[] = {
740 /* These encodings are from http://encoding.spec.whatwg.org/, as retrieved
741 February 2014. Encodings not supported by glibc and encodings relevant
742 only to HTML have been removed. */
776 /* Added by user request. */
780 #define N_ENCODING_NAMES (sizeof encoding_names / sizeof *encoding_names)
790 recode_strings (struct pool *pool,
791 char **strings, bool *ids, size_t n,
792 const char *encoding)
797 utf8_strings = pool_alloc (pool, n * sizeof *utf8_strings);
798 for (i = 0; i < n; i++)
800 struct substring utf8;
803 error = recode_pedantically ("UTF-8", encoding, ss_cstr (strings[i]),
807 ss_rtrim (&utf8, ss_cstr (" "));
808 utf8.string[utf8.length] = '\0';
810 if (ids[i] && !id_is_plausible (utf8.string, false))
817 utf8_strings[i] = utf8.string;
823 static struct encoding *
824 find_duplicate_encoding (struct encoding *encodings, size_t n_encodings,
825 char **utf8_strings, size_t n_strings,
830 for (e = encodings; e < &encodings[n_encodings]; e++)
837 for (i = 0; i < n_strings; i++)
838 if (strcmp (utf8_strings[i], e->utf8_strings[i]))
849 all_equal (const struct encoding *encodings, size_t n_encodings,
855 s0 = encodings[0].utf8_strings[string_idx];
856 for (i = 1; i < n_encodings; i++)
857 if (strcmp (s0, encodings[i].utf8_strings[string_idx]))
864 equal_prefix (const struct encoding *encodings, size_t n_encodings,
871 s0 = encodings[0].utf8_strings[string_idx];
872 prefix = strlen (s0);
873 for (i = 1; i < n_encodings; i++)
875 const char *si = encodings[i].utf8_strings[string_idx];
878 for (j = 0; j < prefix; j++)
888 while (prefix > 0 && s0[prefix - 1] != ' ')
894 equal_suffix (const struct encoding *encodings, size_t n_encodings,
902 s0 = encodings[0].utf8_strings[string_idx];
903 s0_len = strlen (s0);
905 for (i = 1; i < n_encodings; i++)
907 const char *si = encodings[i].utf8_strings[string_idx];
908 size_t si_len = strlen (si);
913 for (j = 0; j < suffix; j++)
914 if (s0[s0_len - j - 1] != si[si_len - j - 1])
923 while (suffix > 0 && s0[s0_len - suffix] != ' ')
929 report_encodings (const struct file_handle *h, struct pool *pool,
930 char **titles, bool *ids, char **strings, size_t n_strings)
932 struct encoding encodings[N_ENCODING_NAMES];
933 size_t n_encodings, n_unique_strings;
939 for (i = 0; i < N_ENCODING_NAMES; i++)
945 utf8_strings = recode_strings (pool, strings, ids, n_strings,
950 /* Hash utf8_strings. */
952 for (j = 0; j < n_strings; j++)
953 hash = hash_string (utf8_strings[j], hash);
955 /* If there's a duplicate encoding, just mark it. */
956 e = find_duplicate_encoding (encodings, n_encodings,
957 utf8_strings, n_strings, hash);
960 e->encodings |= UINT64_C (1) << i;
964 e = &encodings[n_encodings++];
965 e->encodings = UINT64_C (1) << i;
966 e->utf8_strings = utf8_strings;
971 msg (SW, _("No valid encodings found."));
975 t = tab_create (2, n_encodings + 1);
976 tab_title (t, _("Usable encodings for %s."), fh_get_name (h));
977 tab_caption (t, _("Encodings that can successfully read %s (by specifying "
978 "the encoding name on the GET command's ENCODING "
979 "subcommand). Encodings that yield identical text are "
980 "listed together."), fh_get_name (h));
981 tab_headers (t, 1, 0, 1, 0);
982 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 1, n_encodings);
983 tab_hline (t, TAL_1, 0, 1, 1);
984 tab_text (t, 0, 0, TAB_RIGHT, "#");
985 tab_text (t, 1, 0, TAB_LEFT, _("Encodings"));
986 for (i = 0; i < n_encodings; i++)
991 for (j = 0; j < 64; j++)
992 if (encodings[i].encodings & (UINT64_C (1) << j))
993 ds_put_format (&s, "%s, ", encoding_names[j]);
994 ds_chomp (&s, ss_cstr (", "));
996 tab_text_format (t, 0, i + 1, TAB_RIGHT, "%zu", i + 1);
997 tab_text (t, 1, i + 1, TAB_LEFT, ds_cstr (&s));
1002 n_unique_strings = 0;
1003 for (i = 0; i < n_strings; i++)
1004 if (!all_equal (encodings, n_encodings, i))
1006 if (!n_unique_strings)
1009 t = tab_create (3, (n_encodings * n_unique_strings) + 1);
1010 tab_title (t, _("%s encoded text strings."), fh_get_name (h));
1011 tab_caption (t, _("Text strings in the file dictionary that the previously "
1012 "listed encodings interpret differently, along with the "
1013 "interpretations."));
1014 tab_headers (t, 1, 0, 1, 0);
1015 tab_box (t, TAL_1, TAL_1, -1, -1, 0, 0, 2, n_encodings * n_unique_strings);
1016 tab_hline (t, TAL_1, 0, 2, 1);
1018 tab_text (t, 0, 0, TAB_LEFT, _("Purpose"));
1019 tab_text (t, 1, 0, TAB_RIGHT, "#");
1020 tab_text (t, 2, 0, TAB_LEFT, _("Text"));
1023 for (i = 0; i < n_strings; i++)
1024 if (!all_equal (encodings, n_encodings, i))
1026 int prefix = equal_prefix (encodings, n_encodings, i);
1027 int suffix = equal_suffix (encodings, n_encodings, i);
1029 tab_joint_text (t, 0, row, 0, row + n_encodings - 1,
1030 TAB_LEFT, titles[i]);
1031 tab_hline (t, TAL_1, 0, 2, row);
1032 for (j = 0; j < n_encodings; j++)
1034 const char *s = encodings[j].utf8_strings[i] + prefix;
1036 tab_text_format (t, 1, row, TAB_RIGHT, "%zu", j + 1);
1037 if (prefix || suffix)
1039 size_t len = strlen (s) - suffix;
1040 struct string entry;
1042 ds_init_empty (&entry);
1044 ds_put_cstr (&entry, "...");
1045 ds_put_substring (&entry, ss_buffer (s, len));
1047 ds_put_cstr (&entry, "...");
1048 tab_text (t, 2, row, TAB_LEFT, ds_cstr (&entry));
1051 tab_text (t, 2, row, TAB_LEFT, s);