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)
61 if (!parse_varnames ())
66 if ((type == NUMERIC && !parse_numeric ())
67 || (type == ALPHA && !parse_alpha ()))
71 miss_type = MISSING_NONE;
75 msg (SE, _("`)' expected after value specification."));
79 for (i = 0; i < nv; i++)
81 v[i]->miss_type = miss_type;
82 memcpy (v[i]->missing, missing, sizeof v[i]->missing);
93 return lex_end_of_command ();
97 return CMD_PART_SUCCESS_MAYBE;
101 parse_varnames (void)
105 if (!parse_variables (default_dict, &v, &nv, PV_SAME_TYPE))
107 if (!lex_match ('('))
109 msg (SE, _("`(' expected after variable name%s."), nv > 1 ? "s" : "");
118 for (i = 1; i < nv; i++)
119 if (v[i]->type == ALPHA && v[i]->nv != 1)
121 msg (SE, _("Long string value specified."));
124 else if (v[i]->type == ALPHA && (int) width != v[i]->width)
126 msg (SE, _("Short strings must be of equal width."));
133 /* Number or range? */
136 MV_NOR_NOTHING, /* Empty. */
137 MV_NOR_NUMBER, /* Single number. */
138 MV_NOR_RANGE /* Range. */
141 /* A single value or a range. */
144 int type; /* One of NOR_*. */
145 double d[2]; /* d[0]=lower bound or value, d[1]=upper bound. */
148 /* Parses something of the form <num>, or LO[WEST] THRU <num>, or
149 <num> THRU HI[GHEST], or <num> THRU <num>, and sets the appropriate
150 members of NOR. Returns success. */
152 parse_num_or_range (struct num_or_range * nor)
154 if (lex_match_id ("LO") || lex_match_id ("LOWEST"))
156 nor->type = MV_NOR_RANGE;
157 if (!lex_force_match_id ("THRU"))
159 if (!lex_force_num ())
164 else if (token == T_NUM)
169 if (lex_match_id ("THRU"))
171 nor->type = MV_NOR_RANGE;
172 if (lex_match_id ("HI") || lex_match_id ("HIGHEST"))
176 if (!lex_force_num ())
181 if (nor->d[0] > nor->d[1])
183 msg (SE, _("Range %g THRU %g is not valid because %g is "
185 nor->d[0], nor->d[1], nor->d[0], nor->d[1]);
191 nor->type = MV_NOR_NUMBER;
199 /* Parses a set of numeric missing values and stores them into
200 `missing[]' and `miss_type' global variables. */
204 struct num_or_range set[3];
207 set[1].type = set[2].type = MV_NOR_NOTHING;
209 /* Get first number or range. */
210 r = parse_num_or_range (&set[0]);
214 msg (SE, _("Number or range expected."));
218 /* Get second and third optional number or range. */
220 r = parse_num_or_range (&set[1]);
224 r = parse_num_or_range (&set[2]);
229 /* Force range, if present, into set[0]. */
230 if (set[1].type == MV_NOR_RANGE)
232 struct num_or_range t = set[1];
236 if (set[2].type == MV_NOR_RANGE)
238 struct num_or_range t = set[2];
243 /* Ensure there's not more than one range, or one range
245 if (set[1].type == MV_NOR_RANGE || set[2].type == MV_NOR_RANGE)
247 msg (SE, _("At most one range can exist in the missing values "
248 "for any one variable."));
251 if (set[0].type == MV_NOR_RANGE && set[2].type != MV_NOR_NOTHING)
253 msg (SE, _("At most one individual value can be missing along "
258 /* Set missing[] from set[]. */
259 if (set[0].type == MV_NOR_RANGE)
263 if (set[0].d[0] == LOWEST)
265 miss_type = MISSING_LOW;
266 missing[x++].f = set[0].d[1];
268 else if (set[0].d[1] == HIGHEST)
270 miss_type = MISSING_HIGH;
271 missing[x++].f = set[0].d[0];
275 miss_type = MISSING_RANGE;
276 missing[x++].f = set[0].d[0];
277 missing[x++].f = set[0].d[1];
280 if (set[1].type == MV_NOR_NUMBER)
283 missing[x].f = set[1].d[0];
288 if (set[0].type == MV_NOR_NUMBER)
290 miss_type = MISSING_1;
291 missing[0].f = set[0].d[0];
293 if (set[1].type == MV_NOR_NUMBER)
295 miss_type = MISSING_2;
296 missing[1].f = set[1].d[0];
298 if (set[2].type == MV_NOR_NUMBER)
300 miss_type = MISSING_3;
301 missing[2].f = set[2].d[0];
311 for (miss_type = 0; token == T_STRING && miss_type < 3; miss_type++)
313 if (ds_length (&tokstr) != width)
315 msg (SE, _("String is not of proper length."));
318 strncpy (missing[miss_type].s, ds_value (&tokstr), MAX_SHORT_STRING);
324 msg (SE, _("String expected."));
331 /* Copy the missing values from variable SRC to variable DEST. */
333 copy_missing_values (struct variable *dest, const struct variable *src)
335 static const int n_values[MISSING_COUNT] =
337 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
340 assert (dest->width == src->width);
341 assert (src->miss_type >= 0 && src->miss_type < MISSING_COUNT);
346 dest->miss_type = src->miss_type;
347 for (i = 0; i < n_values[src->miss_type]; i++)
348 if (src->type == NUMERIC)
349 dest->missing[i].f = src->missing[i].f;
351 memcpy (dest->missing[i].s, src->missing[i].s, src->width);
364 puts (_("Missing value:"));
365 for (i = 0; i < nvar; i++)
367 printf (" %8s: ", var[i]->name);
368 if (var[i]->type == ALPHA && var[i]->nv > 1)
369 puts (_("(long string variable)"));
371 switch (var[i]->miss_type)
374 printf (_("(no missing values)\n"));
379 printf ("(MISSING_%d)", var[i]->miss_type);
380 for (j = 0; j < var[i]->miss_type; j++)
381 if (var[i]->type == ALPHA)
382 printf (" \"%.*s\"", var[i]->width, var[i]->missing[j].s);
384 printf (" %.2g", var[i]->missing[j].f);
388 printf ("(MISSING_RANGE) %.2g THRU %.2g\n",
389 var[i]->missing[0].f, var[i]->missing[1].f);
391 case MISSING_RANGE_1:
392 printf ("(MISSING_RANGE_1) %.2g THRU %.2g, %.2g\n",
393 var[i]->missing[0].f, var[i]->missing[1].f,
394 var[i]->missing[2].f);
397 printf (_("(!!!INTERNAL ERROR--%d!!!)\n"), var[i]->miss_type);
401 #endif /* DEBUGGING */