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
31 #define _(msgid) gettext (msgid)
33 #include "debug-print.h"
35 /* Variables on MIS VAL. */
36 static struct variable **v;
39 /* Type of the variables on MIS VAL. */
42 /* Width of string variables on MIS VAL. */
45 /* Items to fill-in var structs with. */
47 static union value missing[3];
49 static int parse_varnames (void);
50 static int parse_numeric (void);
51 static int parse_alpha (void);
54 cmd_missing_values (void)
60 if (!parse_varnames ())
65 if ((type == NUMERIC && !parse_numeric ())
66 || (type == ALPHA && !parse_alpha ()))
70 miss_type = MISSING_NONE;
74 msg (SE, _("`)' expected after value specification."));
78 for (i = 0; i < nv; i++)
80 v[i]->miss_type = miss_type;
81 memcpy (v[i]->missing, missing, sizeof v[i]->missing);
88 return lex_end_of_command ();
92 return CMD_PART_SUCCESS_MAYBE;
100 if (!parse_variables (default_dict, &v, &nv, PV_SAME_TYPE))
102 if (!lex_match ('('))
104 msg (SE, _("`(' expected after variable name%s."), nv > 1 ? "s" : "");
113 for (i = 1; i < nv; i++)
114 if (v[i]->type == ALPHA && v[i]->nv != 1)
116 msg (SE, _("Long string value specified."));
119 else if (v[i]->type == ALPHA && (int) width != v[i]->width)
121 msg (SE, _("Short strings must be of equal width."));
128 /* Number or range? */
131 MV_NOR_NOTHING, /* Empty. */
132 MV_NOR_NUMBER, /* Single number. */
133 MV_NOR_RANGE /* Range. */
136 /* A single value or a range. */
139 int type; /* One of NOR_*. */
140 double d[2]; /* d[0]=lower bound or value, d[1]=upper bound. */
143 /* Parses something of the form <num>, or LO[WEST] THRU <num>, or
144 <num> THRU HI[GHEST], or <num> THRU <num>, and sets the appropriate
145 members of NOR. Returns success. */
147 parse_num_or_range (struct num_or_range * nor)
149 if (lex_match_id ("LO") || lex_match_id ("LOWEST"))
151 nor->type = MV_NOR_RANGE;
152 if (!lex_force_match_id ("THRU"))
154 if (!lex_force_num ())
159 else if (lex_is_number ())
164 if (lex_match_id ("THRU"))
166 nor->type = MV_NOR_RANGE;
167 if (lex_match_id ("HI") || lex_match_id ("HIGHEST"))
171 if (!lex_force_num ())
176 if (nor->d[0] > nor->d[1])
178 msg (SE, _("Range %g THRU %g is not valid because %g is "
180 nor->d[0], nor->d[1], nor->d[0], nor->d[1]);
186 nor->type = MV_NOR_NUMBER;
194 /* Parses a set of numeric missing values and stores them into
195 `missing[]' and `miss_type' global variables. */
199 struct num_or_range set[3];
202 set[1].type = set[2].type = MV_NOR_NOTHING;
204 /* Get first number or range. */
205 r = parse_num_or_range (&set[0]);
209 msg (SE, _("Number or range expected."));
213 /* Get second and third optional number or range. */
215 r = parse_num_or_range (&set[1]);
219 r = parse_num_or_range (&set[2]);
224 /* Force range, if present, into set[0]. */
225 if (set[1].type == MV_NOR_RANGE)
227 struct num_or_range t = set[1];
231 if (set[2].type == MV_NOR_RANGE)
233 struct num_or_range t = set[2];
238 /* Ensure there's not more than one range, or one range
240 if (set[1].type == MV_NOR_RANGE || set[2].type == MV_NOR_RANGE)
242 msg (SE, _("At most one range can exist in the missing values "
243 "for any one variable."));
246 if (set[0].type == MV_NOR_RANGE && set[2].type != MV_NOR_NOTHING)
248 msg (SE, _("At most one individual value can be missing along "
253 /* Set missing[] from set[]. */
254 if (set[0].type == MV_NOR_RANGE)
258 if (set[0].d[0] == LOWEST)
260 miss_type = MISSING_LOW;
261 missing[x++].f = set[0].d[1];
263 else if (set[0].d[1] == HIGHEST)
265 miss_type = MISSING_HIGH;
266 missing[x++].f = set[0].d[0];
270 miss_type = MISSING_RANGE;
271 missing[x++].f = set[0].d[0];
272 missing[x++].f = set[0].d[1];
275 if (set[1].type == MV_NOR_NUMBER)
278 missing[x].f = set[1].d[0];
283 if (set[0].type == MV_NOR_NUMBER)
285 miss_type = MISSING_1;
286 missing[0].f = set[0].d[0];
288 if (set[1].type == MV_NOR_NUMBER)
290 miss_type = MISSING_2;
291 missing[1].f = set[1].d[0];
293 if (set[2].type == MV_NOR_NUMBER)
295 miss_type = MISSING_3;
296 missing[2].f = set[2].d[0];
306 for (miss_type = 0; token == T_STRING && miss_type < 3; miss_type++)
308 if (ds_length (&tokstr) != width)
310 msg (SE, _("String is not of proper length."));
313 strncpy (missing[miss_type].s, ds_c_str (&tokstr), MAX_SHORT_STRING);
319 msg (SE, _("String expected."));
326 /* Copy the missing values from variable SRC to variable DEST. */
328 copy_missing_values (struct variable *dest, const struct variable *src)
330 static const int n_values[MISSING_COUNT] =
332 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
335 assert (dest->width == src->width);
336 assert (src->miss_type >= 0 && src->miss_type < MISSING_COUNT);
341 dest->miss_type = src->miss_type;
342 for (i = 0; i < n_values[src->miss_type]; i++)
343 if (src->type == NUMERIC)
344 dest->missing[i].f = src->missing[i].f;
346 memcpy (dest->missing[i].s, src->missing[i].s, src->width);