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_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 (!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_c_str (&tokstr));
83 lex_error ("expecting data format");
98 /* Converts F to its string representation (for instance, "F8.2") and
99 returns a pointer to a static buffer containing that string. */
101 fmt_to_string (const struct fmt_spec *f)
105 if (formats[f->type].n_args >= 2)
106 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
108 sprintf (buf, "%s%d", formats[f->type].name, f->w);
112 /* Checks whether SPEC is valid as an input format and returns
113 nonzero if so. Otherwise, emits an error message and returns
116 check_input_specifier (const struct fmt_spec *spec)
121 f = &formats[spec->type];
122 str = fmt_to_string (spec);
123 if (spec->type == FMT_X)
125 if (f->cat & FCAT_OUTPUT_ONLY)
127 msg (SE, _("Format %s may not be used as an input format."), f->name);
130 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
132 msg (SE, _("Input format %s specifies a bad width %d. "
133 "Format %s requires a width between %d and %d."),
134 str, spec->w, f->name, f->Imin_w, f->Imax_w);
137 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
139 msg (SE, _("Input format %s specifies an odd width %d, but "
140 "format %s requires an even width between %d and "
141 "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w);
144 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
146 msg (SE, _("Input format %s specifies a bad number of "
147 "implied decimal places %d. Input format %s allows "
148 "up to 16 implied decimal places."), str, spec->d, f->name);
154 /* Checks whether SPEC is valid as an output format and returns
155 nonzero if so. Otherwise, emits an error message and returns
158 check_output_specifier (const struct fmt_spec *spec)
163 f = &formats[spec->type];
164 str = fmt_to_string (spec);
165 if (spec->type == FMT_X)
167 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
169 msg (SE, _("Output format %s specifies a bad width %d. "
170 "Format %s requires a width between %d and %d."),
171 str, spec->w, f->name, f->Omin_w, f->Omax_w);
175 && (spec->type == FMT_F || spec->type == FMT_COMMA
176 || spec->type == FMT_DOLLAR)
177 && spec->w < f->Omin_w + 1 + spec->d)
179 msg (SE, _("Output format %s requires minimum width %d to allow "
180 "%d decimal places. Try %s%d.%d instead of %s."),
181 f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
182 f->Omin_w + 1 + spec->d, spec->d, str);
185 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
187 msg (SE, _("Output format %s specifies an odd width %d, but "
188 "output format %s requires an even width between %d and "
189 "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w);
192 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
194 msg (SE, _("Output format %s specifies a bad number of "
195 "implied decimal places %d. Output format %s allows "
196 "a number of implied decimal places between 1 "
197 "and 16."), str, spec->d, f->name);
203 /* If a string variable has width W, you can't display it with a
204 format specifier with a required width MIN_LEN>W. */
206 check_string_specifier (const struct fmt_spec *f, int min_len)
208 if ((f->type == FMT_A && min_len > f->w)
209 || (f->type == FMT_AHEX && min_len * 2 > f->w))
211 msg (SE, _("Can't display a string variable of width %d with "
212 "format specifier %s."), min_len, fmt_to_string (f));
218 /* Converts input format specifier INPUT into output format
221 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
223 output->type = formats[input->type].output;
224 output->w = input->w;
225 if (output->w > formats[output->type].Omax_w)
226 output->w = formats[output->type].Omax_w;
227 output->d = input->d;
233 if (output->d > 1 && output->w < 2 + output->d)
234 output->w = 2 + output->d;
237 output->w = max (max (input->w, input->d+7), 10);
238 output->d = max (input->d, 3);
242 /* nothing is necessary */
251 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
252 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
253 output->w = map[input->w / 2 - 1];
257 output->w = 8, output->d = 2; /* FIXME */
265 output->w = 8, output->d = 2;
267 output->w = 9 + input->d;
277 /* nothing is necessary */
280 output->w = input->w / 2;
287 /* nothing is necessary */
294 /* nothing is necessary */
305 /* nothing is necessary */
312 /* Parses a format specifier from the token stream and returns
313 nonzero only if successful. Emits an error message on
314 failure. Allows X and T format specifiers only if ALLOW_XT is
315 nonzero. The caller should call check_input_specifier() or
316 check_output_specifier() on the parsed format as
319 parse_format_specifier (struct fmt_spec *input, int allow_xt)
321 struct fmt_spec spec;
329 msg (SE, _("Format specifier expected."));
332 type = parse_format_specifier_name (&cp, allow_xt);
337 w = strtol (cp, &cp2, 10);
338 if (cp2 == cp && type != FMT_X)
340 msg (SE, _("Data format %s does not specify a width."),
346 if (f->n_args > 1 && *cp == '.')
349 d = strtol (cp, &cp2, 10);
357 msg (SE, _("Data format %s is not valid."), ds_c_str (&tokstr));
370 /* Returns the width corresponding to the format specifier. The
371 return value is the value of the `width' member of a `struct
372 variable' for such an input format. */
374 get_format_var_width (const struct fmt_spec *spec)
376 if (spec->type == FMT_AHEX)
378 else if (spec->type == FMT_A)
384 /* Returns the PSPP format corresponding to the given SPSS
387 translate_fmt (int spss)
391 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
392 if (formats[type].spss == spss)