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
31 /*#define DEBUGGING 1*/
32 #include "debug-print.h"
35 static void debug_print ();
38 /* Variables on MIS VAL. */
39 static struct variable **v;
42 /* Type of the variables on MIS VAL. */
45 /* Width of string variables on MIS VAL. */
48 /* Items to fill-in var structs with. */
50 static union value missing[3];
52 static int parse_varnames (void);
53 static int parse_numeric (void);
54 static int parse_alpha (void);
57 cmd_missing_values (void)
61 lex_match_id ("MISSING");
62 lex_match_id ("VALUES");
65 if (!parse_varnames ())
70 if ((type == NUMERIC && !parse_numeric ())
71 || (type == ALPHA && !parse_alpha ()))
75 miss_type = MISSING_NONE;
79 msg (SE, _("`)' expected after value specification."));
83 for (i = 0; i < nv; i++)
85 v[i]->miss_type = miss_type;
86 memcpy (v[i]->missing, missing, sizeof v[i]->missing);
97 return lex_end_of_command ();
101 return CMD_PART_SUCCESS_MAYBE;
105 parse_varnames (void)
109 if (!parse_variables (NULL, &v, &nv, PV_SAME_TYPE))
111 if (!lex_match ('('))
113 msg (SE, _("`(' expected after variable name%s."), nv > 1 ? "s" : "");
122 for (i = 1; i < nv; i++)
123 if (v[i]->type == ALPHA && v[i]->nv != 1)
125 msg (SE, _("Long string value specified."));
128 else if (v[i]->type == ALPHA && (int) width != v[i]->width)
130 msg (SE, _("Short strings must be of equal width."));
137 /* Number or range? */
140 MV_NOR_NOTHING, /* Empty. */
141 MV_NOR_NUMBER, /* Single number. */
142 MV_NOR_RANGE /* Range. */
145 /* A single value or a range. */
148 int type; /* One of NOR_*. */
149 double d[2]; /* d[0]=lower bound or value, d[1]=upper bound. */
152 /* Parses something of the form <num>, or LO[WEST] THRU <num>, or
153 <num> THRU HI[GHEST], or <num> THRU <num>, and sets the appropriate
154 members of NOR. Returns success. */
156 parse_num_or_range (struct num_or_range * nor)
158 if (lex_match_id ("LO") || lex_match_id ("LOWEST"))
160 nor->type = MV_NOR_RANGE;
161 if (!lex_force_match_id ("THRU"))
163 if (!lex_force_num ())
168 else if (token == T_NUM)
173 if (lex_match_id ("THRU"))
175 nor->type = MV_NOR_RANGE;
176 if (lex_match_id ("HI") || lex_match_id ("HIGHEST"))
180 if (!lex_force_num ())
185 if (nor->d[0] > nor->d[1])
187 msg (SE, _("Range %g THRU %g is not valid because %g is "
189 nor->d[0], nor->d[1], nor->d[0], nor->d[1]);
195 nor->type = MV_NOR_NUMBER;
203 /* Parses a set of numeric missing values and stores them into
204 `missing[]' and `miss_type' global variables. */
208 struct num_or_range set[3];
211 set[1].type = set[2].type = MV_NOR_NOTHING;
213 /* Get first number or range. */
214 r = parse_num_or_range (&set[0]);
218 msg (SE, _("Number or range expected."));
222 /* Get second and third optional number or range. */
224 r = parse_num_or_range (&set[1]);
228 r = parse_num_or_range (&set[2]);
233 /* Force range, if present, into set[0]. */
234 if (set[1].type == MV_NOR_RANGE)
236 struct num_or_range t = set[1];
240 if (set[2].type == MV_NOR_RANGE)
242 struct num_or_range t = set[2];
247 /* Ensure there's not more than one range, or one range
249 if (set[1].type == MV_NOR_RANGE || set[2].type == MV_NOR_RANGE)
251 msg (SE, _("At most one range can exist in the missing values "
252 "for any one variable."));
255 if (set[0].type == MV_NOR_RANGE && set[2].type != MV_NOR_NOTHING)
257 msg (SE, _("At most one individual value can be missing along "
262 /* Set missing[] from set[]. */
263 if (set[0].type == MV_NOR_RANGE)
267 if (set[0].d[0] == LOWEST)
269 miss_type = MISSING_LOW;
270 missing[x++].f = set[0].d[1];
272 else if (set[0].d[1] == HIGHEST)
274 miss_type = MISSING_HIGH;
275 missing[x++].f = set[0].d[0];
279 miss_type = MISSING_RANGE;
280 missing[x++].f = set[0].d[0];
281 missing[x++].f = set[0].d[1];
284 if (set[1].type == MV_NOR_NUMBER)
287 missing[x].f = set[1].d[0];
292 if (set[0].type == MV_NOR_NUMBER)
294 miss_type = MISSING_1;
295 missing[0].f = set[0].d[0];
297 if (set[1].type == MV_NOR_NUMBER)
299 miss_type = MISSING_2;
300 missing[1].f = set[1].d[0];
302 if (set[2].type == MV_NOR_NUMBER)
304 miss_type = MISSING_3;
305 missing[2].f = set[2].d[0];
315 for (miss_type = 0; token == T_STRING && miss_type < 3; miss_type++)
317 if (ds_length (&tokstr) != width)
319 msg (SE, _("String is not of proper length."));
322 strncpy (missing[miss_type].s, ds_value (&tokstr), MAX_SHORT_STRING);
328 msg (SE, _("String expected."));
335 /* Copy the missing values from variable SRC to variable DEST. */
337 copy_missing_values (struct variable *dest, const struct variable *src)
339 static const int n_values[MISSING_COUNT] =
341 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
344 assert (dest->width == src->width);
345 assert (src->miss_type >= 0 && src->miss_type < MISSING_COUNT);
350 dest->miss_type = src->miss_type;
351 for (i = 0; i < n_values[src->miss_type]; i++)
352 if (src->type == NUMERIC)
353 dest->missing[i].f = src->missing[i].f;
355 memcpy (dest->missing[i].s, src->missing[i].s, src->width);
368 puts (_("Missing value:"));
369 for (i = 0; i < nvar; i++)
371 printf (" %8s: ", var[i]->name);
372 if (var[i]->type == ALPHA && var[i]->nv > 1)
373 puts (_("(long string variable)"));
375 switch (var[i]->miss_type)
378 printf (_("(no missing values)\n"));
383 printf ("(MISSING_%d)", var[i]->miss_type);
384 for (j = 0; j < var[i]->miss_type; j++)
385 if (var[i]->type == ALPHA)
386 printf (" \"%.*s\"", var[i]->width, var[i]->missing[j].s);
388 printf (" %.2g", var[i]->missing[j].f);
392 printf ("(MISSING_RANGE) %.2g THRU %.2g\n",
393 var[i]->missing[0].f, var[i]->missing[1].f);
395 case MISSING_RANGE_1:
396 printf ("(MISSING_RANGE_1) %.2g THRU %.2g, %.2g\n",
397 var[i]->missing[0].f, var[i]->missing[1].f,
398 var[i]->missing[2].f);
401 printf (_("(!!!INTERNAL ERROR--%d!!!)\n"), var[i]->miss_type);
405 #endif /* DEBUGGING */