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
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},
40 const int translate_fmt[40] =
42 -1, FMT_A, FMT_AHEX, FMT_COMMA, FMT_DOLLAR, FMT_F, FMT_IB,
43 FMT_PIBHEX, FMT_P, FMT_PIB, FMT_PK, FMT_RB, FMT_RBHEX, -1,
44 -1, FMT_Z, FMT_N, FMT_E, -1, -1, FMT_DATE, FMT_TIME,
45 FMT_DATETIME, FMT_ADATE, FMT_JDATE, FMT_DTIME, FMT_WKDAY,
46 FMT_MONTH, FMT_MOYR, FMT_QYR, FMT_WKYR, FMT_PCT, FMT_DOT,
47 FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE, FMT_EDATE,
52 parse_format_specifier_name (const char **cp, int allow_xt)
58 ep = ds_value (&tokstr);
59 while (isalpha ((unsigned char) *ep))
64 for (f = formats; f->name[0]; f++)
65 if (!strcmp (f->name, ds_value (&tokstr)))
67 int indx = f - formats;
73 if (!allow_xt && (indx == FMT_T || indx == FMT_X))
75 msg (SE, _("X and T format specifiers not allowed here."));
81 msg (SE, _("%s is not a valid data format."), ds_value (&tokstr));
86 /* Converts F to its string representation (for instance, "F8.2") and
87 returns a pointer to a static buffer containing that string. */
89 fmt_to_string (const struct fmt_spec *f)
93 if (formats[f->type].n_args >= 2)
94 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
96 sprintf (buf, "%s%d", formats[f->type].name, f->w);
101 check_input_specifier (const struct fmt_spec *spec)
106 f = &formats[spec->type];
107 str = fmt_to_string (spec);
108 if (spec->type == FMT_X)
110 if (f->cat & FCAT_OUTPUT_ONLY)
112 msg (SE, _("Format %s may not be used as an input format."), f->name);
115 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
117 msg (SE, _("Input format %s specifies a bad width %d. "
118 "Format %s requires a width between %d and %d."),
119 str, spec->w, f->name, f->Imin_w, f->Imax_w);
122 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
124 msg (SE, _("Input format %s specifies an odd width %d, but "
125 "format %s requires an even width between %d and "
126 "%d."), str, spec->w, f->name, f->Imin_w, f->Imax_w);
129 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
131 msg (SE, _("Input format %s specifies a bad number of "
132 "implied decimal places %d. Input format %s allows "
133 "up to 16 implied decimal places."), str, spec->d, f->name);
140 check_output_specifier (const struct fmt_spec *spec)
145 f = &formats[spec->type];
146 str = fmt_to_string (spec);
147 if (spec->type == FMT_X)
149 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
151 msg (SE, _("Output format %s specifies a bad width %d. "
152 "Format %s requires a width between %d and %d."),
153 str, spec->w, f->name, f->Omin_w, f->Omax_w);
157 && (spec->type == FMT_F || spec->type == FMT_COMMA
158 || spec->type == FMT_DOLLAR)
159 && spec->w < f->Omin_w + 1 + spec->d)
161 msg (SE, _("Output format %s requires minimum width %d to allow "
162 "%d decimal places. Try %s%d.%d instead of %s."),
163 f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
164 f->Omin_w + 1 + spec->d, spec->d, str);
167 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
169 msg (SE, _("Output format %s specifies an odd width %d, but "
170 "output format %s requires an even width between %d and "
171 "%d."), str, spec->w, f->name, f->Omin_w, f->Omax_w);
174 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
176 msg (SE, _("Output format %s specifies a bad number of "
177 "implied decimal places %d. Output format %s allows "
178 "a number of implied decimal places between 1 "
179 "and 16."), str, spec->d, f->name);
185 /* If a string variable has width W, you can't display it with a
186 format specifier with a required width MIN_LEN>W. */
188 check_string_specifier (const struct fmt_spec *f, int min_len)
190 if ((f->type == FMT_A && min_len > f->w)
191 || (f->type == FMT_AHEX && min_len * 2 > f->w))
193 msg (SE, _("Can't display a string variable of width %d with "
194 "format specifier %s."), min_len, fmt_to_string (f));
201 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
203 output->type = formats[input->type].output;
204 output->w = input->w;
205 if (output->w > formats[output->type].Omax_w)
206 output->w = formats[output->type].Omax_w;
207 output->d = input->d;
213 if (output->d > 1 && output->w < 2 + output->d)
214 output->w = 2 + output->d;
217 output->w = max (max (input->w, input->d+7), 10);
218 output->d = max (input->d, 3);
222 /* nothing is necessary */
231 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
232 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
233 output->w = map[input->w / 2 - 1];
237 output->w = 8, output->d = 2; /* FIXME */
245 output->w = 8, output->d = 2;
247 output->w = 9 + input->d;
257 /* nothing is necessary */
260 output->w = input->w / 2;
267 /* nothing is necessary */
274 /* nothing is necessary */
285 /* nothing is necessary */
293 parse_format_specifier (struct fmt_spec *input, int allow_xt)
295 struct fmt_spec spec;
303 msg (SE, _("Format specifier expected."));
306 type = parse_format_specifier_name (&cp, allow_xt);
311 w = strtol (cp, &cp2, 10);
312 if (cp2 == cp && type != FMT_X)
314 msg (SE, _("Data format %s does not specify a width."),
320 if (f->n_args > 1 && *cp == '.')
323 d = strtol (cp, &cp2, 10);
331 msg (SE, _("Data format %s is not valid."), ds_value (&tokstr));