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