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