e325dbb16d9ba35fae564bb2a9fb3b7327fb02f9
[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, "top-margin", "0");
108   string_map_insert (&options, "bottom-margin", "0");
109   string_map_insert (&options, "left-margin", "0");
110   string_map_insert (&options, "right-margin", "0");
111   string_map_insert_nocopy (&options, xstrdup ("paper-size"),
112                             xasprintf ("%dx%dpt", width * 5, length * 8));
113   driver = output_driver_create (&options);
114   if (driver == NULL)
115     exit (EXIT_FAILURE);
116   output_driver_register (driver);
117 #endif
118
119   string_map_destroy (&options);
120 }
121
122 static const char *
123 parse_options (int argc, char **argv)
124 {
125   int width = 79;
126   int length = 66;
127
128   for (;;)
129     {
130       enum {
131         OPT_WIDTH = UCHAR_MAX + 1,
132         OPT_LENGTH,
133         OPT_HELP
134       };
135       static const struct option options[] =
136         {
137           {"width", required_argument, NULL, OPT_WIDTH},
138           {"length", required_argument, NULL, OPT_LENGTH},
139           {"transpose", no_argument, &transpose, 1},
140           {"help", no_argument, NULL, OPT_HELP},
141           {NULL, 0, NULL, 0},
142         };
143
144       int c = getopt_long (argc, argv, "", options, NULL);
145       if (c == -1)
146         break;
147
148       switch (c)
149         {
150         case OPT_WIDTH:
151           width = atoi (optarg);
152           break;
153
154         case OPT_LENGTH:
155           length = atoi (optarg);
156           break;
157
158         case OPT_HELP:
159           usage ();
160
161         case 0:
162           break;
163
164         case '?':
165           exit(EXIT_FAILURE);
166           break;
167
168         default:
169           NOT_REACHED ();
170         }
171
172     }
173
174   configure_drivers (width, length);
175
176   if (optind + 1 != argc)
177     error (1, 0, "exactly one non-option argument required; "
178            "use --help for help");
179   return argv[optind];
180 }
181
182 static void
183 usage (void)
184 {
185   printf ("%s, to test rendering of PSPP tables\n"
186           "usage: %s [OPTIONS] INPUT\n"
187           "\nOptions:\n"
188           "  --driver=NAME:CLASS:DEVICE:OPTIONS  set output driver\n",
189           program_name, program_name);
190   exit (EXIT_SUCCESS);
191 }
192
193 static void
194 replace_newlines (char *p)
195 {
196   char *q;
197
198   for (q = p; *p != '\0'; )
199     if (*p == '\\' && p[1] == 'n')
200       {
201         *q++ = '\n';
202         p += 2;
203       }
204     else
205       *q++ = *p++;
206   *q = '\0';
207 }
208
209 static struct table *
210 read_table (FILE *stream)
211 {
212   struct tab_table *tab;
213   char buffer[1024];
214   int input[6];
215   int n_input = 0;
216   int nr, nc, hl, hr, ht, hb;
217   int r, c;
218
219   if (fgets (buffer, sizeof buffer, stream) == NULL
220       || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
221                             &input[0], &input[1], &input[2],
222                             &input[3], &input[4], &input[5])) < 2)
223     error (1, 0, "syntax error reading row and column count");
224
225   nr = input[0];
226   nc = input[1];
227   hl = n_input >= 3 ? input[2] : 0;
228   hr = n_input >= 4 ? input[3] : 0;
229   ht = n_input >= 5 ? input[4] : 0;
230   hb = n_input >= 6 ? input[5] : 0;
231
232   tab = tab_create (nc, nr);
233   tab_headers (tab, hl, hr, ht, hb);
234   for (r = 0; r < nr; r++)
235     for (c = 0; c < nc; c++)
236       if (tab_cell_is_empty (tab, c, r))
237         {
238           char *new_line;
239           char *text;
240           int rs, cs;
241
242           if (fgets (buffer, sizeof buffer, stream) == NULL)
243             error (1, 0, "unexpected end of input reading row %d, column %d",
244                    r, c);
245           new_line = strchr (buffer, '\n');
246           if (new_line != NULL)
247             *new_line = '\0';
248
249           text = buffer;
250           if (sscanf (text, "%d*%d", &rs, &cs) == 2)
251             {
252               while (*text != ' ' && *text != '\0')
253                 text++;
254               if (*text == ' ')
255                 text++;
256             }
257           else
258             {
259               rs = 1;
260               cs = 1;
261             }
262
263           while (*text && strchr ("<>^,@", *text))
264             switch (*text++)
265               {
266               case '<':
267                 tab_vline (tab, TAL_1, c, r, r + rs - 1);
268                 break;
269
270               case '>':
271                 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
272                 break;
273
274               case '^':
275                 tab_hline (tab, TAL_1, c, c + cs - 1, r);
276                 break;
277
278               case ',':
279                 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
280                 break;
281
282               case '@':
283                 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
284                          c + cs - 1, r + rs - 1);
285                 break;
286
287               default:
288                 NOT_REACHED ();
289               }
290
291           replace_newlines (text);
292
293           tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
294         }
295
296   if (getc (stream) != EOF)
297     error (1, 0, "unread data at end of input");
298
299   return &tab->table;
300 }