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/message-item.h"
36 #include "output/options.h"
37 #include "output/pivot-table.h"
38 #include "output/table-item.h"
41 #include "gl/progname.h"
42 #include "gl/xalloc.h"
43 #include "gl/xvasprintf.h"
45 /* --emphasis: Enable emphasis in ASCII driver? */
48 /* --box: ASCII driver box option. */
51 /* -o, --output: Base name for output files. */
52 static const char *output_base = "render";
54 static const char *parse_options (int argc, char **argv);
55 static void usage (void) NO_RETURN;
56 static void read_table (struct lexer *);
57 static void output_msg (const struct msg *, void *);
60 main (int argc, char **argv)
62 const char *input_file_name;
64 set_program_name (argv[0]);
66 output_engine_push ();
67 input_file_name = parse_options (argc, argv);
71 struct lex_reader *reader = lex_reader_for_file (input_file_name, NULL,
77 struct lexer *lexer = lex_create ();
78 msg_set_handler (output_msg, lexer);
79 lex_include (lexer, reader);
84 while (lex_match (lexer, T_ENDCMD))
86 if (lex_match (lexer, T_STOP))
99 static void PRINTF_FORMAT (2, 3)
100 register_driver (struct string_map *options,
101 const char *output_file, ...)
104 va_start (args, output_file);
105 string_map_insert_nocopy (options, xstrdup ("output-file"),
106 xvasprintf (output_file, args));
109 struct output_driver *driver = output_driver_create (options);
112 output_driver_register (driver);
116 configure_drivers (int width, int length UNUSED, int min_break)
118 /* Render to stdout. */
119 struct string_map options = STRING_MAP_INITIALIZER (options);
120 string_map_insert (&options, "format", "txt");
121 string_map_insert_nocopy (&options, xstrdup ("width"),
122 xasprintf ("%d", width));
124 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
125 xasprintf ("%d", min_break));
126 string_map_insert (&options, "emphasis", emphasis ? "true" : "false");
128 string_map_insert (&options, "box", box);
129 register_driver (&options, "-");
132 /* Render to <base>.pdf. */
133 string_map_insert (&options, "top-margin", "0");
134 string_map_insert (&options, "bottom-margin", "0");
135 string_map_insert (&options, "left-margin", "0");
136 string_map_insert (&options, "right-margin", "0");
137 string_map_insert (&options, "paper-size", "99x99in");
138 string_map_insert (&options, "trim", "true");
139 register_driver (&options, "%s.pdf", output_base);
141 string_map_insert (&options, "box", "unicode");
142 register_driver (&options, "%s.txt", output_base);
144 string_map_insert (&options, "box", "ascii");
145 register_driver (&options, "%s-ascii.txt", output_base);
147 register_driver (&options, "%s.csv", output_base);
148 register_driver (&options, "%s.odt", output_base);
149 register_driver (&options, "%s.spv", output_base);
150 register_driver (&options, "%s.html", output_base);
151 register_driver (&options, "%s.tex", output_base);
153 string_map_destroy (&options);
157 parse_options (int argc, char **argv)
166 OPT_WIDTH = UCHAR_MAX + 1,
174 static const struct option options[] =
176 {"width", required_argument, NULL, OPT_WIDTH},
177 {"length", required_argument, NULL, OPT_LENGTH},
178 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
179 {"emphasis", no_argument, NULL, OPT_EMPHASIS},
180 {"box", required_argument, NULL, OPT_BOX},
181 {"output", required_argument, NULL, 'o'},
182 {"table-look", required_argument, NULL, OPT_TABLE_LOOK},
183 {"help", no_argument, NULL, OPT_HELP},
187 int c = getopt_long (argc, argv, "o:", options, NULL);
194 width = atoi (optarg);
198 length = atoi (optarg);
202 min_break = atoi (optarg);
214 output_base = optarg;
219 struct pivot_table_look *look;
220 char *err = pivot_table_look_read (optarg, &look);
222 error (1, 0, "%s", err);
223 pivot_table_look_set_default (look);
224 pivot_table_look_unref (look);
244 configure_drivers (width, length, min_break);
246 if (optind + 1 != argc)
247 error (1, 0, "exactly one non-option argument required; "
248 "use --help for help");
255 printf ("%s, to test rendering of PSPP tables\n"
256 "usage: %s [OPTIONS] INPUT\n"
258 " --width=WIDTH set page width in characters\n"
259 " --length=LINE set page length in lines\n",
260 program_name, program_name);
265 force_match (struct lexer *lexer, enum token_type type)
267 if (!lex_force_match (lexer, type))
272 force_string (struct lexer *lexer)
274 if (!lex_force_string (lexer))
279 force_int (struct lexer *lexer)
281 if (!lex_force_int (lexer))
286 force_num (struct lexer *lexer)
288 if (!lex_force_num (lexer))
293 parse_settings_value_show (struct lexer *lexer, const char *name,
294 enum settings_value_show *show)
296 if (lex_match_id (lexer, name))
298 lex_match (lexer, T_EQUALS);
300 if (lex_match_id (lexer, "DEFAULT"))
301 *show = SETTINGS_VALUE_SHOW_DEFAULT;
302 else if (lex_match_id (lexer, "VALUE"))
303 *show = SETTINGS_VALUE_SHOW_VALUE;
304 else if (lex_match_id (lexer, "LABEL"))
305 *show = SETTINGS_VALUE_SHOW_LABEL;
306 else if (lex_match_id (lexer, "BOTH"))
307 *show = SETTINGS_VALUE_SHOW_BOTH;
310 lex_error_expecting (lexer, "DEFAULT", "VALUE", "LABEL", "BOTH");
321 parse_string_setting (struct lexer *lexer, const char *name, char **stringp)
323 if (lex_match_id (lexer, name))
325 lex_match (lexer, T_EQUALS);
326 force_string (lexer);
329 *stringp = xstrdup (lex_tokcstr (lexer));
339 match_kw (struct lexer *lexer, const char *kw)
341 return (!strcmp (kw, "ALL")
342 ? lex_match (lexer, T_ALL)
343 : lex_match_id (lexer, kw));
347 parse_bool_setting_with_default (struct lexer *lexer, const char *name,
348 const char *true_kw, const char *false_kw,
349 int default_value, bool *out)
351 if (lex_match_id (lexer, name))
353 if (default_value >= 0)
355 if (!lex_match (lexer, T_EQUALS))
356 *out = default_value;
360 force_match (lexer, T_EQUALS);
362 if (match_kw (lexer, true_kw))
364 else if (match_kw (lexer, false_kw))
368 lex_error_expecting (lexer, true_kw, false_kw);
379 parse_bool_setting (struct lexer *lexer, const char *name,
380 const char *true_kw, const char *false_kw,
383 return parse_bool_setting_with_default (lexer, name, true_kw, false_kw, -1,
388 parse_yesno_setting (struct lexer *lexer, const char *name, bool *out)
390 return parse_bool_setting_with_default (lexer, name, "YES", "NO", true, out);
393 static struct cell_color
394 read_color (struct lexer *lexer)
396 struct cell_color color;
397 if (!parse_color__ (lex_tokcstr (lexer), &color))
399 msg (SE, "%s: unknown color", lex_tokcstr (lexer));
407 parse_color_pair_setting (struct lexer *lexer, const char *name,
408 struct cell_color out[2])
410 if (lex_match_id (lexer, name))
412 lex_match (lexer, T_EQUALS);
413 out[0] = read_color (lexer);
414 out[1] = lex_is_string (lexer) ? read_color (lexer) : out[0];
422 parse_int_setting (struct lexer *lexer, const char *name, int *out)
424 if (lex_match_id (lexer, name))
426 lex_match (lexer, T_EQUALS);
428 *out = lex_integer (lexer);
437 read_font_style (struct lexer *lexer, struct font_style *fs)
439 while (parse_yesno_setting (lexer, "BOLD", &fs->bold)
440 || parse_yesno_setting (lexer, "ITALIC", &fs->italic)
441 || parse_yesno_setting (lexer, "UNDERLINE", &fs->underline)
442 || parse_yesno_setting (lexer, "MARKUP", &fs->markup)
443 || parse_color_pair_setting (lexer, "FG", fs->fg)
444 || parse_color_pair_setting (lexer, "BG", fs->bg)
445 || parse_string_setting (lexer, "FACE", &fs->typeface)
446 || parse_int_setting (lexer, "SIZE", &fs->size))
451 parse_halign_setting (struct lexer *lexer, enum table_halign *halign,
452 double *decimal_offset)
454 if (lex_match_id (lexer, "RIGHT"))
455 *halign = TABLE_HALIGN_RIGHT;
456 else if (lex_match_id (lexer, "LEFT"))
457 *halign = TABLE_HALIGN_LEFT;
458 else if (lex_match_id (lexer, "CELL"))
459 *halign = TABLE_HALIGN_CENTER;
460 else if (lex_match_id (lexer, "MIXED"))
461 *halign = TABLE_HALIGN_MIXED;
462 else if (lex_match_id (lexer, "DECIMAL"))
464 if (lex_is_number (lexer))
466 *decimal_offset = lex_number (lexer);
477 parse_valign_setting (struct lexer *lexer, enum table_valign *valign)
479 if (lex_match_id (lexer, "TOP"))
480 *valign = TABLE_VALIGN_TOP;
481 else if (lex_match_id (lexer, "MIDDLE"))
482 *valign = TABLE_VALIGN_CENTER;
483 else if (lex_match_id (lexer, "BOTTOM"))
484 *valign = TABLE_VALIGN_BOTTOM;
492 parse_margin_setting (struct lexer *lexer, int margin[TABLE_N_AXES][2])
494 if (lex_match_id (lexer, "MARGINS"))
499 lex_match (lexer, T_EQUALS);
501 while (lex_is_number (lexer) && n < 4)
503 values[n++] = lex_number (lexer);
509 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[0];
510 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
514 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
515 margin[TABLE_VERT][0] = margin[TABLE_VERT][1] = values[0];
519 margin[TABLE_VERT][0] = values[0];
520 margin[TABLE_HORZ][0] = margin[TABLE_HORZ][1] = values[1];
521 margin[TABLE_VERT][1] = values[2];
526 margin[TABLE_VERT][0] = values[0];
527 margin[TABLE_HORZ][1] = values[1];
528 margin[TABLE_VERT][1] = values[2];
529 margin[TABLE_HORZ][0] = values[3];
539 read_cell_style (struct lexer *lexer, struct cell_style *cs)
541 while (parse_halign_setting (lexer, &cs->halign, &cs->decimal_offset)
542 || parse_valign_setting (lexer, &cs->valign)
543 || parse_margin_setting (lexer, cs->margin))
548 read_value_option (struct lexer *lexer, const struct pivot_table *pt,
549 struct pivot_value *value,
550 const struct table_area_style *base_style)
552 enum settings_value_show *show
553 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.show
554 : value->type == PIVOT_VALUE_STRING ? &value->string.show
555 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.show
557 if (show && parse_settings_value_show (lexer, "SHOW", show))
561 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.var_name
562 : value->type == PIVOT_VALUE_STRING ? &value->string.var_name
564 if (var_name && parse_string_setting (lexer, "VAR", var_name))
568 = (value->type == PIVOT_VALUE_NUMERIC ? &value->numeric.value_label
569 : value->type == PIVOT_VALUE_STRING ? &value->string.value_label
570 : value->type == PIVOT_VALUE_VARIABLE ? &value->variable.var_label
572 if (label && parse_string_setting (lexer, "LABEL", label))
575 if (value->type == PIVOT_VALUE_STRING && lex_match_id (lexer, "HEX"))
577 value->string.hex = true;
581 if (value->type == PIVOT_VALUE_NUMERIC)
585 bool ok = parse_format_specifier (lexer, &fmt);
590 if (!fmt_check_output (&fmt)
591 || !fmt_check_type_compat (&fmt, VAL_NUMERIC))
594 value->numeric.format = fmt;
599 if (lex_match_id (lexer, "SUBSCRIPTS"))
601 lex_match (lexer, T_EQUALS);
602 size_t allocated_subscripts = value->n_subscripts;
603 while (lex_token (lexer) == T_STRING)
605 if (value->n_subscripts >= allocated_subscripts)
606 value->subscripts = x2nrealloc (value->subscripts,
607 &allocated_subscripts,
608 sizeof *value->subscripts);
610 value->subscripts[value->n_subscripts++] = xstrdup (
611 lex_tokcstr (lexer));
617 if (lex_match_id (lexer, "FONT") && base_style)
619 lex_match (lexer, T_EQUALS);
621 if (!value->font_style)
623 value->font_style = xmalloc (sizeof *value->font_style);
624 font_style_copy (NULL, value->font_style, &base_style->font_style);
626 read_font_style (lexer, value->font_style);
630 if (lex_match_id (lexer, "CELL") && base_style)
632 lex_match (lexer, T_EQUALS);
634 if (!value->cell_style)
636 value->cell_style = xmalloc (sizeof *value->cell_style);
637 *value->cell_style = base_style->cell_style;
639 read_cell_style (lexer, value->cell_style);
643 if (lex_match_id (lexer, "FOOTNOTE"))
645 lex_match (lexer, T_EQUALS);
647 while (lex_is_integer (lexer))
649 size_t idx = lex_integer (lexer);
652 if (idx >= pt->n_footnotes)
654 msg (SE, "Footnote %zu not available "
655 "(only %zu footnotes defined)", idx, pt->n_footnotes);
658 pivot_value_add_footnote (value, pt->footnotes[idx]);
663 lex_error (lexer, "Expecting valid value option");
667 static struct pivot_value *
668 read_value (struct lexer *lexer, const struct pivot_table *pt,
669 const struct table_area_style *base_style)
671 struct pivot_value *value;
672 if (lex_is_number (lexer))
674 value = pivot_value_new_number (lex_number (lexer));
677 else if (lex_is_string (lexer))
679 value = xmalloc (sizeof *value);
680 *value = (struct pivot_value) {
681 .type = PIVOT_VALUE_STRING,
682 .string = { .s = xstrdup (lex_tokcstr (lexer)) },
686 else if (lex_token (lexer) == T_ID)
688 value = xmalloc (sizeof *value);
689 *value = (struct pivot_value) {
690 .type = PIVOT_VALUE_VARIABLE,
691 .variable = { .var_name = xstrdup (lex_tokcstr (lexer)) },
697 msg (SE, "Expecting pivot_value");
701 while (lex_match (lexer, T_LBRACK))
703 read_value_option (lexer, pt, value, base_style);
704 force_match (lexer, T_RBRACK);
711 read_group (struct lexer *lexer, struct pivot_table *pt,
712 struct pivot_category *group,
713 const struct table_area_style *label_style)
715 if (lex_match (lexer, T_ASTERISK))
716 group->show_label = true;
718 force_match (lexer, T_LPAREN);
719 if (lex_match (lexer, T_RPAREN))
724 struct pivot_value *name = read_value (lexer, pt, label_style);
725 if (lex_token (lexer) == T_ASTERISK
726 || lex_token (lexer) == T_LPAREN)
727 read_group (lexer, pt, pivot_category_create_group__ (group, name),
732 if (lex_token (lexer) == T_ID
733 && is_pivot_result_class (lex_tokcstr (lexer)))
735 rc = xstrdup (lex_tokcstr (lexer));
741 pivot_category_create_leaf_rc (group, name, rc);
746 while (lex_match (lexer, T_COMMA));
747 force_match (lexer, T_RPAREN);
751 read_dimension (struct lexer *lexer, struct pivot_table *pt,
752 enum pivot_axis_type a,
753 const struct table_area_style *label_style)
755 if (!pivot_table_is_empty (pt))
756 error (1, 0, "can't add dimensions after adding data");
758 lex_match (lexer, T_EQUALS);
760 struct pivot_value *name = read_value (lexer, pt, label_style);
761 struct pivot_dimension *dim = pivot_dimension_create__ (pt, a, name);
762 read_group (lexer, pt, dim->root, label_style);
766 read_look (struct lexer *lexer, struct pivot_table *pt)
768 lex_match (lexer, T_EQUALS);
770 if (lex_is_string (lexer))
772 struct pivot_table_look *look;
773 char *error = pivot_table_look_read (lex_tokcstr (lexer), &look);
776 msg (SE, "%s", error);
781 pivot_table_set_look (pt, look);
782 pivot_table_look_unref (look);
785 struct pivot_table_look *look = pivot_table_look_unshare (
786 pivot_table_look_ref (pt->look));
789 if (!parse_bool_setting (lexer, "EMPTY", "HIDE", "SHOW",
791 && !parse_bool_setting (lexer, "ROWLABELS", "CORNER", "NESTED",
792 &look->row_labels_in_corner)
793 && !parse_bool_setting (lexer, "MARKERS", "NUMERIC", "ALPHA",
794 &look->show_numeric_markers)
795 && !parse_bool_setting (lexer, "LEVEL", "SUPER", "SUB",
796 &look->footnote_marker_superscripts)
797 && !parse_bool_setting (lexer, "LAYERS", "ALL", "CURRENT",
798 &look->print_all_layers)
799 && !parse_bool_setting (lexer, "PAGINATELAYERS", "YES", "NO",
800 &look->paginate_layers)
801 && !parse_bool_setting (lexer, "HSHRINK", "YES", "NO",
802 &look->shrink_to_fit[TABLE_HORZ])
803 && !parse_bool_setting (lexer, "VSHRINK", "YES", "NO",
804 &look->shrink_to_fit[TABLE_VERT])
805 && !parse_bool_setting (lexer, "TOPCONTINUATION", "YES", "NO",
806 &look->top_continuation)
807 && !parse_bool_setting (lexer, "BOTTOMCONTINUATION", "YES", "NO",
808 &look->bottom_continuation)
809 && !parse_string_setting (lexer, "CONTINUATION",
810 &look->continuation))
813 pivot_table_set_look (pt, look);
814 pivot_table_look_unref (look);
817 static enum table_stroke
818 read_stroke (struct lexer *lexer)
820 for (int stroke = 0; stroke < TABLE_N_STROKES; stroke++)
821 if (lex_match_id (lexer, table_stroke_to_string (stroke)))
824 lex_error (lexer, "expecting stroke");
829 parse_value_setting (struct lexer *lexer, const struct pivot_table *pt,
831 struct pivot_value **valuep,
832 struct table_area_style *base_style)
834 if (lex_match_id (lexer, name))
836 lex_match (lexer, T_EQUALS);
838 pivot_value_destroy (*valuep);
839 *valuep = read_value (lexer, pt, base_style);
848 read_border (struct lexer *lexer, struct pivot_table *pt)
850 static const char *const pivot_border_ids[PIVOT_N_BORDERS] = {
851 [PIVOT_BORDER_TITLE] = "title",
852 [PIVOT_BORDER_OUTER_LEFT] = "outer-left",
853 [PIVOT_BORDER_OUTER_TOP] = "outer-top",
854 [PIVOT_BORDER_OUTER_RIGHT] = "outer-right",
855 [PIVOT_BORDER_OUTER_BOTTOM] = "outer-bottom",
856 [PIVOT_BORDER_INNER_LEFT] = "inner-left",
857 [PIVOT_BORDER_INNER_TOP] = "inner-top",
858 [PIVOT_BORDER_INNER_RIGHT] = "inner-right",
859 [PIVOT_BORDER_INNER_BOTTOM] = "inner-bottom",
860 [PIVOT_BORDER_DATA_LEFT] = "data-left",
861 [PIVOT_BORDER_DATA_TOP] = "data-top",
862 [PIVOT_BORDER_DIM_ROW_HORZ] = "dim-row-horz",
863 [PIVOT_BORDER_DIM_ROW_VERT] = "dim-row-vert",
864 [PIVOT_BORDER_DIM_COL_HORZ] = "dim-col-horz",
865 [PIVOT_BORDER_DIM_COL_VERT] = "dim-col-vert",
866 [PIVOT_BORDER_CAT_ROW_HORZ] = "cat-row-horz",
867 [PIVOT_BORDER_CAT_ROW_VERT] = "cat-row-vert",
868 [PIVOT_BORDER_CAT_COL_HORZ] = "cat-col-horz",
869 [PIVOT_BORDER_CAT_COL_VERT] = "cat-col-vert",
872 lex_match (lexer, T_EQUALS);
874 struct pivot_table_look *look = pivot_table_look_unshare (
875 pivot_table_look_ref (pt->look));
876 while (lex_token (lexer) == T_STRING)
878 char *s = xstrdup (lex_tokcstr (lexer));
880 force_match (lexer, T_LPAREN);
882 struct table_border_style style = TABLE_BORDER_STYLE_INITIALIZER;
883 style.stroke = read_stroke (lexer);
884 if (lex_is_string (lexer))
885 style.color = read_color (lexer);
886 force_match (lexer, T_RPAREN);
889 for (int b = 0; b < PIVOT_N_BORDERS; b++)
891 if (!fnmatch (s, pivot_border_ids[b], 0))
893 look->borders[b] = style;
899 msg (SE, "%s: no matching borders", s);
904 pivot_table_set_look (pt, look);
905 pivot_table_look_unref (look);
909 read_footnote (struct lexer *lexer, struct pivot_table *pt)
912 if (lex_match (lexer, T_LBRACK))
916 idx = lex_integer (lexer);
919 force_match (lexer, T_RBRACK);
922 idx = pt->n_footnotes;
923 lex_match (lexer, T_EQUALS);
925 struct pivot_value *content
926 = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
928 struct pivot_value *marker;
929 if (lex_match_id (lexer, "MARKER"))
931 lex_match (lexer, T_EQUALS);
932 marker = read_value (lexer, pt, &pt->look->areas[PIVOT_AREA_FOOTER]);
937 pivot_table_create_footnote__ (pt, idx, marker, content);
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_command_name ();
1241 message_item_submit (message_item_create (&m));
1243 free (m.command_name);