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