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},
41 const struct fmt_spec f8_2 = {FMT_F, 8, 2};
43 /* Parses the alphabetic prefix of the current token as a format
44 specifier name. Returns the corresponding format specifier
45 type if successful, or -1 on failure. If ALLOW_XT is zero,
46 then X and T format specifiers are not allowed. If CP is
47 nonzero, then *CP is set to the first non-alphabetic character
48 in the current token on success or to a null pointer on
51 parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags)
56 sp = ep = ds_c_str (&tokstr);
57 while (isalpha ((unsigned char) *ep))
63 for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++)
64 if (strlen (formats[idx].name) == ep - sp
65 && !buf_compare_case (formats[idx].name, sp, ep - sp))
69 if (idx < FMT_NUMBER_OF_FORMATS)
71 if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X))
73 if (!(flags & FMTP_SUPPRESS_ERRORS))
74 msg (SE, _("X and T format specifiers not allowed here."));
81 if (!(flags & FMTP_SUPPRESS_ERRORS))
82 msg (SE, _("%.*s is not a valid data format."),
83 (int) (ep - sp), ds_c_str (&tokstr));
89 lex_error ("expecting data format");
104 /* Converts F to its string representation (for instance, "F8.2") and
105 returns a pointer to a static buffer containing that string. */
107 fmt_to_string (const struct fmt_spec *f)
111 if (formats[f->type].n_args >= 2)
112 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
114 sprintf (buf, "%s%d", formats[f->type].name, f->w);
118 /* Does checks in common betwen check_input_specifier() and
119 check_output_specifier() and returns true if so. Otherwise,
120 emits an error message (if EMIT_ERROR is nonzero) and returns
123 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
125 struct fmt_desc *f = &formats[spec->type];
126 char *str = fmt_to_string (spec);
128 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
131 msg (SE, _("Format %s specifies an odd width %d, but "
132 "format %s requires an even width."),
133 str, spec->w, f->name, f->Imin_w, f->Imax_w);
136 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
139 msg (SE, _("Format %s specifies a bad number of "
140 "implied decimal places %d. Input format %s allows "
141 "up to 16 implied decimal places."), str, spec->d, f->name);
147 /* Checks whether SPEC is valid as an input format and returns
148 nonzero if so. Otherwise, emits an error message (if
149 EMIT_ERROR is nonzero) and returns zero. */
151 check_input_specifier (const struct fmt_spec *spec, int emit_error)
153 struct fmt_desc *f = &formats[spec->type];
154 char *str = fmt_to_string (spec);
156 if (!check_common_specifier (spec, emit_error))
158 if (spec->type == FMT_X)
160 if (f->cat & FCAT_OUTPUT_ONLY)
163 msg (SE, _("Format %s may not be used for input."), f->name);
166 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
169 msg (SE, _("Input format %s specifies a bad width %d. "
170 "Format %s requires a width between %d and %d."),
171 str, spec->w, f->name, f->Imin_w, f->Imax_w);
174 if ((spec->type == FMT_F || spec->type == FMT_COMMA
175 || spec->type == FMT_DOLLAR)
176 && spec->d > spec->w)
179 msg (SE, _("Input format %s is invalid because it specifies more "
180 "decimal places than the field width."), str);
186 /* Checks whether SPEC is valid as an output format and returns
187 nonzero if so. Otherwise, emits an error message (if
188 EMIT_ERROR is nonzero) and returns zero. */
190 check_output_specifier (const struct fmt_spec *spec, int emit_error)
192 struct fmt_desc *f = &formats[spec->type];
193 char *str = fmt_to_string (spec);
195 if (!check_common_specifier (spec, emit_error))
197 if (spec->type == FMT_X)
199 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
202 msg (SE, _("Output format %s specifies a bad width %d. "
203 "Format %s requires a width between %d and %d."),
204 str, spec->w, f->name, f->Omin_w, f->Omax_w);
207 if ((spec->type == FMT_F || spec->type == FMT_COMMA
208 || spec->type == FMT_DOLLAR)
209 && spec->d >= spec->w)
212 msg (SE, _("Output format %s is invalid because it specifies as "
213 "many decimal places as the field width, which "
214 "fails to allow space for a decimal point. "
215 "Try %s%d.%d instead."),
216 str, f->name, f->name, spec->d + 1, spec->d);
222 /* Checks that FORMAT is appropriate for a variable of the given
223 TYPE and returns true if so. Otherwise returns false and (if
224 EMIT_ERROR is true) emits an error message. */
226 check_specifier_type (const struct fmt_spec *format,
227 int type, bool emit_error)
229 const struct fmt_desc *f = &formats[format->type];
230 assert (type == NUMERIC || type == ALPHA);
231 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
234 msg (SE, _("%s variables are not compatible with %s format %s."),
235 type == ALPHA ? _("String") : _("Numeric"),
236 type == ALPHA ? _("numeric") : _("string"),
237 fmt_to_string (format));
243 /* Checks that FORMAT is appropriate for a variable of the given
244 WIDTH and returns true if so. Otherwise returns false and (if
245 EMIT_ERROR is true) emits an error message. */
247 check_specifier_width (const struct fmt_spec *format,
248 int width, bool emit_error)
250 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
252 if (get_format_var_width (format) != width)
255 msg (SE, _("String variable with width %d not compatible with "
257 width, fmt_to_string (format));
263 /* Converts input format specifier INPUT into output format
266 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
268 assert (check_input_specifier (input, 0));
270 output->type = formats[input->type].output;
271 output->w = input->w;
272 if (output->w > formats[output->type].Omax_w)
273 output->w = formats[output->type].Omax_w;
274 output->d = input->d;
284 output->w = max (max (input->w, input->d+7), 10);
285 output->d = max (input->d, 3);
289 /* nothing is necessary */
298 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
299 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
300 output->w = map[input->w / 2 - 1];
304 output->w = 8, output->d = 2; /* FIXME */
312 output->w = 8, output->d = 2;
314 output->w = 9 + input->d;
324 /* nothing is necessary */
327 output->w = input->w / 2;
334 /* nothing is necessary */
341 /* nothing is necessary */
352 /* nothing is necessary */
358 assert (check_output_specifier (output, 0));
361 /* Parses a format specifier from the token stream and returns
362 nonzero only if successful. Emits an error message on
363 failure. Allows X and T format specifiers only if ALLOW_XT is
364 nonzero. The caller should call check_input_specifier() or
365 check_output_specifier() on the parsed format as
368 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
370 struct fmt_spec spec;
378 if (!(flags & FMTP_SUPPRESS_ERRORS))
379 msg (SE, _("Format specifier expected."));
382 type = parse_format_specifier_name (&cp, flags);
387 w = strtol (cp, &cp2, 10);
388 if (cp2 == cp && type != FMT_X)
390 if (!(flags & FMTP_SUPPRESS_ERRORS))
391 msg (SE, _("Data format %s does not specify a width."),
397 if (f->n_args > 1 && *cp == '.')
400 d = strtol (cp, &cp2, 10);
408 if (!(flags & FMTP_SUPPRESS_ERRORS))
409 msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
422 /* Returns the width corresponding to the format specifier. The
423 return value is the value of the `width' member of a `struct
424 variable' for such an input format. */
426 get_format_var_width (const struct fmt_spec *spec)
428 if (spec->type == FMT_AHEX)
430 else if (spec->type == FMT_A)
436 /* Returns the PSPP format corresponding to the given SPSS
439 translate_fmt (int spss)
443 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
444 if (formats[type].spss == spss)
449 /* Returns an input format specification with type TYPE, width W,
452 make_input_format (int type, int w, int d)
458 assert (check_input_specifier (&f, 0));
462 /* Returns an output format specification with type TYPE, width
463 W, and D decimals. */
465 make_output_format (int type, int w, int d)
471 assert (check_output_specifier (&f, 0));