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/i18n.h"
29 #include "libpspp/string-map.h"
30 #include "output/ascii.h"
31 #include "output/driver.h"
32 #include "output/table.h"
33 #include "output/table-item.h"
36 #include "gl/progname.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
40 /* --emphasis: ASCII driver emphasis option. */
42 static bool underline;
44 /* --box: ASCII driver box option. */
47 /* --draw-mode: special ASCII driver test mode. */
50 /* --no-txt: Whether to render to <base>.txt. */
51 static int render_txt = true;
53 /* --no-stdout: Whether to render to stdout. */
54 static int render_stdout = true;
56 /* --pdf: Also render PDF output. */
57 static int render_pdf;
59 /* --csv: Also render CSV output. */
60 static int render_csv;
62 /* ASCII driver, for ASCII driver test mode. */
63 static struct output_driver *ascii_driver;
65 /* -o, --output: Base name for output files. */
66 static const char *output_base = "render";
68 static const char *parse_options (int argc, char **argv);
69 static void usage (void) NO_RETURN;
70 static struct table *read_table (FILE *);
71 static void draw (FILE *);
74 main (int argc, char **argv)
76 const char *input_file_name;
79 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];
117 table_item_submit (table_item_create (table, NULL, NULL));
126 output_engine_pop ();
133 configure_drivers (int width, int length, int min_break)
135 struct string_map options, tmp;
136 struct output_driver *driver;
138 string_map_init (&options);
139 string_map_insert (&options, "format", "txt");
140 string_map_insert (&options, "output-file", "-");
141 string_map_insert_nocopy (&options, xstrdup ("width"),
142 xasprintf ("%d", width));
144 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
145 xasprintf ("%d", min_break));
146 if (bold || underline)
147 string_map_insert (&options, "emphasis", "true");
149 string_map_insert (&options, "box", box);
151 /* Render to stdout. */
154 string_map_clone (&tmp, &options);
155 ascii_driver = driver = output_driver_create (&tmp);
158 output_driver_register (driver);
159 string_map_destroy (&tmp);
164 string_map_destroy (&options);
168 /* Render to <base>.txt. */
171 string_map_clear (&options);
172 string_map_insert_nocopy (&options, xstrdup ("output-file"),
173 xasprintf ("%s.txt", output_base));
174 driver = output_driver_create (&options);
177 output_driver_register (driver);
181 /* Render to <base>.pdf. */
184 string_map_clear (&options);
185 string_map_insert_nocopy (&options, xstrdup ("output-file"),
186 xasprintf ("%s.pdf", output_base));
187 string_map_insert (&options, "top-margin", "0");
188 string_map_insert (&options, "bottom-margin", "0");
189 string_map_insert (&options, "left-margin", "0");
190 string_map_insert (&options, "right-margin", "0");
191 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
192 xasprintf ("%dx%dpt", width * 5, length * 8));
195 string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
196 xasprintf ("%d", min_break * 5));
197 string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
198 xasprintf ("%d", min_break * 8));
200 driver = output_driver_create (&options);
203 output_driver_register (driver);
207 /* Render to <base>.csv. */
210 string_map_clear (&options);
211 string_map_insert_nocopy (&options, xstrdup ("output-file"),
212 xasprintf ("%s.csv", output_base));
213 driver = output_driver_create (&options);
216 output_driver_register (driver);
219 /* Render to <base>.odt. */
220 string_map_replace_nocopy (&options, xstrdup ("output-file"),
221 xasprintf ("%s.odt", output_base));
222 driver = output_driver_create (&options);
225 output_driver_register (driver);
227 string_map_destroy (&options);
231 parse_options (int argc, char **argv)
240 OPT_WIDTH = UCHAR_MAX + 1,
247 static const struct option options[] =
249 {"width", required_argument, NULL, OPT_WIDTH},
250 {"length", required_argument, NULL, OPT_LENGTH},
251 {"min-break", required_argument, NULL, OPT_MIN_BREAK},
252 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
253 {"box", required_argument, NULL, OPT_BOX},
254 {"draw-mode", no_argument, &draw_mode, 1},
255 {"no-txt", no_argument, &render_txt, 0},
256 {"no-stdout", no_argument, &render_stdout, 0},
257 {"pdf", no_argument, &render_pdf, 1},
258 {"csv", no_argument, &render_csv, 1},
259 {"output", required_argument, NULL, 'o'},
260 {"help", no_argument, NULL, OPT_HELP},
264 int c = getopt_long (argc, argv, "o:", options, NULL);
271 width = atoi (optarg);
275 length = atoi (optarg);
279 min_break = atoi (optarg);
283 if (!strcmp (optarg, "bold"))
288 else if (!strcmp (optarg, "underline"))
293 else if (!strcmp (optarg, "none"))
295 bold = underline = false;
298 error (1, 0, "argument to --emphasis must be \"bold\" or "
299 "\"underline\" or \"none\"");
307 output_base = optarg;
326 configure_drivers (width, length, min_break);
328 if (optind + 1 != argc)
329 error (1, 0, "exactly one non-option argument required; "
330 "use --help for help");
337 printf ("%s, to test rendering of PSPP tables\n"
338 "usage: %s [OPTIONS] INPUT\n"
340 " --width=WIDTH set page width in characters\n"
341 " --length=LINE set page length in lines\n",
342 program_name, program_name);
347 replace_newlines (char *p)
351 for (q = p; *p != '\0';)
352 if (*p == '\\' && p[1] == 'n')
362 static struct table *
363 read_table (FILE *stream)
369 int nr, nc, hl, hr, ht, hb;
371 size_t n_footnotes = 0;
373 if (fgets (buffer, sizeof buffer, stream) == NULL
374 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
375 &input[0], &input[1], &input[2],
376 &input[3], &input[4], &input[5])) < 2)
377 error (1, 0, "syntax error reading row and column count");
381 hl = n_input >= 3 ? input[2] : 0;
382 hr = n_input >= 4 ? input[3] : 0;
383 ht = n_input >= 5 ? input[4] : 0;
384 hb = n_input >= 6 ? input[5] : 0;
386 tab = table_create (nc, nr, hl, hr, ht, hb);
387 for (r = 0; r < nr; r++)
388 for (c = 0; c < nc; c++)
389 if (table_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')
416 #define S(H) { AREA_STYLE_INITIALIZER__, .cell_style.halign = H }
417 static const struct area_style left_style = S (TABLE_HALIGN_LEFT);
418 static const struct area_style right_style = S (TABLE_HALIGN_RIGHT);
419 static const struct area_style center_style
420 = S (TABLE_HALIGN_CENTER);
422 const struct area_style *style = &right_style;
423 while (*text && strchr ("<>^,@()|", *text))
427 table_vline (tab, TABLE_STROKE_SOLID, c, r, r + rs - 1);
431 table_vline (tab, TABLE_STROKE_SOLID, c + cs, r, r + rs - 1);
435 table_hline (tab, TABLE_STROKE_SOLID, c, c + cs - 1, r);
439 table_hline (tab, TABLE_STROKE_SOLID, c, c + cs - 1, r + rs);
443 table_box (tab, TABLE_STROKE_SOLID, TABLE_STROKE_SOLID,
444 -1, -1, c, r, c + cs - 1, r + rs - 1);
452 style = &right_style;
456 style = ¢er_style;
463 replace_newlines (text);
469 for (i = 0; (content = strsep (&pos, "#")) != NULL; i++)
472 table_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0,
474 table_add_style (tab, c, r, style);
478 char marker[2] = { 'a' + n_footnotes, '\0' };
479 struct footnote *f = table_create_footnote (
480 tab, n_footnotes, content, marker,
481 area_style_clone (tab->container, &left_style));
482 table_add_footnote (tab, c, r, f);
496 while (fgets (buffer, sizeof buffer, stream))
498 char text[sizeof buffer];
504 if (strchr ("#\r\n", buffer[0]))
507 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
508 ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
509 emph ? underline : false);
510 else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
511 ascii_test_set_length (ascii_driver, y, length);
513 error (1, 0, "line %d has invalid format", line);
515 ascii_test_flush (ascii_driver);