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., 51 Franklin Street, Fifth Floor, Boston, MA
32 #define _(msgid) gettext (msgid)
34 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
36 {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
37 struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
40 {"", -1, -1, -1, -1, -1, 0000, -1, -1},
44 const struct fmt_spec f8_2 = {FMT_F, 8, 2};
46 /* Converts F to its string representation (for instance, "F8.2") and
47 returns a pointer to a static buffer containing that string. */
49 fmt_to_string (const struct fmt_spec *f)
53 if (formats[f->type].n_args >= 2)
54 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
56 sprintf (buf, "%s%d", formats[f->type].name, f->w);
60 /* Does checks in common betwen check_input_specifier() and
61 check_output_specifier() and returns true if so. Otherwise,
62 emits an error message (if EMIT_ERROR is nonzero) and returns
65 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
67 struct fmt_desc *f = &formats[spec->type];
68 char *str = fmt_to_string (spec);
70 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
73 msg (SE, _("Format %s specifies an odd width %d, but "
74 "an even width is required."),
78 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
81 msg (SE, _("Format %s specifies a bad number of "
82 "implied decimal places %d. Input format %s allows "
83 "up to 16 implied decimal places."), str, spec->d, f->name);
89 /* Checks whether SPEC is valid as an input format and returns
90 nonzero if so. Otherwise, emits an error message (if
91 EMIT_ERROR is nonzero) and returns zero. */
93 check_input_specifier (const struct fmt_spec *spec, int emit_error)
95 struct fmt_desc *f = &formats[spec->type];
96 char *str = fmt_to_string (spec);
98 if (!check_common_specifier (spec, emit_error))
100 if (spec->type == FMT_X)
102 if (f->cat & FCAT_OUTPUT_ONLY)
105 msg (SE, _("Format %s may not be used for input."), f->name);
108 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
111 msg (SE, _("Input format %s specifies a bad width %d. "
112 "Format %s requires a width between %d and %d."),
113 str, spec->w, f->name, f->Imin_w, f->Imax_w);
116 if ((spec->type == FMT_F || spec->type == FMT_COMMA
117 || spec->type == FMT_DOLLAR)
118 && spec->d > spec->w)
121 msg (SE, _("Input format %s is invalid because it specifies more "
122 "decimal places than the field width."), str);
128 /* Checks whether SPEC is valid as an output format and returns
129 nonzero if so. Otherwise, emits an error message (if
130 EMIT_ERROR is nonzero) and returns zero. */
132 check_output_specifier (const struct fmt_spec *spec, int emit_error)
134 struct fmt_desc *f = &formats[spec->type];
135 char *str = fmt_to_string (spec);
137 if (!check_common_specifier (spec, emit_error))
139 if (spec->type == FMT_X)
141 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
144 msg (SE, _("Output format %s specifies a bad width %d. "
145 "Format %s requires a width between %d and %d."),
146 str, spec->w, f->name, f->Omin_w, f->Omax_w);
149 if ((spec->type == FMT_F || spec->type == FMT_COMMA
150 || spec->type == FMT_DOLLAR)
151 && spec->d >= spec->w)
154 msg (SE, _("Output format %s is invalid because it specifies as "
155 "many decimal places as the field width, which "
156 "fails to allow space for a decimal point. "
157 "Try %s%d.%d instead."),
158 str, f->name, spec->d + 1, spec->d);
164 /* Checks that FORMAT is appropriate for a variable of the given
165 TYPE and returns true if so. Otherwise returns false and (if
166 EMIT_ERROR is true) emits an error message. */
168 check_specifier_type (const struct fmt_spec *format,
169 int type, bool emit_error)
171 const struct fmt_desc *f = &formats[format->type];
172 assert (type == NUMERIC || type == ALPHA);
173 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
176 msg (SE, _("%s variables are not compatible with %s format %s."),
177 type == ALPHA ? _("String") : _("Numeric"),
178 type == ALPHA ? _("numeric") : _("string"),
179 fmt_to_string (format));
185 /* Checks that FORMAT is appropriate for a variable of the given
186 WIDTH and returns true if so. Otherwise returns false and (if
187 EMIT_ERROR is true) emits an error message. */
189 check_specifier_width (const struct fmt_spec *format,
190 int width, bool emit_error)
192 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
194 if (get_format_var_width (format) != width)
197 msg (SE, _("String variable with width %d not compatible with "
199 width, fmt_to_string (format));
205 /* Converts input format specifier INPUT into output format
208 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
210 assert (check_input_specifier (input, 0));
212 output->type = formats[input->type].output;
213 output->w = input->w;
214 if (output->w > formats[output->type].Omax_w)
215 output->w = formats[output->type].Omax_w;
216 output->d = input->d;
226 output->w = max (max (input->w, input->d+7), 10);
227 output->d = max (input->d, 3);
231 /* nothing is necessary */
240 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
241 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
242 output->w = map[input->w / 2 - 1];
246 output->w = 8, output->d = 2; /* FIXME */
254 output->w = 8, output->d = 2;
256 output->w = 9 + input->d;
266 /* nothing is necessary */
269 output->w = input->w / 2;
276 /* nothing is necessary */
283 /* nothing is necessary */
294 /* nothing is necessary */
300 assert (check_output_specifier (output, 0));
303 /* Returns the width corresponding to the format specifier. The
304 return value is the value of the `width' member of a `struct
305 variable' for such an input format. */
307 get_format_var_width (const struct fmt_spec *spec)
309 if (spec->type == FMT_AHEX)
311 else if (spec->type == FMT_A)
317 /* Returns the PSPP format corresponding to the given SPSS
320 translate_fmt (int spss)
324 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
325 if (formats[type].spss == spss)
330 /* Returns an input format specification with type TYPE, width W,
333 make_input_format (int type, int w, int d)
339 assert (check_input_specifier (&f, 0));
343 /* Returns an output format specification with type TYPE, width
344 W, and D decimals. */
346 make_output_format (int type, int w, int d)
352 assert (check_output_specifier (&f, 0));