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 /* --transpose: Transpose the table before outputting? */
42 /* --emphasis: ASCII driver emphasis option. */
44 static bool underline;
46 /* --box: ASCII driver box option. */
49 /* --draw-mode: special ASCII driver test mode. */
52 /* --no-txt: Whether to render to <base>.txt. */
53 static int render_txt = true;
55 /* --no-stdout: Whether to render to stdout. */
56 static int render_stdout = true;
58 /* --pdf: Also render PDF output. */
59 static int render_pdf;
61 /* --csv: Also render CSV output. */
62 static int render_csv;
64 /* ASCII driver, for ASCII driver test mode. */
65 static struct output_driver *ascii_driver;
67 /* -o, --output: Base name for output files. */
68 static const char *output_base = "render";
70 static const char *parse_options (int argc, char **argv);
71 static void usage (void) NO_RETURN;
72 static struct table *read_table (FILE *);
73 static void draw (FILE *);
76 main (int argc, char **argv)
78 const char *input_file_name;
81 set_program_name (argv[0]);
82 output_engine_push ();
83 input_file_name = parse_options (argc, argv);
85 if (!strcmp (input_file_name, "-"))
89 input = fopen (input_file_name, "r");
91 error (1, errno, "%s: open failed", input_file_name);
96 struct table **tables = NULL;
97 size_t allocated_tables = 0;
105 if (n_tables >= allocated_tables)
106 tables = x2nrealloc (tables, &allocated_tables, sizeof *tables);
108 tables[n_tables] = read_table (input);
117 table = tables[n_tables - 1];
119 table = table_transpose (table);
120 table_item_submit (table_item_create (table, NULL, NULL));
129 output_engine_pop ();
136 configure_drivers (int width, int length, int min_break)
138 struct string_map options, tmp;
139 struct output_driver *driver;
141 string_map_init (&options);
142 string_map_insert (&options, "format", "txt");
143 string_map_insert (&options, "output-file", "-");
144 string_map_insert_nocopy (&options, xstrdup ("width"),
145 xasprintf ("%d", width));
147 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
148 xasprintf ("%d", min_break));
149 if (bold || underline)
150 string_map_insert (&options, "emphasis", "true");
152 string_map_insert (&options, "box", box);
154 /* Render to stdout. */
157 string_map_clone (&tmp, &options);
158 ascii_driver = driver = output_driver_create (&tmp);
161 output_driver_register (driver);
162 string_map_destroy (&tmp);
167 string_map_destroy (&options);
171 /* Render to <base>.txt. */
174 string_map_clear (&options);
175 string_map_insert_nocopy (&options, xstrdup ("output-file"),
176 xasprintf ("%s.txt", output_base));
177 driver = output_driver_create (&options);
180 output_driver_register (driver);
184 /* Render to <base>.pdf. */
187 string_map_clear (&options);
188 string_map_insert_nocopy (&options, xstrdup ("output-file"),
189 xasprintf ("%s.pdf", output_base));
190 string_map_insert (&options, "top-margin", "0");
191 string_map_insert (&options, "bottom-margin", "0");
192 string_map_insert (&options, "left-margin", "0");
193 string_map_insert (&options, "right-margin", "0");
194 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
195 xasprintf ("%dx%dpt", width * 5, length * 8));
198 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
199 xasprintf ("%d", min_break * 5));
200 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
201 xasprintf ("%d", min_break * 8));
203 driver = output_driver_create (&options);
206 output_driver_register (driver);
210 /* Render to <base>.csv. */
213 string_map_clear (&options);
214 string_map_insert_nocopy (&options, xstrdup ("output-file"),
215 xasprintf ("%s.csv", output_base));
216 driver = output_driver_create (&options);
219 output_driver_register (driver);
222 /* Render to <base>.odt. */
223 string_map_replace_nocopy (&options, xstrdup ("output-file"),
224 xasprintf ("%s.odt", output_base));
225 driver = output_driver_create (&options);
228 output_driver_register (driver);
230 string_map_destroy (&options);
234 parse_options (int argc, char **argv)
243 OPT_WIDTH = UCHAR_MAX + 1,
250 static const struct option options[] =
252 {"width", required_argument, NULL, OPT_WIDTH},
253 {"length", required_argument, NULL, OPT_LENGTH},
254 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
255 {"transpose", no_argument, &transpose, 1},
256 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
257 {"box", required_argument, NULL, OPT_BOX},
258 {"draw-mode", no_argument, &draw_mode, 1},
259 {"no-txt", no_argument, &render_txt, 0},
260 {"no-stdout", no_argument, &render_stdout, 0},
261 {"pdf", no_argument, &render_pdf, 1},
262 {"csv", no_argument, &render_csv, 1},
263 {"output", required_argument, NULL, 'o'},
264 {"help", no_argument, NULL, OPT_HELP},
268 int c = getopt_long (argc, argv, "o:", options, NULL);
275 width = atoi (optarg);
279 length = atoi (optarg);
283 min_break = atoi (optarg);
287 if (!strcmp (optarg, "bold"))
292 else if (!strcmp (optarg, "underline"))
297 else if (!strcmp (optarg, "none"))
299 bold = underline = false;
302 error (1, 0, "argument to --emphasis must be \"bold\" or "
303 "\"underline\" or \"none\"");
311 output_base = optarg;
330 configure_drivers (width, length, min_break);
332 if (optind + 1 != argc)
333 error (1, 0, "exactly one non-option argument required; "
334 "use --help for help");
341 printf ("%s, to test rendering of PSPP tables\n"
342 "usage: %s [OPTIONS] INPUT\n"
344 " --width=WIDTH set page width in characters\n"
345 " --length=LINE set page length in lines\n",
346 program_name, program_name);
351 replace_newlines (char *p)
355 for (q = p; *p != '\0'; )
356 if (*p == '\\' && p[1] == 'n')
366 static struct table *
367 read_table (FILE *stream)
369 struct tab_table *tab;
373 int nr, nc, hl, hr, ht, hb;
375 size_t n_footnotes = 0;
377 if (fgets (buffer, sizeof buffer, stream) == NULL
378 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
379 &input[0], &input[1], &input[2],
380 &input[3], &input[4], &input[5])) < 2)
381 error (1, 0, "syntax error reading row and column count");
385 hl = n_input >= 3 ? input[2] : 0;
386 hr = n_input >= 4 ? input[3] : 0;
387 ht = n_input >= 5 ? input[4] : 0;
388 hb = n_input >= 6 ? input[5] : 0;
390 tab = tab_create (nc, nr);
391 tab_headers (tab, hl, hr, ht, hb);
392 for (r = 0; r < nr; r++)
393 for (c = 0; c < nc; c++)
394 if (tab_cell_is_empty (tab, c, r))
401 if (fgets (buffer, sizeof buffer, stream) == NULL)
402 error (1, 0, "unexpected end of input reading row %d, column %d",
404 new_line = strchr (buffer, '\n');
405 if (new_line != NULL)
409 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
411 while (*text != ' ' && *text != '\0')
423 while (*text && strchr ("<>^,@()|", *text))
427 tab_vline (tab, TAL_1, c, r, r + rs - 1);
431 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
435 tab_hline (tab, TAL_1, c, c + cs - 1, r);
439 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
443 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
444 c + cs - 1, r + rs - 1);
466 replace_newlines (text);
472 for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
474 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, opt,
478 char marker[2] = { 'a' + n_footnotes, '\0' };
479 struct footnote *f = tab_create_footnote (
480 tab, n_footnotes, content, marker, NULL);
481 tab_add_footnote (tab, c, r, f);
495 while (fgets (buffer, sizeof buffer, stream))
497 char text[sizeof buffer];
503 if (strchr ("#\r\n", buffer[0]))
506 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
507 ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
508 emph ? underline : false);
509 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
510 ascii_test_set_length (ascii_driver, y, length);
512 error (1, 0, "line %d has invalid format", line);
514 ascii_test_flush (ascii_driver);