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
26 #include "bitvector.h"
27 #include "dictionary.h"
36 #define _(msgid) gettext (msgid)
38 /* Parses a name as a variable within VS. Sets *IDX to the
39 variable's index and returns true if successful. On failure
40 emits an error message and returns false. */
42 parse_vs_variable_idx (const struct var_set *vs, size_t *idx)
48 lex_error (_("expecting variable name"));
51 else if (var_set_lookup_var_idx (vs, tokid, idx))
58 msg (SE, _("%s is not a variable name."), tokid);
63 /* Parses a name as a variable within VS and returns the variable
64 if successful. On failure emits an error message and returns
66 static struct variable *
67 parse_vs_variable (const struct var_set *vs)
70 return parse_vs_variable_idx (vs, &idx) ? var_set_get_var (vs, idx) : NULL;
73 /* Parses a variable name in dictionary D and returns the
74 variable if successful. On failure emits an error message and
75 returns a null pointer. */
77 parse_dict_variable (const struct dictionary *d)
79 struct var_set *vs = var_set_create_from_dict (d);
80 struct variable *var = parse_vs_variable (vs);
85 /* Parses a variable name in default_dict and returns the
86 variable if successful. On failure emits an error message and
87 returns a null pointer. */
91 return parse_dict_variable (default_dict);
94 /* Returns the dictionary class corresponding to a variable named
97 dict_class_from_id (const char *name)
99 assert (name != NULL);
112 /* Returns the name of dictionary class DICT_CLASS. */
114 dict_class_to_name (enum dict_class dict_class)
119 return _("ordinary");
130 /* Parses a set of variables from dictionary D given options
131 OPTS. Resulting list of variables stored in *VAR and the
132 number of variables into *CNT. Returns nonzero only if
135 parse_variables (const struct dictionary *d, struct variable ***var,
136 size_t *cnt, int opts)
142 assert (var != NULL);
143 assert (cnt != NULL);
145 vs = var_set_create_from_dict (d);
146 success = parse_var_set_vars (vs, var, cnt, opts);
149 var_set_destroy (vs);
153 /* Parses a variable name from VS. If successful, sets *IDX to
154 the variable's index in VS, *CLASS to the variable's
155 dictionary class, and returns nonzero. Returns zero on
158 parse_var_idx_class (const struct var_set *vs, size_t *idx,
159 enum dict_class *class)
161 if (!parse_vs_variable_idx (vs, idx))
164 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
168 /* Add the variable from VS with index IDX to the list of
169 variables V that has *NV elements and room for *MV.
170 Uses and updates INCLUDED to avoid duplicates if indicated by
171 PV_OPTS, which also affects what variables are allowed in
174 add_variable (struct variable ***v, size_t *nv, size_t *mv,
175 char *included, int pv_opts,
176 const struct var_set *vs, size_t idx)
178 struct variable *add = var_set_get_var (vs, idx);
180 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
181 msg (SW, _("%s is not a numeric variable. It will not be "
182 "included in the variable list."), add->name);
183 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
184 msg (SE, _("%s is not a string variable. It will not be "
185 "included in the variable list."), add->name);
186 else if ((pv_opts & PV_NO_SCRATCH)
187 && dict_class_from_id (add->name) == DC_SCRATCH)
188 msg (SE, _("Scratch variables (such as %s) are not allowed "
189 "here."), add->name);
190 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
191 msg (SE, _("%s and %s are not the same type. All variables in "
192 "this variable list must be of the same type. %s "
193 "will be omitted from list."),
194 (*v)[0]->name, add->name, add->name);
195 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
196 msg (SE, _("Variable %s appears twice in variable list."), add->name);
202 *v = xrealloc (*v, *mv * sizeof **v);
205 if ((pv_opts & PV_DUPLICATE) || !included[idx])
208 if (!(pv_opts & PV_DUPLICATE))
214 /* Adds the variables in VS with indexes FIRST_IDX through
215 LAST_IDX, inclusive, to the list of variables V that has *NV
216 elements and room for *MV. Uses and updates INCLUDED to avoid
217 duplicates if indicated by PV_OPTS, which also affects what
218 variables are allowed in appropriate ways. */
220 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
222 const struct var_set *vs, int first_idx, int last_idx,
223 enum dict_class class)
227 for (i = first_idx; i <= last_idx; i++)
228 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
229 add_variable (v, nv, mv, included, pv_opts, vs, i);
232 /* Note that if parse_variables() returns 0, *v is free()'d.
233 Conversely, if parse_variables() returns non-zero, then *nv is
234 nonzero and *v is non-NULL. */
236 parse_var_set_vars (const struct var_set *vs,
237 struct variable ***v, size_t *nv,
247 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
249 assert ((((pv_opts & PV_NUMERIC) != 0)
250 + ((pv_opts & PV_STRING) != 0)
251 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
253 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
254 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
256 if (!(pv_opts & PV_APPEND))
265 if (!(pv_opts & PV_DUPLICATE))
269 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
270 for (i = 0; i < *nv; i++)
271 included[(*v)[i]->index] = 1;
276 if (lex_match (T_ALL))
277 add_variables (v, nv, &mv, included, pv_opts,
278 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
283 enum dict_class class;
286 if (!parse_var_idx_class (vs, &first_idx, &class))
289 if (!lex_match (T_TO))
290 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
294 enum dict_class last_class;
295 struct variable *first_var, *last_var;
297 if (!parse_var_idx_class (vs, &last_idx, &last_class))
300 first_var = var_set_get_var (vs, first_idx);
301 last_var = var_set_get_var (vs, last_idx);
303 if (last_idx < first_idx)
305 msg (SE, _("%s TO %s is not valid syntax since %s "
306 "precedes %s in the dictionary."),
307 first_var->name, last_var->name,
308 first_var->name, last_var->name);
312 if (class != last_class)
314 msg (SE, _("When using the TO keyword to specify several "
315 "variables, both variables must be from "
316 "the same variable dictionaries, of either "
317 "ordinary, scratch, or system variables. "
318 "%s is a %s variable, whereas %s is %s."),
319 first_var->name, dict_class_to_name (class),
320 last_var->name, dict_class_to_name (last_class));
324 add_variables (v, nv, &mv, included, pv_opts,
325 vs, first_idx, last_idx, class);
327 if (pv_opts & PV_SINGLE)
331 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
348 /* Extracts a numeric suffix from variable name S, copying it
349 into string R. Sets *D to the length of R and *N to its
352 extract_num (char *s, char *r, int *n, int *d)
356 /* Find first digit. */
357 cp = s + strlen (s) - 1;
358 while (isdigit ((unsigned char) *cp) && cp > s)
363 strncpy (r, s, cp - s);
366 /* Count initial zeros. */
375 while (isdigit ((unsigned char) *cp))
378 *n = (*n * 10) + (*cp - '0');
383 if (*n == 0 && *d == 0)
385 msg (SE, _("incorrect use of TO convention"));
391 /* Parses a list of variable names according to the DATA LIST version
392 of the TO convention. */
394 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
400 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
401 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
404 assert (names != NULL);
405 assert (nnames != NULL);
406 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
407 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
408 /* FIXME: PV_NO_DUPLICATE is not implemented. */
410 if (pv_opts & PV_APPEND)
411 nvar = mvar = *nnames;
422 lex_error ("expecting variable name");
425 if (dict_class_from_id (tokid) == DC_SCRATCH
426 && (pv_opts & PV_NO_SCRATCH))
428 msg (SE, _("Scratch variables not allowed here."));
431 strcpy (name1, tokid);
438 lex_error ("expecting variable name");
441 strcpy (name2, tokid);
444 if (!extract_num (name1, root1, &n1, &d1)
445 || !extract_num (name2, root2, &n2, &d2))
448 if (strcasecmp (root1, root2))
450 msg (SE, _("Prefixes don't match in use of TO convention."));
455 msg (SE, _("Bad bounds in use of TO convention."));
461 if (mvar < nvar + (n2 - n1 + 1))
463 mvar += ROUND_UP (n2 - n1 + 1, 16);
464 *names = xrealloc (*names, mvar * sizeof **names);
467 for (n = n1; n <= n2; n++)
469 char name[LONG_NAME_LEN + 1];
470 sprintf (name, "%s%0*d", root1, d1, n);
471 (*names)[nvar] = xstrdup (name);
480 *names = xrealloc (*names, mvar * sizeof **names);
482 (*names)[nvar++] = xstrdup (name1);
487 if (pv_opts & PV_SINGLE)
490 while (token == T_ID);
498 for (i = 0; i < nvar; i++)
507 /* Parses a list of variables where some of the variables may be
508 existing and the rest are to be created. Same args as
509 parse_DATA_LIST_vars(). */
511 parse_mixed_vars (char ***names, size_t *nnames, int pv_opts)
515 assert (names != NULL);
516 assert (nnames != NULL);
517 assert ((pv_opts & ~PV_APPEND) == 0);
519 if (!(pv_opts & PV_APPEND))
524 while (token == T_ID || token == T_ALL)
526 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
531 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
533 *names = xrealloc (*names, (*nnames + nv) * sizeof **names);
534 for (i = 0; i < nv; i++)
535 (*names)[*nnames + i] = xstrdup (v[i]->name);
539 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
545 for (i = 0; i < *nnames; i++)
546 free ((*names)[*nnames]);
553 /* A set of variables. */
556 size_t (*get_cnt) (const struct var_set *);
557 struct variable *(*get_var) (const struct var_set *, size_t idx);
558 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
559 void (*destroy) (struct var_set *);
563 /* Returns the number of variables in VS. */
565 var_set_get_cnt (const struct var_set *vs)
569 return vs->get_cnt (vs);
572 /* Return variable with index IDX in VS.
573 IDX must be less than the number of variables in VS. */
575 var_set_get_var (const struct var_set *vs, size_t idx)
578 assert (idx < var_set_get_cnt (vs));
580 return vs->get_var (vs, idx);
583 /* Returns the variable in VS named NAME, or a null pointer if VS
584 contains no variable with that name. */
586 var_set_lookup_var (const struct var_set *vs, const char *name)
589 return (var_set_lookup_var_idx (vs, name, &idx)
590 ? var_set_get_var (vs, idx)
594 /* If VS contains a variable named NAME, sets *IDX to its index
595 and returns true. Otherwise, returns false. */
597 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
601 assert (name != NULL);
602 assert (strlen (name) <= LONG_NAME_LEN);
604 return vs->lookup_var_idx (vs, name, idx);
609 var_set_destroy (struct var_set *vs)
615 /* Returns the number of variables in VS. */
617 dict_var_set_get_cnt (const struct var_set *vs)
619 struct dictionary *d = vs->aux;
621 return dict_get_var_cnt (d);
624 /* Return variable with index IDX in VS.
625 IDX must be less than the number of variables in VS. */
626 static struct variable *
627 dict_var_set_get_var (const struct var_set *vs, size_t idx)
629 struct dictionary *d = vs->aux;
631 return dict_get_var (d, idx);
634 /* If VS contains a variable named NAME, sets *IDX to its index
635 and returns true. Otherwise, returns false. */
637 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
640 struct dictionary *d = vs->aux;
641 struct variable *v = dict_lookup_var (d, name);
653 dict_var_set_destroy (struct var_set *vs)
658 /* Returns a variable set based on D. */
660 var_set_create_from_dict (const struct dictionary *d)
662 struct var_set *vs = xmalloc (sizeof *vs);
663 vs->get_cnt = dict_var_set_get_cnt;
664 vs->get_var = dict_var_set_get_var;
665 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
666 vs->destroy = dict_var_set_destroy;
667 vs->aux = (void *) d;
671 /* A variable set based on an array. */
674 struct variable *const *var;/* Array of variables. */
675 size_t var_cnt; /* Number of elements in var. */
676 struct hsh_table *name_tab; /* Hash from variable names to variables. */
679 /* Returns the number of variables in VS. */
681 array_var_set_get_cnt (const struct var_set *vs)
683 struct array_var_set *avs = vs->aux;
688 /* Return variable with index IDX in VS.
689 IDX must be less than the number of variables in VS. */
690 static struct variable *
691 array_var_set_get_var (const struct var_set *vs, size_t idx)
693 struct array_var_set *avs = vs->aux;
695 return (struct variable *) avs->var[idx];
698 /* If VS contains a variable named NAME, sets *IDX to its index
699 and returns true. Otherwise, returns false. */
701 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
704 struct array_var_set *avs = vs->aux;
705 struct variable v, *vp, *const *vpp;
707 strcpy (v.name, name);
709 vpp = hsh_find (avs->name_tab, &vp);
712 *idx = vpp - avs->var;
721 array_var_set_destroy (struct var_set *vs)
723 struct array_var_set *avs = vs->aux;
725 hsh_destroy (avs->name_tab);
730 /* Returns a variable set based on the VAR_CNT variables in
733 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
736 struct array_var_set *avs;
739 vs = xmalloc (sizeof *vs);
740 vs->get_cnt = array_var_set_get_cnt;
741 vs->get_var = array_var_set_get_var;
742 vs->lookup_var_idx = array_var_set_lookup_var_idx;
743 vs->destroy = array_var_set_destroy;
744 vs->aux = avs = xmalloc (sizeof *avs);
746 avs->var_cnt = var_cnt;
747 avs->name_tab = hsh_create (2 * var_cnt,
748 compare_var_ptr_names, hash_var_ptr_name, NULL,
750 for (i = 0; i < var_cnt; i++)
751 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
753 var_set_destroy (vs);