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 "libpspp/assertion.h"
26 #include "libpspp/compiler.h"
27 #include "libpspp/string-map.h"
28 #include "output/ascii.h"
29 #include "output/driver.h"
30 #include "output/tab.h"
31 #include "output/table-item.h"
34 #include "gl/progname.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
38 /* --transpose: Transpose the table before outputting? */
41 /* --emphasis: ASCII driver emphasis option. */
43 static bool underline;
45 /* --box: ASCII driver box option. */
48 /* --draw-mode: special ASCII driver test mode. */
51 /* --no-txt: Whether to render to <base>.txt. */
52 static int render_txt = true;
54 /* --no-stdout: Whether to render to stdout. */
55 static int render_stdout = true;
57 /* --pdf: Also render PDF output. */
58 static int render_pdf;
60 /* --csv: Also render CSV output. */
61 static int render_csv;
63 /* ASCII driver, for ASCII driver test mode. */
64 static struct output_driver *ascii_driver;
66 /* -o, --output: Base name for output files. */
67 static const char *output_base = "render";
69 static const char *parse_options (int argc, char **argv);
70 static void usage (void) NO_RETURN;
71 static struct table *read_table (FILE *);
72 static void draw (FILE *);
75 main (int argc, char **argv)
77 const char *input_file_name;
80 set_program_name (argv[0]);
81 output_engine_push ();
82 input_file_name = parse_options (argc, argv);
84 if (!strcmp (input_file_name, "-"))
88 input = fopen (input_file_name, "r");
90 error (1, errno, "%s: open failed", input_file_name);
95 struct table **tables = NULL;
96 size_t allocated_tables = 0;
104 if (n_tables >= allocated_tables)
105 tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
107 tables[n_tables] = read_table (input);
116 table = tables[n_tables - 1];
118 table = table_transpose (table);
119 table_item_submit (table_item_create (table, NULL, NULL));
128 output_engine_pop ();
134 configure_drivers (int width, int length, int min_break)
136 struct string_map options, tmp;
137 struct output_driver *driver;
139 string_map_init (&options);
140 string_map_insert (&options, "format", "txt");
141 string_map_insert (&options, "output-file", "-");
142 string_map_insert_nocopy (&options, xstrdup ("width"),
143 xasprintf ("%d", width));
145 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
146 xasprintf ("%d", min_break));
147 if (bold || underline)
148 string_map_insert (&options, "emphasis", "true");
150 string_map_insert (&options, "box", box);
152 /* Render to stdout. */
155 string_map_clone (&tmp, &options);
156 ascii_driver = driver = output_driver_create (&tmp);
159 output_driver_register (driver);
160 string_map_destroy (&tmp);
165 string_map_destroy (&options);
169 /* Render to <base>.txt. */
172 string_map_clear (&options);
173 string_map_insert_nocopy (&options, xstrdup ("output-file"),
174 xasprintf ("%s.txt", output_base));
175 driver = output_driver_create (&options);
178 output_driver_register (driver);
182 /* Render to <base>.pdf. */
185 string_map_clear (&options);
186 string_map_insert_nocopy (&options, xstrdup ("output-file"),
187 xasprintf ("%s.pdf", output_base));
188 string_map_insert (&options, "top-margin", "0");
189 string_map_insert (&options, "bottom-margin", "0");
190 string_map_insert (&options, "left-margin", "0");
191 string_map_insert (&options, "right-margin", "0");
192 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
193 xasprintf ("%dx%dpt", width * 5, length * 8));
196 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
197 xasprintf ("%d", min_break * 5));
198 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
199 xasprintf ("%d", min_break * 8));
201 driver = output_driver_create (&options);
204 output_driver_register (driver);
208 /* Render to <base>.csv. */
211 string_map_clear (&options);
212 string_map_insert_nocopy (&options, xstrdup ("output-file"),
213 xasprintf ("%s.csv", output_base));
214 driver = output_driver_create (&options);
217 output_driver_register (driver);
220 /* Render to <base>.odt. */
221 string_map_replace_nocopy (&options, xstrdup ("output-file"),
222 xasprintf ("%s.odt", output_base));
223 driver = output_driver_create (&options);
226 output_driver_register (driver);
228 string_map_destroy (&options);
232 parse_options (int argc, char **argv)
241 OPT_WIDTH = UCHAR_MAX + 1,
248 static const struct option options[] =
250 {"width", required_argument, NULL, OPT_WIDTH},
251 {"length", required_argument, NULL, OPT_LENGTH},
252 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
253 {"transpose", no_argument, &transpose, 1},
254 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
255 {"box", required_argument, NULL, OPT_BOX},
256 {"draw-mode", no_argument, &draw_mode, 1},
257 {"no-txt", no_argument, &render_txt, 0},
258 {"no-stdout", no_argument, &render_stdout, 0},
259 {"pdf", no_argument, &render_pdf, 1},
260 {"csv", no_argument, &render_csv, 1},
261 {"output", required_argument, NULL, 'o'},
262 {"help", no_argument, NULL, OPT_HELP},
266 int c = getopt_long (argc, argv, "o:", options, NULL);
273 width = atoi (optarg);
277 length = atoi (optarg);
281 min_break = atoi (optarg);
285 if (!strcmp (optarg, "bold"))
290 else if (!strcmp (optarg, "underline"))
295 else if (!strcmp (optarg, "none"))
297 bold = underline = false;
300 error (1, 0, "argument to --emphasis must be \"bold\" or "
301 "\"underline\" or \"none\"");
309 output_base = optarg;
328 configure_drivers (width, length, min_break);
330 if (optind + 1 != argc)
331 error (1, 0, "exactly one non-option argument required; "
332 "use --help for help");
339 printf ("%s, to test rendering of PSPP tables\n"
340 "usage: %s [OPTIONS] INPUT\n"
342 " --width=WIDTH set page width in characters\n"
343 " --length=LINE set page length in lines\n",
344 program_name, program_name);
349 replace_newlines (char *p)
353 for (q = p; *p != '\0'; )
354 if (*p == '\\' && p[1] == 'n')
364 static struct table *
365 read_table (FILE *stream)
367 struct tab_table *tab;
371 int nr, nc, hl, hr, ht, hb;
373 size_t n_footnotes = 0;
375 if (fgets (buffer, sizeof buffer, stream) == NULL
376 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
377 &input[0], &input[1], &input[2],
378 &input[3], &input[4], &input[5])) < 2)
379 error (1, 0, "syntax error reading row and column count");
383 hl = n_input >= 3 ? input[2] : 0;
384 hr = n_input >= 4 ? input[3] : 0;
385 ht = n_input >= 5 ? input[4] : 0;
386 hb = n_input >= 6 ? input[5] : 0;
388 tab = tab_create (nc, nr);
389 tab_headers (tab, hl, hr, ht, hb);
390 for (r = 0; r < nr; r++)
391 for (c = 0; c < nc; c++)
392 if (tab_cell_is_empty (tab, c, r))
399 if (fgets (buffer, sizeof buffer, stream) == NULL)
400 error (1, 0, "unexpected end of input reading row %d, column %d",
402 new_line = strchr (buffer, '\n');
403 if (new_line != NULL)
407 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
409 while (*text != ' ' && *text != '\0')
421 while (*text && strchr ("<>^,@()|", *text))
425 tab_vline (tab, TAL_1, c, r, r + rs - 1);
429 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
433 tab_hline (tab, TAL_1, c, c + cs - 1, r);
437 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
441 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
442 c + cs - 1, r + rs - 1);
464 replace_newlines (text);
470 for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
472 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt,
476 char marker[2] = { 'a' + n_footnotes, '\0' };
477 struct footnote *f = tab_create_footnote (
478 tab, n_footnotes, content, marker);
479 tab_add_footnote (tab, c, r, f);
493 while (fgets (buffer, sizeof buffer, stream))
495 char text[sizeof buffer];
501 if (strchr ("#\r\n", buffer[0]))
504 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
505 ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
506 emph ? underline : false);
507 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
508 ascii_test_set_length (ascii_driver, y, length);
510 error (1, 0, "line %d has invalid format", line);
512 ascii_test_flush (ascii_driver);