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"
37 #define _(msgid) gettext (msgid)
39 /* Parses a name as a variable within VS. Sets *IDX to the
40 variable's index and returns true if successful. On failure
41 emits an error message and returns false. */
43 parse_vs_variable_idx (const struct var_set *vs, size_t *idx)
49 lex_error (_("expecting variable name"));
52 else if (var_set_lookup_var_idx (vs, tokid, idx))
59 msg (SE, _("%s is not a variable name."), tokid);
64 /* Parses a name as a variable within VS and returns the variable
65 if successful. On failure emits an error message and returns
67 static struct variable *
68 parse_vs_variable (const struct var_set *vs)
71 return parse_vs_variable_idx (vs, &idx) ? var_set_get_var (vs, idx) : NULL;
74 /* Parses a variable name in dictionary D and returns the
75 variable if successful. On failure emits an error message and
76 returns a null pointer. */
78 parse_dict_variable (const struct dictionary *d)
80 struct var_set *vs = var_set_create_from_dict (d);
81 struct variable *var = parse_vs_variable (vs);
86 /* Parses a variable name in default_dict and returns the
87 variable if successful. On failure emits an error message and
88 returns a null pointer. */
92 return parse_dict_variable (default_dict);
96 /* Parses a set of variables from dictionary D given options
97 OPTS. Resulting list of variables stored in *VAR and the
98 number of variables into *CNT. Returns nonzero only if
101 parse_variables (const struct dictionary *d, struct variable ***var,
102 size_t *cnt, int opts)
108 assert (var != NULL);
109 assert (cnt != NULL);
111 vs = var_set_create_from_dict (d);
112 success = parse_var_set_vars (vs, var, cnt, opts);
115 var_set_destroy (vs);
119 /* Parses a variable name from VS. If successful, sets *IDX to
120 the variable's index in VS, *CLASS to the variable's
121 dictionary class, and returns nonzero. Returns zero on
124 parse_var_idx_class (const struct var_set *vs, size_t *idx,
125 enum dict_class *class)
127 if (!parse_vs_variable_idx (vs, idx))
130 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
134 /* Add the variable from VS with index IDX to the list of
135 variables V that has *NV elements and room for *MV.
136 Uses and updates INCLUDED to avoid duplicates if indicated by
137 PV_OPTS, which also affects what variables are allowed in
140 add_variable (struct variable ***v, size_t *nv, size_t *mv,
141 char *included, int pv_opts,
142 const struct var_set *vs, size_t idx)
144 struct variable *add = var_set_get_var (vs, idx);
146 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
147 msg (SW, _("%s is not a numeric variable. It will not be "
148 "included in the variable list."), add->name);
149 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
150 msg (SE, _("%s is not a string variable. It will not be "
151 "included in the variable list."), add->name);
152 else if ((pv_opts & PV_NO_SCRATCH)
153 && dict_class_from_id (add->name) == DC_SCRATCH)
154 msg (SE, _("Scratch variables (such as %s) are not allowed "
155 "here."), add->name);
156 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
157 msg (SE, _("%s and %s are not the same type. All variables in "
158 "this variable list must be of the same type. %s "
159 "will be omitted from list."),
160 (*v)[0]->name, add->name, add->name);
161 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
162 msg (SE, _("Variable %s appears twice in variable list."), add->name);
168 *v = xnrealloc (*v, *mv, sizeof **v);
171 if ((pv_opts & PV_DUPLICATE) || !included[idx])
174 if (!(pv_opts & PV_DUPLICATE))
180 /* Adds the variables in VS with indexes FIRST_IDX through
181 LAST_IDX, inclusive, to the list of variables V that has *NV
182 elements and room for *MV. Uses and updates INCLUDED to avoid
183 duplicates if indicated by PV_OPTS, which also affects what
184 variables are allowed in appropriate ways. */
186 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
188 const struct var_set *vs, int first_idx, int last_idx,
189 enum dict_class class)
193 for (i = first_idx; i <= last_idx; i++)
194 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
195 add_variable (v, nv, mv, included, pv_opts, vs, i);
198 /* Note that if parse_variables() returns 0, *v is free()'d.
199 Conversely, if parse_variables() returns non-zero, then *nv is
200 nonzero and *v is non-NULL. */
202 parse_var_set_vars (const struct var_set *vs,
203 struct variable ***v, size_t *nv,
213 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
215 assert ((((pv_opts & PV_NUMERIC) != 0)
216 + ((pv_opts & PV_STRING) != 0)
217 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
219 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
220 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
222 if (!(pv_opts & PV_APPEND))
231 if (!(pv_opts & PV_DUPLICATE))
235 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
236 for (i = 0; i < *nv; i++)
237 included[(*v)[i]->index] = 1;
242 if (lex_match (T_ALL))
243 add_variables (v, nv, &mv, included, pv_opts,
244 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
249 enum dict_class class;
252 if (!parse_var_idx_class (vs, &first_idx, &class))
255 if (!lex_match (T_TO))
256 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
260 enum dict_class last_class;
261 struct variable *first_var, *last_var;
263 if (!parse_var_idx_class (vs, &last_idx, &last_class))
266 first_var = var_set_get_var (vs, first_idx);
267 last_var = var_set_get_var (vs, last_idx);
269 if (last_idx < first_idx)
271 msg (SE, _("%s TO %s is not valid syntax since %s "
272 "precedes %s in the dictionary."),
273 first_var->name, last_var->name,
274 first_var->name, last_var->name);
278 if (class != last_class)
280 msg (SE, _("When using the TO keyword to specify several "
281 "variables, both variables must be from "
282 "the same variable dictionaries, of either "
283 "ordinary, scratch, or system variables. "
284 "%s is a %s variable, whereas %s is %s."),
285 first_var->name, dict_class_to_name (class),
286 last_var->name, dict_class_to_name (last_class));
290 add_variables (v, nv, &mv, included, pv_opts,
291 vs, first_idx, last_idx, class);
293 if (pv_opts & PV_SINGLE)
297 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
314 /* Extracts a numeric suffix from variable name S, copying it
315 into string R. Sets *D to the length of R and *N to its
318 extract_num (char *s, char *r, int *n, int *d)
322 /* Find first digit. */
323 cp = s + strlen (s) - 1;
324 while (isdigit ((unsigned char) *cp) && cp > s)
329 strncpy (r, s, cp - s);
332 /* Count initial zeros. */
341 while (isdigit ((unsigned char) *cp))
344 *n = (*n * 10) + (*cp - '0');
349 if (*n == 0 && *d == 0)
351 msg (SE, _("incorrect use of TO convention"));
357 /* Parses a list of variable names according to the DATA LIST version
358 of the TO convention. */
360 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
366 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
367 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
370 assert (names != NULL);
371 assert (nnames != NULL);
372 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
373 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
374 /* FIXME: PV_NO_DUPLICATE is not implemented. */
376 if (pv_opts & PV_APPEND)
377 nvar = mvar = *nnames;
388 lex_error ("expecting variable name");
391 if (dict_class_from_id (tokid) == DC_SCRATCH
392 && (pv_opts & PV_NO_SCRATCH))
394 msg (SE, _("Scratch variables not allowed here."));
397 strcpy (name1, tokid);
404 lex_error ("expecting variable name");
407 strcpy (name2, tokid);
410 if (!extract_num (name1, root1, &n1, &d1)
411 || !extract_num (name2, root2, &n2, &d2))
414 if (strcasecmp (root1, root2))
416 msg (SE, _("Prefixes don't match in use of TO convention."));
421 msg (SE, _("Bad bounds in use of TO convention."));
427 if (mvar < nvar + (n2 - n1 + 1))
429 mvar += ROUND_UP (n2 - n1 + 1, 16);
430 *names = xnrealloc (*names, mvar, sizeof **names);
433 for (n = n1; n <= n2; n++)
435 char name[LONG_NAME_LEN + 1];
436 sprintf (name, "%s%0*d", root1, d1, n);
437 (*names)[nvar] = xstrdup (name);
446 *names = xnrealloc (*names, mvar, sizeof **names);
448 (*names)[nvar++] = xstrdup (name1);
453 if (pv_opts & PV_SINGLE)
456 while (token == T_ID);
464 for (i = 0; i < nvar; i++)
473 /* Parses a list of variables where some of the variables may be
474 existing and the rest are to be created. Same args as
475 parse_DATA_LIST_vars(). */
477 parse_mixed_vars (char ***names, size_t *nnames, int pv_opts)
481 assert (names != NULL);
482 assert (nnames != NULL);
483 assert ((pv_opts & ~PV_APPEND) == 0);
485 if (!(pv_opts & PV_APPEND))
490 while (token == T_ID || token == T_ALL)
492 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
497 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
499 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
500 for (i = 0; i < nv; i++)
501 (*names)[*nnames + i] = xstrdup (v[i]->name);
505 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
511 for (i = 0; i < *nnames; i++)
519 /* Parses a list of variables where some of the variables may be
520 existing and the rest are to be created. Same args as
521 parse_DATA_LIST_vars(), except that all allocations are taken
522 from the given POOL. */
524 parse_mixed_vars_pool (struct pool *pool,
525 char ***names, size_t *nnames, int pv_opts)
527 int retval = parse_mixed_vars (names, nnames, pv_opts);
532 for (i = 0; i < *nnames; i++)
533 pool_register (pool, free, (*names)[i]);
534 pool_register (pool, free, *names);
540 /* A set of variables. */
543 size_t (*get_cnt) (const struct var_set *);
544 struct variable *(*get_var) (const struct var_set *, size_t idx);
545 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
546 void (*destroy) (struct var_set *);
550 /* Returns the number of variables in VS. */
552 var_set_get_cnt (const struct var_set *vs)
556 return vs->get_cnt (vs);
559 /* Return variable with index IDX in VS.
560 IDX must be less than the number of variables in VS. */
562 var_set_get_var (const struct var_set *vs, size_t idx)
565 assert (idx < var_set_get_cnt (vs));
567 return vs->get_var (vs, idx);
570 /* Returns the variable in VS named NAME, or a null pointer if VS
571 contains no variable with that name. */
573 var_set_lookup_var (const struct var_set *vs, const char *name)
576 return (var_set_lookup_var_idx (vs, name, &idx)
577 ? var_set_get_var (vs, idx)
581 /* If VS contains a variable named NAME, sets *IDX to its index
582 and returns true. Otherwise, returns false. */
584 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
588 assert (name != NULL);
589 assert (strlen (name) <= LONG_NAME_LEN);
591 return vs->lookup_var_idx (vs, name, idx);
596 var_set_destroy (struct var_set *vs)
602 /* Returns the number of variables in VS. */
604 dict_var_set_get_cnt (const struct var_set *vs)
606 struct dictionary *d = vs->aux;
608 return dict_get_var_cnt (d);
611 /* Return variable with index IDX in VS.
612 IDX must be less than the number of variables in VS. */
613 static struct variable *
614 dict_var_set_get_var (const struct var_set *vs, size_t idx)
616 struct dictionary *d = vs->aux;
618 return dict_get_var (d, idx);
621 /* If VS contains a variable named NAME, sets *IDX to its index
622 and returns true. Otherwise, returns false. */
624 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
627 struct dictionary *d = vs->aux;
628 struct variable *v = dict_lookup_var (d, name);
640 dict_var_set_destroy (struct var_set *vs)
645 /* Returns a variable set based on D. */
647 var_set_create_from_dict (const struct dictionary *d)
649 struct var_set *vs = xmalloc (sizeof *vs);
650 vs->get_cnt = dict_var_set_get_cnt;
651 vs->get_var = dict_var_set_get_var;
652 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
653 vs->destroy = dict_var_set_destroy;
654 vs->aux = (void *) d;
658 /* A variable set based on an array. */
661 struct variable *const *var;/* Array of variables. */
662 size_t var_cnt; /* Number of elements in var. */
663 struct hsh_table *name_tab; /* Hash from variable names to variables. */
666 /* Returns the number of variables in VS. */
668 array_var_set_get_cnt (const struct var_set *vs)
670 struct array_var_set *avs = vs->aux;
675 /* Return variable with index IDX in VS.
676 IDX must be less than the number of variables in VS. */
677 static struct variable *
678 array_var_set_get_var (const struct var_set *vs, size_t idx)
680 struct array_var_set *avs = vs->aux;
682 return (struct variable *) avs->var[idx];
685 /* If VS contains a variable named NAME, sets *IDX to its index
686 and returns true. Otherwise, returns false. */
688 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
691 struct array_var_set *avs = vs->aux;
692 struct variable v, *vp, *const *vpp;
694 strcpy (v.name, name);
696 vpp = hsh_find (avs->name_tab, &vp);
699 *idx = vpp - avs->var;
708 array_var_set_destroy (struct var_set *vs)
710 struct array_var_set *avs = vs->aux;
712 hsh_destroy (avs->name_tab);
717 /* Returns a variable set based on the VAR_CNT variables in
720 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
723 struct array_var_set *avs;
726 vs = xmalloc (sizeof *vs);
727 vs->get_cnt = array_var_set_get_cnt;
728 vs->get_var = array_var_set_get_var;
729 vs->lookup_var_idx = array_var_set_lookup_var_idx;
730 vs->destroy = array_var_set_destroy;
731 vs->aux = avs = xmalloc (sizeof *avs);
733 avs->var_cnt = var_cnt;
734 avs->name_tab = hsh_create (2 * var_cnt,
735 compare_var_ptr_names, hash_var_ptr_name, NULL,
737 for (i = 0; i < var_cnt; i++)
738 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
740 var_set_destroy (vs);