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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "bitvector.h"
26 #include "dictionary.h"
33 /* Parses a name as a variable within VS and returns the
34 variable's index if successful. On failure emits an error
35 message and returns a null pointer. */
37 parse_vs_variable_idx (const struct var_set *vs)
43 lex_error (_("expecting variable name"));
47 idx = var_set_lookup_var_idx (vs, tokid);
49 msg (SE, _("%s is not a variable name."), tokid);
55 /* Parses a name as a variable within VS and returns the variable
56 if successful. On failure emits an error message and returns
58 static struct variable *
59 parse_vs_variable (const struct var_set *vs)
61 int idx = parse_vs_variable_idx (vs);
62 return idx >= 0 ? var_set_get_var (vs, idx) : NULL;
65 /* Parses a variable name in dictionary D and returns the
66 variable if successful. On failure emits an error message and
67 returns a null pointer. */
69 parse_dict_variable (const struct dictionary *d)
71 struct var_set *vs = var_set_create_from_dict (d);
72 struct variable *var = parse_vs_variable (vs);
77 /* Parses a variable name in default_dict and returns the
78 variable if successful. On failure emits an error message and
79 returns a null pointer. */
83 return parse_dict_variable (default_dict);
86 /* Returns the dictionary class corresponding to a variable named
89 dict_class_from_id (const char *name)
91 assert (name != NULL);
104 /* Returns the name of dictionary class DICT_CLASS. */
106 dict_class_to_name (enum dict_class dict_class)
111 return _("ordinary");
122 /* Parses a set of variables from dictionary D given options
123 OPTS. Resulting list of variables stored in *VAR and the
124 number of variables into *CNT. Returns nonzero only if
127 parse_variables (const struct dictionary *d, struct variable ***var,
134 assert (var != NULL);
135 assert (cnt != NULL);
137 vs = var_set_create_from_dict (d);
138 success = parse_var_set_vars (vs, var, cnt, opts);
141 var_set_destroy (vs);
145 /* Parses a variable name from VS. If successful, sets *IDX to
146 the variable's index in VS, *CLASS to the variable's
147 dictionary class, and returns nonzero. Returns zero on
150 parse_var_idx_class (const struct var_set *vs, int *idx,
151 enum dict_class *class)
153 *idx = parse_vs_variable_idx (vs);
157 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
161 /* Add the variable from VS with index IDX to the list of
162 variables V that has *NV elements and room for *MV.
163 Uses and updates INCLUDED to avoid duplicates if indicated by
164 PV_OPTS, which also affects what variables are allowed in
167 add_variable (struct variable ***v, int *nv, int *mv,
168 char *included, int pv_opts,
169 const struct var_set *vs, int idx)
171 struct variable *add = var_set_get_var (vs, idx);
173 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
174 msg (SW, _("%s is not a numeric variable. It will not be "
175 "included in the variable list."), add->name);
176 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
177 msg (SE, _("%s is not a string variable. It will not be "
178 "included in the variable list."), add->name);
179 else if ((pv_opts & PV_NO_SCRATCH)
180 && dict_class_from_id (add->name) == DC_SCRATCH)
181 msg (SE, _("Scratch variables (such as %s) are not allowed "
182 "here."), add->name);
183 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
184 msg (SE, _("%s and %s are not the same type. All variables in "
185 "this variable list must be of the same type. %s "
186 "will be omitted from list."),
187 (*v)[0]->name, add->name, add->name);
188 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
189 msg (SE, _("Variable %s appears twice in variable list."), add->name);
195 *v = xrealloc (*v, *mv * sizeof **v);
198 if ((pv_opts & PV_DUPLICATE) || !included[idx])
201 if (!(pv_opts & PV_DUPLICATE))
207 /* Adds the variables in VS with indexes FIRST_IDX through
208 LAST_IDX, inclusive, to the list of variables V that has *NV
209 elements and room for *MV. Uses and updates INCLUDED to avoid
210 duplicates if indicated by PV_OPTS, which also affects what
211 variables are allowed in appropriate ways. */
213 add_variables (struct variable ***v, int *nv, int *mv, char *included,
215 const struct var_set *vs, int first_idx, int last_idx,
216 enum dict_class class)
220 for (i = first_idx; i <= last_idx; i++)
221 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
222 add_variable (v, nv, mv, included, pv_opts, vs, i);
225 /* Note that if parse_variables() returns 0, *v is free()'d.
226 Conversely, if parse_variables() returns non-zero, then *nv is
227 nonzero and *v is non-NULL. */
229 parse_var_set_vars (const struct var_set *vs,
230 struct variable ***v, int *nv,
240 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
242 assert ((((pv_opts & PV_NUMERIC) != 0)
243 + ((pv_opts & PV_STRING) != 0)
244 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
246 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
247 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
249 if (!(pv_opts & PV_APPEND))
258 if (!(pv_opts & PV_DUPLICATE))
262 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
263 for (i = 0; i < *nv; i++)
264 included[(*v)[i]->index] = 1;
269 if (lex_match (T_ALL))
270 add_variables (v, nv, &mv, included, pv_opts,
271 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
276 enum dict_class class;
279 if (!parse_var_idx_class (vs, &first_idx, &class))
282 if (!lex_match (T_TO))
283 add_variable (v, nv, &mv, included, pv_opts,
288 enum dict_class last_class;
289 struct variable *first_var, *last_var;
291 if (!parse_var_idx_class (vs, &last_idx, &last_class))
294 first_var = var_set_get_var (vs, first_idx);
295 last_var = var_set_get_var (vs, last_idx);
297 if (last_idx < first_idx)
299 msg (SE, _("%s TO %s is not valid syntax since %s "
300 "precedes %s in the dictionary."),
301 first_var->name, last_var->name,
302 first_var->name, last_var->name);
306 if (class != last_class)
308 msg (SE, _("When using the TO keyword to specify several "
309 "variables, both variables must be from "
310 "the same variable dictionaries, of either "
311 "ordinary, scratch, or system variables. "
312 "%s is a %s variable, whereas %s is %s."),
313 first_var->name, dict_class_to_name (class),
314 last_var->name, dict_class_to_name (last_class));
318 add_variables (v, nv, &mv, included, pv_opts,
319 vs, first_idx, last_idx, class);
321 if (pv_opts & PV_SINGLE)
325 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
342 /* Extracts a numeric suffix from variable name S, copying it
343 into string R. Sets *D to the length of R and *N to its
346 extract_num (char *s, char *r, int *n, int *d)
350 /* Find first digit. */
351 cp = s + strlen (s) - 1;
352 while (isdigit ((unsigned char) *cp) && cp > s)
357 strncpy (r, s, cp - s);
360 /* Count initial zeros. */
369 while (isdigit ((unsigned char) *cp))
372 *n = (*n * 10) + (*cp - '0');
377 if (*n == 0 && *d == 0)
379 msg (SE, _("incorrect use of TO convention"));
385 /* Parses a list of variable names according to the DATA LIST version
386 of the TO convention. */
388 parse_DATA_LIST_vars (char ***names, int *nnames, int pv_opts)
394 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
395 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
398 assert (names != NULL);
399 assert (nnames != NULL);
400 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
401 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
402 /* FIXME: PV_NO_DUPLICATE is not implemented. */
404 if (pv_opts & PV_APPEND)
405 nvar = mvar = *nnames;
416 lex_error ("expecting variable name");
419 if (dict_class_from_id (tokid) == DC_SCRATCH
420 && (pv_opts & PV_NO_SCRATCH))
422 msg (SE, _("Scratch variables not allowed here."));
425 strcpy (name1, tokid);
432 lex_error ("expecting variable name");
435 strcpy (name2, tokid);
438 if (!extract_num (name1, root1, &n1, &d1)
439 || !extract_num (name2, root2, &n2, &d2))
442 if (strcasecmp (root1, root2))
444 msg (SE, _("Prefixes don't match in use of TO convention."));
449 msg (SE, _("Bad bounds in use of TO convention."));
455 if (mvar < nvar + (n2 - n1 + 1))
457 mvar += ROUND_UP (n2 - n1 + 1, 16);
458 *names = xrealloc (*names, mvar * sizeof **names);
461 for (n = n1; n <= n2; n++)
463 char name[LONG_NAME_LEN + 1];
464 sprintf (name, "%s%0*d", root1, d1, n);
465 (*names)[nvar] = xstrdup (name);
474 *names = xrealloc (*names, mvar * sizeof **names);
476 (*names)[nvar++] = xstrdup (name1);
481 if (pv_opts & PV_SINGLE)
484 while (token == T_ID);
492 for (i = 0; i < nvar; i++)
501 /* Parses a list of variables where some of the variables may be
502 existing and the rest are to be created. Same args as
503 parse_DATA_LIST_vars(). */
505 parse_mixed_vars (char ***names, int *nnames, int pv_opts)
509 assert (names != NULL);
510 assert (nnames != NULL);
511 assert ((pv_opts & ~PV_APPEND) == 0);
513 if (!(pv_opts & PV_APPEND))
518 while (token == T_ID || token == T_ALL)
520 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
525 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
527 *names = xrealloc (*names, (*nnames + nv) * sizeof **names);
528 for (i = 0; i < nv; i++)
529 (*names)[*nnames + i] = xstrdup (v[i]->name);
533 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
539 for (i = 0; i < *nnames; i++)
540 free ((*names)[*nnames]);
547 /* A set of variables. */
550 size_t (*get_cnt) (const struct var_set *);
551 struct variable *(*get_var) (const struct var_set *, size_t idx);
552 int (*lookup_var_idx) (const struct var_set *, const char *);
553 void (*destroy) (struct var_set *);
557 /* Returns the number of variables in VS. */
559 var_set_get_cnt (const struct var_set *vs)
563 return vs->get_cnt (vs);
566 /* Return variable with index IDX in VS.
567 IDX must be less than the number of variables in VS. */
569 var_set_get_var (const struct var_set *vs, size_t idx)
572 assert (idx < var_set_get_cnt (vs));
574 return vs->get_var (vs, idx);
577 /* Returns the variable in VS named NAME, or a null pointer if VS
578 contains no variable with that name. */
580 var_set_lookup_var (const struct var_set *vs, const char *name)
582 int idx = var_set_lookup_var_idx (vs, name);
583 return idx >= 0 ? var_set_get_var (vs, idx) : NULL;
586 /* Returns the index in VS of the variable named NAME, or -1 if
587 VS contains no variable with that name. */
589 var_set_lookup_var_idx (const struct var_set *vs, const char *name)
592 assert (name != NULL);
593 assert (strlen (name) <= LONG_NAME_LEN );
595 return vs->lookup_var_idx (vs, name);
600 var_set_destroy (struct var_set *vs)
606 /* Returns the number of variables in VS. */
608 dict_var_set_get_cnt (const struct var_set *vs)
610 struct dictionary *d = vs->aux;
612 return dict_get_var_cnt (d);
615 /* Return variable with index IDX in VS.
616 IDX must be less than the number of variables in VS. */
617 static struct variable *
618 dict_var_set_get_var (const struct var_set *vs, size_t idx)
620 struct dictionary *d = vs->aux;
622 return dict_get_var (d, idx);
625 /* Returns the index of the variable in VS named NAME, or -1 if
626 VS contains no variable with that name. */
628 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name)
630 struct dictionary *d = vs->aux;
631 struct variable *v = dict_lookup_var (d, name);
632 return v != NULL ? v->index : -1;
637 dict_var_set_destroy (struct var_set *vs)
642 /* Returns a variable set based on D. */
644 var_set_create_from_dict (const struct dictionary *d)
646 struct var_set *vs = xmalloc (sizeof *vs);
647 vs->get_cnt = dict_var_set_get_cnt;
648 vs->get_var = dict_var_set_get_var;
649 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
650 vs->destroy = dict_var_set_destroy;
651 vs->aux = (void *) d;
655 /* A variable set based on an array. */
658 struct variable *const *var;/* Array of variables. */
659 size_t var_cnt; /* Number of elements in var. */
660 struct hsh_table *name_tab; /* Hash from variable names to variables. */
663 /* Returns the number of variables in VS. */
665 array_var_set_get_cnt (const struct var_set *vs)
667 struct array_var_set *avs = vs->aux;
672 /* Return variable with index IDX in VS.
673 IDX must be less than the number of variables in VS. */
674 static struct variable *
675 array_var_set_get_var (const struct var_set *vs, size_t idx)
677 struct array_var_set *avs = vs->aux;
679 return (struct variable *) avs->var[idx];
682 /* Returns the index of the variable in VS named NAME, or -1 if
683 VS contains no variable with that name. */
685 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name)
687 struct array_var_set *avs = vs->aux;
688 struct variable v, *vp, *const *vpp;
690 strcpy (v.name, name);
692 vpp = hsh_find (avs->name_tab, &vp);
693 return vpp != NULL ? vpp - avs->var : -1;
698 array_var_set_destroy (struct var_set *vs)
700 struct array_var_set *avs = vs->aux;
702 hsh_destroy (avs->name_tab);
707 /* Returns a variable set based on the VAR_CNT variables in
710 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
713 struct array_var_set *avs;
716 vs = xmalloc (sizeof *vs);
717 vs->get_cnt = array_var_set_get_cnt;
718 vs->get_var = array_var_set_get_var;
719 vs->lookup_var_idx = array_var_set_lookup_var_idx;
720 vs->destroy = array_var_set_destroy;
721 vs->aux = avs = xmalloc (sizeof *avs);
723 avs->var_cnt = var_cnt;
724 avs->name_tab = hsh_create (2 * var_cnt,
725 compare_var_ptr_names, hash_var_ptr_name, NULL,
727 for (i = 0; i < var_cnt; i++)
728 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
730 var_set_destroy (vs);