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., 59 Temple Place - Suite 330, Boston, MA
30 #include "debug-print.h"
32 /* Variables on MIS VAL. */
33 static struct variable **v;
36 /* Type of the variables on MIS VAL. */
39 /* Width of string variables on MIS VAL. */
42 /* Items to fill-in var structs with. */
44 static union value missing[3];
46 static int parse_varnames (void);
47 static int parse_numeric (void);
48 static int parse_alpha (void);
51 cmd_missing_values (void)
57 if (!parse_varnames ())
62 if ((type == NUMERIC && !parse_numeric ())
63 || (type == ALPHA && !parse_alpha ()))
67 miss_type = MISSING_NONE;
71 msg (SE, _("`)' expected after value specification."));
75 for (i = 0; i < nv; i++)
77 v[i]->miss_type = miss_type;
78 memcpy (v[i]->missing, missing, sizeof v[i]->missing);
85 return lex_end_of_command ();
89 return CMD_PART_SUCCESS_MAYBE;
97 if (!parse_variables (default_dict, &v, &nv, PV_SAME_TYPE))
101 msg (SE, _("`(' expected after variable name%s."), nv > 1 ? "s" : "");
110 for (i = 1; i < nv; i++)
111 if (v[i]->type == ALPHA && v[i]->nv != 1)
113 msg (SE, _("Long string value specified."));
116 else if (v[i]->type == ALPHA && (int) width != v[i]->width)
118 msg (SE, _("Short strings must be of equal width."));
125 /* Number or range? */
128 MV_NOR_NOTHING, /* Empty. */
129 MV_NOR_NUMBER, /* Single number. */
130 MV_NOR_RANGE /* Range. */
133 /* A single value or a range. */
136 int type; /* One of NOR_*. */
137 double d[2]; /* d[0]=lower bound or value, d[1]=upper bound. */
140 /* Parses something of the form <num>, or LO[WEST] THRU <num>, or
141 <num> THRU HI[GHEST], or <num> THRU <num>, and sets the appropriate
142 members of NOR. Returns success. */
144 parse_num_or_range (struct num_or_range * nor)
146 if (lex_match_id ("LO") || lex_match_id ("LOWEST"))
148 nor->type = MV_NOR_RANGE;
149 if (!lex_force_match_id ("THRU"))
151 if (!lex_force_num ())
156 else if (token == T_NUM)
161 if (lex_match_id ("THRU"))
163 nor->type = MV_NOR_RANGE;
164 if (lex_match_id ("HI") || lex_match_id ("HIGHEST"))
168 if (!lex_force_num ())
173 if (nor->d[0] > nor->d[1])
175 msg (SE, _("Range %g THRU %g is not valid because %g is "
177 nor->d[0], nor->d[1], nor->d[0], nor->d[1]);
183 nor->type = MV_NOR_NUMBER;
191 /* Parses a set of numeric missing values and stores them into
192 `missing[]' and `miss_type' global variables. */
196 struct num_or_range set[3];
199 set[1].type = set[2].type = MV_NOR_NOTHING;
201 /* Get first number or range. */
202 r = parse_num_or_range (&set[0]);
206 msg (SE, _("Number or range expected."));
210 /* Get second and third optional number or range. */
212 r = parse_num_or_range (&set[1]);
216 r = parse_num_or_range (&set[2]);
221 /* Force range, if present, into set[0]. */
222 if (set[1].type == MV_NOR_RANGE)
224 struct num_or_range t = set[1];
228 if (set[2].type == MV_NOR_RANGE)
230 struct num_or_range t = set[2];
235 /* Ensure there's not more than one range, or one range
237 if (set[1].type == MV_NOR_RANGE || set[2].type == MV_NOR_RANGE)
239 msg (SE, _("At most one range can exist in the missing values "
240 "for any one variable."));
243 if (set[0].type == MV_NOR_RANGE && set[2].type != MV_NOR_NOTHING)
245 msg (SE, _("At most one individual value can be missing along "
250 /* Set missing[] from set[]. */
251 if (set[0].type == MV_NOR_RANGE)
255 if (set[0].d[0] == LOWEST)
257 miss_type = MISSING_LOW;
258 missing[x++].f = set[0].d[1];
260 else if (set[0].d[1] == HIGHEST)
262 miss_type = MISSING_HIGH;
263 missing[x++].f = set[0].d[0];
267 miss_type = MISSING_RANGE;
268 missing[x++].f = set[0].d[0];
269 missing[x++].f = set[0].d[1];
272 if (set[1].type == MV_NOR_NUMBER)
275 missing[x].f = set[1].d[0];
280 if (set[0].type == MV_NOR_NUMBER)
282 miss_type = MISSING_1;
283 missing[0].f = set[0].d[0];
285 if (set[1].type == MV_NOR_NUMBER)
287 miss_type = MISSING_2;
288 missing[1].f = set[1].d[0];
290 if (set[2].type == MV_NOR_NUMBER)
292 miss_type = MISSING_3;
293 missing[2].f = set[2].d[0];
303 for (miss_type = 0; token == T_STRING && miss_type < 3; miss_type++)
305 if (ds_length (&tokstr) != width)
307 msg (SE, _("String is not of proper length."));
310 strncpy (missing[miss_type].s, ds_c_str (&tokstr), MAX_SHORT_STRING);
316 msg (SE, _("String expected."));
323 /* Copy the missing values from variable SRC to variable DEST. */
325 copy_missing_values (struct variable *dest, const struct variable *src)
327 static const int n_values[MISSING_COUNT] =
329 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
332 assert (dest->width == src->width);
333 assert (src->miss_type >= 0 && src->miss_type < MISSING_COUNT);
338 dest->miss_type = src->miss_type;
339 for (i = 0; i < n_values[src->miss_type]; i++)
340 if (src->type == NUMERIC)
341 dest->missing[i].f = src->missing[i].f;
343 memcpy (dest->missing[i].s, src->missing[i].s, src->width);