render-test: Add --no-txt and --no-stdout options.
[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 /* --no-txt: Whether to render to <base>.txt. */
51 static int render_txt = true;
52
53 /* --no-stdout: Whether to render to stdout. */
54 static int render_stdout = true;
55
56 /* --pdf: Also render PDF output. */
57 static int render_pdf;
58
59 /* ASCII driver, for ASCII driver test mode. */
60 static struct output_driver *ascii_driver;
61
62 /* -o, --output: Base name for output files. */
63 static const char *output_base = "render";
64
65 static const char *parse_options (int argc, char **argv);
66 static void usage (void) NO_RETURN;
67 static struct table *read_table (FILE *);
68 static void draw (FILE *);
69
70 int
71 main (int argc, char **argv)
72 {
73   const char *input_file_name;
74   FILE *input;
75
76   set_program_name (argv[0]);
77   input_file_name = parse_options (argc, argv);
78
79   if (!strcmp (input_file_name, "-"))
80     input = stdin;
81   else
82     {
83       input = fopen (input_file_name, "r");
84       if (input == NULL)
85         error (1, errno, "%s: open failed", input_file_name);
86     }
87
88   if (!draw_mode)
89     {
90       struct table *table;
91
92       table = read_table (input);
93
94       if (transpose)
95         table = table_transpose (table);
96
97       table_item_submit (table_item_create (table, NULL));
98     }
99   else
100     draw (input);
101
102   if (input != stdin)
103     fclose (input);
104
105   output_close ();
106
107   return 0;
108 }
109
110 static void
111 configure_drivers (int width, int length, int min_break)
112 {
113   struct string_map options, tmp;
114   struct output_driver *driver;
115
116   string_map_init (&options);
117   string_map_insert (&options, "format", "txt");
118   string_map_insert (&options, "output-file", "-");
119   string_map_insert_nocopy (&options, xstrdup ("width"),
120                             xasprintf ("%d", width));
121   string_map_insert_nocopy (&options, xstrdup ("length"),
122                             xasprintf ("%d", length));
123   if (min_break >= 0)
124     {
125       string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
126                                 xasprintf ("%d", min_break));
127       string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
128                                 xasprintf ("%d", min_break));
129     }
130   if (emphasis != NULL)
131     string_map_insert (&options, "emphasis", emphasis);
132   if (box != NULL)
133     string_map_insert (&options, "box", box);
134
135   /* Render to stdout. */
136   if (render_stdout)
137     {
138       string_map_clone (&tmp, &options);
139       ascii_driver = driver = output_driver_create (&tmp);
140       if (driver == NULL)
141         exit (EXIT_FAILURE);
142       output_driver_register (driver);
143       string_map_destroy (&tmp);
144     }
145
146   if (draw_mode)
147    {
148      string_map_destroy (&options);
149      return;
150    }
151
152   /* Render to <base>.txt. */
153   if (render_txt)
154     {
155       string_map_clear (&options);
156       string_map_insert_nocopy (&options, xstrdup ("output-file"),
157                                 xasprintf ("%s.txt", output_base));
158       driver = output_driver_create (&options);
159       if (driver == NULL)
160         exit (EXIT_FAILURE);
161       output_driver_register (driver);
162     }
163
164 #ifdef HAVE_CAIRO
165   /* Render to <base>.pdf. */
166   if (render_pdf)
167     {
168       string_map_clear (&options);
169       string_map_insert_nocopy (&options, xstrdup ("output-file"),
170                                  xasprintf ("%s.pdf", output_base));
171       string_map_insert (&options, "top-margin", "0");
172       string_map_insert (&options, "bottom-margin", "0");
173       string_map_insert (&options, "left-margin", "0");
174       string_map_insert (&options, "right-margin", "0");
175       string_map_insert_nocopy (&options, xstrdup ("paper-size"),
176                                 xasprintf ("%dx%dpt", width * 5, length * 8));
177       if (min_break >= 0)
178         {
179           string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
180                                     xasprintf ("%d", min_break * 5));
181           string_map_insert_nocopy (&options, xstrdup ("min-vbreak"),
182                                     xasprintf ("%d", min_break * 8));
183         }
184       driver = output_driver_create (&options);
185       if (driver == NULL)
186         exit (EXIT_FAILURE);
187       output_driver_register (driver);
188     }
189 #endif
190
191   /* Render to <base>.odt. */
192   string_map_replace_nocopy (&options, xstrdup ("output-file"),
193                              xasprintf ("%s.odt", output_base));
194   driver = output_driver_create (&options);
195   if (driver == NULL)
196     exit (EXIT_FAILURE);
197   output_driver_register (driver);
198
199   string_map_destroy (&options);
200 }
201
202 static const char *
203 parse_options (int argc, char **argv)
204 {
205   int width = 79;
206   int length = 66;
207   int min_break = -1;
208
209   for (;;)
210     {
211       enum {
212         OPT_WIDTH = UCHAR_MAX + 1,
213         OPT_LENGTH,
214         OPT_MIN_BREAK,
215         OPT_EMPHASIS,
216         OPT_BOX,
217         OPT_HELP
218       };
219       static const struct option options[] =
220         {
221           {"width", required_argument, NULL, OPT_WIDTH},
222           {"length", required_argument, NULL, OPT_LENGTH},
223           {"min-break", required_argument, NULL, OPT_MIN_BREAK},
224           {"transpose", no_argument, &transpose, 1},
225           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
226           {"box", required_argument, NULL, OPT_BOX},
227           {"draw-mode", no_argument, &draw_mode, 1},
228           {"no-txt", no_argument, &render_txt, 0},
229           {"no-stdout", no_argument, &render_stdout, 0},
230           {"pdf", no_argument, &render_pdf, 1},
231           {"output", required_argument, NULL, 'o'},
232           {"help", no_argument, NULL, OPT_HELP},
233           {NULL, 0, NULL, 0},
234         };
235
236       int c = getopt_long (argc, argv, "o:", options, NULL);
237       if (c == -1)
238         break;
239
240       switch (c)
241         {
242         case OPT_WIDTH:
243           width = atoi (optarg);
244           break;
245
246         case OPT_LENGTH:
247           length = atoi (optarg);
248           break;
249
250         case OPT_MIN_BREAK:
251           min_break = atoi (optarg);
252           break;
253
254         case OPT_EMPHASIS:
255           emphasis = optarg;
256           break;
257
258         case OPT_BOX:
259           box = optarg;
260           break;
261
262         case 'o':
263           output_base = optarg;
264           break;
265
266         case OPT_HELP:
267           usage ();
268
269         case 0:
270           break;
271
272         case '?':
273           exit(EXIT_FAILURE);
274           break;
275
276         default:
277           NOT_REACHED ();
278         }
279
280     }
281
282   configure_drivers (width, length, min_break);
283
284   if (optind + 1 != argc)
285     error (1, 0, "exactly one non-option argument required; "
286            "use --help for help");
287   return argv[optind];
288 }
289
290 static void
291 usage (void)
292 {
293   printf ("%s, to test rendering of PSPP tables\n"
294           "usage: %s [OPTIONS] INPUT\n"
295           "\nOptions:\n"
296           "  --width=WIDTH   set page width in characters\n"
297           "  --length=LINE   set page length in lines\n",
298           program_name, program_name);
299   exit (EXIT_SUCCESS);
300 }
301
302 static void
303 replace_newlines (char *p)
304 {
305   char *q;
306
307   for (q = p; *p != '\0'; )
308     if (*p == '\\' && p[1] == 'n')
309       {
310         *q++ = '\n';
311         p += 2;
312       }
313     else
314       *q++ = *p++;
315   *q = '\0';
316 }
317
318 static struct table *
319 read_table (FILE *stream)
320 {
321   struct tab_table *tab;
322   char buffer[1024];
323   int input[6];
324   int n_input = 0;
325   int nr, nc, hl, hr, ht, hb;
326   int r, c;
327
328   if (fgets (buffer, sizeof buffer, stream) == NULL
329       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
330                             &input[0], &input[1], &input[2],
331                             &input[3], &input[4], &input[5])) < 2)
332     error (1, 0, "syntax error reading row and column count");
333
334   nr = input[0];
335   nc = input[1];
336   hl = n_input >= 3 ? input[2] : 0;
337   hr = n_input >= 4 ? input[3] : 0;
338   ht = n_input >= 5 ? input[4] : 0;
339   hb = n_input >= 6 ? input[5] : 0;
340
341   tab = tab_create (nc, nr);
342   tab_headers (tab, hl, hr, ht, hb);
343   for (r = 0; r < nr; r++)
344     for (c = 0; c < nc; c++)
345       if (tab_cell_is_empty (tab, c, r))
346         {
347           char *new_line;
348           char *text;
349           int rs, cs;
350
351           if (fgets (buffer, sizeof buffer, stream) == NULL)
352             error (1, 0, "unexpected end of input reading row %d, column %d",
353                    r, c);
354           new_line = strchr (buffer, '\n');
355           if (new_line != NULL)
356             *new_line = '\0';
357
358           text = buffer;
359           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
360             {
361               while (*text != ' ' && *text != '\0')
362                 text++;
363               if (*text == ' ')
364                 text++;
365             }
366           else
367             {
368               rs = 1;
369               cs = 1;
370             }
371
372           while (*text && strchr ("<>^,@", *text))
373             switch (*text++)
374               {
375               case '<':
376                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
377                 break;
378
379               case '>':
380                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
381                 break;
382
383               case '^':
384                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
385                 break;
386
387               case ',':
388                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
389                 break;
390
391               case '@':
392                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
393                          c + cs - 1, r + rs - 1);
394                 break;
395
396               default:
397                 NOT_REACHED ();
398               }
399
400           replace_newlines (text);
401
402           tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
403         }
404
405   if (getc (stream) != EOF)
406     error (1, 0, "unread data at end of input");
407
408   return &tab->table;
409 }
410
411 static void
412 draw (FILE *stream)
413 {
414   char buffer[1024];
415   int line = 0;
416
417   while (fgets (buffer, sizeof buffer, stream))
418     {
419       char text[sizeof buffer];
420       int length;
421       int emph;
422       int x, y;
423
424       line++;
425       if (strchr ("#\r\n", buffer[0]))
426         continue;
427
428       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
429         ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
430       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
431         ascii_test_set_length (ascii_driver, y, length);
432       else
433         error (1, 0, "line %d has invalid format", line);
434     }
435 }