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/assertion.h>
24 #include <libpspp/message.h>
26 #include <libpspp/compiler.h>
27 #include <libpspp/misc.h>
28 #include "identifier.h"
29 #include <libpspp/str.h>
33 #define _(msgid) gettext (msgid)
35 #define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
37 {NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
38 struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
41 {"", -1, -1, -1, -1, -1, 0000, -1, -1},
45 const struct fmt_spec f8_2 = {FMT_F, 8, 2};
47 /* Converts F to its string representation (for instance, "F8.2") and
48 returns a pointer to a static buffer containing that string. */
50 fmt_to_string (const struct fmt_spec *f)
54 if (formats[f->type].n_args >= 2)
55 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
57 sprintf (buf, "%s%d", formats[f->type].name, f->w);
61 /* Does checks in common betwen check_input_specifier() and
62 check_output_specifier() and returns true if so. Otherwise,
63 emits an error message (if EMIT_ERROR is nonzero) and returns
66 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
71 if ( spec->type > FMT_NUMBER_OF_FORMATS )
74 msg (SE, _("Format specifies a bad type (%d)"), spec->type);
79 f = &formats[spec->type];
80 str = fmt_to_string (spec);
82 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
85 msg (SE, _("Format %s specifies an odd width %d, but "
86 "an even width is required."),
90 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
93 msg (SE, _("Format %s specifies a bad number of "
94 "implied decimal places %d. Input format %s allows "
95 "up to 16 implied decimal places."), str, spec->d, f->name);
101 /* Checks whether SPEC is valid as an input format and returns
102 nonzero if so. Otherwise, emits an error message (if
103 EMIT_ERROR is nonzero) and returns zero. */
105 check_input_specifier (const struct fmt_spec *spec, int emit_error)
110 if (!check_common_specifier (spec, emit_error))
113 f = &formats[spec->type];
114 str = fmt_to_string (spec);
117 if (spec->type == FMT_X)
119 if (f->cat & FCAT_OUTPUT_ONLY)
122 msg (SE, _("Format %s may not be used for input."), f->name);
125 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
128 msg (SE, _("Input format %s specifies a bad width %d. "
129 "Format %s requires a width between %d and %d."),
130 str, spec->w, f->name, f->Imin_w, f->Imax_w);
133 if ((spec->type == FMT_F || spec->type == FMT_COMMA
134 || spec->type == FMT_DOLLAR)
135 && spec->d > spec->w)
138 msg (SE, _("Input format %s is invalid because it specifies more "
139 "decimal places than the field width."), str);
145 /* Checks whether SPEC is valid as an output format and returns
146 nonzero if so. Otherwise, emits an error message (if
147 EMIT_ERROR is nonzero) and returns zero. */
149 check_output_specifier (const struct fmt_spec *spec, int emit_error)
154 if (!check_common_specifier (spec, emit_error))
157 f = &formats[spec->type];
158 str = fmt_to_string (spec);
160 if (spec->type == FMT_X)
162 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
165 msg (SE, _("Output format %s specifies a bad width %d. "
166 "Format %s requires a width between %d and %d."),
167 str, spec->w, f->name, f->Omin_w, f->Omax_w);
170 if ((spec->type == FMT_F || spec->type == FMT_COMMA
171 || spec->type == FMT_DOLLAR)
172 && spec->d >= spec->w)
175 msg (SE, _("Output format %s is invalid because it specifies as "
176 "many decimal places as the field width, which "
177 "fails to allow space for a decimal point. "
178 "Try %s%d.%d instead."),
179 str, f->name, spec->d + 1, spec->d);
185 /* Checks that FORMAT is appropriate for a variable of the given
186 TYPE and returns true if so. Otherwise returns false and (if
187 EMIT_ERROR is true) emits an error message. */
189 check_specifier_type (const struct fmt_spec *format,
190 int type, bool emit_error)
192 const struct fmt_desc *f = &formats[format->type];
193 assert (type == NUMERIC || type == ALPHA);
194 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
197 msg (SE, _("%s variables are not compatible with %s format %s."),
198 type == ALPHA ? _("String") : _("Numeric"),
199 type == ALPHA ? _("numeric") : _("string"),
200 fmt_to_string (format));
206 /* Checks that FORMAT is appropriate for a variable of the given
207 WIDTH and returns true if so. Otherwise returns false and (if
208 EMIT_ERROR is true) emits an error message. */
210 check_specifier_width (const struct fmt_spec *format,
211 int width, bool emit_error)
213 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
215 if (get_format_var_width (format) != width)
218 msg (SE, _("String variable with width %d not compatible with "
220 width, fmt_to_string (format));
226 /* Converts input format specifier INPUT into output format
229 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
231 assert (check_input_specifier (input, 0));
233 output->type = formats[input->type].output;
234 output->w = input->w;
235 if (output->w > formats[output->type].Omax_w)
236 output->w = formats[output->type].Omax_w;
237 output->d = input->d;
247 output->w = max (max (input->w, input->d+7), 10);
248 output->d = max (input->d, 3);
252 /* nothing is necessary */
261 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
262 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
263 output->w = map[input->w / 2 - 1];
267 output->w = 8, output->d = 2; /* FIXME */
275 output->w = 8, output->d = 2;
277 output->w = 9 + input->d;
287 /* nothing is necessary */
290 output->w = input->w / 2;
297 /* nothing is necessary */
304 /* nothing is necessary */
315 /* nothing is necessary */
321 assert (check_output_specifier (output, 0));
324 /* Returns the width corresponding to the format specifier. The
325 return value is the value of the `width' member of a `struct
326 variable' for such an input format. */
328 get_format_var_width (const struct fmt_spec *spec)
330 if (spec->type == FMT_AHEX)
332 else if (spec->type == FMT_A)
338 /* Returns the PSPP format corresponding to the given SPSS
341 translate_fmt (int spss)
345 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
346 if (formats[type].spss == spss)
351 /* Returns an input format specification with type TYPE, width W,
354 make_input_format (int type, int w, int d)
360 assert (check_input_specifier (&f, 0));
364 /* Returns an output format specification with type TYPE, width
365 W, and D decimals. */
367 make_output_format (int type, int w, int d)
373 assert (check_output_specifier (&f, 0));
379 measure_is_valid(enum measure m)
381 if ( m <= 0 ) return false;
382 if ( m >= n_MEASURES) return false;
387 alignment_is_valid(enum alignment a)
389 if ( a >= n_ALIGN) return false;