1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 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/>. */
27 #include "data/file-handle-def.h"
28 #include "language/lexer/lexer.h"
29 #include "language/lexer/format-parser.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/compiler.h"
32 #include "libpspp/i18n.h"
33 #include "libpspp/string-map.h"
34 #include "output/driver.h"
35 #include "output/options.h"
36 #include "output/output-item.h"
37 #include "output/pivot-table.h"
40 #include "gl/progname.h"
41 #include "gl/xalloc.h"
42 #include "gl/xvasprintf.h"
44 /* --emphasis: Enable emphasis in ASCII driver? */
47 /* --box: ASCII driver box option. */
50 /* -o, --output: Base name for output files. */
51 static const char *output_base = "render";
53 static const char *parse_options (int argc, char **argv);
54 static void usage (void) NO_RETURN;
55 static void read_table (struct lexer *);
56 static void output_msg (const struct msg *, void *);
59 main (int argc, char **argv)
61 const char *input_file_name;
63 set_program_name (argv[0]);
65 output_engine_push ();
66 input_file_name = parse_options (argc, argv);
70 struct lex_reader *reader = lex_reader_for_file (input_file_name, NULL,
76 struct lexer *lexer = lex_create ();
77 msg_set_handler (output_msg, lexer);
78 lex_include (lexer, reader);
83 while (lex_match (lexer, T_ENDCMD))
85 if (lex_match (lexer, T_STOP))
98 static void PRINTF_FORMAT (2, 3)
99 register_driver (struct string_map *options,
100 const char *output_file, ...)
103 va_start (args, output_file);
104 string_map_insert_nocopy (options, xstrdup ("output-file"),
105 xvasprintf (output_file, args));
108 struct output_driver *driver = output_driver_create (options);
111 output_driver_register (driver);
115 configure_drivers (int width, int length UNUSED, int min_break)
117 /* Render to stdout. */
118 struct string_map options = STRING_MAP_INITIALIZER (options);
119 string_map_insert (&options, "format", "txt");
120 string_map_insert_nocopy (&options, xstrdup ("width"),
121 xasprintf ("%d", width));
123 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
124 xasprintf ("%d", min_break));
125 string_map_insert (&options, "emphasis", emphasis ? "true" : "false");
127 string_map_insert (&options, "box", box);
128 register_driver (&options, "-");
131 /* Render to <base>.pdf. */
132 string_map_insert (&options, "top-margin", "0");
133 string_map_insert (&options, "bottom-margin", "0");
134 string_map_insert (&options, "left-margin", "0");
135 string_map_insert (&options, "right-margin", "0");
136 string_map_insert (&options, "paper-size", "99x99in");
137 string_map_insert (&options, "trim", "true");
138 register_driver (&options, "%s.pdf", output_base);
140 string_map_insert (&options, "box", "unicode");
141 register_driver (&options, "%s.txt", output_base);
143 string_map_insert (&options, "box", "ascii");
144 register_driver (&options, "%s-ascii.txt", output_base);
146 register_driver (&options, "%s.csv", output_base);
147 register_driver (&options, "%s.odt", output_base);
148 register_driver (&options, "%s.spv", output_base);
149 register_driver (&options, "%s.html", output_base);
150 register_driver (&options, "%s.tex", output_base);
152 string_map_destroy (&options);
156 parse_options (int argc, char **argv)
165 OPT_WIDTH = UCHAR_MAX + 1,
173 static const struct option options[] =
175 {"width", required_argument, NULL, OPT_WIDTH},
176 {"length", required_argument, NULL, OPT_LENGTH},
177 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
178 {"emphasis", no_argument, NULL, OPT_EMPHASIS},
179 {"box", required_argument, NULL, OPT_BOX},
180 {"output", required_argument, NULL, 'o'},
181 {"table-look", required_argument, NULL, OPT_TABLE_LOOK},
182 {"help", no_argument, NULL, OPT_HELP},
186 int c = getopt_long (argc, argv, "o:", options, NULL);
193 width = atoi (optarg);
197 length = atoi (optarg);
201 min_break = atoi (optarg);
213 output_base = optarg;
218 struct pivot_table_look *look;
219 char *err = pivot_table_look_read (optarg, &look);
221 error (1, 0, "%s", err);
222 pivot_table_look_set_default (look);
223 pivot_table_look_unref (look);
243 configure_drivers (width, length, min_break);
245 if (optind + 1 != argc)
246 error (1, 0, "exactly one non-option argument required; "
247 "use --help for help");
254 printf ("%s, to test rendering of PSPP tables\n"
255 "usage: %s [OPTIONS] INPUT\n"
257 " --width=WIDTH set page width in characters\n"
258 " --length=LINE set page length in lines\n",
259 program_name, program_name);
264 force_match (struct lexer *lexer, enum token_type type)
266 if (!lex_force_match (lexer, type))
271 force_string (struct lexer *lexer)
273 if (!lex_force_string (lexer))
278 force_int (struct lexer *lexer)
280 if (!lex_force_int (lexer))
285 force_num (struct lexer *lexer)
287 if (!lex_force_num (lexer))
292 parse_settings_value_show (struct lexer *lexer, const char *name,
293 enum settings_value_show *show)
295 if (lex_match_id (lexer, name))
297 lex_match (lexer, T_EQUALS);
299 if (lex_match_id (lexer, "DEFAULT"))
300 *show = SETTINGS_VALUE_SHOW_DEFAULT;
301 else if (lex_match_id (lexer, "VALUE"))
302 *show = SETTINGS_VALUE_SHOW_VALUE;
303 else if (lex_match_id (lexer, "LABEL"))
304 *show = SETTINGS_VALUE_SHOW_LABEL;
305 else if (lex_match_id (lexer, "BOTH"))
306 *show = SETTINGS_VALUE_SHOW_BOTH;
309 lex_error_expecting (lexer, "DEFAULT", "VALUE", "LABEL", "BOTH");
320 parse_string_setting (struct lexer *lexer, const char *name, char **stringp)
322 if (lex_match_id (lexer, name))
324 lex_match (lexer, T_EQUALS);
325 force_string (lexer);
328 *stringp = xstrdup (lex_tokcstr (lexer));
338 match_kw (struct lexer *lexer, const char *kw)
340 return (!strcmp (kw, "ALL")
341 ? lex_match (lexer, T_ALL)
342 : lex_match_id (lexer, kw));
346 parse_bool_setting_with_default (struct lexer *lexer, const char *name,
347 const char *true_kw, const char *false_kw,
348 int default_value, bool *out)
350 if (lex_match_id (lexer, name))
352 if (default_value >= 0)
354 if (!lex_match (lexer, T_EQUALS))
355 *out = default_value;
359 force_match (lexer, T_EQUALS);
361 if (match_kw (lexer, true_kw))
363 else if (match_kw (lexer, false_kw))
367 lex_error_expecting (lexer, true_kw, false_kw);
378 parse_bool_setting (struct lexer *lexer, const char *name,
379 const char *true_kw, const char *false_kw,
382 return parse_bool_setting_with_default (lexer, name, true_kw, false_kw, -1,
387 parse_yesno_setting (struct lexer *lexer, const char *name, bool *out)
389 return parse_bool_setting_with_default (lexer, name, "YES", "NO", true, out);
392 static struct cell_color
393 read_color (struct lexer *lexer)
395 struct cell_color color;
396 if (!parse_color__ (lex_tokcstr (lexer), &color))
398 msg (SE, "%s: unknown color", lex_tokcstr (lexer));
406 parse_color_pair_setting (struct lexer *lexer, const char *name,
407 struct cell_color out[2])
409 if (lex_match_id (lexer, name))
411 lex_match (lexer, T_EQUALS);
412 out[0] = read_color (lexer);
413 out[1] = lex_is_string (lexer) ? read_color (lexer) : out[0];
421 parse_int_setting (struct lexer *lexer, const char *name, int *out)
423 if (lex_match_id (lexer, name))
425 lex_match (lexer, T_EQUALS);
427 *out = lex_integer (lexer);
436 read_font_style (struct lexer *lexer, struct font_style *fs)
438 while (parse_yesno_setting (lexer, "BOLD", &fs->bold)
439 || parse_yesno_setting (lexer, "ITALIC", &fs->italic)
440 || parse_yesno_setting (lexer, "UNDERLINE", &fs->underline)
441 || parse_yesno_setting (lexer, "MARKUP", &fs->markup)
442 || parse_color_pair_setting (lexer, "FG", fs->fg)
443 || parse_color_pair_setting (lexer, "BG", fs->bg)
444 || parse_string_setting (lexer, "FACE", &fs->typeface)
445 || parse_int_setting (lexer, "SIZE", &fs->size))
450 parse_halign_setting (struct lexer *lexer, enum table_halign *halign,
451 double *decimal_offset)
453 if (lex_match_id (lexer, "RIGHT"))
454 *halign = TABLE_HALIGN_RIGHT;
455 else if (lex_match_id (lexer, "LEFT"))
456 *halign = TABLE_HALIGN_LEFT;
457 else if (lex_match_id (lexer, "CELL"))
458 *halign = TABLE_HALIGN_CENTER;
459 else if (lex_match_id (lexer, "MIXED"))
460 *halign = TABLE_HALIGN_MIXED;
461 else if (lex_match_id (lexer, "DECIMAL"))
463 if (lex_is_number (lexer))
465 *decimal_offset = lex_number (lexer);
476 parse_valign_setting (struct lexer *lexer, enum table_valign *valign)
478 if (lex_match_id (lexer, "TOP"))
479 *valign = TABLE_VALIGN_TOP;
480 else if (lex_match_id (lexer, "MIDDLE"))
481 *valign = TABLE_VALIGN_CENTER;
482 else if (lex_match_id (lexer, "BOTTOM"))
483 *valign = TABLE_VALIGN_BOTTOM;
491 parse_margin_setting (struct lexer *lexer, int margin[TABLE_N_AXES][2])
493 if (lex_match_id (lexer, "MARGINS"))
498 lex_match (lexer, T_EQUALS);
500 while (lex_is_number (lexer) && n < 4)
502 values[n++] = lex_number (lexer);
508 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[0];
509 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
513 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
514 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
518 margin[TABLE_VERT][0] = values[0];
519 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
520 margin[TABLE_VERT][1] = values[2];
525 margin[TABLE_VERT][0] = values[0];
526 margin[TABLE_HORZ][1] = values[1];
527 margin[TABLE_VERT][1] = values[2];
528 margin[TABLE_HORZ][0] = values[3];
538 read_cell_style (struct lexer *lexer, struct cell_style *cs)
540 while (parse_halign_setting (lexer, &cs->halign, &cs->decimal_offset)
541 || parse_valign_setting (lexer, &cs->valign)
542 || parse_margin_setting (lexer, cs->margin))
547 read_value_option (struct lexer *lexer, const struct pivot_table *pt,
548 struct pivot_value *value,
549 const struct table_area_style *base_style)
551 enum settings_value_show *show
552 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.show
553 : value->type == PIVOT_VALUE_STRING ? &value->string.show
554 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.show
556 if (show && parse_settings_value_show (lexer, "SHOW", show))
560 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.var_name
561 : value->type == PIVOT_VALUE_STRING ? &value->string.var_name
563 if (var_name && parse_string_setting (lexer, "VAR", var_name))
567 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.value_label
568 : value->type == PIVOT_VALUE_STRING ? &value->string.value_label
569 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.var_label
571 if (label && parse_string_setting (lexer, "LABEL", label))
574 if (value->type == PIVOT_VALUE_STRING && lex_match_id (lexer, "HEX"))
576 value->string.hex = true;
580 if (value->type == PIVOT_VALUE_NUMERIC)
584 bool ok = parse_format_specifier (lexer, &fmt);
589 if (!fmt_check_output (&fmt)
590 || !fmt_check_type_compat (&fmt, VAL_NUMERIC))
593 value->numeric.format = fmt;
598 if (lex_match_id (lexer, "SUBSCRIPTS"))
600 lex_match (lexer, T_EQUALS);
601 size_t allocated_subscripts = value->n_subscripts;
602 while (lex_token (lexer) == T_STRING)
604 if (value->n_subscripts >= allocated_subscripts)
605 value->subscripts = x2nrealloc (value->subscripts,
606 &allocated_subscripts,
607 sizeof *value->subscripts);
609 value->subscripts[value->n_subscripts++] = xstrdup (
610 lex_tokcstr (lexer));
616 if (lex_match_id (lexer, "FONT") && base_style)
618 lex_match (lexer, T_EQUALS);
620 if (!value->font_style)
622 value->font_style = xmalloc (sizeof *value->font_style);
623 font_style_copy (NULL, value->font_style, &base_style->font_style);
625 read_font_style (lexer, value->font_style);
629 if (lex_match_id (lexer, "CELL") && base_style)
631 lex_match (lexer, T_EQUALS);
633 if (!value->cell_style)
635 value->cell_style = xmalloc (sizeof *value->cell_style);
636 *value->cell_style = base_style->cell_style;
638 read_cell_style (lexer, value->cell_style);
642 if (lex_match_id (lexer, "FOOTNOTE"))
644 lex_match (lexer, T_EQUALS);
646 while (lex_is_integer (lexer))
648 size_t idx = lex_integer (lexer);
651 if (idx >= pt->n_footnotes)
653 msg (SE, "Footnote %zu not available "
654 "(only %zu footnotes defined)", idx, pt->n_footnotes);
657 pivot_value_add_footnote (value, pt->footnotes[idx]);
662 lex_error (lexer, "Expecting valid value option");
666 static struct pivot_value *
667 read_value (struct lexer *lexer, const struct pivot_table *pt,
668 const struct table_area_style *base_style)
670 struct pivot_value *value;
671 if (lex_is_number (lexer))
673 value = pivot_value_new_number (lex_number (lexer));
676 else if (lex_is_string (lexer))
678 value = xmalloc (sizeof *value);
679 *value = (struct pivot_value) {
680 .type = PIVOT_VALUE_STRING,
681 .string = { .s = xstrdup (lex_tokcstr (lexer)) },
685 else if (lex_token (lexer) == T_ID)
687 value = xmalloc (sizeof *value);
688 *value = (struct pivot_value) {
689 .type = PIVOT_VALUE_VARIABLE,
690 .variable = { .var_name = xstrdup (lex_tokcstr (lexer)) },
696 msg (SE, "Expecting pivot_value");
700 while (lex_match (lexer, T_LBRACK))
702 read_value_option (lexer, pt, value, base_style);
703 force_match (lexer, T_RBRACK);
710 read_group (struct lexer *lexer, struct pivot_table *pt,
711 struct pivot_category *group,
712 const struct table_area_style *label_style)
714 if (lex_match (lexer, T_ASTERISK))
715 group->show_label = true;
717 force_match (lexer, T_LPAREN);
718 if (lex_match (lexer, T_RPAREN))
723 struct pivot_value *name = read_value (lexer, pt, label_style);
724 if (lex_token (lexer) == T_ASTERISK
725 || lex_token (lexer) == T_LPAREN)
726 read_group (lexer, pt, pivot_category_create_group__ (group, name),
731 if (lex_token (lexer) == T_ID
732 && is_pivot_result_class (lex_tokcstr (lexer)))
734 rc = xstrdup (lex_tokcstr (lexer));
740 pivot_category_create_leaf_rc (group, name, rc);
745 while (lex_match (lexer, T_COMMA));
746 force_match (lexer, T_RPAREN);
750 read_dimension (struct lexer *lexer, struct pivot_table *pt,
751 enum pivot_axis_type a,
752 const struct table_area_style *label_style)
754 if (!pivot_table_is_empty (pt))
755 error (1, 0, "can't add dimensions after adding data");
757 lex_match (lexer, T_EQUALS);
759 struct pivot_value *name = read_value (lexer, pt, label_style);
760 struct pivot_dimension *dim = pivot_dimension_create__ (pt, a, name);
761 read_group (lexer, pt, dim->root, label_style);
765 read_look (struct lexer *lexer, struct pivot_table *pt)
767 lex_match (lexer, T_EQUALS);
769 if (lex_is_string (lexer))
771 struct pivot_table_look *look;
772 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
775 msg (SE, "%s", error);
780 pivot_table_set_look (pt, look);
781 pivot_table_look_unref (look);
784 struct pivot_table_look *look = pivot_table_look_unshare (
785 pivot_table_look_ref (pt->look));
788 if (!parse_bool_setting (lexer, "EMPTY", "HIDE", "SHOW",
790 && !parse_bool_setting (lexer, "ROWLABELS", "CORNER", "NESTED",
791 &look->row_labels_in_corner)
792 && !parse_bool_setting (lexer, "MARKERS", "NUMERIC", "ALPHA",
793 &look->show_numeric_markers)
794 && !parse_bool_setting (lexer, "LEVEL", "SUPER", "SUB",
795 &look->footnote_marker_superscripts)
796 && !parse_bool_setting (lexer, "LAYERS", "ALL", "CURRENT",
797 &look->print_all_layers)
798 && !parse_bool_setting (lexer, "PAGINATELAYERS", "YES", "NO",
799 &look->paginate_layers)
800 && !parse_bool_setting (lexer, "HSHRINK", "YES", "NO",
801 &look->shrink_to_fit[TABLE_HORZ])
802 && !parse_bool_setting (lexer, "VSHRINK", "YES", "NO",
803 &look->shrink_to_fit[TABLE_VERT])
804 && !parse_bool_setting (lexer, "TOPCONTINUATION", "YES", "NO",
805 &look->top_continuation)
806 && !parse_bool_setting (lexer, "BOTTOMCONTINUATION", "YES", "NO",
807 &look->bottom_continuation)
808 && !parse_string_setting (lexer, "CONTINUATION",
809 &look->continuation))
812 pivot_table_set_look (pt, look);
813 pivot_table_look_unref (look);
816 static enum table_stroke
817 read_stroke (struct lexer *lexer)
819 for (int stroke = 0; stroke < TABLE_N_STROKES; stroke++)
820 if (lex_match_id (lexer, table_stroke_to_string (stroke)))
823 lex_error (lexer, "expecting stroke");
828 parse_value_setting (struct lexer *lexer, const struct pivot_table *pt,
830 struct pivot_value **valuep,
831 struct table_area_style *base_style)
833 if (lex_match_id (lexer, name))
835 lex_match (lexer, T_EQUALS);
837 pivot_value_destroy (*valuep);
838 *valuep = read_value (lexer, pt, base_style);
847 read_border (struct lexer *lexer, struct pivot_table *pt)
849 static const char *const pivot_border_ids[PIVOT_N_BORDERS] = {
850 [PIVOT_BORDER_TITLE] = "title",
851 [PIVOT_BORDER_OUTER_LEFT] = "outer-left",
852 [PIVOT_BORDER_OUTER_TOP] = "outer-top",
853 [PIVOT_BORDER_OUTER_RIGHT] = "outer-right",
854 [PIVOT_BORDER_OUTER_BOTTOM] = "outer-bottom",
855 [PIVOT_BORDER_INNER_LEFT] = "inner-left",
856 [PIVOT_BORDER_INNER_TOP] = "inner-top",
857 [PIVOT_BORDER_INNER_RIGHT] = "inner-right",
858 [PIVOT_BORDER_INNER_BOTTOM] = "inner-bottom",
859 [PIVOT_BORDER_DATA_LEFT] = "data-left",
860 [PIVOT_BORDER_DATA_TOP] = "data-top",
861 [PIVOT_BORDER_DIM_ROW_HORZ] = "dim-row-horz",
862 [PIVOT_BORDER_DIM_ROW_VERT] = "dim-row-vert",
863 [PIVOT_BORDER_DIM_COL_HORZ] = "dim-col-horz",
864 [PIVOT_BORDER_DIM_COL_VERT] = "dim-col-vert",
865 [PIVOT_BORDER_CAT_ROW_HORZ] = "cat-row-horz",
866 [PIVOT_BORDER_CAT_ROW_VERT] = "cat-row-vert",
867 [PIVOT_BORDER_CAT_COL_HORZ] = "cat-col-horz",
868 [PIVOT_BORDER_CAT_COL_VERT] = "cat-col-vert",
871 lex_match (lexer, T_EQUALS);
873 struct pivot_table_look *look = pivot_table_look_unshare (
874 pivot_table_look_ref (pt->look));
875 while (lex_token (lexer) == T_STRING)
877 char *s = xstrdup (lex_tokcstr (lexer));
879 force_match (lexer, T_LPAREN);
881 struct table_border_style style = TABLE_BORDER_STYLE_INITIALIZER;
882 style.stroke = read_stroke (lexer);
883 if (lex_is_string (lexer))
884 style.color = read_color (lexer);
885 force_match (lexer, T_RPAREN);
888 for (int b = 0; b < PIVOT_N_BORDERS; b++)
890 if (!fnmatch (s, pivot_border_ids[b], 0))
892 look->borders[b] = style;
898 msg (SE, "%s: no matching borders", s);
903 pivot_table_set_look (pt, look);
904 pivot_table_look_unref (look);
908 read_footnote (struct lexer *lexer, struct pivot_table *pt)
911 if (lex_match (lexer, T_LBRACK))
915 idx = lex_integer (lexer);
918 force_match (lexer, T_RBRACK);
921 idx = pt->n_footnotes;
922 lex_match (lexer, T_EQUALS);
924 struct pivot_value *content
925 = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
927 struct pivot_value *marker;
928 if (lex_match_id (lexer, "MARKER"))
930 lex_match (lexer, T_EQUALS);
931 marker = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
936 bool show = !lex_match_id (lexer, "HIDE");
937 pivot_table_create_footnote__ (pt, idx, marker, content)->show = show;
941 read_cell (struct lexer *lexer, struct pivot_table *pt)
943 force_match (lexer, T_LBRACK);
945 size_t *lo = xnmalloc (pt->n_dimensions, sizeof *lo);
946 size_t *hi = xnmalloc (pt->n_dimensions, sizeof *hi);
947 for (size_t i = 0; i < pt->n_dimensions; i++)
949 const struct pivot_dimension *d = pt->dimensions[i];
952 force_match (lexer, T_COMMA);
956 msg (SE, "can't define data because dimension %zu has no categories",
961 if (lex_match (lexer, T_ALL))
964 hi[i] = d->n_leaves - 1;
969 lo[i] = hi[i] = lex_integer (lexer);
972 if (lex_match_id (lexer, "THRU"))
975 hi[i] = lex_integer (lexer);
981 msg (SE, "%zu THRU %zu is not a valid range", lo[i], hi[i]);
984 if (hi[i] >= d->n_leaves)
986 msg (SE, "dimension %zu (%s) has only %zu categories",
987 i, pivot_value_to_string (d->root->name, pt),
993 force_match (lexer, T_RBRACK);
995 struct pivot_value *value = NULL;
997 if (lex_match (lexer, T_EQUALS))
999 if (lex_match_id (lexer, "DELETE"))
1002 value = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_DATA]);
1005 size_t *dindexes = xmemdup (lo, pt->n_dimensions * sizeof *lo);
1006 for (size_t i = 0; ; i++)
1009 pivot_table_delete (pt, dindexes);
1011 pivot_table_put (pt, dindexes, pt->n_dimensions,
1013 ? pivot_value_clone (value)
1014 : pivot_value_new_integer (i)));
1016 for (size_t j = 0; j < pt->n_dimensions; j++)
1018 if (++dindexes[j] <= hi[j])
1020 dindexes[j] = lo[j];
1027 pivot_value_destroy (value);
1033 static struct pivot_dimension *
1034 parse_dim_name (struct lexer *lexer, struct pivot_table *table)
1036 force_string (lexer);
1037 for (size_t i = 0; i < table->n_dimensions; i++)
1039 struct pivot_dimension *dim = table->dimensions[i];
1041 struct string s = DS_EMPTY_INITIALIZER;
1042 pivot_value_format_body (dim->root->name, table, &s);
1043 bool match = !strcmp (ds_cstr (&s), lex_tokcstr (lexer));
1053 lex_error (lexer, "unknown dimension");
1057 static enum pivot_axis_type
1058 parse_axis_type (struct lexer *lexer)
1060 if (lex_match_id (lexer, "ROW"))
1061 return PIVOT_AXIS_ROW;
1062 else if (lex_match_id (lexer, "COLUMN"))
1063 return PIVOT_AXIS_COLUMN;
1064 else if (lex_match_id (lexer, "LAYER"))
1065 return PIVOT_AXIS_LAYER;
1068 lex_error_expecting (lexer, "ROW", "COLUMN", "LAYER");
1074 move_dimension (struct lexer *lexer, struct pivot_table *table)
1076 struct pivot_dimension *dim = parse_dim_name (lexer, table);
1078 enum pivot_axis_type axis = parse_axis_type (lexer);
1081 if (lex_is_integer (lexer))
1083 position = lex_integer (lexer);
1089 pivot_table_move_dimension (table, dim, axis, position);
1093 swap_axes (struct lexer *lexer, struct pivot_table *table)
1095 enum pivot_axis_type a = parse_axis_type (lexer);
1096 enum pivot_axis_type b = parse_axis_type (lexer);
1097 pivot_table_swap_axes (table, a, b);
1101 read_current_layer (struct lexer *lexer, struct pivot_table *table)
1103 lex_match (lexer, T_EQUALS);
1105 const struct pivot_axis *layer_axis = &table->axes[PIVOT_AXIS_LAYER];
1106 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
1108 const struct pivot_dimension *dim = layer_axis->dimensions[i];
1111 size_t index = lex_integer (lexer);
1112 if (index >= dim->n_leaves)
1114 lex_error (lexer, "only %zu dimensions", dim->n_leaves);
1119 table->current_layer[i] = index;
1124 read_table (struct lexer *lexer)
1126 bool displayed = false;
1128 struct pivot_table *pt = pivot_table_create ("Default Title");
1129 while (lex_match (lexer, T_SLASH))
1131 assert (!pivot_table_is_shared (pt));
1134 if (lex_match_id (lexer, "ROW"))
1135 read_dimension (lexer, pt, PIVOT_AXIS_ROW,
1136 &pt->look->areas[PIVOT_AREA_ROW_LABELS]);
1137 else if (lex_match_id (lexer, "COLUMN"))
1138 read_dimension (lexer, pt, PIVOT_AXIS_COLUMN,
1139 &pt->look->areas[PIVOT_AREA_COLUMN_LABELS]);
1140 else if (lex_match_id (lexer, "LAYER"))
1141 read_dimension (lexer, pt, PIVOT_AXIS_LAYER,
1142 &pt->look->areas[PIVOT_AREA_LAYERS]);
1143 else if (lex_match_id (lexer, "LOOK"))
1144 read_look (lexer, pt);
1145 else if (lex_match_id (lexer, "ROTATE"))
1147 lex_match (lexer, T_EQUALS);
1148 while (lex_token (lexer) == T_ID)
1149 if (!parse_bool_setting (lexer, "INNERCOLUMNS", "YES", "NO",
1150 &pt->rotate_inner_column_labels)
1151 && !parse_bool_setting (lexer, "OUTERROWS", "YES", "NO",
1152 &pt->rotate_outer_row_labels))
1155 else if (lex_match_id (lexer, "SHOW"))
1157 lex_match (lexer, T_EQUALS);
1158 while (lex_token (lexer) == T_ID)
1160 if (parse_bool_setting (lexer, "GRID", "YES", "NO",
1161 &pt->show_grid_lines)
1162 || parse_bool_setting (lexer, "CAPTION", "YES", "NO",
1164 || parse_bool_setting (lexer, "TITLE", "YES", "NO",
1168 if (parse_settings_value_show (lexer, "VALUES", &pt->show_values)
1169 || parse_settings_value_show (lexer, "VARIABLES",
1170 &pt->show_variables))
1173 if (lex_match_id (lexer, "LAYER"))
1174 read_current_layer (lexer, pt);
1179 else if (parse_value_setting (lexer, pt, "TITLE", &pt->title,
1180 &pt->look->areas[PIVOT_AREA_TITLE])
1181 || parse_value_setting (lexer, pt, "SUBTYPE", &pt->subtype,
1183 || parse_value_setting (lexer, pt, "CORNER", &pt->corner_text,
1184 &pt->look->areas[PIVOT_AREA_CORNER])
1185 || parse_value_setting (lexer, pt, "CAPTION", &pt->caption,
1186 &pt->look->areas[PIVOT_AREA_CAPTION])
1187 || parse_string_setting (lexer, "NOTES", &pt->notes))
1191 else if (lex_match_id (lexer, "BORDER"))
1192 read_border (lexer, pt);
1193 else if (lex_match_id (lexer, "TRANSPOSE"))
1194 pivot_table_transpose (pt);
1195 else if (lex_match_id (lexer, "SWAP"))
1196 swap_axes (lexer, pt);
1197 else if (lex_match_id (lexer, "MOVE"))
1198 move_dimension (lexer, pt);
1199 else if (lex_match_id (lexer, "CELLS"))
1200 read_cell (lexer, pt);
1201 else if (lex_match_id (lexer, "FOOTNOTE"))
1202 read_footnote (lexer, pt);
1203 else if (lex_match_id (lexer, "DUMP"))
1204 pivot_table_dump (pt, 0);
1205 else if (lex_match_id (lexer, "DISPLAY"))
1207 pivot_table_submit (pivot_table_ref (pt));
1208 pt = pivot_table_unshare (pt);
1213 msg (SE, "Expecting keyword");
1219 pivot_table_submit (pt);
1221 pivot_table_unshare (pt);
1223 force_match (lexer, T_ENDCMD);
1227 output_msg (const struct msg *m_, void *lexer_)
1229 struct lexer *lexer = lexer_;
1232 if (m.file_name == NULL)
1234 m.file_name = CONST_CAST (char *, lex_get_file_name (lexer));
1235 m.first_line = lex_get_first_line_number (lexer, 0);
1236 m.last_line = lex_get_last_line_number (lexer, 0);
1239 m.command_name = output_get_uppercase_command_name ();
1241 output_item_submit (message_item_create (&m));
1243 free (m.command_name);