render-test: Add output file option.
[pspp] / tests / output / render-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
24
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"
32
33 #include "gl/error.h"
34 #include "gl/progname.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
37
38 /* --transpose: Transpose the table before outputting? */
39 static int transpose;
40
41 /* --emphasis: ASCII driver emphasis option. */
42 static char *emphasis;
43
44 /* --box: ASCII driver box option. */
45 static char *box;
46
47 /* --draw-mode: special ASCII driver test mode. */
48 static int draw_mode;
49
50 /* --pdf: Also render PDF output. */
51 static int render_pdf;
52
53 /* ASCII driver, for ASCII driver test mode. */
54 static struct output_driver *ascii_driver;
55
56 /* -o, --output: Base name for output files. */
57 static const char *output_base = "render";
58
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 *);
63
64 int
65 main (int argc, char **argv)
66 {
67   const char *input_file_name;
68   FILE *input;
69
70   set_program_name (argv[0]);
71   input_file_name = parse_options (argc, argv);
72
73   if (!strcmp (input_file_name, "-"))
74     input = stdin;
75   else
76     {
77       input = fopen (input_file_name, "r");
78       if (input == NULL)
79         error (1, errno, "%s: open failed", input_file_name);
80     }
81
82   if (!draw_mode)
83     {
84       struct table *table;
85
86       table = read_table (input);
87
88       if (transpose)
89         table = table_transpose (table);
90
91       table_item_submit (table_item_create (table, NULL));
92     }
93   else
94     draw (input);
95
96   if (input != stdin)
97     fclose (input);
98
99   output_close ();
100
101   return 0;
102 }
103
104 static void
105 configure_drivers (int width, int length, int min_break)
106 {
107   struct string_map options, tmp;
108   struct output_driver *driver;
109
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));
117   if (min_break >= 0)
118     {
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));
123     }
124   if (emphasis != NULL)
125     string_map_insert (&options, "emphasis", emphasis);
126   if (box != NULL)
127     string_map_insert (&options, "box", box);
128
129   /* Render to stdout. */
130   string_map_clone (&tmp, &options);
131   ascii_driver = driver = output_driver_create (&tmp);
132   if (driver == NULL)
133     exit (EXIT_FAILURE);
134   output_driver_register (driver);
135   string_map_destroy (&tmp);
136
137   if (draw_mode)
138    {
139     string_map_destroy (&options);
140     return;
141    }
142
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);
147   if (driver == NULL)
148     exit (EXIT_FAILURE);
149   output_driver_register (driver);
150
151 #ifdef HAVE_CAIRO
152   /* Render to <base>.txt. */
153   if (render_pdf)
154     {
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));
163       if (min_break >= 0)
164         {
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));
169         }
170       driver = output_driver_create (&options);
171       if (driver == NULL)
172         exit (EXIT_FAILURE);
173       output_driver_register (driver);
174     }
175 #endif
176
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);
181   if (driver == NULL)
182     exit (EXIT_FAILURE);
183   output_driver_register (driver);
184
185   string_map_destroy (&options);
186 }
187
188 static const char *
189 parse_options (int argc, char **argv)
190 {
191   int width = 79;
192   int length = 66;
193   int min_break = -1;
194
195   for (;;)
196     {
197       enum {
198         OPT_WIDTH = UCHAR_MAX + 1,
199         OPT_LENGTH,
200         OPT_MIN_BREAK,
201         OPT_EMPHASIS,
202         OPT_BOX,
203         OPT_HELP
204       };
205       static const struct option options[] =
206         {
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},
217           {NULL, 0, NULL, 0},
218         };
219
220       int c = getopt_long (argc, argv, "o:", options, NULL);
221       if (c == -1)
222         break;
223
224       switch (c)
225         {
226         case OPT_WIDTH:
227           width = atoi (optarg);
228           break;
229
230         case OPT_LENGTH:
231           length = atoi (optarg);
232           break;
233
234         case OPT_MIN_BREAK:
235           min_break = atoi (optarg);
236           break;
237
238         case OPT_EMPHASIS:
239           emphasis = optarg;
240           break;
241
242         case OPT_BOX:
243           box = optarg;
244           break;
245
246         case 'o':
247           output_base = optarg;
248           break;
249
250         case OPT_HELP:
251           usage ();
252
253         case 0:
254           break;
255
256         case '?':
257           exit(EXIT_FAILURE);
258           break;
259
260         default:
261           NOT_REACHED ();
262         }
263
264     }
265
266   configure_drivers (width, length, min_break);
267
268   if (optind + 1 != argc)
269     error (1, 0, "exactly one non-option argument required; "
270            "use --help for help");
271   return argv[optind];
272 }
273
274 static void
275 usage (void)
276 {
277   printf ("%s, to test rendering of PSPP tables\n"
278           "usage: %s [OPTIONS] INPUT\n"
279           "\nOptions:\n"
280           "  --width=WIDTH   set page width in characters\n"
281           "  --length=LINE   set page length in lines\n",
282           program_name, program_name);
283   exit (EXIT_SUCCESS);
284 }
285
286 static void
287 replace_newlines (char *p)
288 {
289   char *q;
290
291   for (q = p; *p != '\0'; )
292     if (*p == '\\' && p[1] == 'n')
293       {
294         *q++ = '\n';
295         p += 2;
296       }
297     else
298       *q++ = *p++;
299   *q = '\0';
300 }
301
302 static struct table *
303 read_table (FILE *stream)
304 {
305   struct tab_table *tab;
306   char buffer[1024];
307   int input[6];
308   int n_input = 0;
309   int nr, nc, hl, hr, ht, hb;
310   int r, c;
311
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");
317
318   nr = input[0];
319   nc = input[1];
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;
324
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))
330         {
331           char *new_line;
332           char *text;
333           int rs, cs;
334
335           if (fgets (buffer, sizeof buffer, stream) == NULL)
336             error (1, 0, "unexpected end of input reading row %d, column %d",
337                    r, c);
338           new_line = strchr (buffer, '\n');
339           if (new_line != NULL)
340             *new_line = '\0';
341
342           text = buffer;
343           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
344             {
345               while (*text != ' ' && *text != '\0')
346                 text++;
347               if (*text == ' ')
348                 text++;
349             }
350           else
351             {
352               rs = 1;
353               cs = 1;
354             }
355
356           while (*text && strchr ("<>^,@", *text))
357             switch (*text++)
358               {
359               case '<':
360                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
361                 break;
362
363               case '>':
364                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
365                 break;
366
367               case '^':
368                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
369                 break;
370
371               case ',':
372                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
373                 break;
374
375               case '@':
376                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
377                          c + cs - 1, r + rs - 1);
378                 break;
379
380               default:
381                 NOT_REACHED ();
382               }
383
384           replace_newlines (text);
385
386           tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
387         }
388
389   if (getc (stream) != EOF)
390     error (1, 0, "unread data at end of input");
391
392   return &tab->table;
393 }
394
395 static void
396 draw (FILE *stream)
397 {
398   char buffer[1024];
399   int line = 0;
400
401   while (fgets (buffer, sizeof buffer, stream))
402     {
403       char text[sizeof buffer];
404       int length;
405       int emph;
406       int x, y;
407
408       line++;
409       if (strchr ("#\r\n", buffer[0]))
410         continue;
411
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);
416       else
417         error (1, 0, "line %d has invalid format", line);
418     }
419 }