1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010 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 "libpspp/string-map.h"
28 #include "output/driver.h"
29 #include "output/tab.h"
30 #include "output/table-item.h"
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
37 /* --transpose: Transpose the table before outputting? */
40 static const char *parse_options (int argc, char **argv);
41 static void usage (void) NO_RETURN;
42 static struct table *read_table (FILE *);
45 main (int argc, char **argv)
48 const char *input_file_name;
51 set_program_name (argv[0]);
52 input_file_name = parse_options (argc, argv);
54 if (!strcmp (input_file_name, "-"))
58 input = fopen (input_file_name, "r");
60 error (1, errno, "%s: open failed", input_file_name);
62 table = read_table (input);
67 table = table_transpose (table);
69 table_item_submit (table_item_create (table, NULL));
76 configure_drivers (int width, int length)
78 struct string_map options, tmp;
79 struct output_driver *driver;
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));
89 /* Render to stdout. */
90 string_map_clone (&tmp, &options);
91 driver = output_driver_create (&tmp);
94 output_driver_register (driver);
95 string_map_destroy (&tmp);
97 /* Render to render.txt. */
98 string_map_replace (&options, "output-file", "render.txt");
99 driver = output_driver_create (&options);
102 output_driver_register (driver);
105 /* Render to render.pdf. */
106 string_map_insert (&options, "output-file", "render.pdf");
107 string_map_insert (&options, "headers", "off");
108 string_map_insert (&options, "top-margin", "0");
109 string_map_insert (&options, "bottom-margin", "0");
110 string_map_insert (&options, "left-margin", "0");
111 string_map_insert (&options, "right-margin", "0");
112 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
113 xasprintf ("%dx%dpt", width * 5, length * 6));
114 driver = output_driver_create (&options);
117 output_driver_register (driver);
120 string_map_destroy (&options);
124 parse_options (int argc, char **argv)
132 OPT_WIDTH = UCHAR_MAX + 1,
136 static const struct option options[] =
138 {"width", required_argument, NULL, OPT_WIDTH},
139 {"length", required_argument, NULL, OPT_LENGTH},
140 {"transpose", no_argument, &transpose, 1},
141 {"help", no_argument, NULL, OPT_HELP},
145 int c = getopt_long (argc, argv, "", options, NULL);
152 width = atoi (optarg);
156 length = atoi (optarg);
175 configure_drivers (width, length);
177 if (optind + 1 != argc)
178 error (1, 0, "exactly one non-option argument required; "
179 "use --help for help");
186 printf ("%s, to test rendering of PSPP tables\n"
187 "usage: %s [OPTIONS] INPUT\n"
189 " --driver=NAME:CLASS:DEVICE:OPTIONS set output driver\n",
190 program_name, program_name);
195 replace_newlines (char *p)
199 for (q = p; *p != '\0'; )
200 if (*p == '\\' && p[1] == 'n')
210 static struct table *
211 read_table (FILE *stream)
213 struct tab_table *tab;
217 int nr, nc, hl, hr, ht, hb;
220 if (fgets (buffer, sizeof buffer, stream) == NULL
221 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
222 &input[0], &input[1], &input[2],
223 &input[3], &input[4], &input[5])) < 2)
224 error (1, 0, "syntax error reading row and column count");
228 hl = n_input >= 3 ? input[2] : 0;
229 hr = n_input >= 4 ? input[3] : 0;
230 ht = n_input >= 5 ? input[4] : 0;
231 hb = n_input >= 6 ? input[5] : 0;
233 tab = tab_create (nc, nr);
234 tab_headers (tab, hl, hr, ht, hb);
235 for (r = 0; r < nr; r++)
236 for (c = 0; c < nc; c++)
237 if (tab_cell_is_empty (tab, c, r))
243 if (fgets (buffer, sizeof buffer, stream) == NULL)
244 error (1, 0, "unexpected end of input reading row %d, column %d",
246 new_line = strchr (buffer, '\n');
247 if (new_line != NULL)
251 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
253 while (*text != ' ' && *text != '\0')
264 while (*text && strchr ("<>^,@", *text))
268 tab_vline (tab, TAL_1, c, r, r + rs - 1);
272 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
276 tab_hline (tab, TAL_1, c, c + cs - 1, r);
280 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
284 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
285 c + cs - 1, r + rs - 1);
292 replace_newlines (text);
294 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
297 if (getc (stream) != EOF)
298 error (1, 0, "unread data at end of input");