7c5d8a7cbdf1afcdfb83249899398dfdfc5e9892
[pspp] / tests / output / ascii-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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 "data/file-handle-def.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/compiler.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/string-map.h"
30 #include "output/ascii.h"
31 #include "output/driver.h"
32 #include "output/table.h"
33 #include "output/table-item.h"
34
35 #include "gl/error.h"
36 #include "gl/progname.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
39
40 /* --emphasis: ASCII driver emphasis option. */
41 static bool bold;
42 static bool underline;
43
44 /* --box: ASCII driver box option. */
45 static char *box;
46
47 /* ASCII driver, for ASCII driver test mode. */
48 static struct output_driver *ascii_driver;
49
50 static const char *parse_options (int argc, char **argv);
51 static void usage (void) NO_RETURN;
52 static void draw (FILE *);
53
54 int
55 main (int argc, char **argv)
56 {
57   set_program_name (argv[0]);
58   i18n_init ();
59   output_engine_push ();
60   const char *input_file_name = parse_options (argc, argv);
61
62   FILE *input = (!strcmp (input_file_name, "-")
63                  ? stdin
64                  : fopen (input_file_name, "r"));
65   if (!input)
66     error (1, errno, "%s: open failed", input_file_name);
67
68   draw (input);
69
70   if (input != stdin)
71     fclose (input);
72
73   output_engine_pop ();
74   fh_done ();
75
76   return 0;
77 }
78
79 static struct output_driver *
80 configure_driver (int width, int min_break)
81 {
82   struct string_map options = STRING_MAP_INITIALIZER (options);
83   string_map_insert (&options, "format", "txt");
84   string_map_insert (&options, "output-file", "-");
85   string_map_insert_nocopy (&options, xstrdup ("width"),
86                             xasprintf ("%d", width));
87   if (min_break >= 0)
88     string_map_insert_nocopy (&options, xstrdup ("min-hbreak"),
89                               xasprintf ("%d", min_break));
90   if (bold || underline)
91     string_map_insert (&options, "emphasis", "true");
92   if (box != NULL)
93     string_map_insert (&options, "box", box);
94
95   struct output_driver *driver = output_driver_create (&options);
96   string_map_destroy (&options);
97   if (!driver)
98     exit (EXIT_FAILURE);
99   output_driver_register (driver);
100   return driver;
101 }
102
103 static const char *
104 parse_options (int argc, char **argv)
105 {
106   int width = 79;
107   int min_break = -1;
108
109   for (;;)
110     {
111       enum {
112         OPT_WIDTH = UCHAR_MAX + 1,
113         OPT_LENGTH,
114         OPT_MIN_BREAK,
115         OPT_EMPHASIS,
116         OPT_BOX,
117         OPT_HELP
118       };
119       static const struct option options[] =
120         {
121           {"width", required_argument, NULL, OPT_WIDTH},
122           {"length", required_argument, NULL, OPT_LENGTH},
123           {"emphasis", required_argument, NULL, OPT_EMPHASIS},
124           {"box", required_argument, NULL, OPT_BOX},
125           {"help", no_argument, NULL, OPT_HELP},
126           {NULL, 0, NULL, 0},
127         };
128
129       int c = getopt_long (argc, argv, "o:", options, NULL);
130       if (c == -1)
131         break;
132
133       switch (c)
134         {
135         case OPT_WIDTH:
136           width = atoi (optarg);
137           break;
138
139         case OPT_EMPHASIS:
140           if (!strcmp (optarg, "bold"))
141             {
142               bold = true;
143               underline = false;
144             }
145           else if (!strcmp (optarg, "underline"))
146             {
147               bold = false;
148               underline = true;
149             }
150           else if (!strcmp (optarg, "none"))
151             {
152               bold = underline = false;
153             }
154           else
155             error (1, 0, "argument to --emphasis must be \"bold\" or "
156                    "\"underline\" or \"none\"");
157           break;
158
159         case OPT_BOX:
160           box = optarg;
161           break;
162
163         case OPT_HELP:
164           usage ();
165
166         case 0:
167           break;
168
169         case '?':
170           exit(EXIT_FAILURE);
171           break;
172
173         default:
174           NOT_REACHED ();
175         }
176
177     }
178
179   ascii_driver = configure_driver (width, min_break);
180
181   if (optind + 1 != argc)
182     error (1, 0, "exactly one non-option argument required; "
183            "use --help for help");
184   return argv[optind];
185 }
186
187 static void
188 usage (void)
189 {
190   printf ("%s, to test PSPP ASCII driver drawing\n"
191           "usage: %s [OPTIONS] INPUT\n"
192           "\nOptions:\n"
193           "  --width=WIDTH   set page width in characters\n"
194           "  --length=LINE   set page length in lines\n",
195           program_name, program_name);
196   exit (EXIT_SUCCESS);
197 }
198
199 static void
200 draw (FILE *stream)
201 {
202   char buffer[1024];
203   int line = 0;
204
205   while (fgets (buffer, sizeof buffer, stream))
206     {
207       char text[sizeof buffer];
208       int length;
209       int emph;
210       int x, y;
211
212       line++;
213       if (strchr ("#\r\n", buffer[0]))
214         continue;
215
216       if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
217         ascii_test_write (ascii_driver, text, x, y, emph ? bold : false,
218                           emph ? underline : false);
219       else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
220         ascii_test_set_length (ascii_driver, y, length);
221       else
222         error (1, 0, "line %d has invalid format", line);
223     }
224   ascii_test_flush (ascii_driver);
225 }