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