1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
31 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
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] =
37 {"", -1, -1, -1, -1, -1, 0000, -1, -1},
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
48 parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags)
53 sp = ep = ds_c_str (&tokstr);
54 while (isalpha ((unsigned char) *ep))
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))
66 if (idx < FMT_NUMBER_OF_FORMATS)
68 if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X))
70 if (!(flags & FMTP_SUPPRESS_ERRORS))
71 msg (SE, _("X and T format specifiers not allowed here."));
78 if (!(flags & FMTP_SUPPRESS_ERRORS))
79 msg (SE, _("%.*s is not a valid data format."),
80 (int) (ep - sp), ds_c_str (&tokstr));
86 lex_error ("expecting data format");
101 /* Converts F to its string representation (for instance, "F8.2") and
102 returns a pointer to a static buffer containing that string. */
104 fmt_to_string (const struct fmt_spec *f)
108 if (formats[f->type].n_args >= 2)
109 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
111 sprintf (buf, "%s%d", formats[f->type].name, f->w);
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
120 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
122 struct fmt_desc *f = &formats[spec->type];
123 char *str = fmt_to_string (spec);
125 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
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);
133 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
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);
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. */
148 check_input_specifier (const struct fmt_spec *spec, int emit_error)
150 struct fmt_desc *f = &formats[spec->type];
151 char *str = fmt_to_string (spec);
153 if (!check_common_specifier (spec, emit_error))
155 if (spec->type == FMT_X)
157 if (f->cat & FCAT_OUTPUT_ONLY)
160 msg (SE, _("Format %s may not be used for input."), f->name);
163 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
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);
171 if ((spec->type == FMT_F || spec->type == FMT_COMMA
172 || spec->type == FMT_DOLLAR)
173 && spec->d > spec->w)
176 msg (SE, _("Input format %s is invalid because it specifies more "
177 "decimal places than the field width."), str);
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. */
187 check_output_specifier (const struct fmt_spec *spec, int emit_error)
189 struct fmt_desc *f = &formats[spec->type];
190 char *str = fmt_to_string (spec);
192 if (!check_common_specifier (spec, emit_error))
194 if (spec->type == FMT_X)
196 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
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);
204 if ((spec->type == FMT_F || spec->type == FMT_COMMA
205 || spec->type == FMT_DOLLAR)
206 && spec->d >= spec->w)
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);
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. */
223 check_specifier_type (const struct fmt_spec *format,
224 int type, bool emit_error)
226 const struct fmt_desc *f = &formats[format->type];
227 assert (type == NUMERIC || type == ALPHA);
228 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
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));
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. */
244 check_specifier_width (const struct fmt_spec *format,
245 int width, bool emit_error)
247 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
249 if (get_format_var_width (format) != width)
252 msg (SE, _("String variable with width %d not compatible with "
254 width, fmt_to_string (format));
260 /* Converts input format specifier INPUT into output format
263 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
265 assert (check_input_specifier (input, 0));
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;
281 output->w = max (max (input->w, input->d+7), 10);
282 output->d = max (input->d, 3);
286 /* nothing is necessary */
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];
301 output->w = 8, output->d = 2; /* FIXME */
309 output->w = 8, output->d = 2;
311 output->w = 9 + input->d;
321 /* nothing is necessary */
324 output->w = input->w / 2;
331 /* nothing is necessary */
338 /* nothing is necessary */
349 /* nothing is necessary */
355 assert (check_output_specifier (output, 0));
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
365 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
367 struct fmt_spec spec;
375 if (!(flags & FMTP_SUPPRESS_ERRORS))
376 msg (SE, _("Format specifier expected."));
379 type = parse_format_specifier_name (&cp, flags);
384 w = strtol (cp, &cp2, 10);
385 if (cp2 == cp && type != FMT_X)
387 if (!(flags & FMTP_SUPPRESS_ERRORS))
388 msg (SE, _("Data format %s does not specify a width."),
394 if (f->n_args > 1 && *cp == '.')
397 d = strtol (cp, &cp2, 10);
405 if (!(flags & FMTP_SUPPRESS_ERRORS))
406 msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
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. */
423 get_format_var_width (const struct fmt_spec *spec)
425 if (spec->type == FMT_AHEX)
427 else if (spec->type == FMT_A)
433 /* Returns the PSPP format corresponding to the given SPSS
436 translate_fmt (int spss)
440 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
441 if (formats[type].spss == spss)