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., 59 Temple Place - Suite 330, Boston, MA
30 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
32 {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
33 struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
36 {"", -1, -1, -1, -1, -1, 0000, -1, -1},
39 /* Parses the alphabetic prefix of the current token as a format
40 specifier name. Returns the corresponding format specifier
41 type if successful, or -1 on failure. If ALLOW_XT is zero,
42 then X and T format specifiers are not allowed. If CP is
43 nonzero, then *CP is set to the first non-alphabetic character
44 in the current token on success or to a null pointer on
47 parse_format_specifier_name (const char **cp, enum fmt_parse_flags flags)
52 sp = ep = ds_c_str (&tokstr);
53 while (isalpha ((unsigned char) *ep))
59 for (idx = 0; idx < FMT_NUMBER_OF_FORMATS; idx++)
60 if (strlen (formats[idx].name) == ep - sp
61 && !memcmp (formats[idx].name, sp, ep - sp))
65 if (idx < FMT_NUMBER_OF_FORMATS)
67 if (!(flags & FMTP_ALLOW_XT) && (idx == FMT_T || idx == FMT_X))
69 if (!(flags & FMTP_SUPPRESS_ERRORS))
70 msg (SE, _("X and T format specifiers not allowed here."));
77 if (!(flags & FMTP_SUPPRESS_ERRORS))
78 msg (SE, _("%.*s is not a valid data format."),
79 (int) (ep - sp), ds_c_str (&tokstr));
85 lex_error ("expecting data format");
100 /* Converts F to its string representation (for instance, "F8.2") and
101 returns a pointer to a static buffer containing that string. */
103 fmt_to_string (const struct fmt_spec *f)
107 if (formats[f->type].n_args >= 2)
108 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
110 sprintf (buf, "%s%d", formats[f->type].name, f->w);
114 /* Checks whether SPEC is valid as an input format and returns
115 nonzero if so. Otherwise, emits an error message (if
116 EMIT_ERROR is nonzero) and returns zero. */
118 check_input_specifier (const struct fmt_spec *spec, int emit_error)
123 f = &formats[spec->type];
124 str = fmt_to_string (spec);
125 if (spec->type == FMT_X)
127 if (f->cat & FCAT_OUTPUT_ONLY)
130 msg (SE, _("Format %s may not be used as an input format."), f->name);
133 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
136 msg (SE, _("Input format %s specifies a bad width %d. "
137 "Format %s requires a width between %d and %d."),
138 str, spec->w, f->name, f->Imin_w, f->Imax_w);
141 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
144 msg (SE, _("Input format %s specifies an odd width %d, but "
145 "format %s requires an even width between %d and "
146 "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w);
149 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
152 msg (SE, _("Input format %s specifies a bad number of "
153 "implied decimal places %d. Input format %s allows "
154 "up to 16 implied decimal places."), str, spec->d, f->name);
160 /* Checks whether SPEC is valid as an output format and returns
161 nonzero if so. Otherwise, emits an error message (if
162 EMIT_ERROR is nonzero) and returns zero. */
164 check_output_specifier (const struct fmt_spec *spec, int emit_error)
169 f = &formats[spec->type];
170 str = fmt_to_string (spec);
171 if (spec->type == FMT_X)
173 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
176 msg (SE, _("Output format %s specifies a bad width %d. "
177 "Format %s requires a width between %d and %d."),
178 str, spec->w, f->name, f->Omin_w, f->Omax_w);
182 && (spec->type == FMT_F || spec->type == FMT_COMMA
183 || spec->type == FMT_DOLLAR)
184 && spec->w < f->Omin_w + 1 + spec->d)
187 msg (SE, _("Output format %s requires minimum width %d to allow "
188 "%d decimal places. Try %s%d.%d instead of %s."),
189 f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
190 f->Omin_w + 1 + spec->d, spec->d, str);
193 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
196 msg (SE, _("Output format %s specifies an odd width %d, but "
197 "output format %s requires an even width between %d and "
198 "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w);
201 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
204 msg (SE, _("Output format %s specifies a bad number of "
205 "implied decimal places %d. Output format %s allows "
206 "a number of implied decimal places between 1 "
207 "and 16."), str, spec->d, f->name);
213 /* If a string variable has width W, you can't display it with a
214 format specifier with a required width MIN_LEN>W. */
216 check_string_specifier (const struct fmt_spec *f, int min_len)
218 if ((f->type == FMT_A && min_len > f->w)
219 || (f->type == FMT_AHEX && min_len * 2 > f->w))
221 msg (SE, _("Can't display a string variable of width %d with "
222 "format specifier %s."), min_len, fmt_to_string (f));
228 /* Converts input format specifier INPUT into output format
231 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
233 output->type = formats[input->type].output;
234 output->w = input->w;
235 if (output->w > formats[output->type].Omax_w)
236 output->w = formats[output->type].Omax_w;
237 output->d = input->d;
243 if (output->d > 1 && output->w < 2 + output->d)
244 output->w = 2 + output->d;
247 output->w = max (max (input->w, input->d+7), 10);
248 output->d = max (input->d, 3);
252 /* nothing is necessary */
261 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
262 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
263 output->w = map[input->w / 2 - 1];
267 output->w = 8, output->d = 2; /* FIXME */
275 output->w = 8, output->d = 2;
277 output->w = 9 + input->d;
287 /* nothing is necessary */
290 output->w = input->w / 2;
297 /* nothing is necessary */
304 /* nothing is necessary */
315 /* nothing is necessary */
322 /* Parses a format specifier from the token stream and returns
323 nonzero only if successful. Emits an error message on
324 failure. Allows X and T format specifiers only if ALLOW_XT is
325 nonzero. The caller should call check_input_specifier() or
326 check_output_specifier() on the parsed format as
329 parse_format_specifier (struct fmt_spec *input, enum fmt_parse_flags flags)
331 struct fmt_spec spec;
339 if (!(flags & FMTP_SUPPRESS_ERRORS))
340 msg (SE, _("Format specifier expected."));
343 type = parse_format_specifier_name (&cp, flags);
348 w = strtol (cp, &cp2, 10);
349 if (cp2 == cp && type != FMT_X)
351 if (!(flags & FMTP_SUPPRESS_ERRORS))
352 msg (SE, _("Data format %s does not specify a width."),
358 if (f->n_args > 1 && *cp == '.')
361 d = strtol (cp, &cp2, 10);
369 if (!(flags & FMTP_SUPPRESS_ERRORS))
370 msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
383 /* Returns the width corresponding to the format specifier. The
384 return value is the value of the `width' member of a `struct
385 variable' for such an input format. */
387 get_format_var_width (const struct fmt_spec *spec)
389 if (spec->type == FMT_AHEX)
391 else if (spec->type == FMT_A)
397 /* Returns the PSPP format corresponding to the given SPSS
400 translate_fmt (int spss)
404 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
405 if (formats[type].spss == spss)