Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / output / measure.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 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 "output/measure.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #if HAVE_LC_PAPER
24 #include <langinfo.h>
25 #endif
26 #include <stdlib.h>
27
28 #include "data/file-name.h"
29 #include "libpspp/str.h"
30
31 #include "gl/error.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 static double parse_unit (const char *);
37 static bool parse_paper_size (const char *, int *h, int *v);
38 static bool get_standard_paper_size (struct substring name, int *h, int *v);
39 static bool read_paper_conf (const char *file_name, int *h, int *v);
40 static bool get_default_paper_size (int *h, int *v);
41
42 /* Determines the size of a dimensional measurement and returns
43    the size in units of 1/72000".  Units are assumed to be
44    millimeters unless otherwise specified.  Returns -1 on
45    error. */
46 int
47 measure_dimension (const char *dimen)
48 {
49   double raw, factor;
50   char *tail;
51
52   /* Number. */
53   raw = strtod (dimen, &tail);
54   if (raw < 0.0)
55     goto syntax_error;
56
57   /* Unit. */
58   factor = parse_unit (tail);
59   if (factor == 0.0)
60     goto syntax_error;
61
62   return raw * factor;
63
64 syntax_error:
65   error (0, 0, _("`%s' is not a valid length."), dimen);
66   return -1;
67 }
68
69 /* Stores the dimensions, in 1/72000" units, of paper identified
70    by SIZE into *H and *V.  SIZE can be the name of a kind of
71    paper ("a4", "letter", ...) or a pair of dimensions
72    ("210x297", "8.5x11in", ...).  Returns true on success, false
73    on failure.  On failure, *H and *V are set for A4 paper. */
74 bool
75 measure_paper (const char *size, int *h, int *v)
76 {
77   struct substring s;
78   bool ok;
79
80   s = ss_cstr (size);
81   ss_trim (&s, ss_cstr (CC_SPACES));
82
83   if (ss_is_empty (s))
84     {
85       /* Treat empty string as default paper size. */
86       ok = get_default_paper_size (h, v);
87     }
88   else if (isdigit (ss_first (s)))
89     {
90       /* Treat string that starts with digit as explicit size. */
91       ok = parse_paper_size (size, h, v);
92       if (!ok)
93         error (0, 0, _("syntax error in paper size `%s'"), size);
94     }
95   else
96     {
97       /* Check against standard paper sizes. */
98       ok = get_standard_paper_size (s, h, v);
99     }
100
101   /* Default to A4 on error. */
102   if (!ok)
103     {
104       *h = 210 * (72000 / 25.4);
105       *v = 297 * (72000 / 25.4);
106     }
107   return ok;
108 }
109 \f
110 /* Parses UNIT as a dimensional unit.  Returns the multiplicative
111    factor needed to change a quantity measured in that unit into
112    1/72000" units.  If UNIT is empty, it is treated as
113    millimeters.  If the unit is unrecognized, returns 0. */
114 static double
115 parse_unit (const char *unit)
116 {
117   struct unit
118     {
119       char name[3];
120       double factor;
121     };
122
123   static const struct unit units[] =
124     {
125       {"pt", 72000 / 72},
126       {"pc", 72000 / 72 * 12.0},
127       {"in", 72000},
128       {"cm", 72000 / 2.54},
129       {"mm", 72000 / 25.4},
130       {"", 72000 / 25.4},
131     };
132
133   const struct unit *p;
134
135   unit += strspn (unit, CC_SPACES);
136   for (p = units; p < units + sizeof units / sizeof *units; p++)
137     if (!strcasecmp (unit, p->name))
138       return p->factor;
139   return 0.0;
140 }
141
142 /* Stores the dimensions in 1/72000" units of paper identified by
143    SIZE, which is of form `HORZ x VERT [UNIT]' where HORZ and
144    VERT are numbers and UNIT is an optional unit of measurement,
145    into *H and *V.  Return true on success. */
146 static bool
147 parse_paper_size (const char *size, int *h, int *v)
148 {
149   double raw_h, raw_v, factor;
150   char *tail;
151
152   /* Width. */
153   raw_h = strtod (size, &tail);
154   if (raw_h <= 0.0)
155     return false;
156
157   /* Delimiter. */
158   tail += strspn (tail, CC_SPACES "x,");
159
160   /* Length. */
161   raw_v = strtod (tail, &tail);
162   if (raw_v <= 0.0)
163     return false;
164
165   /* Unit. */
166   factor = parse_unit (tail);
167   if (factor == 0.0)
168     return false;
169
170   *h = raw_h * factor + .5;
171   *v = raw_v * factor + .5;
172   return true;
173 }
174
175 static bool
176 get_standard_paper_size (struct substring name, int *h, int *v)
177 {
178   static const char *sizes[][2] =
179     {
180       {"a0", "841 x 1189 mm"},
181       {"a1", "594 x 841 mm"},
182       {"a2", "420 x 594 mm"},
183       {"a3", "297 x 420 mm"},
184       {"a4", "210 x 297 mm"},
185       {"a5", "148 x 210 mm"},
186       {"b5", "176 x 250 mm"},
187       {"a6", "105 x 148 mm"},
188       {"a7", "74 x 105 mm"},
189       {"a8", "52 x 74 mm"},
190       {"a9", "37 x 52 mm"},
191       {"a10", "26 x 37 mm"},
192       {"b0", "1000 x 1414 mm"},
193       {"b1", "707 x 1000 mm"},
194       {"b2", "500 x 707 mm"},
195       {"b3", "353 x 500 mm"},
196       {"b4", "250 x 353 mm"},
197       {"letter", "612 x 792 pt"},
198       {"legal", "612 x 1008 pt"},
199       {"executive", "522 x 756 pt"},
200       {"note", "612 x 792 pt"},
201       {"11x17", "792 x 1224 pt"},
202       {"tabloid", "792 x 1224 pt"},
203       {"statement", "396 x 612 pt"},
204       {"halfletter", "396 x 612 pt"},
205       {"halfexecutive", "378 x 522 pt"},
206       {"folio", "612 x 936 pt"},
207       {"quarto", "610 x 780 pt"},
208       {"ledger", "1224 x 792 pt"},
209       {"archA", "648 x 864 pt"},
210       {"archB", "864 x 1296 pt"},
211       {"archC", "1296 x 1728 pt"},
212       {"archD", "1728 x 2592 pt"},
213       {"archE", "2592 x 3456 pt"},
214       {"flsa", "612 x 936 pt"},
215       {"flse", "612 x 936 pt"},
216       {"csheet", "1224 x 1584 pt"},
217       {"dsheet", "1584 x 2448 pt"},
218       {"esheet", "2448 x 3168 pt"},
219     };
220
221   size_t i;
222
223   for (i = 0; i < sizeof sizes / sizeof *sizes; i++)
224     if (ss_equals_case (ss_cstr (sizes[i][0]), name))
225       {
226         bool ok = parse_paper_size (sizes[i][1], h, v);
227         assert (ok);
228         return ok;
229       }
230   error (0, 0, _("unknown paper type `%.*s'"),
231          (int) ss_length (name), ss_data (name));
232   return false;
233 }
234
235 /* Reads file FILE_NAME to find a paper size.  Stores the
236    dimensions, in 1/72000" units, into *H and *V.  Returns true
237    on success, false on failure. */
238 static bool
239 read_paper_conf (const char *file_name, int *h, int *v)
240 {
241   struct string line = DS_EMPTY_INITIALIZER;
242   int line_number = 0;
243   FILE *file;
244
245   file = fopen (file_name, "r");
246   if (file == NULL)
247     {
248       error (0, errno, _("error opening input file `%s'"), file_name);
249       return false;
250     }
251
252   for (;;)
253     {
254       struct substring name;
255
256       if (!ds_read_config_line (&line, &line_number, file))
257         {
258           if (ferror (file))
259             error (0, errno, _("error reading file `%s'"), file_name);
260           break;
261         }
262
263       name = ds_ss (&line);
264       ss_trim (&name, ss_cstr (CC_SPACES));
265       if (!ss_is_empty (name))
266         {
267           bool ok = get_standard_paper_size (name, h, v);
268           fclose (file);
269           ds_destroy (&line);
270           return ok;
271         }
272     }
273
274   fclose (file);
275   ds_destroy (&line);
276   error (0, 0, _("paper size file `%s' does not state a paper size"),
277          file_name);
278   return false;
279 }
280
281 /* The user didn't specify a paper size, so let's choose a
282    default based on his environment.  Stores the
283    dimensions, in 1/72000" units, into *H and *V.  Returns true
284    on success, false on failure. */
285 static bool
286 get_default_paper_size (int *h, int *v)
287 {
288   /* libpaper in Debian (and other distributions?) allows the
289      paper size to be specified in $PAPERSIZE or in a file
290      specified in $PAPERCONF. */
291   if (getenv ("PAPERSIZE") != NULL)
292     return get_standard_paper_size (ss_cstr (getenv ("PAPERSIZE")), h, v);
293   if (getenv ("PAPERCONF") != NULL)
294     return read_paper_conf (getenv ("PAPERCONF"), h, v);
295
296 #if HAVE_LC_PAPER
297   /* LC_PAPER is a non-standard glibc extension. */
298   *h = (int) nl_langinfo(_NL_PAPER_WIDTH) * (72000 / 25.4);
299   *v = (int) nl_langinfo(_NL_PAPER_HEIGHT) * (72000 / 25.4);
300   if (*h > 0 && *v > 0)
301      return true;
302 #endif
303
304   /* libpaper defaults to /etc/papersize. */
305   if (fn_exists ("/etc/papersize"))
306     return read_paper_conf ("/etc/papersize", h, v);
307
308   /* Can't find a default. */
309   return false;
310 }
311