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 as an input format."), 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);
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. */
178 check_output_specifier (const struct fmt_spec *spec, int emit_error)
180 struct fmt_desc *f = &formats[spec->type];
181 char *str = fmt_to_string (spec);
183 if (!check_common_specifier (spec, emit_error))
185 if (spec->type == FMT_X)
187 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
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);
196 && (spec->type == FMT_F || spec->type == FMT_COMMA
197 || spec->type == FMT_DOLLAR)
198 && spec->w < f->Omin_w + 1 + spec->d)
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);
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. */
214 check_specifier_type (const struct fmt_spec *format,
215 int type, bool emit_error)
217 const struct fmt_desc *f = &formats[format->type];
218 assert (type == NUMERIC || type == ALPHA);
219 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
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));
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. */
235 check_specifier_width (const struct fmt_spec *format,
236 int width, bool emit_error)
238 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
240 if (get_format_var_width (format) != width)
243 msg (SE, _("String variable with width %d not compatible with "
245 width, fmt_to_string (format));
251 /* Converts input format specifier INPUT into output format
254 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
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;
266 if (output->d > 1 && output->w < 2 + output->d)
267 output->w = 2 + output->d;
270 output->w = max (max (input->w, input->d+7), 10);
271 output->d = max (input->d, 3);
275 /* nothing is necessary */
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];
290 output->w = 8, output->d = 2; /* FIXME */
298 output->w = 8, output->d = 2;
300 output->w = 9 + input->d;
310 /* nothing is necessary */
313 output->w = input->w / 2;
320 /* nothing is necessary */
327 /* nothing is necessary */
338 /* nothing is necessary */
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
352 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
354 struct fmt_spec spec;
362 if (!(flags & FMTP_SUPPRESS_ERRORS))
363 msg (SE, _("Format specifier expected."));
366 type = parse_format_specifier_name (&cp, flags);
371 w = strtol (cp, &cp2, 10);
372 if (cp2 == cp && type != FMT_X)
374 if (!(flags & FMTP_SUPPRESS_ERRORS))
375 msg (SE, _("Data format %s does not specify a width."),
381 if (f->n_args > 1 && *cp == '.')
384 d = strtol (cp, &cp2, 10);
392 if (!(flags & FMTP_SUPPRESS_ERRORS))
393 msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
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. */
410 get_format_var_width (const struct fmt_spec *spec)
412 if (spec->type == FMT_AHEX)
414 else if (spec->type == FMT_A)
420 /* Returns the PSPP format corresponding to the given SPSS
423 translate_fmt (int spss)
427 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
428 if (formats[type].spss == spss)