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/>. */
25 #include "data/file-handle-def.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/compiler.h"
28 #include "libpspp/string-map.h"
29 #include "output/ascii.h"
30 #include "output/driver.h"
31 #include "output/tab.h"
32 #include "output/table-item.h"
35 #include "gl/progname.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
39 /* --emphasis: ASCII driver emphasis option. */
41 static bool underline;
43 /* --box: ASCII driver box option. */
46 /* --draw-mode: special ASCII driver test mode. */
49 /* --no-txt: Whether to render to <base>.txt. */
50 static int render_txt = true;
52 /* --no-stdout: Whether to render to stdout. */
53 static int render_stdout = true;
55 /* --pdf: Also render PDF output. */
56 static int render_pdf;
58 /* --csv: Also render CSV output. */
59 static int render_csv;
61 /* ASCII driver, for ASCII driver test mode. */
62 static struct output_driver *ascii_driver;
64 /* -o, --output: Base name for output files. */
65 static const char *output_base = "render";
67 static const char *parse_options (int argc, char **argv);
68 static void usage (void) NO_RETURN;
69 static struct table *read_table (FILE *);
70 static void draw (FILE *);
73 main (int argc, char **argv)
75 const char *input_file_name;
78 set_program_name (argv[0]);
79 output_engine_push ();
80 input_file_name = parse_options (argc, argv);
82 if (!strcmp (input_file_name, "-"))
86 input = fopen (input_file_name, "r");
88 error (1, errno, "%s: open failed", input_file_name);
93 struct table **tables = NULL;
94 size_t allocated_tables = 0;
102 if (n_tables >= allocated_tables)
103 tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
105 tables[n_tables] = read_table (input);
114 table = tables[n_tables - 1];
115 table_item_submit (table_item_create (table, NULL, NULL));
124 output_engine_pop ();
131 configure_drivers (int width, int length, int min_break)
133 struct string_map options, tmp;
134 struct output_driver *driver;
136 string_map_init (&options);
137 string_map_insert (&options, "format", "txt");
138 string_map_insert (&options, "output-file", "-");
139 string_map_insert_nocopy (&options, xstrdup ("width"),
140 xasprintf ("%d", width));
142 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
143 xasprintf ("%d", min_break));
144 if (bold || underline)
145 string_map_insert (&options, "emphasis", "true");
147 string_map_insert (&options, "box", box);
149 /* Render to stdout. */
152 string_map_clone (&tmp, &options);
153 ascii_driver = driver = output_driver_create (&tmp);
156 output_driver_register (driver);
157 string_map_destroy (&tmp);
162 string_map_destroy (&options);
166 /* Render to <base>.txt. */
169 string_map_clear (&options);
170 string_map_insert_nocopy (&options, xstrdup ("output-file"),
171 xasprintf ("%s.txt", output_base));
172 driver = output_driver_create (&options);
175 output_driver_register (driver);
179 /* Render to <base>.pdf. */
182 string_map_clear (&options);
183 string_map_insert_nocopy (&options, xstrdup ("output-file"),
184 xasprintf ("%s.pdf", output_base));
185 string_map_insert (&options, "top-margin", "0");
186 string_map_insert (&options, "bottom-margin", "0");
187 string_map_insert (&options, "left-margin", "0");
188 string_map_insert (&options, "right-margin", "0");
189 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
190 xasprintf ("%dx%dpt", width * 5, length * 8));
193 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
194 xasprintf ("%d", min_break * 5));
195 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
196 xasprintf ("%d", min_break * 8));
198 driver = output_driver_create (&options);
201 output_driver_register (driver);
205 /* Render to <base>.csv. */
208 string_map_clear (&options);
209 string_map_insert_nocopy (&options, xstrdup ("output-file"),
210 xasprintf ("%s.csv", output_base));
211 driver = output_driver_create (&options);
214 output_driver_register (driver);
217 /* Render to <base>.odt. */
218 string_map_replace_nocopy (&options, xstrdup ("output-file"),
219 xasprintf ("%s.odt", output_base));
220 driver = output_driver_create (&options);
223 output_driver_register (driver);
225 string_map_destroy (&options);
229 parse_options (int argc, char **argv)
238 OPT_WIDTH = UCHAR_MAX + 1,
245 static const struct option options[] =
247 {"width", required_argument, NULL, OPT_WIDTH},
248 {"length", required_argument, NULL, OPT_LENGTH},
249 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
250 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
251 {"box", required_argument, NULL, OPT_BOX},
252 {"draw-mode", no_argument, &draw_mode, 1},
253 {"no-txt", no_argument, &render_txt, 0},
254 {"no-stdout", no_argument, &render_stdout, 0},
255 {"pdf", no_argument, &render_pdf, 1},
256 {"csv", no_argument, &render_csv, 1},
257 {"output", required_argument, NULL, 'o'},
258 {"help", no_argument, NULL, OPT_HELP},
262 int c = getopt_long (argc, argv, "o:", options, NULL);
269 width = atoi (optarg);
273 length = atoi (optarg);
277 min_break = atoi (optarg);
281 if (!strcmp (optarg, "bold"))
286 else if (!strcmp (optarg, "underline"))
291 else if (!strcmp (optarg, "none"))
293 bold = underline = false;
296 error (1, 0, "argument to --emphasis must be \"bold\" or "
297 "\"underline\" or \"none\"");
305 output_base = optarg;
324 configure_drivers (width, length, min_break);
326 if (optind + 1 != argc)
327 error (1, 0, "exactly one non-option argument required; "
328 "use --help for help");
335 printf ("%s, to test rendering of PSPP tables\n"
336 "usage: %s [OPTIONS] INPUT\n"
338 " --width=WIDTH set page width in characters\n"
339 " --length=LINE set page length in lines\n",
340 program_name, program_name);
345 replace_newlines (char *p)
349 for (q = p; *p != '\0'; )
350 if (*p == '\\' && p[1] == 'n')
360 static struct table *
361 read_table (FILE *stream)
363 struct tab_table *tab;
367 int nr, nc, hl, hr, ht, hb;
369 size_t n_footnotes = 0;
371 if (fgets (buffer, sizeof buffer, stream) == NULL
372 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
373 &input[0], &input[1], &input[2],
374 &input[3], &input[4], &input[5])) < 2)
375 error (1, 0, "syntax error reading row and column count");
379 hl = n_input >= 3 ? input[2] : 0;
380 hr = n_input >= 4 ? input[3] : 0;
381 ht = n_input >= 5 ? input[4] : 0;
382 hb = n_input >= 6 ? input[5] : 0;
384 tab = tab_create (nc, nr);
385 tab_headers (tab, hl, hr, ht, hb);
386 for (r = 0; r < nr; r++)
387 for (c = 0; c < nc; c++)
388 if (tab_cell_is_empty (tab, c, r))
395 if (fgets (buffer, sizeof buffer, stream) == NULL)
396 error (1, 0, "unexpected end of input reading row %d, column %d",
398 new_line = strchr (buffer, '\n');
399 if (new_line != NULL)
403 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
405 while (*text != ' ' && *text != '\0')
417 while (*text && strchr ("<>^,@()|", *text))
421 tab_vline (tab, TAL_1, c, r, r + rs - 1);
425 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
429 tab_hline (tab, TAL_1, c, c + cs - 1, r);
433 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
437 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
438 c + cs - 1, r + rs - 1);
460 replace_newlines (text);
466 for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
468 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt,
472 char marker[2] = { 'a' + n_footnotes, '\0' };
473 struct footnote *f = tab_create_footnote (
474 tab, n_footnotes, content, marker, NULL);
475 tab_add_footnote (tab, c, r, f);
489 while (fgets (buffer, sizeof buffer, stream))
491 char text[sizeof buffer];
497 if (strchr ("#\r\n", buffer[0]))
500 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
501 ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
502 emph ? underline : false);
503 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
504 ascii_test_set_length (ascii_driver, y, length);
506 error (1, 0, "line %d has invalid format", line);
508 ascii_test_flush (ascii_driver);