1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
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>
32 #include "gl/progname.h"
33 #include "gl/xvasprintf.h"
35 /* --transpose: Transpose the table before outputting? */
38 static const char *parse_options (int argc, char **argv);
39 static void usage (void) NO_RETURN;
40 static struct table *read_table (FILE *);
43 main (int argc, char **argv)
46 const char *input_file_name;
49 set_program_name (argv[0]);
50 input_file_name = parse_options (argc, argv);
52 if (!strcmp (input_file_name, "-"))
56 input = fopen (input_file_name, "r");
58 error (1, errno, "%s: open failed", input_file_name);
60 table = read_table (input);
65 table = table_transpose (table);
67 table_item_submit (table_item_create (table, NULL));
74 parse_options (int argc, char **argv)
76 bool configured_driver = false;
83 OPT_DRIVER = UCHAR_MAX + 1,
88 static const struct option options[] =
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},
98 int c = getopt_long (argc, argv, "", options, NULL);
105 output_configure_driver (optarg);
106 configured_driver = true;
110 width = atoi (optarg);
114 length = atoi (optarg);
133 if (!configured_driver)
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",
142 output_configure_driver (config);
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);
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);
160 if (optind + 1 != argc)
161 error (1, 0, "exactly one non-option argument required; "
162 "use --help for help");
169 printf ("%s, to test rendering of PSPP tables\n"
170 "usage: %s [OPTIONS] INPUT\n"
172 " --driver=NAME:CLASS:DEVICE:OPTIONS set output driver\n",
173 program_name, program_name);
178 replace_newlines (char *p)
182 for (q = p; *p != '\0'; )
183 if (*p == '\\' && p[1] == 'n')
193 static struct table *
194 read_table (FILE *stream)
196 struct tab_table *tab;
200 int nr, nc, hl, hr, ht, hb;
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");
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;
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))
226 if (fgets (buffer, sizeof buffer, stream) == NULL)
227 error (1, 0, "unexpected end of input reading row %d, column %d",
229 new_line = strchr (buffer, '\n');
230 if (new_line != NULL)
234 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
236 while (*text != ' ' && *text != '\0')
247 while (*text && strchr ("<>^,@", *text))
251 tab_vline (tab, TAL_1, c, r, r + rs - 1);
255 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
259 tab_hline (tab, TAL_1, c, c + cs - 1, r);
263 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
267 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
268 c + cs - 1, r + rs - 1);
275 replace_newlines (text);
277 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
280 if (getc (stream) != EOF)
281 error (1, 0, "unread data at end of input");