Changed all the licence notices in all the files.
[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             && !memcmp (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 as an input format."), 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   return 1;
172 }
173
174 /* Checks whether SPEC is valid as an output format and returns
175    nonzero if so.  Otherwise, emits an error message (if
176    EMIT_ERROR is nonzero) and returns zero. */
177 int
178 check_output_specifier (const struct fmt_spec *spec, int emit_error)
179 {
180   struct fmt_desc *f = &formats[spec->type];
181   char *str = fmt_to_string (spec);
182
183   if (!check_common_specifier (spec, emit_error))
184     return false;
185   if (spec->type == FMT_X)
186     return 1;
187   if (spec->w < f->Omin_w || spec->w > f->Omax_w)
188     {
189       if (emit_error)
190         msg (SE, _("Output format %s specifies a bad width %d.  "
191                    "Format %s requires a width between %d and %d."),
192              str, spec->w, f->name, f->Omin_w, f->Omax_w);
193       return 0;
194     }
195   if (spec->d > 1
196       && (spec->type == FMT_F || spec->type == FMT_COMMA
197           || spec->type == FMT_DOLLAR)
198       && spec->w < f->Omin_w + 1 + spec->d)
199     {
200       if (emit_error)
201         msg (SE, _("Output format %s requires minimum width %d to allow "
202                    "%d decimal places.  Try %s%d.%d instead of %s."),
203              f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
204              f->Omin_w + 1 + spec->d, spec->d, str);
205       return 0;
206     }
207   return 1;
208 }
209
210 /* Checks that FORMAT is appropriate for a variable of the given
211    TYPE and returns true if so.  Otherwise returns false and (if
212    EMIT_ERROR is true) emits an error message. */
213 bool
214 check_specifier_type (const struct fmt_spec *format,
215                       int type, bool emit_error) 
216 {
217   const struct fmt_desc *f = &formats[format->type];
218   assert (type == NUMERIC || type == ALPHA);
219   if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
220     {
221       if (emit_error)
222         msg (SE, _("%s variables are not compatible with %s format %s."),
223              type == ALPHA ? _("String") : _("Numeric"),
224              type == ALPHA ? _("numeric") : _("string"),
225              fmt_to_string (format));
226       return false;
227     }
228   return true;
229 }
230   
231 /* Checks that FORMAT is appropriate for a variable of the given
232    WIDTH and returns true if so.  Otherwise returns false and (if
233    EMIT_ERROR is true) emits an error message. */
234 bool
235 check_specifier_width (const struct fmt_spec *format,
236                        int width, bool emit_error) 
237 {
238   if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
239     return false;
240   if (get_format_var_width (format) != width)
241     {
242       if (emit_error)
243         msg (SE, _("String variable with width %d not compatible with "
244                    "format %s."),
245              width, fmt_to_string (format));
246       return false;
247     }
248   return true;
249 }
250
251 /* Converts input format specifier INPUT into output format
252    specifier OUTPUT. */
253 void
254 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
255 {
256   output->type = formats[input->type].output;
257   output->w = input->w;
258   if (output->w > formats[output->type].Omax_w)
259     output->w = formats[output->type].Omax_w;
260   output->d = input->d;
261
262   switch (input->type)
263     {
264     case FMT_F:
265     case FMT_N:
266       if (output->d > 1 && output->w < 2 + output->d)
267         output->w = 2 + output->d;
268       break;
269     case FMT_E:
270       output->w = max (max (input->w, input->d+7), 10);
271       output->d = max (input->d, 3);
272       break;
273     case FMT_COMMA:
274     case FMT_DOT:
275       /* nothing is necessary */
276       break;
277     case FMT_DOLLAR:
278     case FMT_PCT:
279       if (output->w < 2)
280         output->w = 2;
281       break;
282     case FMT_PIBHEX:
283       {
284         static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
285         assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
286         output->w = map[input->w / 2 - 1];
287         break;
288       }
289     case FMT_RBHEX:
290       output->w = 8, output->d = 2;     /* FIXME */
291       break;
292     case FMT_IB:
293     case FMT_PIB:
294     case FMT_P:
295     case FMT_PK:
296     case FMT_RB:
297       if (input->d < 1)
298         output->w = 8, output->d = 2;
299       else
300         output->w = 9 + input->d;
301       break;
302     case FMT_CCA:
303     case FMT_CCB:
304     case FMT_CCC:
305     case FMT_CCD:
306     case FMT_CCE:
307       assert (0);
308     case FMT_Z:
309     case FMT_A:
310       /* nothing is necessary */
311       break;
312     case FMT_AHEX:
313       output->w = input->w / 2;
314       break;
315     case FMT_DATE:
316     case FMT_EDATE:
317     case FMT_SDATE:
318     case FMT_ADATE:
319     case FMT_JDATE:
320       /* nothing is necessary */
321       break;
322     case FMT_QYR:
323       if (output->w < 6)
324         output->w = 6;
325       break;
326     case FMT_MOYR:
327       /* nothing is necessary */
328       break;
329     case FMT_WKYR:
330       if (output->w < 8)
331         output->w = 8;
332       break;
333     case FMT_TIME:
334     case FMT_DTIME:
335     case FMT_DATETIME:
336     case FMT_WKDAY:
337     case FMT_MONTH:
338       /* nothing is necessary */
339       break;
340     default:
341       assert (0);
342     }
343 }
344
345 /* Parses a format specifier from the token stream and returns
346    nonzero only if successful.  Emits an error message on
347    failure.  Allows X and T format specifiers only if ALLOW_XT is
348    nonzero.  The caller should call check_input_specifier() or
349    check_output_specifier() on the parsed format as
350    necessary.  */
351 int
352 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
353 {
354   struct fmt_spec spec;
355   struct fmt_desc *f;
356   const char *cp;
357   char *cp2;
358   int type, w, d;
359
360   if (token != T_ID)
361     {
362       if (!(flags & FMTP_SUPPRESS_ERRORS))
363         msg (SE, _("Format specifier expected."));
364       return 0;
365     }
366   type = parse_format_specifier_name (&cp, flags);
367   if (type == -1)
368     return 0;
369   f = &formats[type];
370
371   w = strtol (cp, &cp2, 10);
372   if (cp2 == cp && type != FMT_X)
373     {
374       if (!(flags & FMTP_SUPPRESS_ERRORS))
375         msg (SE, _("Data format %s does not specify a width."),
376              ds_c_str (&tokstr));
377       return 0;
378     }
379
380   cp = cp2;
381   if (f->n_args > 1 && *cp == '.')
382     {
383       cp++;
384       d = strtol (cp, &cp2, 10);
385       cp = cp2;
386     }
387   else
388     d = 0;
389
390   if (*cp)
391     {
392       if (!(flags & FMTP_SUPPRESS_ERRORS))
393         msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
394       return 0;
395     }
396   lex_get ();
397
398   spec.type = type;
399   spec.w = w;
400   spec.d = d;
401   *input = spec;
402
403   return 1;
404 }
405
406 /* Returns the width corresponding to the format specifier.  The
407    return value is the value of the `width' member of a `struct
408    variable' for such an input format. */
409 int
410 get_format_var_width (const struct fmt_spec *spec) 
411 {
412   if (spec->type == FMT_AHEX)
413     return spec->w / 2;
414   else if (spec->type == FMT_A)
415     return spec->w;
416   else
417     return 0;
418 }
419
420 /* Returns the PSPP format corresponding to the given SPSS
421    format. */
422 int
423 translate_fmt (int spss) 
424 {
425   int type;
426   
427   for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
428     if (formats[type].spss == spss)
429       return type;
430   return -1;
431 }