1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 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/ascii.h"
29 #include "output/driver.h"
30 #include "output/tab.h"
31 #include "output/table-item.h"
34 #include "gl/progname.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
38 /* --transpose: Transpose the table before outputting? */
41 /* --emphasis: ASCII driver emphasis option. */
42 static char *emphasis;
44 /* --box: ASCII driver box option. */
47 /* --draw-mode: special ASCII driver test mode. */
50 /* ASCII driver, for ASCII driver test mode. */
51 static struct output_driver *ascii_driver;
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 *);
59 main (int argc, char **argv)
61 const char *input_file_name;
64 set_program_name (argv[0]);
65 input_file_name = parse_options (argc, argv);
67 if (!strcmp (input_file_name, "-"))
71 input = fopen (input_file_name, "r");
73 error (1, errno, "%s: open failed", input_file_name);
80 table = read_table (input);
83 table = table_transpose (table);
85 table_item_submit (table_item_create (table, NULL));
99 configure_drivers (int width, int length)
101 struct string_map options, tmp;
102 struct output_driver *driver;
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);
114 string_map_insert (&options, "box", box);
116 /* Render to stdout. */
117 string_map_clone (&tmp, &options);
118 ascii_driver = driver = output_driver_create (&tmp);
121 output_driver_register (driver);
122 string_map_destroy (&tmp);
127 /* Render to render.txt. */
128 string_map_replace (&options, "output-file", "render.txt");
129 driver = output_driver_create (&options);
132 output_driver_register (driver);
135 /* Render to render.pdf. */
136 string_map_insert (&options, "output-file", "render.pdf");
137 string_map_insert (&options, "top-margin", "0");
138 string_map_insert (&options, "bottom-margin", "0");
139 string_map_insert (&options, "left-margin", "0");
140 string_map_insert (&options, "right-margin", "0");
141 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
142 xasprintf ("%dx%dpt", width * 5, length * 8));
143 driver = output_driver_create (&options);
146 output_driver_register (driver);
149 string_map_insert (&options, "output-file", "render.odt");
150 driver = output_driver_create (&options);
153 output_driver_register (driver);
155 string_map_destroy (&options);
159 parse_options (int argc, char **argv)
167 OPT_WIDTH = UCHAR_MAX + 1,
173 static const struct option options[] =
175 {"width", required_argument, NULL, OPT_WIDTH},
176 {"length", required_argument, NULL, OPT_LENGTH},
177 {"transpose", no_argument, &transpose, 1},
178 {"emphasis", required_argument, NULL, OPT_EMPHASIS},
179 {"box", required_argument, NULL, OPT_BOX},
180 {"draw-mode", no_argument, &draw_mode, 1},
181 {"help", no_argument, NULL, OPT_HELP},
185 int c = getopt_long (argc, argv, "", options, NULL);
192 width = atoi (optarg);
196 length = atoi (optarg);
223 configure_drivers (width, length);
225 if (optind + 1 != argc)
226 error (1, 0, "exactly one non-option argument required; "
227 "use --help for help");
234 printf ("%s, to test rendering of PSPP tables\n"
235 "usage: %s [OPTIONS] INPUT\n"
237 " --width=WIDTH set page width in characters\n"
238 " --length=LINE set page length in lines\n",
239 program_name, program_name);
244 replace_newlines (char *p)
248 for (q = p; *p != '\0'; )
249 if (*p == '\\' && p[1] == 'n')
259 static struct table *
260 read_table (FILE *stream)
262 struct tab_table *tab;
266 int nr, nc, hl, hr, ht, hb;
269 if (fgets (buffer, sizeof buffer, stream) == NULL
270 || (n_input = sscanf (buffer, "%d %d %d %d %d %d",
271 &input[0], &input[1], &input[2],
272 &input[3], &input[4], &input[5])) < 2)
273 error (1, 0, "syntax error reading row and column count");
277 hl = n_input >= 3 ? input[2] : 0;
278 hr = n_input >= 4 ? input[3] : 0;
279 ht = n_input >= 5 ? input[4] : 0;
280 hb = n_input >= 6 ? input[5] : 0;
282 tab = tab_create (nc, nr);
283 tab_headers (tab, hl, hr, ht, hb);
284 for (r = 0; r < nr; r++)
285 for (c = 0; c < nc; c++)
286 if (tab_cell_is_empty (tab, c, r))
292 if (fgets (buffer, sizeof buffer, stream) == NULL)
293 error (1, 0, "unexpected end of input reading row %d, column %d",
295 new_line = strchr (buffer, '\n');
296 if (new_line != NULL)
300 if (sscanf (text, "%d*%d", &rs, &cs) == 2)
302 while (*text != ' ' && *text != '\0')
313 while (*text && strchr ("<>^,@", *text))
317 tab_vline (tab, TAL_1, c, r, r + rs - 1);
321 tab_vline (tab, TAL_1, c + cs, r, r + rs - 1);
325 tab_hline (tab, TAL_1, c, c + cs - 1, r);
329 tab_hline (tab, TAL_1, c, c + cs - 1, r + rs);
333 tab_box (tab, TAL_1, TAL_1, -1, -1, c, r,
334 c + cs - 1, r + rs - 1);
341 replace_newlines (text);
343 tab_joint_text (tab, c, r, c + cs - 1, r + rs - 1, 0, text);
346 if (getc (stream) != EOF)
347 error (1, 0, "unread data at end of input");
358 while (fgets (buffer, sizeof buffer, stream))
360 char text[sizeof buffer];
365 if (strchr ("#\r\n", buffer[0]))
368 if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) != 4)
369 error (1, 0, "line %d has invalid format", line);
371 ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);