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