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
23 #include <libpspp/message.h>
25 #include <libpspp/compiler.h>
26 #include <libpspp/misc.h>
27 #include "identifier.h"
28 #include <libpspp/str.h>
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)
70 if ( spec->type > FMT_NUMBER_OF_FORMATS )
73 msg (SE, _("Format specifies a bad type (%d)"), spec->type);
78 f = &formats[spec->type];
79 str = fmt_to_string (spec);
81 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
84 msg (SE, _("Format %s specifies an odd width %d, but "
85 "an even width is required."),
89 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
92 msg (SE, _("Format %s specifies a bad number of "
93 "implied decimal places %d. Input format %s allows "
94 "up to 16 implied decimal places."), str, spec->d, f->name);
100 /* Checks whether SPEC is valid as an input format and returns
101 nonzero if so. Otherwise, emits an error message (if
102 EMIT_ERROR is nonzero) and returns zero. */
104 check_input_specifier (const struct fmt_spec *spec, int emit_error)
109 if (!check_common_specifier (spec, emit_error))
112 f = &formats[spec->type];
113 str = fmt_to_string (spec);
116 if (spec->type == FMT_X)
118 if (f->cat & FCAT_OUTPUT_ONLY)
121 msg (SE, _("Format %s may not be used for input."), f->name);
124 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 ((spec->type == FMT_F || spec->type == FMT_COMMA
133 || spec->type == FMT_DOLLAR)
134 && spec->d > spec->w)
137 msg (SE, _("Input format %s is invalid because it specifies more "
138 "decimal places than the field width."), str);
144 /* Checks whether SPEC is valid as an output format and returns
145 nonzero if so. Otherwise, emits an error message (if
146 EMIT_ERROR is nonzero) and returns zero. */
148 check_output_specifier (const struct fmt_spec *spec, int emit_error)
153 if (!check_common_specifier (spec, emit_error))
156 f = &formats[spec->type];
157 str = fmt_to_string (spec);
159 if (spec->type == FMT_X)
161 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);
169 if ((spec->type == FMT_F || spec->type == FMT_COMMA
170 || spec->type == FMT_DOLLAR)
171 && spec->d >= spec->w)
174 msg (SE, _("Output format %s is invalid because it specifies as "
175 "many decimal places as the field width, which "
176 "fails to allow space for a decimal point. "
177 "Try %s%d.%d instead."),
178 str, f->name, spec->d + 1, spec->d);
184 /* Checks that FORMAT is appropriate for a variable of the given
185 TYPE and returns true if so. Otherwise returns false and (if
186 EMIT_ERROR is true) emits an error message. */
188 check_specifier_type (const struct fmt_spec *format,
189 int type, bool emit_error)
191 const struct fmt_desc *f = &formats[format->type];
192 assert (type == NUMERIC || type == ALPHA);
193 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
196 msg (SE, _("%s variables are not compatible with %s format %s."),
197 type == ALPHA ? _("String") : _("Numeric"),
198 type == ALPHA ? _("numeric") : _("string"),
199 fmt_to_string (format));
205 /* Checks that FORMAT is appropriate for a variable of the given
206 WIDTH and returns true if so. Otherwise returns false and (if
207 EMIT_ERROR is true) emits an error message. */
209 check_specifier_width (const struct fmt_spec *format,
210 int width, bool emit_error)
212 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
214 if (get_format_var_width (format) != width)
217 msg (SE, _("String variable with width %d not compatible with "
219 width, fmt_to_string (format));
225 /* Converts input format specifier INPUT into output format
228 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
230 assert (check_input_specifier (input, 0));
232 output->type = formats[input->type].output;
233 output->w = input->w;
234 if (output->w > formats[output->type].Omax_w)
235 output->w = formats[output->type].Omax_w;
236 output->d = input->d;
246 output->w = max (max (input->w, input->d+7), 10);
247 output->d = max (input->d, 3);
251 /* nothing is necessary */
260 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
261 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
262 output->w = map[input->w / 2 - 1];
266 output->w = 8, output->d = 2; /* FIXME */
274 output->w = 8, output->d = 2;
276 output->w = 9 + input->d;
286 /* nothing is necessary */
289 output->w = input->w / 2;
296 /* nothing is necessary */
303 /* nothing is necessary */
314 /* nothing is necessary */
320 assert (check_output_specifier (output, 0));
323 /* Returns the width corresponding to the format specifier. The
324 return value is the value of the `width' member of a `struct
325 variable' for such an input format. */
327 get_format_var_width (const struct fmt_spec *spec)
329 if (spec->type == FMT_AHEX)
331 else if (spec->type == FMT_A)
337 /* Returns the PSPP format corresponding to the given SPSS
340 translate_fmt (int spss)
344 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
345 if (formats[type].spss == spss)
350 /* Returns an input format specification with type TYPE, width W,
353 make_input_format (int type, int w, int d)
359 assert (check_input_specifier (&f, 0));
363 /* Returns an output format specification with type TYPE, width
364 W, and D decimals. */
366 make_output_format (int type, int w, int d)
372 assert (check_output_specifier (&f, 0));