Move GCC attribute declarations from pref.h.orig to new file
[pspp-builds.git] / src / data / 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 "message.h"
24 #include <stdlib.h>
25 #include "compiler.h"
26 #include "misc.h"
27 #include "identifier.h"
28 #include "str.h"
29 #include "variable.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
35                OUTPUT, SPSS_FMT) \
36         {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
37 struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
38 {
39 #include "format.def"
40   {"",         -1, -1,  -1, -1,   -1, 0000, -1, -1},
41 };
42
43 /* Common formats. */
44 const struct fmt_spec f8_2 = {FMT_F, 8, 2};
45
46 /* Converts F to its string representation (for instance, "F8.2") and
47    returns a pointer to a static buffer containing that string. */
48 char *
49 fmt_to_string (const struct fmt_spec *f)
50 {
51   static char buf[32];
52
53   if (formats[f->type].n_args >= 2)
54     sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
55   else
56     sprintf (buf, "%s%d", formats[f->type].name, f->w);
57   return buf;
58 }
59
60 /* Does checks in common betwen check_input_specifier() and
61    check_output_specifier() and returns true if so.  Otherwise,
62    emits an error message (if EMIT_ERROR is nonzero) and returns
63    false. */
64 static bool
65 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
66 {
67   struct fmt_desc *f ; 
68   char *str;
69
70   if ( spec->type > FMT_NUMBER_OF_FORMATS ) 
71     {
72       if (emit_error)
73         msg (SE, _("Format specifies a bad type (%d)"), spec->type);
74       
75       return false;
76     }
77
78   f = &formats[spec->type];
79   str = fmt_to_string (spec);
80
81   if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
82     {
83       if (emit_error)
84         msg (SE, _("Format %s specifies an odd width %d, but "
85                    "an even width is required."),
86              str, spec->w);
87       return false;
88     }
89   if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
90     {
91       if (emit_error)
92         msg (SE, _("Format %s specifies a bad number of "
93                    "implied decimal places %d.  Input format %s allows "
94                    "up to 16 implied decimal places."), str, spec->d, f->name);
95       return false;
96     }
97   return true;
98 }
99
100 /* Checks whether SPEC is valid as an input format and returns
101    nonzero if so.  Otherwise, emits an error message (if
102    EMIT_ERROR is nonzero) and returns zero. */
103 int
104 check_input_specifier (const struct fmt_spec *spec, int emit_error)
105 {
106   struct fmt_desc *f ;
107   char *str ;
108
109   if (!check_common_specifier (spec, emit_error))
110     return false;
111
112   f = &formats[spec->type];
113   str = fmt_to_string (spec);
114
115
116   if (spec->type == FMT_X)
117     return 1;
118   if (f->cat & FCAT_OUTPUT_ONLY)
119     {
120       if (emit_error)
121         msg (SE, _("Format %s may not be used for input."), f->name);
122       return 0;
123     }
124   if (spec->w < f->Imin_w || spec->w > f->Imax_w)
125     {
126       if (emit_error)
127         msg (SE, _("Input format %s specifies a bad width %d.  "
128                    "Format %s requires a width between %d and %d."),
129              str, spec->w, f->name, f->Imin_w, f->Imax_w);
130       return 0;
131     }
132   if ((spec->type == FMT_F || spec->type == FMT_COMMA
133           || spec->type == FMT_DOLLAR)
134       && spec->d > spec->w)
135     {
136       if (emit_error)
137         msg (SE, _("Input format %s is invalid because it specifies more "
138                    "decimal places than the field width."), str);
139       return 0;
140     }
141   return 1;
142 }
143
144 /* Checks whether SPEC is valid as an output 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_output_specifier (const struct fmt_spec *spec, int emit_error)
149 {
150   struct fmt_desc *f;
151   char *str ; 
152
153   if (!check_common_specifier (spec, emit_error))
154     return false;
155
156   f = &formats[spec->type];
157   str = fmt_to_string (spec);
158
159   if (spec->type == FMT_X)
160     return 1;
161   if (spec->w < f->Omin_w || spec->w > f->Omax_w)
162     {
163       if (emit_error)
164         msg (SE, _("Output format %s specifies a bad width %d.  "
165                    "Format %s requires a width between %d and %d."),
166              str, spec->w, f->name, f->Omin_w, f->Omax_w);
167       return 0;
168     }
169   if ((spec->type == FMT_F || spec->type == FMT_COMMA
170           || spec->type == FMT_DOLLAR)
171       && spec->d >= spec->w)
172     {
173       if (emit_error)
174         msg (SE, _("Output format %s is invalid because it specifies as "
175                    "many decimal places as the field width, which "
176                    "fails to allow space for a decimal point.  "
177                    "Try %s%d.%d instead."),
178              str, f->name, spec->d + 1, spec->d);
179       return 0;
180     }
181   return 1;
182 }
183
184 /* Checks that FORMAT is appropriate for a variable of the given
185    TYPE and returns true if so.  Otherwise returns false and (if
186    EMIT_ERROR is true) emits an error message. */
187 bool
188 check_specifier_type (const struct fmt_spec *format,
189                       int type, bool emit_error) 
190 {
191   const struct fmt_desc *f = &formats[format->type];
192   assert (type == NUMERIC || type == ALPHA);
193   if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
194     {
195       if (emit_error)
196         msg (SE, _("%s variables are not compatible with %s format %s."),
197              type == ALPHA ? _("String") : _("Numeric"),
198              type == ALPHA ? _("numeric") : _("string"),
199              fmt_to_string (format));
200       return false;
201     }
202   return true;
203 }
204   
205 /* Checks that FORMAT is appropriate for a variable of the given
206    WIDTH and returns true if so.  Otherwise returns false and (if
207    EMIT_ERROR is true) emits an error message. */
208 bool
209 check_specifier_width (const struct fmt_spec *format,
210                        int width, bool emit_error) 
211 {
212   if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
213     return false;
214   if (get_format_var_width (format) != width)
215     {
216       if (emit_error)
217         msg (SE, _("String variable with width %d not compatible with "
218                    "format %s."),
219              width, fmt_to_string (format));
220       return false;
221     }
222   return true;
223 }
224
225 /* Converts input format specifier INPUT into output format
226    specifier OUTPUT. */
227 void
228 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
229 {
230   assert (check_input_specifier (input, 0));
231
232   output->type = formats[input->type].output;
233   output->w = input->w;
234   if (output->w > formats[output->type].Omax_w)
235     output->w = formats[output->type].Omax_w;
236   output->d = input->d;
237
238   switch (input->type)
239     {
240     case FMT_F:
241     case FMT_N:
242       if (output->d > 0)
243         output->w++;
244       break;
245     case FMT_E:
246       output->w = max (max (input->w, input->d+7), 10);
247       output->d = max (input->d, 3);
248       break;
249     case FMT_COMMA:
250     case FMT_DOT:
251       /* nothing is necessary */
252       break;
253     case FMT_DOLLAR:
254     case FMT_PCT:
255       if (output->w < 2)
256         output->w = 2;
257       break;
258     case FMT_PIBHEX:
259       {
260         static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
261         assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
262         output->w = map[input->w / 2 - 1];
263         break;
264       }
265     case FMT_RBHEX:
266       output->w = 8, output->d = 2;     /* FIXME */
267       break;
268     case FMT_IB:
269     case FMT_PIB:
270     case FMT_P:
271     case FMT_PK:
272     case FMT_RB:
273       if (input->d < 1)
274         output->w = 8, output->d = 2;
275       else
276         output->w = 9 + input->d;
277       break;
278     case FMT_CCA:
279     case FMT_CCB:
280     case FMT_CCC:
281     case FMT_CCD:
282     case FMT_CCE:
283       assert (0);
284     case FMT_Z:
285     case FMT_A:
286       /* nothing is necessary */
287       break;
288     case FMT_AHEX:
289       output->w = input->w / 2;
290       break;
291     case FMT_DATE:
292     case FMT_EDATE:
293     case FMT_SDATE:
294     case FMT_ADATE:
295     case FMT_JDATE:
296       /* nothing is necessary */
297       break;
298     case FMT_QYR:
299       if (output->w < 6)
300         output->w = 6;
301       break;
302     case FMT_MOYR:
303       /* nothing is necessary */
304       break;
305     case FMT_WKYR:
306       if (output->w < 8)
307         output->w = 8;
308       break;
309     case FMT_TIME:
310     case FMT_DTIME:
311     case FMT_DATETIME:
312     case FMT_WKDAY:
313     case FMT_MONTH:
314       /* nothing is necessary */
315       break;
316     default:
317       assert (0);
318     }
319
320   assert (check_output_specifier (output, 0));
321 }
322
323 /* Returns the width corresponding to the format specifier.  The
324    return value is the value of the `width' member of a `struct
325    variable' for such an input format. */
326 int
327 get_format_var_width (const struct fmt_spec *spec) 
328 {
329   if (spec->type == FMT_AHEX)
330     return spec->w / 2;
331   else if (spec->type == FMT_A)
332     return spec->w;
333   else
334     return 0;
335 }
336
337 /* Returns the PSPP format corresponding to the given SPSS
338    format. */
339 int
340 translate_fmt (int spss) 
341 {
342   int type;
343   
344   for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
345     if (formats[type].spss == spss)
346       return type;
347   return -1;
348 }
349
350 /* Returns an input format specification with type TYPE, width W,
351    and D decimals. */
352 struct fmt_spec
353 make_input_format (int type, int w, int d) 
354 {
355   struct fmt_spec f;
356   f.type = type;
357   f.w = w;
358   f.d = d;
359   assert (check_input_specifier (&f, 0));
360   return f;
361 }
362
363 /* Returns an output format specification with type TYPE, width
364    W, and D decimals. */
365 struct fmt_spec
366 make_output_format (int type, int w, int d)
367 {
368   struct fmt_spec f;
369   f.type = type;
370   f.w = w;
371   f.d = d;
372   assert (check_output_specifier (&f, 0));
373   return f;
374 }