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, "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 * 8));
113 driver = output_driver_create (&options);
116 output_driver_register (driver);
119 string_map_destroy (&options);
123 parse_options (int argc, char **argv)
131 OPT_WIDTH = UCHAR_MAX + 1,
135 static const struct option options[] =
137 {"width", required_argument, NULL, OPT_WIDTH},
138 {"length", required_argument, NULL, OPT_LENGTH},
139 {"transpose", no_argument, &transpose, 1},
140 {"help", no_argument, NULL, OPT_HELP},
144 int c = getopt_long (argc, argv, "", options, NULL);
151 width = atoi (optarg);
155 length = atoi (optarg);
174 configure_drivers (width, length);
176 if (optind + 1 != argc)
177 error (1, 0, "exactly one non-option argument required; "
178 "use --help for help");
185 printf ("%s, to test rendering of PSPP tables\n"
186 "usage: %s [OPTIONS] INPUT\n"
188 " --driver=NAME:CLASS:DEVICE:OPTIONS set output driver\n",
189 program_name, program_name);
194 replace_newlines (char *p)
198 for (q = p; *p != '\0'; )
199 if (*p == '\\' && p[1] == 'n')
209 static struct table *
210 read_table (FILE *stream)
212 struct tab_table *tab;
216 int nr, nc, hl, hr, ht, hb;
219 if (fgets (buffer, sizeof buffer, stream) == NULL
220 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
221 &input[0], &input[1], &input[2],
222 &input[3], &input[4], &input[5])) < 2)
223 error (1, 0, "syntax error reading row and column count");
227 hl = n_input >= 3 ? input[2] : 0;
228 hr = n_input >= 4 ? input[3] : 0;
229 ht = n_input >= 5 ? input[4] : 0;
230 hb = n_input >= 6 ? input[5] : 0;
232 tab = tab_create (nc, nr);
233 tab_headers (tab, hl, hr, ht, hb);
234 for (r = 0; r < nr; r++)
235 for (c = 0; c < nc; c++)
236 if (tab_cell_is_empty (tab, c, r))
242 if (fgets (buffer, sizeof buffer, stream) == NULL)
243 error (1, 0, "unexpected end of input reading row %d, column %d",
245 new_line = strchr (buffer, '\n');
246 if (new_line != NULL)
250 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
252 while (*text != ' ' && *text != '\0')
263 while (*text && strchr ("<>^,@", *text))
267 tab_vline (tab, TAL_1, c, r, r + rs - 1);
271 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
275 tab_hline (tab, TAL_1, c, c + cs - 1, r);
279 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
283 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
284 c + cs - 1, r + rs - 1);
291 replace_newlines (text);
293 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
296 if (getc (stream) != EOF)
297 error (1, 0, "unread data at end of input");