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