output: Support --without-cairo.
[pspp-builds.git] / tests / output / render-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010 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/driver.h"
29 #include "output/tab.h"
30 #include "output/table-item.h"
31
32 #include "gl/error.h"
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
36
37 /* --transpose: Transpose the table before outputting? */
38 static int transpose;
39
40 static const char *parse_options (int argc, char **argv);
41 static void usage (void) NO_RETURN;
42 static struct table *read_table (FILE *);
43
44 int
45 main (int argc, char **argv)
46 {
47   struct table *table;
48   const char *input_file_name;
49   FILE *input;
50
51   set_program_name (argv[0]);
52   input_file_name = parse_options (argc, argv);
53
54   if (!strcmp (input_file_name, "-"))
55     input = stdin;
56   else
57     {
58       input = fopen (input_file_name, "r");
59       if (input == NULL)
60         error (1, errno, "%s: open failed", input_file_name);
61     }
62   table = read_table (input);
63   if (input != stdin)
64     fclose (input);
65
66   if (transpose)
67     table = table_transpose (table);
68
69   table_item_submit (table_item_create (table, NULL));
70   output_close ();
71
72   return 0;
73 }
74
75 static void
76 configure_drivers (int width, int length)
77 {
78   struct string_map options, tmp;
79   struct output_driver *driver;
80
81   string_map_init (&options);
82   string_map_insert (&options, "format", "txt");
83   string_map_insert (&options, "output-file", "-");
84   string_map_insert_nocopy (&options, xstrdup ("width"),
85                             xasprintf ("%d", width));
86   string_map_insert_nocopy (&options, xstrdup ("length"),
87                             xasprintf ("%d", length));
88
89   /* Render to stdout. */
90   string_map_clone (&tmp, &options);
91   driver = output_driver_create (&tmp);
92   if (driver == NULL)
93     exit (EXIT_FAILURE);
94   output_driver_register (driver);
95   string_map_destroy (&tmp);
96
97   /* Render to render.txt. */
98   string_map_replace (&options, "output-file", "render.txt");
99   driver = output_driver_create (&options);
100   if (driver == NULL)
101     exit (EXIT_FAILURE);
102   output_driver_register (driver);
103
104 #ifdef HAVE_CAIRO
105   /* Render to render.pdf. */
106   string_map_insert (&options, "output-file", "render.pdf");
107   string_map_insert (&options, "headers", "off");
108   string_map_insert (&options, "top-margin", "0");
109   string_map_insert (&options, "bottom-margin", "0");
110   string_map_insert (&options, "left-margin", "0");
111   string_map_insert (&options, "right-margin", "0");
112   string_map_insert_nocopy (&options, xstrdup ("paper-size"),
113                             xasprintf ("%dx%dpt", width * 5, length * 6));
114   driver = output_driver_create (&options);
115   if (driver == NULL)
116     exit (EXIT_FAILURE);
117   output_driver_register (driver);
118 #endif
119
120   string_map_destroy (&options);
121 }
122
123 static const char *
124 parse_options (int argc, char **argv)
125 {
126   int width = 79;
127   int length = 66;
128
129   for (;;)
130     {
131       enum {
132         OPT_WIDTH = UCHAR_MAX + 1,
133         OPT_LENGTH,
134         OPT_HELP
135       };
136       static const struct option options[] =
137         {
138           {"width", required_argument, NULL, OPT_WIDTH},
139           {"length", required_argument, NULL, OPT_LENGTH},
140           {"transpose", no_argument, &transpose, 1},
141           {"help", no_argument, NULL, OPT_HELP},
142           {NULL, 0, NULL, 0},
143         };
144
145       int c = getopt_long (argc, argv, "", options, NULL);
146       if (c == -1)
147         break;
148
149       switch (c)
150         {
151         case OPT_WIDTH:
152           width = atoi (optarg);
153           break;
154
155         case OPT_LENGTH:
156           length = atoi (optarg);
157           break;
158
159         case OPT_HELP:
160           usage ();
161
162         case 0:
163           break;
164
165         case '?':
166           exit(EXIT_FAILURE);
167           break;
168
169         default:
170           NOT_REACHED ();
171         }
172
173     }
174
175   configure_drivers (width, length);
176
177   if (optind + 1 != argc)
178     error (1, 0, "exactly one non-option argument required; "
179            "use --help for help");
180   return argv[optind];
181 }
182
183 static void
184 usage (void)
185 {
186   printf ("%s, to test rendering of PSPP tables\n"
187           "usage: %s [OPTIONS] INPUT\n"
188           "\nOptions:\n"
189           "  --driver=NAME:CLASS:DEVICE:OPTIONS  set output driver\n",
190           program_name, program_name);
191   exit (EXIT_SUCCESS);
192 }
193
194 static void
195 replace_newlines (char *p)
196 {
197   char *q;
198
199   for (q = p; *p != '\0'; )
200     if (*p == '\\' && p[1] == 'n')
201       {
202         *q++ = '\n';
203         p += 2;
204       }
205     else
206       *q++ = *p++;
207   *q = '\0';
208 }
209
210 static struct table *
211 read_table (FILE *stream)
212 {
213   struct tab_table *tab;
214   char buffer[1024];
215   int input[6];
216   int n_input = 0;
217   int nr, nc, hl, hr, ht, hb;
218   int r, c;
219
220   if (fgets (buffer, sizeof buffer, stream) == NULL
221       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
222                             &input[0], &input[1], &input[2],
223                             &input[3], &input[4], &input[5])) < 2)
224     error (1, 0, "syntax error reading row and column count");
225
226   nr = input[0];
227   nc = input[1];
228   hl = n_input >= 3 ? input[2] : 0;
229   hr = n_input >= 4 ? input[3] : 0;
230   ht = n_input >= 5 ? input[4] : 0;
231   hb = n_input >= 6 ? input[5] : 0;
232
233   tab = tab_create (nc, nr);
234   tab_headers (tab, hl, hr, ht, hb);
235   for (r = 0; r < nr; r++)
236     for (c = 0; c < nc; c++)
237       if (tab_cell_is_empty (tab, c, r))
238         {
239           char *new_line;
240           char *text;
241           int rs, cs;
242
243           if (fgets (buffer, sizeof buffer, stream) == NULL)
244             error (1, 0, "unexpected end of input reading row %d, column %d",
245                    r, c);
246           new_line = strchr (buffer, '\n');
247           if (new_line != NULL)
248             *new_line = '\0';
249
250           text = buffer;
251           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
252             {
253               while (*text != ' ' && *text != '\0')
254                 text++;
255               if (*text == ' ')
256                 text++;
257             }
258           else
259             {
260               rs = 1;
261               cs = 1;
262             }
263
264           while (*text && strchr ("<>^,@", *text))
265             switch (*text++)
266               {
267               case '<':
268                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
269                 break;
270
271               case '>':
272                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
273                 break;
274
275               case '^':
276                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
277                 break;
278
279               case ',':
280                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
281                 break;
282
283               case '@':
284                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
285                          c + cs - 1, r + rs - 1);
286                 break;
287
288               default:
289                 NOT_REACHED ();
290               }
291
292           replace_newlines (text);
293
294           tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
295         }
296
297   if (getc (stream) != EOF)
298     error (1, 0, "unread data at end of input");
299
300   return &tab->table;
301 }