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