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 const int translate_fmt[40] =
41 -1, FMT_A, FMT_AHEX, FMT_COMMA, FMT_DOLLAR, FMT_F, FMT_IB,
42 FMT_PIBHEX, FMT_P, FMT_PIB, FMT_PK, FMT_RB, FMT_RBHEX, -1,
43 -1, FMT_Z, FMT_N, FMT_E, -1, -1, FMT_DATE, FMT_TIME,
44 FMT_DATETIME, FMT_ADATE, FMT_JDATE, FMT_DTIME, FMT_WKDAY,
45 FMT_MONTH, FMT_MOYR, FMT_QYR, FMT_WKYR, FMT_PCT, FMT_DOT,
46 FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE, FMT_EDATE,
51 parse_format_specifier_name (const char **cp, int allow_xt)
57 ep = ds_value (&tokstr);
58 while (isalpha ((unsigned char) *ep))
63 for (f = formats; f->name[0]; f++)
64 if (!strcmp (f->name, ds_value (&tokstr)))
66 int indx = f - formats;
72 if (!allow_xt && (indx == FMT_T || indx == FMT_X))
74 msg (SE, _("X and T format specifiers not allowed here."));
80 msg (SE, _("%s is not a valid data format."), ds_value (&tokstr));
85 /* Converts F to its string representation (for instance, "F8.2") and
86 returns a pointer to a static buffer containing that string. */
88 fmt_to_string (const struct fmt_spec *f)
92 if (formats[f->type].n_args >= 2)
93 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
95 sprintf (buf, "%s%d", formats[f->type].name, f->w);
100 check_input_specifier (const struct fmt_spec *spec)
105 f = &formats[spec->type];
106 str = fmt_to_string (spec);
107 if (spec->type == FMT_X)
109 if (f->cat & FCAT_OUTPUT_ONLY)
111 msg (SE, _("Format %s may not be used as an input format."), f->name);
114 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
116 msg (SE, _("Input format %s specifies a bad width %d. "
117 "Format %s requires a width between %d and %d."),
118 str, spec->w, f->name, f->Imin_w, f->Imax_w);
121 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
123 msg (SE, _("Input format %s specifies an odd width %d, but "
124 "format %s requires an even width between %d and "
125 "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w);
128 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
130 msg (SE, _("Input format %s specifies a bad number of "
131 "implied decimal places %d. Input format %s allows "
132 "up to 16 implied decimal places."), str, spec->d, f->name);
139 check_output_specifier (const struct fmt_spec *spec)
144 f = &formats[spec->type];
145 str = fmt_to_string (spec);
146 if (spec->type == FMT_X)
148 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
150 msg (SE, _("Output format %s specifies a bad width %d. "
151 "Format %s requires a width between %d and %d."),
152 str, spec->w, f->name, f->Omin_w, f->Omax_w);
156 && (spec->type == FMT_F || spec->type == FMT_COMMA
157 || spec->type == FMT_DOLLAR)
158 && spec->w < f->Omin_w + 1 + spec->d)
160 msg (SE, _("Output format %s requires minimum width %d to allow "
161 "%d decimal places. Try %s%d.%d instead of %s."),
162 f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
163 f->Omin_w + 1 + spec->d, spec->d, str);
166 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
168 msg (SE, _("Output format %s specifies an odd width %d, but "
169 "output format %s requires an even width between %d and "
170 "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w);
173 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
175 msg (SE, _("Output format %s specifies a bad number of "
176 "implied decimal places %d. Output format %s allows "
177 "a number of implied decimal places between 1 "
178 "and 16."), str, spec->d, f->name);
184 /* If a string variable has width W, you can't display it with a
185 format specifier with a required width MIN_LEN>W. */
187 check_string_specifier (const struct fmt_spec *f, int min_len)
189 if ((f->type == FMT_A && min_len > f->w)
190 || (f->type == FMT_AHEX && min_len * 2 > f->w))
192 msg (SE, _("Can't display a string variable of width %d with "
193 "format specifier %s."), min_len, fmt_to_string (f));
200 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
202 output->type = formats[input->type].output;
203 output->w = input->w;
204 if (output->w > formats[output->type].Omax_w)
205 output->w = formats[output->type].Omax_w;
206 output->d = input->d;
212 if (output->d > 1 && output->w < 2 + output->d)
213 output->w = 2 + output->d;
216 output->w = max (max (input->w, input->d+7), 10);
217 output->d = max (input->d, 3);
221 /* nothing is necessary */
230 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
231 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
232 output->w = map[input->w / 2 - 1];
236 output->w = 8, output->d = 2; /* FIXME */
244 output->w = 8, output->d = 2;
246 output->w = 9 + input->d;
256 /* nothing is necessary */
259 output->w = input->w / 2;
266 /* nothing is necessary */
273 /* nothing is necessary */
284 /* nothing is necessary */
292 parse_format_specifier (struct fmt_spec *input, int allow_xt)
294 struct fmt_spec spec;
302 msg (SE, _("Format specifier expected."));
305 type = parse_format_specifier_name (&cp, allow_xt);
310 w = strtol (cp, &cp2, 10);
311 if (cp2 == cp && type != FMT_X)
313 msg (SE, _("Data format %s does not specify a width."),
319 if (f->n_args > 1 && *cp == '.')
322 d = strtol (cp, &cp2, 10);
330 msg (SE, _("Data format %s is not valid."), ds_value (&tokstr));
344 get_format_var_width (const struct fmt_spec *spec)
346 if (spec->type == FMT_AHEX)
348 else if (spec->type == FMT_A)