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/varset.h"
34 #include "data/vector.h"
35 #include "language/command.h"
36 #include "language/commands/file-handle.h"
37 #include "language/lexer/lexer.h"
38 #include "language/lexer/macro.h"
39 #include "language/lexer/variable-parser.h"
40 #include "libpspp/array.h"
41 #include "libpspp/hash-functions.h"
42 #include "libpspp/i18n.h"
43 #include "libpspp/message.h"
44 #include "libpspp/misc.h"
45 #include "libpspp/pool.h"
46 #include "libpspp/string-array.h"
47 #include "output/pivot-table.h"
48 #include "output/output-item.h"
50 #include "gl/count-one-bits.h"
51 #include "gl/localcharset.h"
52 #include "gl/intprops.h"
53 #include "gl/minmax.h"
54 #include "gl/xalloc.h"
57 #define _(msgid) gettext (msgid)
58 #define N_(msgid) (msgid)
60 /* Information to include in displaying a dictionary. */
67 DF_MEASUREMENT_LEVEL = 1 << 3,
70 DF_ALIGNMENT = 1 << 6,
71 DF_PRINT_FORMAT = 1 << 7,
72 DF_WRITE_FORMAT = 1 << 8,
73 DF_MISSING_VALUES = 1 << 9,
74 #define DF_ALL_VARIABLE ((1 << 10) - 1)
76 /* Value labels table. */
77 DF_VALUE_LABELS = 1 << 10,
79 /* Attribute table. */
80 DF_AT_ATTRIBUTES = 1 << 11, /* Attributes whose names begin with @. */
81 DF_ATTRIBUTES = 1 << 12, /* All other attributes. */
84 static void display_variables (const struct variable **, size_t, int flags);
85 static void display_value_labels (const struct variable **, size_t);
86 static void display_attributes (const struct attrset *,
87 const struct variable **, size_t, int flags);
89 static void report_encodings (const struct file_handle *, struct pool *,
90 char **titles, bool *ids,
91 char **strings, size_t n_strings);
93 static char *get_documents_as_string (const struct dictionary *);
96 add_row (struct pivot_table *table, const char *attribute,
97 struct pivot_value *value)
99 int row = pivot_category_create_leaf (table->dimensions[0]->root,
100 pivot_value_new_text (attribute));
102 pivot_table_put1 (table, row, value);
105 /* SYSFILE INFO utility. */
107 cmd_sysfile_info (struct lexer *lexer, struct dataset *ds)
109 struct any_reader *any_reader;
110 struct file_handle *h;
111 struct dictionary *d;
112 struct casereader *reader;
113 struct any_read_info info;
120 lex_match (lexer, T_SLASH);
122 if (lex_match_id (lexer, "FILE") || lex_is_string (lexer))
124 lex_match (lexer, T_EQUALS);
127 h = fh_parse (lexer, FH_REF_FILE, NULL);
131 else if (lex_match_id (lexer, "ENCODING"))
133 lex_match (lexer, T_EQUALS);
135 if (!lex_force_string (lexer))
139 encoding = ss_xstrdup (lex_tokss (lexer));
149 lex_sbc_missing (lexer, "FILE");
153 any_reader = any_reader_open (h);
160 if (encoding && !strcasecmp (encoding, "detect"))
162 char **titles, **strings;
167 pool = pool_create ();
168 n_strings = any_reader_get_strings (any_reader, pool,
169 &titles, &ids, &strings);
170 any_reader_close (any_reader);
172 report_encodings (h, pool, titles, ids, strings, n_strings);
180 reader = any_reader_decode (any_reader, encoding, &d, &info);
183 casereader_destroy (reader);
185 struct pivot_table *table = pivot_table_create (N_("File Information"));
186 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Attribute"));
188 add_row (table, N_("File"),
189 pivot_value_new_user_text (fh_get_file_name (h), -1));
191 const char *label = dict_get_label (d);
192 add_row (table, N_("Label"),
193 label ? pivot_value_new_user_text (label, -1) : NULL);
195 add_row (table, N_("Created"),
196 pivot_value_new_user_text_nocopy (
197 xasprintf ("%s %s by %s", info.creation_date,
198 info.creation_time, info.product)));
200 if (info.product_ext)
201 add_row (table, N_("Product"),
202 pivot_value_new_user_text (info.product_ext, -1));
204 add_row (table, N_("Integer Format"),
205 pivot_value_new_text (
206 info.integer_format == INTEGER_MSB_FIRST ? N_("Big Endian")
207 : info.integer_format == INTEGER_LSB_FIRST ? N_("Little Endian")
210 add_row (table, N_("Real Format"),
211 pivot_value_new_text (
212 info.float_format == FLOAT_IEEE_DOUBLE_LE ? N_("IEEE 754 LE.")
213 : info.float_format == FLOAT_IEEE_DOUBLE_BE ? N_("IEEE 754 BE.")
214 : info.float_format == FLOAT_VAX_D ? N_("VAX D.")
215 : info.float_format == FLOAT_VAX_G ? N_("VAX G.")
216 : info.float_format == FLOAT_Z_LONG ? N_("IBM 390 Hex Long.")
219 add_row (table, N_("Variables"),
220 pivot_value_new_integer (dict_get_n_vars (d)));
222 add_row (table, N_("Cases"),
224 ? pivot_value_new_text (N_("Unknown"))
225 : pivot_value_new_integer (info.n_cases)));
227 add_row (table, N_("Type"),
228 pivot_value_new_text (info.klass->name));
230 struct variable *weight_var = dict_get_weight (d);
231 add_row (table, N_("Weight"),
233 ? pivot_value_new_variable (weight_var)
234 : pivot_value_new_text (N_("Not weighted"))));
236 add_row (table, N_("Compression"),
237 (info.compression == ANY_COMP_NONE
238 ? pivot_value_new_text (N_("None"))
239 : pivot_value_new_user_text (
240 info.compression == ANY_COMP_SIMPLE ? "SAV" : "ZSAV", -1)));
242 add_row (table, N_("Encoding"),
243 pivot_value_new_user_text (dict_get_encoding (d), -1));
245 if (dict_get_document_n_lines (d) > 0)
246 add_row (table, N_("Documents"),
247 pivot_value_new_user_text_nocopy (get_documents_as_string (d)));
249 pivot_table_submit (table);
251 size_t n_vars = dict_get_n_vars (d);
252 const struct variable **vars = xnmalloc (n_vars, sizeof *vars);
253 for (size_t i = 0; i < dict_get_n_vars (d); i++)
254 vars[i] = dict_get_var (d, i);
255 display_variables (vars, n_vars, DF_ALL_VARIABLE);
256 display_value_labels (vars, n_vars);
257 display_attributes (dict_get_attributes (dataset_dict (ds)),
258 vars, n_vars, DF_ATTRIBUTES);
265 any_read_info_destroy (&info);
274 /* DISPLAY utility. */
276 static void display_documents (const struct dictionary *dict);
277 static void display_vectors (const struct dictionary *dict, int sorted);
280 cmd_display (struct lexer *lexer, struct dataset *ds)
282 /* Variables to display. */
284 const struct variable **vl;
286 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"))
293 const char *label = dict_get_label (dataset_dict (ds));
295 struct pivot_table *table = pivot_table_create (N_("File Label"));
296 pivot_dimension_create (table, PIVOT_AXIS_ROW, N_("Label"),
298 pivot_table_put1 (table, 0,
299 (label ? pivot_value_new_user_text (label, -1)
300 : pivot_value_new_text (N_("(none)"))));
301 pivot_table_submit (table);
307 bool sorted = lex_match_id (lexer, "SORTED");
309 if (lex_match_id (lexer, "VECTORS"))
311 display_vectors (dataset_dict(ds), sorted);
314 else if (lex_match_id (lexer, "SCRATCH"))
316 dict_get_vars (dataset_dict (ds), &vl, &n, DC_ORDINARY);
326 static const struct subcommand subcommands[] =
328 {"@ATTRIBUTES", DF_ATTRIBUTES | DF_AT_ATTRIBUTES},
329 {"ATTRIBUTES", DF_ATTRIBUTES},
330 {"DICTIONARY", (DF_NAME | DF_POSITION | DF_LABEL
331 | DF_MEASUREMENT_LEVEL | DF_ROLE | DF_WIDTH
332 | DF_ALIGNMENT | DF_PRINT_FORMAT
333 | DF_WRITE_FORMAT | DF_MISSING_VALUES
335 {"INDEX", DF_NAME | DF_POSITION},
336 {"LABELS", DF_NAME | DF_POSITION | DF_LABEL},
338 {"VARIABLES", (DF_NAME | DF_POSITION | DF_PRINT_FORMAT
339 | DF_WRITE_FORMAT | DF_MISSING_VALUES)},
342 const struct subcommand *sbc;
343 struct dictionary *dict = dataset_dict (ds);
346 for (sbc = subcommands; sbc->name != NULL; sbc++)
347 if (lex_match_id (lexer, sbc->name))
353 lex_match (lexer, T_SLASH);
354 lex_match_id (lexer, "VARIABLES");
355 lex_match (lexer, T_EQUALS);
357 if (lex_token (lexer) != T_ENDCMD)
359 if (!parse_variables_const (lexer, dict, &vl, &n, PV_NONE))
366 dict_get_vars (dict, &vl, &n, 0);
371 sort (vl, n, sizeof *vl, (sorted
372 ? compare_var_ptrs_by_name
373 : compare_var_ptrs_by_dict_index), NULL);
375 int variable_flags = flags & DF_ALL_VARIABLE;
377 display_variables (vl, n, variable_flags);
379 if (flags & DF_VALUE_LABELS)
380 display_value_labels (vl, n);
382 int attribute_flags = flags & (DF_ATTRIBUTES | DF_AT_ATTRIBUTES);
384 display_attributes (dict_get_attributes (dataset_dict (ds)),
385 vl, n, attribute_flags);
388 msg (SN, _("No variables to display."));
397 compare_macros_by_name (const void *a_, const void *b_, const void *aux UNUSED)
399 const struct macro *const *ap = a_;
400 const struct macro *const *bp = b_;
401 const struct macro *a = *ap;
402 const struct macro *b = *bp;
404 return utf8_strcasecmp (a->name, b->name);
408 cmd_display_macros (struct lexer *lexer, struct dataset *ds UNUSED)
410 const struct macro_set *set = lex_get_macros (lexer);
412 if (hmap_is_empty (&set->macros))
414 msg (SN, _("No macros to display."));
418 const struct macro **macros = xnmalloc (hmap_count (&set->macros),
421 const struct macro *m;
422 HMAP_FOR_EACH (m, struct macro, hmap_node, &set->macros)
424 assert (n == hmap_count (&set->macros));
425 sort (macros, n, sizeof *macros, compare_macros_by_name, NULL);
427 struct pivot_table *table = pivot_table_create (N_("Macros"));
429 struct pivot_dimension *attributes = pivot_dimension_create (
430 table, PIVOT_AXIS_COLUMN, N_("Attributes"));
431 pivot_category_create_leaf (attributes->root,
432 pivot_value_new_text (N_("Source Location")));
434 struct pivot_dimension *names = pivot_dimension_create (
435 table, PIVOT_AXIS_ROW, N_("Name"));
436 names->root->show_label = true;
438 for (size_t i = 0; i < n; i++)
440 const struct macro *m = macros[i];
442 pivot_category_create_leaf (names->root,
443 pivot_value_new_user_text (m->name, -1));
445 struct string location = DS_EMPTY_INITIALIZER;
446 msg_location_format (m->location, &location);
449 pivot_value_new_user_text_nocopy (ds_steal_cstr (&location)));
452 pivot_table_submit (table);
460 cmd_display_variable_sets (struct lexer *lexer UNUSED, struct dataset *ds)
462 const struct dictionary *dict = dataset_dict (ds);
463 size_t n_varsets = dict_get_n_varsets (dict);
466 msg (SN, _("No variable sets defined."));
470 struct pivot_table *table = pivot_table_create (N_("Variable Sets"));
471 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Attributes"),
473 struct pivot_dimension *varset_dim = pivot_dimension_create (
474 table, PIVOT_AXIS_ROW, N_("Variable Set and Position"));
475 varset_dim->root->show_label = true;
477 for (size_t i = 0; i < n_varsets; i++)
479 const struct varset *vs = dict_get_varset (dict, i);
481 struct pivot_category *group = pivot_category_create_group__ (
482 varset_dim->root, pivot_value_new_user_text (
485 for (size_t j = 0; j < vs->n_vars; j++)
487 struct variable *var = vs->vars[j];
489 int row = pivot_category_create_leaf (
490 group, pivot_value_new_integer (j + 1));
492 pivot_table_put2 (table, 0, row, pivot_value_new_variable (var));
497 int row = pivot_category_create_leaf (
498 group, pivot_value_new_user_text ("n/a", -1));
500 pivot_table_put2 (table, 0, row,
501 pivot_value_new_text (N_("(empty)")));
505 pivot_table_submit (table);
511 get_documents_as_string (const struct dictionary *dict)
513 const struct string_array *documents = dict_get_documents (dict);
514 struct string s = DS_EMPTY_INITIALIZER;
515 for (size_t i = 0; i < documents->n; i++)
518 ds_put_byte (&s, '\n');
519 ds_put_cstr (&s, documents->strings[i]);
521 return ds_steal_cstr (&s);
525 display_documents (const struct dictionary *dict)
527 struct pivot_table *table = pivot_table_create (N_("Documents"));
528 struct pivot_dimension *d = pivot_dimension_create (
529 table, PIVOT_AXIS_COLUMN, N_("Documents"), N_("Document"));
530 d->hide_all_labels = true;
532 if (!dict_get_documents (dict)->n)
533 pivot_table_put1 (table, 0, pivot_value_new_text (N_("(none)")));
536 char *docs = get_documents_as_string (dict);
537 pivot_table_put1 (table, 0, pivot_value_new_user_text_nocopy (docs));
540 pivot_table_submit (table);
544 display_variables (const struct variable **vl, size_t n, int flags)
546 struct pivot_table *table = pivot_table_create (N_("Variables"));
548 struct pivot_dimension *attributes = pivot_dimension_create (
549 table, PIVOT_AXIS_COLUMN, N_("Attributes"));
556 static const struct heading headings[] = {
557 { DF_POSITION, N_("Position") },
558 { DF_LABEL, N_("Label") },
559 { DF_MEASUREMENT_LEVEL, N_("Measurement Level") },
560 { DF_ROLE, N_("Role") },
561 { DF_WIDTH, N_("Width") },
562 { DF_ALIGNMENT, N_("Alignment") },
563 { DF_PRINT_FORMAT, N_("Print Format") },
564 { DF_WRITE_FORMAT, N_("Write Format") },
565 { DF_MISSING_VALUES, N_("Missing Values") },
567 for (size_t i = 0; i < sizeof headings / sizeof *headings; i++)
568 if (flags & headings[i].flag)
569 pivot_category_create_leaf (attributes->root,
570 pivot_value_new_text (headings[i].title));
572 struct pivot_dimension *names = pivot_dimension_create (
573 table, PIVOT_AXIS_ROW, N_("Name"));
574 names->root->show_label = true;
576 for (size_t i = 0; i < n; i++)
578 const struct variable *v = vl[i];
580 struct pivot_value *name = pivot_value_new_variable (v);
581 name->variable.show = SETTINGS_VALUE_SHOW_VALUE;
582 int row = pivot_category_create_leaf (names->root, name);
585 if (flags & DF_POSITION)
586 pivot_table_put2 (table, x++, row, pivot_value_new_integer (
587 var_get_dict_index (v) + 1));
589 if (flags & DF_LABEL)
591 const char *label = var_get_label (v);
593 pivot_table_put2 (table, x, row,
594 pivot_value_new_user_text (label, -1));
598 if (flags & DF_MEASUREMENT_LEVEL)
601 pivot_value_new_text (measure_to_string (var_get_measure (v))));
606 pivot_value_new_text (var_role_to_string (var_get_role (v))));
608 if (flags & DF_WIDTH)
611 pivot_value_new_integer (var_get_display_width (v)));
613 if (flags & DF_ALIGNMENT)
616 pivot_value_new_text (alignment_to_string (
617 var_get_alignment (v))));
619 if (flags & DF_PRINT_FORMAT)
621 struct fmt_spec print = var_get_print_format (v);
622 char s[FMT_STRING_LEN_MAX + 1];
626 pivot_value_new_user_text (fmt_to_string (print, s), -1));
629 if (flags & DF_WRITE_FORMAT)
631 struct fmt_spec write = var_get_write_format (v);
632 char s[FMT_STRING_LEN_MAX + 1];
636 pivot_value_new_user_text (fmt_to_string (write, s), -1));
639 if (flags & DF_MISSING_VALUES)
641 char *s = mv_to_string (var_get_missing_values (v),
642 var_get_encoding (v));
646 pivot_value_new_user_text_nocopy (s));
652 pivot_table_submit (table);
656 any_value_labels (const struct variable **vars, size_t n_vars)
658 for (size_t i = 0; i < n_vars; i++)
659 if (val_labs_count (var_get_value_labels (vars[i])))
665 display_value_labels (const struct variable **vars, size_t n_vars)
667 if (!any_value_labels (vars, n_vars))
670 struct pivot_table *table = pivot_table_create (N_("Value Labels"));
672 pivot_dimension_create (table, PIVOT_AXIS_COLUMN,
673 N_("Label"), N_("Label"));
675 struct pivot_dimension *values = pivot_dimension_create (
676 table, PIVOT_AXIS_ROW, N_("Variable Value"));
677 values->root->show_label = true;
679 struct pivot_footnote *missing_footnote = pivot_table_create_footnote (
680 table, pivot_value_new_text (N_("User-missing value")));
682 for (size_t i = 0; i < n_vars; i++)
684 const struct val_labs *val_labs = var_get_value_labels (vars[i]);
685 size_t n_labels = val_labs_count (val_labs);
689 struct pivot_category *group = pivot_category_create_group__ (
690 values->root, pivot_value_new_variable (vars[i]));
692 const struct val_lab **labels = val_labs_sorted (val_labs);
693 for (size_t j = 0; j < n_labels; j++)
695 const struct val_lab *vl = labels[j];
697 struct pivot_value *value = pivot_value_new_var_value (
698 vars[i], &vl->value);
699 if (value->type == PIVOT_VALUE_NUMERIC)
700 value->numeric.show = SETTINGS_VALUE_SHOW_VALUE;
702 value->string.show = SETTINGS_VALUE_SHOW_VALUE;
703 if (var_is_value_missing (vars[i], &vl->value) == MV_USER)
704 pivot_value_add_footnote (value, missing_footnote);
705 int row = pivot_category_create_leaf (group, value);
707 struct pivot_value *label = pivot_value_new_var_value (
708 vars[i], &vl->value);
709 char *escaped_label = xstrdup (val_lab_get_escaped_label (vl));
710 if (label->type == PIVOT_VALUE_NUMERIC)
712 free (label->numeric.value_label);
713 label->numeric.value_label = escaped_label;
714 label->numeric.show = SETTINGS_VALUE_SHOW_LABEL;
718 free (label->string.value_label);
719 label->string.value_label = escaped_label;
720 label->string.show = SETTINGS_VALUE_SHOW_LABEL;
722 pivot_table_put2 (table, 0, row, label);
726 pivot_table_submit (table);
730 is_at_name (const char *name)
732 return name[0] == '@' || (name[0] == '$' && name[1] == '@');
736 count_attributes (const struct attrset *set, int flags)
738 struct attrset_iterator i;
739 struct attribute *attr;
743 for (attr = attrset_first (set, &i); attr != NULL;
744 attr = attrset_next (set, &i))
745 if (flags & DF_AT_ATTRIBUTES || !is_at_name (attribute_get_name (attr)))
746 n_attrs += attribute_get_n_values (attr);
751 display_attrset (struct pivot_table *table, struct pivot_value *set_name,
752 const struct attrset *set, int flags)
754 size_t n_total = count_attributes (set, flags);
757 pivot_value_destroy (set_name);
761 struct pivot_category *group = pivot_category_create_group__ (
762 table->dimensions[1]->root, set_name);
764 size_t n_attrs = attrset_count (set);
765 struct attribute **attrs = attrset_sorted (set);
766 for (size_t i = 0; i < n_attrs; i++)
768 const struct attribute *attr = attrs[i];
769 const char *name = attribute_get_name (attr);
771 if (!(flags & DF_AT_ATTRIBUTES) && is_at_name (name))
774 size_t n_values = attribute_get_n_values (attr);
775 for (size_t j = 0; j < n_values; j++)
777 int row = pivot_category_create_leaf (
780 ? pivot_value_new_user_text_nocopy (xasprintf (
781 "%s[%zu]", name, j + 1))
782 : pivot_value_new_user_text (name, -1)));
783 pivot_table_put2 (table, 0, row,
784 pivot_value_new_user_text (
785 attribute_get_value (attr, j), -1));
792 display_attributes (const struct attrset *dict_attrset,
793 const struct variable **vars, size_t n_vars, int flags)
795 struct pivot_table *table = pivot_table_create (
796 N_("Variable and Dataset Attributes"));
798 pivot_dimension_create (table, PIVOT_AXIS_COLUMN,
799 N_("Value"), N_("Value"));
801 struct pivot_dimension *variables = pivot_dimension_create (
802 table, PIVOT_AXIS_ROW, N_("Variable and Name"));
803 variables->root->show_label = true;
805 display_attrset (table, pivot_value_new_text (N_("(dataset)")),
806 dict_attrset, flags);
807 for (size_t i = 0; i < n_vars; i++)
808 display_attrset (table, pivot_value_new_variable (vars[i]),
809 var_get_attributes (vars[i]), flags);
811 if (pivot_table_is_empty (table))
812 pivot_table_unref (table);
814 pivot_table_submit (table);
817 /* Display a list of vectors. If SORTED is nonzero then they are
818 sorted alphabetically. */
820 display_vectors (const struct dictionary *dict, int sorted)
822 size_t n_vectors = dict_get_n_vectors (dict);
825 msg (SN, _("No vectors defined."));
829 const struct vector **vectors = xnmalloc (n_vectors, sizeof *vectors);
830 for (size_t i = 0; i < n_vectors; i++)
831 vectors[i] = dict_get_vector (dict, i);
833 qsort (vectors, n_vectors, sizeof *vectors, compare_vector_ptrs_by_name);
835 struct pivot_table *table = pivot_table_create (N_("Vectors"));
836 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Attributes"),
837 N_("Variable"), N_("Print Format"));
838 struct pivot_dimension *vector_dim = pivot_dimension_create (
839 table, PIVOT_AXIS_ROW, N_("Vector and Position"));
840 vector_dim->root->show_label = true;
842 for (size_t i = 0; i < n_vectors; i++)
844 const struct vector *vec = vectors[i];
846 struct pivot_category *group = pivot_category_create_group__ (
847 vector_dim->root, pivot_value_new_user_text (
848 vector_get_name (vectors[i]), -1));
850 for (size_t j = 0; j < vector_get_n_vars (vec); j++)
852 struct variable *var = vector_get_var (vec, j);
854 int row = pivot_category_create_leaf (
855 group, pivot_value_new_integer (j + 1));
857 pivot_table_put2 (table, 0, row, pivot_value_new_variable (var));
858 char fmt_string[FMT_STRING_LEN_MAX + 1];
859 fmt_to_string (var_get_print_format (var), fmt_string);
860 pivot_table_put2 (table, 1, row,
861 pivot_value_new_user_text (fmt_string, -1));
865 pivot_table_submit (table);
870 /* Encoding analysis. */
872 static const char *encoding_names[] = {
873 /* These encodings are from http://encoding.spec.whatwg.org/, as retrieved
874 February 2014. Encodings not supported by glibc and encodings relevant
875 only to HTML have been removed. */
909 /* Added by user request. */
913 #define N_ENCODING_NAMES (sizeof encoding_names / sizeof *encoding_names)
923 recode_strings (struct pool *pool,
924 char **strings, bool *ids, size_t n,
925 const char *encoding)
930 utf8_strings = pool_alloc (pool, n * sizeof *utf8_strings);
931 for (i = 0; i < n; i++)
933 struct substring utf8;
936 error = recode_pedantically ("UTF-8", encoding, ss_cstr (strings[i]),
940 ss_rtrim (&utf8, ss_cstr (" "));
941 utf8.string[utf8.length] = '\0';
943 if (ids[i] && !id_is_plausible (utf8.string))
950 utf8_strings[i] = utf8.string;
956 static struct encoding *
957 find_duplicate_encoding (struct encoding *encodings, size_t n_encodings,
958 char **utf8_strings, size_t n_strings,
963 for (e = encodings; e < &encodings[n_encodings]; e++)
970 for (i = 0; i < n_strings; i++)
971 if (strcmp (utf8_strings[i], e->utf8_strings[i]))
982 all_equal (const struct encoding *encodings, size_t n_encodings,
988 s0 = encodings[0].utf8_strings[string_idx];
989 for (i = 1; i < n_encodings; i++)
990 if (strcmp (s0, encodings[i].utf8_strings[string_idx]))
997 equal_prefix (const struct encoding *encodings, size_t n_encodings,
1004 s0 = encodings[0].utf8_strings[string_idx];
1005 prefix = strlen (s0);
1006 for (i = 1; i < n_encodings; i++)
1008 const char *si = encodings[i].utf8_strings[string_idx];
1011 for (j = 0; j < prefix; j++)
1021 while (prefix > 0 && s0[prefix - 1] != ' ')
1027 equal_suffix (const struct encoding *encodings, size_t n_encodings,
1035 s0 = encodings[0].utf8_strings[string_idx];
1036 s0_len = strlen (s0);
1038 for (i = 1; i < n_encodings; i++)
1040 const char *si = encodings[i].utf8_strings[string_idx];
1041 size_t si_len = strlen (si);
1044 if (si_len < suffix)
1046 for (j = 0; j < suffix; j++)
1047 if (s0[s0_len - j - 1] != si[si_len - j - 1])
1056 while (suffix > 0 && s0[s0_len - suffix] != ' ')
1062 report_encodings (const struct file_handle *h, struct pool *pool,
1063 char **titles, bool *ids, char **strings, size_t n_strings)
1065 struct encoding encodings[N_ENCODING_NAMES];
1066 size_t n_encodings, n_unique_strings;
1069 for (size_t i = 0; i < N_ENCODING_NAMES; i++)
1071 char **utf8_strings;
1075 utf8_strings = recode_strings (pool, strings, ids, n_strings,
1080 /* Hash utf8_strings. */
1082 for (size_t j = 0; j < n_strings; j++)
1083 hash = hash_string (utf8_strings[j], hash);
1085 /* If there's a duplicate encoding, just mark it. */
1086 e = find_duplicate_encoding (encodings, n_encodings,
1087 utf8_strings, n_strings, hash);
1090 e->encodings |= UINT64_C (1) << i;
1094 e = &encodings[n_encodings++];
1095 e->encodings = UINT64_C (1) << i;
1096 e->utf8_strings = utf8_strings;
1101 msg (SW, _("No valid encodings found."));
1105 /* Table of valid encodings. */
1106 struct pivot_table *table = pivot_table_create__ (
1107 pivot_value_new_text_format (N_("Usable encodings for %s."),
1108 fh_get_name (h)), "Usable Encodings");
1109 pivot_table_set_caption (
1110 table, pivot_value_new_text_format (
1111 N_("Encodings that can successfully read %s (by specifying the encoding "
1112 "name on the GET command's ENCODING subcommand). Encodings that "
1113 "yield identical text are listed together."),
1116 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Encodings"),
1118 struct pivot_dimension *number = pivot_dimension_create__ (
1119 table, PIVOT_AXIS_ROW, pivot_value_new_user_text ("#", -1));
1120 number->root->show_label = true;
1122 for (size_t i = 0; i < n_encodings; i++)
1124 struct string s = DS_EMPTY_INITIALIZER;
1125 for (size_t j = 0; j < 64; j++)
1126 if (encodings[i].encodings & (UINT64_C (1) << j))
1127 ds_put_format (&s, "%s, ", encoding_names[j]);
1128 ds_chomp (&s, ss_cstr (", "));
1130 int row = pivot_category_create_leaf (number->root,
1131 pivot_value_new_integer (i + 1));
1133 table, 0, row, pivot_value_new_user_text_nocopy (ds_steal_cstr (&s)));
1135 pivot_table_submit (table);
1137 n_unique_strings = 0;
1138 for (size_t i = 0; i < n_strings; i++)
1139 if (!all_equal (encodings, n_encodings, i))
1141 if (!n_unique_strings)
1144 /* Table of alternative interpretations. */
1145 table = pivot_table_create__ (
1146 pivot_value_new_text_format (N_("%s Encoded Text Strings"),
1148 "Alternate Encoded Text Strings");
1149 pivot_table_set_caption (
1150 table, pivot_value_new_text (
1151 N_("Text strings in the file dictionary that the previously listed "
1152 "encodings interpret differently, along with the interpretations.")));
1154 pivot_dimension_create (table, PIVOT_AXIS_COLUMN, N_("Text"), N_("Text"));
1156 number = pivot_dimension_create__ (table, PIVOT_AXIS_ROW,
1157 pivot_value_new_user_text ("#", -1));
1158 number->root->show_label = true;
1159 for (size_t i = 0; i < n_encodings; i++)
1160 pivot_category_create_leaf (number->root,
1161 pivot_value_new_integer (i + 1));
1163 struct pivot_dimension *purpose = pivot_dimension_create (
1164 table, PIVOT_AXIS_ROW, N_("Purpose"));
1165 purpose->root->show_label = true;
1167 for (size_t i = 0; i < n_strings; i++)
1168 if (!all_equal (encodings, n_encodings, i))
1170 int prefix = equal_prefix (encodings, n_encodings, i);
1171 int suffix = equal_suffix (encodings, n_encodings, i);
1173 int purpose_idx = pivot_category_create_leaf (
1174 purpose->root, pivot_value_new_user_text (titles[i], -1));
1176 for (size_t j = 0; j < n_encodings; j++)
1178 const char *s = encodings[j].utf8_strings[i] + prefix;
1180 if (prefix || suffix)
1182 size_t len = strlen (s) - suffix;
1183 struct string entry;
1185 ds_init_empty (&entry);
1187 ds_put_cstr (&entry, "...");
1188 ds_put_substring (&entry, ss_buffer (s, len));
1190 ds_put_cstr (&entry, "...");
1192 pivot_table_put3 (table, 0, j, purpose_idx,
1193 pivot_value_new_user_text_nocopy (
1194 ds_steal_cstr (&entry)));
1197 pivot_table_put3 (table, 0, j, purpose_idx,
1198 pivot_value_new_user_text (s, -1));
1202 pivot_table_submit (table);