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