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
25 #include "bitvector.h"
32 /* Parses a name as a variable within VS and returns the variable
33 if successful. On failure emits an error message and returns
35 static struct variable *
36 parse_vs_variable (const struct var_set *vs)
42 lex_error ("expecting variable name");
46 vp = var_set_lookup_var (vs, tokid);
48 msg (SE, _("%s is not a variable name."), tokid);
54 /* Parses a variable name in dictionary D and returns the
55 variable if successful. On failure emits an error message and
56 returns a null pointer. */
58 parse_dict_variable (const struct dictionary *d)
60 struct var_set *vs = var_set_create_from_dict (d);
61 struct variable *var = parse_vs_variable (vs);
66 /* Parses a variable name in default_dict and returns the
67 variable if successful. On failure emits an error message and
68 returns a null pointer. */
72 return parse_dict_variable (default_dict);
75 /* Returns the dictionary class corresponding to a variable named
78 dict_class_from_id (const char *name)
80 assert (name != NULL);
93 /* Returns the name of dictionary class DICT_CLASS. */
95 dict_class_to_name (enum dict_class dict_class)
100 return _("ordinary");
111 /* Parses a set of variables from dictionary D given options
112 OPTS. Resulting list of variables stored in *VAR and the
113 number of variables into *CNT. Returns nonzero only if
116 parse_variables (const struct dictionary *d, struct variable ***var,
123 assert (var != NULL);
124 assert (cnt != NULL);
126 vs = var_set_create_from_dict (d);
127 success = parse_var_set_vars (vs, var, cnt, opts);
128 var_set_destroy (vs);
132 /* Note that if parse_variables() returns 0, *v is free()'d.
133 Conversely, if parse_variables() returns non-zero, then *nv is
134 nonzero and *v is non-NULL. */
136 parse_var_set_vars (const struct var_set *vs,
137 struct variable ***v, int *nv,
142 char *included = NULL;
144 struct variable *v1, *v2;
146 enum dict_class dict_class;
152 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
154 assert ((((pv_opts & PV_NUMERIC) != 0)
155 + ((pv_opts & PV_STRING) != 0)
156 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
158 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
159 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
161 vs_var_cnt = var_set_get_cnt (vs);
163 if (!(pv_opts & PV_APPEND))
172 if (!(pv_opts & PV_DUPLICATE))
174 included = xmalloc (vs_var_cnt);
175 memset (included, 0, vs_var_cnt);
176 for (i = 0; i < *nv; i++)
177 included[(*v)[i]->index] = 1;
182 if (lex_match (T_ALL))
184 v1 = var_set_get_var (vs, 0);
185 v2 = var_set_get_var (vs, vs_var_cnt - 1);
187 dict_class = DC_ORDINARY;
191 v1 = parse_vs_variable (vs);
195 if (lex_match (T_TO))
197 enum dict_class dict_class_2;
199 v2 = parse_vs_variable (vs);
202 lex_error ("expecting variable name");
206 count = v2->index - v1->index + 1;
209 msg (SE, _("%s TO %s is not valid syntax since %s "
210 "precedes %s in the dictionary."),
211 v1->name, v2->name, v2->name, v1->name);
215 dict_class = dict_class_from_id (v1->name);
216 dict_class_2 = dict_class_from_id (v2->name);
217 if (dict_class != dict_class_2)
219 msg (SE, _("When using the TO keyword to specify several "
220 "variables, both variables must be from "
221 "the same variable dictionaries, of either "
222 "ordinary, scratch, or system variables. "
223 "%s is a %s variable, whereas %s is %s."),
224 v1->name, dict_class_to_name (dict_class),
225 v2->name, dict_class_to_name (dict_class_2));
233 dict_class = dict_class_from_id (v1->name);
235 if (dict_class == DC_SCRATCH && (pv_opts & PV_NO_SCRATCH))
237 msg (SE, _("Scratch variables (such as %s) are not allowed "
243 if (*nv + count > mv)
245 mv += ROUND_UP (count, 16);
246 *v = xrealloc (*v, mv * sizeof **v);
249 /* Add v1...v2 to the list. */
250 for (i = v1->index; i <= v2->index; i++)
252 struct variable *add = var_set_get_var (vs, i);
254 /* Skip over other dictionaries. */
255 if (dict_class != dict_class_from_id (add->name))
258 /* Different kinds of errors. */
259 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
260 msg (SW, _("%s is not a numeric variable. It will not be "
261 "included in the variable list."), add->name);
262 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
263 msg (SE, _("%s is not a string variable. It will not be "
264 "included in the variable list."), add->name);
265 else if ((pv_opts & PV_SAME_TYPE) && *nv
266 && add->type != (*v)[0]->type)
267 msg (SE, _("%s and %s are not the same type. All variables in "
268 "this variable list must be of the same type. %s "
269 "will be omitted from list."),
270 (*v)[0]->name, add->name, add->name);
271 else if ((pv_opts & PV_NO_DUPLICATE) && included[add->index])
272 msg (SE, _("Variable %s appears twice in variable list."),
275 /* Success--add the variable to the list. */
276 if ((pv_opts & PV_DUPLICATE) || !included[add->index])
278 (*v)[(*nv)++] = var_set_get_var (vs, i);
279 if (!(pv_opts & PV_DUPLICATE))
280 included[add->index] = 1;
287 /* Arrive here only on failure. */
288 if (pv_opts & PV_SINGLE)
292 /* We finished adding v1...v2 to the list. */
293 if (pv_opts & PV_SINGLE)
297 while ((token == T_ID && var_set_lookup_var (vs, tokid) != NULL)
300 if (!(pv_opts & PV_DUPLICATE))
310 if (!(pv_opts & PV_DUPLICATE))
315 /* Extracts a numeric suffix from variable name S, copying it
316 into string R. Sets *D to the length of R and *N to its
319 extract_num (char *s, char *r, int *n, int *d)
323 /* Find first digit. */
324 cp = s + strlen (s) - 1;
325 while (isdigit ((unsigned char) *cp) && cp > s)
330 strncpy (r, s, cp - s);
333 /* Count initial zeros. */
342 while (isdigit ((unsigned char) *cp))
345 *n = (*n * 10) + (*cp - '0');
350 if (*n == 0 && *d == 0)
352 msg (SE, _("incorrect use of TO convention"));
358 /* Parses a list of variable names according to the DATA LIST version
359 of the TO convention. */
361 parse_DATA_LIST_vars (char ***names, int *nnames, int pv_opts)
371 assert (names != NULL);
372 assert (nnames != NULL);
373 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
374 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
375 /* FIXME: PV_NO_DUPLICATE is not implemented. */
377 if (pv_opts & PV_APPEND)
378 nvar = mvar = *nnames;
385 name1 = xmalloc (36);
386 name2 = &name1[1 * 9];
387 root1 = &name1[2 * 9];
388 root2 = &name1[3 * 9];
393 lex_error ("expecting variable name");
396 if (dict_class_from_id (tokid) == DC_SCRATCH
397 && (pv_opts & PV_NO_SCRATCH))
399 msg (SE, _("Scratch variables not allowed here."));
402 strcpy (name1, tokid);
409 lex_error ("expecting variable name");
412 strcpy (name2, tokid);
415 if (!extract_num (name1, root1, &n1, &d1)
416 || !extract_num (name2, root2, &n2, &d2))
419 if (strcmp (root1, root2))
421 msg (SE, _("Prefixes don't match in use of TO convention."));
426 msg (SE, _("Bad bounds in use of TO convention."));
432 if (mvar < nvar + (n2 - n1 + 1))
434 mvar += ROUND_UP (n2 - n1 + 1, 16);
435 *names = xrealloc (*names, mvar * sizeof **names);
438 for (n = n1; n <= n2; n++)
440 (*names)[nvar] = xmalloc (9);
441 sprintf ((*names)[nvar], "%s%0*d", root1, d1, n);
450 *names = xrealloc (*names, mvar * sizeof **names);
452 (*names)[nvar++] = xstrdup (name1);
457 if (pv_opts & PV_SINGLE)
460 while (token == T_ID);
469 for (i = 0; i < nvar; i++)
478 /* Parses a list of variables where some of the variables may be
479 existing and the rest are to be created. Same args as
480 parse_DATA_LIST_vars(). */
482 parse_mixed_vars (char ***names, int *nnames, int pv_opts)
486 assert (names != NULL);
487 assert (nnames != NULL);
488 assert ((pv_opts & ~PV_APPEND) == 0);
490 if (!(pv_opts & PV_APPEND))
495 while (token == T_ID || token == T_ALL)
497 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
502 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
504 *names = xrealloc (*names, (*nnames + nv) * sizeof **names);
505 for (i = 0; i < nv; i++)
506 (*names)[*nnames + i] = xstrdup (v[i]->name);
510 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
516 for (i = 0; i < *nnames; i++)
517 free ((*names)[*nnames]);
524 /* A set of variables. */
527 size_t (*get_cnt) (const struct var_set *);
528 struct variable *(*get_var) (const struct var_set *, size_t idx);
529 struct variable *(*lookup_var) (const struct var_set *, const char *);
530 void (*destroy) (struct var_set *);
534 /* Returns the number of variables in VS. */
536 var_set_get_cnt (const struct var_set *vs)
540 return vs->get_cnt (vs);
543 /* Return variable with index IDX in VS.
544 IDX must be less than the number of variables in VS. */
546 var_set_get_var (const struct var_set *vs, size_t idx)
549 assert (idx < var_set_get_cnt (vs));
551 return vs->get_var (vs, idx);
554 /* Returns the variable in VS named NAME, or a null pointer if VS
555 contains no variable with that name. */
557 var_set_lookup_var (const struct var_set *vs, const char *name)
560 assert (name != NULL);
561 assert (strlen (name) < 9);
563 return vs->lookup_var (vs, name);
568 var_set_destroy (struct var_set *vs)
574 /* Returns the number of variables in VS. */
576 dict_var_set_get_cnt (const struct var_set *vs)
578 struct dictionary *d = vs->aux;
580 return dict_get_var_cnt (d);
583 /* Return variable with index IDX in VS.
584 IDX must be less than the number of variables in VS. */
585 static struct variable *
586 dict_var_set_get_var (const struct var_set *vs, size_t idx)
588 struct dictionary *d = vs->aux;
590 return dict_get_var (d, idx);
593 /* Returns the variable in VS named NAME, or a null pointer if VS
594 contains no variable with that name. */
595 static struct variable *
596 dict_var_set_lookup_var (const struct var_set *vs, const char *name)
598 struct dictionary *d = vs->aux;
600 return dict_lookup_var (d, name);
605 dict_var_set_destroy (struct var_set *vs)
610 /* Returns a variable set based on D. */
612 var_set_create_from_dict (const struct dictionary *d)
614 struct var_set *vs = xmalloc (sizeof *vs);
615 vs->get_cnt = dict_var_set_get_cnt;
616 vs->get_var = dict_var_set_get_var;
617 vs->lookup_var = dict_var_set_lookup_var;
618 vs->destroy = dict_var_set_destroy;
619 vs->aux = (void *) d;
623 /* A variable set based on an array. */
626 struct variable *const *var;/* Array of variables. */
627 size_t var_cnt; /* Number of elements in var. */
628 struct hsh_table *name_tab; /* Hash from variable names to variables. */
631 /* Returns the number of variables in VS. */
633 array_var_set_get_cnt (const struct var_set *vs)
635 struct array_var_set *avs = vs->aux;
640 /* Return variable with index IDX in VS.
641 IDX must be less than the number of variables in VS. */
642 static struct variable *
643 array_var_set_get_var (const struct var_set *vs, size_t idx)
645 struct array_var_set *avs = vs->aux;
647 return (struct variable *) avs->var[idx];
650 /* Returns the variable in VS named NAME, or a null pointer if VS
651 contains no variable with that name. */
652 static struct variable *
653 array_var_set_lookup_var (const struct var_set *vs, const char *name)
655 struct array_var_set *avs = vs->aux;
658 strcpy (v.name, name);
660 return hsh_find (avs->name_tab, &v);
665 array_var_set_destroy (struct var_set *vs)
667 struct array_var_set *avs = vs->aux;
669 hsh_destroy (avs->name_tab);
674 /* Returns a variable set based on the VAR_CNT variables in
677 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
680 struct array_var_set *avs;
683 vs = xmalloc (sizeof *vs);
684 vs->get_cnt = array_var_set_get_cnt;
685 vs->get_var = array_var_set_get_var;
686 vs->lookup_var = array_var_set_lookup_var;
687 vs->destroy = array_var_set_destroy;
688 vs->aux = avs = xmalloc (sizeof *avs);
690 avs->var_cnt = var_cnt;
691 avs->name_tab = hsh_create (2 * var_cnt,
692 compare_variables, hash_variable, NULL,
694 for (i = 0; i < var_cnt; i++)
695 if (hsh_insert (avs->name_tab, (void *) var[i]) != NULL)
697 var_set_destroy (vs);