Fix PR 13054.
[pspp-builds.git] / src / format.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "format.h"
22 #include <ctype.h>
23 #include "error.h"
24 #include <stdlib.h>
25 #include "error.h"
26 #include "lexer.h"
27 #include "misc.h"
28 #include "str.h"
29 #include "var.h"
30
31 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
32                OUTPUT, SPSS_FMT) \
33         {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
34 struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
35 {
36 #include "format.def"
37   {"",         -1, -1,  -1, -1,   -1, 0000, -1, -1},
38 };
39
40 /* Parses the alphabetic prefix of the current token as a format
41    specifier name.  Returns the corresponding format specifier
42    type if successful, or -1 on failure.  If ALLOW_XT is zero,
43    then X and T format specifiers are not allowed.  If CP is
44    nonzero, then *CP is set to the first non-alphabetic character
45    in the current token on success or to a null pointer on
46    failure. */
47 int
48 parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags)
49 {
50   char *sp, *ep;
51   int idx;
52
53   sp = ep = ds_c_str (&tokstr);
54   while (isalpha ((unsigned char) *ep))
55     ep++;
56
57   if (sp != ep) 
58     {
59       /* Find format. */
60       for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++)
61         if (strlen (formats[idx].name) == ep - sp
62             && !mm_case_compare (formats[idx].name, sp, ep - sp))
63           break;
64
65       /* Check format. */
66       if (idx < FMT_NUMBER_OF_FORMATS)
67         {
68           if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X)) 
69             {
70               if (!(flags & FMTP_SUPPRESS_ERRORS))
71                 msg (SE, _("X and T format specifiers not allowed here."));
72               idx = -1; 
73             }
74         }
75       else 
76         {
77           /* No match. */
78           if (!(flags & FMTP_SUPPRESS_ERRORS))
79             msg (SE, _("%.*s is not a valid data format."),
80                  (int) (ep - sp), ds_c_str (&tokstr));
81           idx = -1; 
82         }
83     }
84   else 
85     {
86       lex_error ("expecting data format");
87       idx = -1;
88     }
89       
90   if (cp != NULL) 
91     {
92       if (idx != -1)
93         *cp = ep;
94       else
95         *cp = NULL;
96     }
97
98   return idx;
99 }
100
101 /* Converts F to its string representation (for instance, "F8.2") and
102    returns a pointer to a static buffer containing that string. */
103 char *
104 fmt_to_string (const struct fmt_spec *f)
105 {
106   static char buf[32];
107
108   if (formats[f->type].n_args >= 2)
109     sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
110   else
111     sprintf (buf, "%s%d", formats[f->type].name, f->w);
112   return buf;
113 }
114
115 /* Does checks in common betwen check_input_specifier() and
116    check_output_specifier() and returns true if so.  Otherwise,
117    emits an error message (if EMIT_ERROR is nonzero) and returns
118    false. */
119 static bool
120 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
121 {
122   struct fmt_desc *f = &formats[spec->type];
123   char *str = fmt_to_string (spec);
124
125   if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
126     {
127       if (emit_error)
128         msg (SE, _("Format %s specifies an odd width %d, but "
129                    "format %s requires an even width."),
130              str, spec->w, f->name, f->Imin_w, f->Imax_w);
131       return false;
132     }
133   if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
134     {
135       if (emit_error)
136         msg (SE, _("Format %s specifies a bad number of "
137                    "implied decimal places %d.  Input format %s allows "
138                    "up to 16 implied decimal places."), str, spec->d, f->name);
139       return false;
140     }
141   return true;
142 }
143
144 /* Checks whether SPEC is valid as an input format and returns
145    nonzero if so.  Otherwise, emits an error message (if
146    EMIT_ERROR is nonzero) and returns zero. */
147 int
148 check_input_specifier (const struct fmt_spec *spec, int emit_error)
149 {
150   struct fmt_desc *f = &formats[spec->type];
151   char *str = fmt_to_string (spec);
152
153   if (!check_common_specifier (spec, emit_error))
154     return false;
155   if (spec->type == FMT_X)
156     return 1;
157   if (f->cat & FCAT_OUTPUT_ONLY)
158     {
159       if (emit_error)
160         msg (SE, _("Format %s may not be used for input."), f->name);
161       return 0;
162     }
163   if (spec->w < f->Imin_w || spec->w > f->Imax_w)
164     {
165       if (emit_error)
166         msg (SE, _("Input format %s specifies a bad width %d.  "
167                    "Format %s requires a width between %d and %d."),
168              str, spec->w, f->name, f->Imin_w, f->Imax_w);
169       return 0;
170     }
171   if ((spec->type == FMT_F || spec->type == FMT_COMMA
172           || spec->type == FMT_DOLLAR)
173       && spec->d > spec->w)
174     {
175       if (emit_error)
176         msg (SE, _("Input format %s is invalid because it specifies more "
177                    "decimal places than the field width."), str);
178       return 0;
179     }
180   return 1;
181 }
182
183 /* Checks whether SPEC is valid as an output format and returns
184    nonzero if so.  Otherwise, emits an error message (if
185    EMIT_ERROR is nonzero) and returns zero. */
186 int
187 check_output_specifier (const struct fmt_spec *spec, int emit_error)
188 {
189   struct fmt_desc *f = &formats[spec->type];
190   char *str = fmt_to_string (spec);
191
192   if (!check_common_specifier (spec, emit_error))
193     return false;
194   if (spec->type == FMT_X)
195     return 1;
196   if (spec->w < f->Omin_w || spec->w > f->Omax_w)
197     {
198       if (emit_error)
199         msg (SE, _("Output format %s specifies a bad width %d.  "
200                    "Format %s requires a width between %d and %d."),
201              str, spec->w, f->name, f->Omin_w, f->Omax_w);
202       return 0;
203     }
204   if ((spec->type == FMT_F || spec->type == FMT_COMMA
205           || spec->type == FMT_DOLLAR)
206       && spec->d >= spec->w)
207     {
208       if (emit_error)
209         msg (SE, _("Output format %s is invalid because it specifies as "
210                    "many decimal places as the field width, which "
211                    "fails to allow space for a decimal point.  "
212                    "Try %s%d.%d instead."),
213              str, f->name, f->name, spec->d + 1, spec->d);
214       return 0;
215     }
216   return 1;
217 }
218
219 /* Checks that FORMAT is appropriate for a variable of the given
220    TYPE and returns true if so.  Otherwise returns false and (if
221    EMIT_ERROR is true) emits an error message. */
222 bool
223 check_specifier_type (const struct fmt_spec *format,
224                       int type, bool emit_error) 
225 {
226   const struct fmt_desc *f = &formats[format->type];
227   assert (type == NUMERIC || type == ALPHA);
228   if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
229     {
230       if (emit_error)
231         msg (SE, _("%s variables are not compatible with %s format %s."),
232              type == ALPHA ? _("String") : _("Numeric"),
233              type == ALPHA ? _("numeric") : _("string"),
234              fmt_to_string (format));
235       return false;
236     }
237   return true;
238 }
239   
240 /* Checks that FORMAT is appropriate for a variable of the given
241    WIDTH and returns true if so.  Otherwise returns false and (if
242    EMIT_ERROR is true) emits an error message. */
243 bool
244 check_specifier_width (const struct fmt_spec *format,
245                        int width, bool emit_error) 
246 {
247   if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
248     return false;
249   if (get_format_var_width (format) != width)
250     {
251       if (emit_error)
252         msg (SE, _("String variable with width %d not compatible with "
253                    "format %s."),
254              width, fmt_to_string (format));
255       return false;
256     }
257   return true;
258 }
259
260 /* Converts input format specifier INPUT into output format
261    specifier OUTPUT. */
262 void
263 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
264 {
265   assert (check_input_specifier (input, 0));
266
267   output->type = formats[input->type].output;
268   output->w = input->w;
269   if (output->w > formats[output->type].Omax_w)
270     output->w = formats[output->type].Omax_w;
271   output->d = input->d;
272
273   switch (input->type)
274     {
275     case FMT_F:
276     case FMT_N:
277       if (output->d > 0)
278         output->w++;
279       break;
280     case FMT_E:
281       output->w = max (max (input->w, input->d+7), 10);
282       output->d = max (input->d, 3);
283       break;
284     case FMT_COMMA:
285     case FMT_DOT:
286       /* nothing is necessary */
287       break;
288     case FMT_DOLLAR:
289     case FMT_PCT:
290       if (output->w < 2)
291         output->w = 2;
292       break;
293     case FMT_PIBHEX:
294       {
295         static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
296         assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
297         output->w = map[input->w / 2 - 1];
298         break;
299       }
300     case FMT_RBHEX:
301       output->w = 8, output->d = 2;     /* FIXME */
302       break;
303     case FMT_IB:
304     case FMT_PIB:
305     case FMT_P:
306     case FMT_PK:
307     case FMT_RB:
308       if (input->d < 1)
309         output->w = 8, output->d = 2;
310       else
311         output->w = 9 + input->d;
312       break;
313     case FMT_CCA:
314     case FMT_CCB:
315     case FMT_CCC:
316     case FMT_CCD:
317     case FMT_CCE:
318       assert (0);
319     case FMT_Z:
320     case FMT_A:
321       /* nothing is necessary */
322       break;
323     case FMT_AHEX:
324       output->w = input->w / 2;
325       break;
326     case FMT_DATE:
327     case FMT_EDATE:
328     case FMT_SDATE:
329     case FMT_ADATE:
330     case FMT_JDATE:
331       /* nothing is necessary */
332       break;
333     case FMT_QYR:
334       if (output->w < 6)
335         output->w = 6;
336       break;
337     case FMT_MOYR:
338       /* nothing is necessary */
339       break;
340     case FMT_WKYR:
341       if (output->w < 8)
342         output->w = 8;
343       break;
344     case FMT_TIME:
345     case FMT_DTIME:
346     case FMT_DATETIME:
347     case FMT_WKDAY:
348     case FMT_MONTH:
349       /* nothing is necessary */
350       break;
351     default:
352       assert (0);
353     }
354
355   assert (check_output_specifier (output, 0));
356 }
357
358 /* Parses a format specifier from the token stream and returns
359    nonzero only if successful.  Emits an error message on
360    failure.  Allows X and T format specifiers only if ALLOW_XT is
361    nonzero.  The caller should call check_input_specifier() or
362    check_output_specifier() on the parsed format as
363    necessary.  */
364 int
365 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
366 {
367   struct fmt_spec spec;
368   struct fmt_desc *f;
369   const char *cp;
370   char *cp2;
371   int type, w, d;
372
373   if (token != T_ID)
374     {
375       if (!(flags & FMTP_SUPPRESS_ERRORS))
376         msg (SE, _("Format specifier expected."));
377       return 0;
378     }
379   type = parse_format_specifier_name (&cp, flags);
380   if (type == -1)
381     return 0;
382   f = &formats[type];
383
384   w = strtol (cp, &cp2, 10);
385   if (cp2 == cp && type != FMT_X)
386     {
387       if (!(flags & FMTP_SUPPRESS_ERRORS))
388         msg (SE, _("Data format %s does not specify a width."),
389              ds_c_str (&tokstr));
390       return 0;
391     }
392
393   cp = cp2;
394   if (f->n_args > 1 && *cp == '.')
395     {
396       cp++;
397       d = strtol (cp, &cp2, 10);
398       cp = cp2;
399     }
400   else
401     d = 0;
402
403   if (*cp)
404     {
405       if (!(flags & FMTP_SUPPRESS_ERRORS))
406         msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
407       return 0;
408     }
409   lex_get ();
410
411   spec.type = type;
412   spec.w = w;
413   spec.d = d;
414   *input = spec;
415
416   return 1;
417 }
418
419 /* Returns the width corresponding to the format specifier.  The
420    return value is the value of the `width' member of a `struct
421    variable' for such an input format. */
422 int
423 get_format_var_width (const struct fmt_spec *spec) 
424 {
425   if (spec->type == FMT_AHEX)
426     return spec->w / 2;
427   else if (spec->type == FMT_A)
428     return spec->w;
429   else
430     return 0;
431 }
432
433 /* Returns the PSPP format corresponding to the given SPSS
434    format. */
435 int
436 translate_fmt (int spss) 
437 {
438   int type;
439   
440   for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
441     if (formats[type].spss == spss)
442       return type;
443   return -1;
444 }