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, int allow_xt)
52 sp = ep = ds_value (&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 (!allow_xt && (idx == FMT_T || idx == FMT_X))
69 msg (SE, _("X and T format specifiers not allowed here."));
76 msg (SE, _("%.*s is not a valid data format."),
77 (int) (ep - sp), ds_value (&tokstr));
83 lex_error ("expecting data format");
93 /* Converts F to its string representation (for instance, "F8.2") and
94 returns a pointer to a static buffer containing that string. */
96 fmt_to_string (const struct fmt_spec *f)
100 if (formats[f->type].n_args >= 2)
101 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
103 sprintf (buf, "%s%d", formats[f->type].name, f->w);
107 /* Checks whether SPEC is valid as an input format and returns
108 nonzero if so. Otherwise, emits an error message and returns
111 check_input_specifier (const struct fmt_spec *spec)
116 f = &formats[spec->type];
117 str = fmt_to_string (spec);
118 if (spec->type == FMT_X)
120 if (f->cat & FCAT_OUTPUT_ONLY)
122 msg (SE, _("Format %s may not be used as an input format."), f->name);
125 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
127 msg (SE, _("Input format %s specifies a bad width %d. "
128 "Format %s requires a width between %d and %d."),
129 str, spec->w, f->name, f->Imin_w, f->Imax_w);
132 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
134 msg (SE, _("Input format %s specifies an odd width %d, but "
135 "format %s requires an even width between %d and "
136 "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w);
139 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
141 msg (SE, _("Input format %s specifies a bad number of "
142 "implied decimal places %d. Input format %s allows "
143 "up to 16 implied decimal places."), str, spec->d, f->name);
149 /* Checks whether SPEC is valid as an output format and returns
150 nonzero if so. Otherwise, emits an error message and returns
153 check_output_specifier (const struct fmt_spec *spec)
158 f = &formats[spec->type];
159 str = fmt_to_string (spec);
160 if (spec->type == FMT_X)
162 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
164 msg (SE, _("Output format %s specifies a bad width %d. "
165 "Format %s requires a width between %d and %d."),
166 str, spec->w, f->name, f->Omin_w, f->Omax_w);
170 && (spec->type == FMT_F || spec->type == FMT_COMMA
171 || spec->type == FMT_DOLLAR)
172 && spec->w < f->Omin_w + 1 + spec->d)
174 msg (SE, _("Output format %s requires minimum width %d to allow "
175 "%d decimal places. Try %s%d.%d instead of %s."),
176 f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
177 f->Omin_w + 1 + spec->d, spec->d, str);
180 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
182 msg (SE, _("Output format %s specifies an odd width %d, but "
183 "output format %s requires an even width between %d and "
184 "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w);
187 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
189 msg (SE, _("Output format %s specifies a bad number of "
190 "implied decimal places %d. Output format %s allows "
191 "a number of implied decimal places between 1 "
192 "and 16."), str, spec->d, f->name);
198 /* If a string variable has width W, you can't display it with a
199 format specifier with a required width MIN_LEN>W. */
201 check_string_specifier (const struct fmt_spec *f, int min_len)
203 if ((f->type == FMT_A && min_len > f->w)
204 || (f->type == FMT_AHEX && min_len * 2 > f->w))
206 msg (SE, _("Can't display a string variable of width %d with "
207 "format specifier %s."), min_len, fmt_to_string (f));
213 /* Converts input format specifier INPUT into output format
216 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
218 output->type = formats[input->type].output;
219 output->w = input->w;
220 if (output->w > formats[output->type].Omax_w)
221 output->w = formats[output->type].Omax_w;
222 output->d = input->d;
228 if (output->d > 1 && output->w < 2 + output->d)
229 output->w = 2 + output->d;
232 output->w = max (max (input->w, input->d+7), 10);
233 output->d = max (input->d, 3);
237 /* nothing is necessary */
246 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
247 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
248 output->w = map[input->w / 2 - 1];
252 output->w = 8, output->d = 2; /* FIXME */
260 output->w = 8, output->d = 2;
262 output->w = 9 + input->d;
272 /* nothing is necessary */
275 output->w = input->w / 2;
282 /* nothing is necessary */
289 /* nothing is necessary */
300 /* nothing is necessary */
307 /* Parses a format specifier from the token stream and returns
308 nonzero only if successful. Emits an error message on
309 failure. Allows X and T format specifiers only if ALLOW_XT is
310 nonzero. The caller should call check_input_specifier() or
311 check_output_specifier() on the parsed format as
314 parse_format_specifier (struct fmt_spec *input, int allow_xt)
316 struct fmt_spec spec;
324 msg (SE, _("Format specifier expected."));
327 type = parse_format_specifier_name (&cp, allow_xt);
332 w = strtol (cp, &cp2, 10);
333 if (cp2 == cp && type != FMT_X)
335 msg (SE, _("Data format %s does not specify a width."),
341 if (f->n_args > 1 && *cp == '.')
344 d = strtol (cp, &cp2, 10);
352 msg (SE, _("Data format %s is not valid."), ds_value (&tokstr));
365 /* Returns the width corresponding to the format specifier. The
366 return value is the value of the `width' member of a `struct
367 variable' for such an input format. */
369 get_format_var_width (const struct fmt_spec *spec)
371 if (spec->type == FMT_AHEX)
373 else if (spec->type == FMT_A)
379 /* Returns the PSPP format corresponding to the given SPSS
382 translate_fmt (int spss)
386 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
387 if (formats[type].spss == spss)