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 /* Tries to parse NAME as a format type.
48 If successful, stores the type in *TYPE and returns true.
49 On failure, returns false. */
51 fmt_type_from_string (const char *name, int *type)
55 for (i = 0; i < FMT_NUMBER_OF_FORMATS; i++)
56 if (!strcasecmp (name, formats[i].name))
64 /* Converts F to its string representation (for instance, "F8.2") and
65 returns a pointer to a static buffer containing that string.
66 If F has decimals, then they are included in the output
67 string, even if F's format type does not, so that we can
68 accurately present incorrect formats to the user. */
70 fmt_to_string (const struct fmt_spec *f)
74 if (formats[f->type].n_args >= 2 || f->d > 0)
75 sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
77 sprintf (buf, "%s%d", formats[f->type].name, f->w);
81 /* Does checks in common betwen check_input_specifier() and
82 check_output_specifier() and returns true if so. Otherwise,
83 emits an error message (if EMIT_ERROR is nonzero) and returns
86 check_common_specifier (const struct fmt_spec *spec, bool emit_error)
91 assert (spec->type < FMT_NUMBER_OF_FORMATS);
92 f = &formats[spec->type];
93 str = fmt_to_string (spec);
95 if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
98 msg (SE, _("Format %s specifies an odd width %d, but "
99 "an even width is required."),
103 if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
106 msg (SE, _("Format %s specifies a bad number of "
107 "implied decimal places %d. Format type %s allows "
108 "up to 16 implied decimal places."), str, spec->d, f->name);
111 if (f->n_args <= 1 && spec->d)
114 msg (SE, _("Format %s specifies %d decimal places, but "
115 "format type %s does not allow for decimal places."),
116 str, spec->d, f->name);
122 /* Checks whether SPEC is valid as an input format and returns
123 nonzero if so. Otherwise, emits an error message (if
124 EMIT_ERROR is nonzero) and returns zero. */
126 check_input_specifier (const struct fmt_spec *spec, int emit_error)
131 if (!check_common_specifier (spec, emit_error))
134 f = &formats[spec->type];
135 str = fmt_to_string (spec);
136 if (f->cat & FCAT_OUTPUT_ONLY)
139 msg (SE, _("Format %s may not be used for input."), f->name);
142 if (spec->w < f->Imin_w || spec->w > f->Imax_w)
145 msg (SE, _("Input format %s specifies a bad width %d. "
146 "Format %s requires a width between %d and %d."),
147 str, spec->w, f->name, f->Imin_w, f->Imax_w);
150 if ((spec->type == FMT_F || spec->type == FMT_COMMA
151 || spec->type == FMT_DOLLAR)
152 && spec->d > spec->w)
155 msg (SE, _("Input format %s is invalid because it specifies more "
156 "decimal places than the field width."), str);
162 /* Checks whether SPEC is valid as an output format and returns
163 nonzero if so. Otherwise, emits an error message (if
164 EMIT_ERROR is nonzero) and returns zero. */
166 check_output_specifier (const struct fmt_spec *spec, int emit_error)
171 if (!check_common_specifier (spec, emit_error))
174 f = &formats[spec->type];
175 str = fmt_to_string (spec);
176 if (spec->w < f->Omin_w || spec->w > f->Omax_w)
179 msg (SE, _("Output format %s specifies a bad width %d. "
180 "Format %s requires a width between %d and %d."),
181 str, spec->w, f->name, f->Omin_w, f->Omax_w);
184 if ((spec->type == FMT_F || spec->type == FMT_COMMA
185 || spec->type == FMT_DOLLAR)
186 && spec->d >= spec->w)
189 msg (SE, _("Output format %s is invalid because it specifies as "
190 "many decimal places as the field width, which "
191 "fails to allow space for a decimal point. "
192 "Try %s%d.%d instead."),
193 str, f->name, spec->d + 1, spec->d);
199 /* Checks that FORMAT is appropriate for a variable of the given
200 TYPE and returns true if so. Otherwise returns false and (if
201 EMIT_ERROR is true) emits an error message. */
203 check_specifier_type (const struct fmt_spec *format,
204 int type, bool emit_error)
206 const struct fmt_desc *f = &formats[format->type];
207 assert (type == NUMERIC || type == ALPHA);
208 if ((type == ALPHA) != ((f->cat & FCAT_STRING) != 0))
211 msg (SE, _("%s variables are not compatible with %s format %s."),
212 type == ALPHA ? _("String") : _("Numeric"),
213 type == ALPHA ? _("numeric") : _("string"),
214 fmt_to_string (format));
220 /* Checks that FORMAT is appropriate for a variable of the given
221 WIDTH and returns true if so. Otherwise returns false and (if
222 EMIT_ERROR is true) emits an error message. */
224 check_specifier_width (const struct fmt_spec *format,
225 int width, bool emit_error)
227 if (!check_specifier_type (format, width != 0 ? ALPHA : NUMERIC, emit_error))
229 if (get_format_var_width (format) != width)
232 msg (SE, _("String variable with width %d not compatible with "
234 width, fmt_to_string (format));
240 /* Converts input format specifier INPUT into output format
243 convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
245 assert (check_input_specifier (input, 0));
247 output->type = formats[input->type].output;
248 output->w = input->w;
249 if (output->w > formats[output->type].Omax_w)
250 output->w = formats[output->type].Omax_w;
251 output->d = input->d;
261 output->w = max (max (input->w, input->d+7), 10);
262 output->d = max (input->d, 3);
266 /* nothing is necessary */
275 static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
276 assert (input->w % 2 == 0 && input->w >= 2 && input->w <= 16);
277 output->w = map[input->w / 2 - 1];
281 output->w = 8, output->d = 2; /* FIXME */
289 output->w = 8, output->d = 2;
291 output->w = 9 + input->d;
301 /* nothing is necessary */
304 output->w = input->w / 2;
311 /* nothing is necessary */
318 /* nothing is necessary */
329 /* nothing is necessary */
335 assert (check_output_specifier (output, 0));
338 /* Returns the width corresponding to the format specifier. The
339 return value is the value of the `width' member of a `struct
340 variable' for such an input format. */
342 get_format_var_width (const struct fmt_spec *spec)
344 if (spec->type == FMT_AHEX)
346 else if (spec->type == FMT_A)
352 /* Returns the PSPP format corresponding to the given SPSS
355 translate_fmt (int spss)
359 for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
360 if (formats[type].spss == spss)
365 /* Returns an input format specification with type TYPE, width W,
368 make_input_format (int type, int w, int d)
374 assert (check_input_specifier (&f, 0));
378 /* Returns an output format specification with type TYPE, width
379 W, and D decimals. */
381 make_output_format (int type, int w, int d)
387 assert (check_output_specifier (&f, 0));
393 measure_is_valid(enum measure m)
395 if ( m <= 0 ) return false;
396 if ( m >= n_MEASURES) return false;
401 alignment_is_valid(enum alignment a)
403 if ( a >= n_ALIGN) return false;