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"
33 static void debug_print ();
36 /* Variables on MIS VAL. */
37 static struct variable **v;
40 /* Type of the variables on MIS VAL. */
43 /* Width of string variables on MIS VAL. */
46 /* Items to fill-in var structs with. */
48 static union value missing[3];
50 static int parse_varnames (void);
51 static int parse_numeric (void);
52 static int parse_alpha (void);
55 cmd_missing_values (void)
59 lex_match_id ("MISSING");
60 lex_match_id ("VALUES");
63 if (!parse_varnames ())
68 if ((type == NUMERIC && !parse_numeric ())
69 || (type == ALPHA && !parse_alpha ()))
73 miss_type = MISSING_NONE;
77 msg (SE, _("`)' expected after value specification."));
81 for (i = 0; i < nv; i++)
83 v[i]->miss_type = miss_type;
84 memcpy (v[i]->missing, missing, sizeof v[i]->missing);
95 return lex_end_of_command ();
99 return CMD_PART_SUCCESS_MAYBE;
103 parse_varnames (void)
107 if (!parse_variables (default_dict, &v, &nv, PV_SAME_TYPE))
109 if (!lex_match ('('))
111 msg (SE, _("`(' expected after variable name%s."), nv > 1 ? "s" : "");
120 for (i = 1; i < nv; i++)
121 if (v[i]->type == ALPHA && v[i]->nv != 1)
123 msg (SE, _("Long string value specified."));
126 else if (v[i]->type == ALPHA && (int) width != v[i]->width)
128 msg (SE, _("Short strings must be of equal width."));
135 /* Number or range? */
138 MV_NOR_NOTHING, /* Empty. */
139 MV_NOR_NUMBER, /* Single number. */
140 MV_NOR_RANGE /* Range. */
143 /* A single value or a range. */
146 int type; /* One of NOR_*. */
147 double d[2]; /* d[0]=lower bound or value, d[1]=upper bound. */
150 /* Parses something of the form <num>, or LO[WEST] THRU <num>, or
151 <num> THRU HI[GHEST], or <num> THRU <num>, and sets the appropriate
152 members of NOR. Returns success. */
154 parse_num_or_range (struct num_or_range * nor)
156 if (lex_match_id ("LO") || lex_match_id ("LOWEST"))
158 nor->type = MV_NOR_RANGE;
159 if (!lex_force_match_id ("THRU"))
161 if (!lex_force_num ())
166 else if (token == T_NUM)
171 if (lex_match_id ("THRU"))
173 nor->type = MV_NOR_RANGE;
174 if (lex_match_id ("HI") || lex_match_id ("HIGHEST"))
178 if (!lex_force_num ())
183 if (nor->d[0] > nor->d[1])
185 msg (SE, _("Range %g THRU %g is not valid because %g is "
187 nor->d[0], nor->d[1], nor->d[0], nor->d[1]);
193 nor->type = MV_NOR_NUMBER;
201 /* Parses a set of numeric missing values and stores them into
202 `missing[]' and `miss_type' global variables. */
206 struct num_or_range set[3];
209 set[1].type = set[2].type = MV_NOR_NOTHING;
211 /* Get first number or range. */
212 r = parse_num_or_range (&set[0]);
216 msg (SE, _("Number or range expected."));
220 /* Get second and third optional number or range. */
222 r = parse_num_or_range (&set[1]);
226 r = parse_num_or_range (&set[2]);
231 /* Force range, if present, into set[0]. */
232 if (set[1].type == MV_NOR_RANGE)
234 struct num_or_range t = set[1];
238 if (set[2].type == MV_NOR_RANGE)
240 struct num_or_range t = set[2];
245 /* Ensure there's not more than one range, or one range
247 if (set[1].type == MV_NOR_RANGE || set[2].type == MV_NOR_RANGE)
249 msg (SE, _("At most one range can exist in the missing values "
250 "for any one variable."));
253 if (set[0].type == MV_NOR_RANGE && set[2].type != MV_NOR_NOTHING)
255 msg (SE, _("At most one individual value can be missing along "
260 /* Set missing[] from set[]. */
261 if (set[0].type == MV_NOR_RANGE)
265 if (set[0].d[0] == LOWEST)
267 miss_type = MISSING_LOW;
268 missing[x++].f = set[0].d[1];
270 else if (set[0].d[1] == HIGHEST)
272 miss_type = MISSING_HIGH;
273 missing[x++].f = set[0].d[0];
277 miss_type = MISSING_RANGE;
278 missing[x++].f = set[0].d[0];
279 missing[x++].f = set[0].d[1];
282 if (set[1].type == MV_NOR_NUMBER)
285 missing[x].f = set[1].d[0];
290 if (set[0].type == MV_NOR_NUMBER)
292 miss_type = MISSING_1;
293 missing[0].f = set[0].d[0];
295 if (set[1].type == MV_NOR_NUMBER)
297 miss_type = MISSING_2;
298 missing[1].f = set[1].d[0];
300 if (set[2].type == MV_NOR_NUMBER)
302 miss_type = MISSING_3;
303 missing[2].f = set[2].d[0];
313 for (miss_type = 0; token == T_STRING && miss_type < 3; miss_type++)
315 if (ds_length (&tokstr) != width)
317 msg (SE, _("String is not of proper length."));
320 strncpy (missing[miss_type].s, ds_value (&tokstr), MAX_SHORT_STRING);
326 msg (SE, _("String expected."));
333 /* Copy the missing values from variable SRC to variable DEST. */
335 copy_missing_values (struct variable *dest, const struct variable *src)
337 static const int n_values[MISSING_COUNT] =
339 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
342 assert (dest->width == src->width);
343 assert (src->miss_type >= 0 && src->miss_type < MISSING_COUNT);
348 dest->miss_type = src->miss_type;
349 for (i = 0; i < n_values[src->miss_type]; i++)
350 if (src->type == NUMERIC)
351 dest->missing[i].f = src->missing[i].f;
353 memcpy (dest->missing[i].s, src->missing[i].s, src->width);
366 puts (_("Missing value:"));
367 for (i = 0; i < nvar; i++)
369 printf (" %8s: ", var[i]->name);
370 if (var[i]->type == ALPHA && var[i]->nv > 1)
371 puts (_("(long string variable)"));
373 switch (var[i]->miss_type)
376 printf (_("(no missing values)\n"));
381 printf ("(MISSING_%d)", var[i]->miss_type);
382 for (j = 0; j < var[i]->miss_type; j++)
383 if (var[i]->type == ALPHA)
384 printf (" \"%.*s\"", var[i]->width, var[i]->missing[j].s);
386 printf (" %.2g", var[i]->missing[j].f);
390 printf ("(MISSING_RANGE) %.2g THRU %.2g\n",
391 var[i]->missing[0].f, var[i]->missing[1].f);
393 case MISSING_RANGE_1:
394 printf ("(MISSING_RANGE_1) %.2g THRU %.2g, %.2g\n",
395 var[i]->missing[0].f, var[i]->missing[1].f,
396 var[i]->missing[2].f);
399 printf (_("(!!!INTERNAL ERROR--%d!!!)\n"), var[i]->miss_type);
403 #endif /* DEBUGGING */