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. */
42 static char *emphasis;
44 /* --box: ASCII driver box option. */
47 /* --draw-mode: special ASCII driver test mode. */
50 /* --pdf: Also render PDF output. */
51 static int render_pdf;
53 /* ASCII driver, for ASCII driver test mode. */
54 static struct output_driver *ascii_driver;
56 /* -o, --output: Base name for output files. */
57 static const char *output_base = "render";
59 static const char *parse_options (int argc, char **argv);
60 static void usage (void) NO_RETURN;
61 static struct table *read_table (FILE *);
62 static void draw (FILE *);
65 main (int argc, char **argv)
67 const char *input_file_name;
70 set_program_name (argv[0]);
71 input_file_name = parse_options (argc, argv);
73 if (!strcmp (input_file_name, "-"))
77 input = fopen (input_file_name, "r");
79 error (1, errno, "%s: open failed", input_file_name);
86 table = read_table (input);
89 table = table_transpose (table);
91 table_item_submit (table_item_create (table, NULL));
105 configure_drivers (int width, int length, int min_break)
107 struct string_map options, tmp;
108 struct output_driver *driver;
110 string_map_init (&options);
111 string_map_insert (&options, "format", "txt");
112 string_map_insert (&options, "output-file", "-");
113 string_map_insert_nocopy (&options, xstrdup ("width"),
114 xasprintf ("%d", width));
115 string_map_insert_nocopy (&options, xstrdup ("length"),
116 xasprintf ("%d", length));
119 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
120 xasprintf ("%d", min_break));
121 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
122 xasprintf ("%d", min_break));
124 if (emphasis != NULL)
125 string_map_insert (&options, "emphasis", emphasis);
127 string_map_insert (&options, "box", box);
129 /* Render to stdout. */
130 string_map_clone (&tmp, &options);
131 ascii_driver = driver = output_driver_create (&tmp);
134 output_driver_register (driver);
135 string_map_destroy (&tmp);
139 string_map_destroy (&options);
143 /* Render to <base>.txt. */
144 string_map_replace_nocopy (&options, xstrdup ("output-file"),
145 xasprintf ("%s.txt", output_base));
146 driver = output_driver_create (&options);
149 output_driver_register (driver);
152 /* Render to <base>.txt. */
155 string_map_replace_nocopy (&options, xstrdup ("output-file"),
156 xasprintf ("%s.pdf", output_base));
157 string_map_insert (&options, "top-margin", "0");
158 string_map_insert (&options, "bottom-margin", "0");
159 string_map_insert (&options, "left-margin", "0");
160 string_map_insert (&options, "right-margin", "0");
161 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
162 xasprintf ("%dx%dpt", width * 5, length * 8));
165 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
166 xasprintf ("%d", min_break * 5));
167 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
168 xasprintf ("%d", min_break * 8));
170 driver = output_driver_create (&options);
173 output_driver_register (driver);
177 /* Render to <base>.odt. */
178 string_map_replace_nocopy (&options, xstrdup ("output-file"),
179 xasprintf ("%s.odt", output_base));
180 driver = output_driver_create (&options);
183 output_driver_register (driver);
185 string_map_destroy (&options);
189 parse_options (int argc, char **argv)
198 OPT_WIDTH = UCHAR_MAX + 1,
205 static const struct option options[] =
207 {"width", required_argument, NULL, OPT_WIDTH},
208 {"length", required_argument, NULL, OPT_LENGTH},
209 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
210 {"transpose", no_argument, &transpose, 1},
211 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
212 {"box", required_argument, NULL, OPT_BOX},
213 {"draw-mode", no_argument, &draw_mode, 1},
214 {"pdf", no_argument, &render_pdf, 1},
215 {"output", required_argument, NULL, 'o'},
216 {"help", no_argument, NULL, OPT_HELP},
220 int c = getopt_long (argc, argv, "o:", options, NULL);
227 width = atoi (optarg);
231 length = atoi (optarg);
235 min_break = atoi (optarg);
247 output_base = optarg;
266 configure_drivers (width, length, min_break);
268 if (optind + 1 != argc)
269 error (1, 0, "exactly one non-option argument required; "
270 "use --help for help");
277 printf ("%s, to test rendering of PSPP tables\n"
278 "usage: %s [OPTIONS] INPUT\n"
280 " --width=WIDTH set page width in characters\n"
281 " --length=LINE set page length in lines\n",
282 program_name, program_name);
287 replace_newlines (char *p)
291 for (q = p; *p != '\0'; )
292 if (*p == '\\' && p[1] == 'n')
302 static struct table *
303 read_table (FILE *stream)
305 struct tab_table *tab;
309 int nr, nc, hl, hr, ht, hb;
312 if (fgets (buffer, sizeof buffer, stream) == NULL
313 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
314 &input[0], &input[1], &input[2],
315 &input[3], &input[4], &input[5])) < 2)
316 error (1, 0, "syntax error reading row and column count");
320 hl = n_input >= 3 ? input[2] : 0;
321 hr = n_input >= 4 ? input[3] : 0;
322 ht = n_input >= 5 ? input[4] : 0;
323 hb = n_input >= 6 ? input[5] : 0;
325 tab = tab_create (nc, nr);
326 tab_headers (tab, hl, hr, ht, hb);
327 for (r = 0; r < nr; r++)
328 for (c = 0; c < nc; c++)
329 if (tab_cell_is_empty (tab, c, r))
335 if (fgets (buffer, sizeof buffer, stream) == NULL)
336 error (1, 0, "unexpected end of input reading row %d, column %d",
338 new_line = strchr (buffer, '\n');
339 if (new_line != NULL)
343 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
345 while (*text != ' ' && *text != '\0')
356 while (*text && strchr ("<>^,@", *text))
360 tab_vline (tab, TAL_1, c, r, r + rs - 1);
364 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
368 tab_hline (tab, TAL_1, c, c + cs - 1, r);
372 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
376 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
377 c + cs - 1, r + rs - 1);
384 replace_newlines (text);
386 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
389 if (getc (stream) != EOF)
390 error (1, 0, "unread data at end of input");
401 while (fgets (buffer, sizeof buffer, stream))
403 char text[sizeof buffer];
409 if (strchr ("#\r\n", buffer[0]))
412 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
413 ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
414 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
415 ascii_test_set_length (ascii_driver, y, length);
417 error (1, 0, "line %d has invalid format", line);