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