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
27 #include <data/dictionary.h>
28 #include <data/procedure.h>
29 #include <data/variable.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/bit-vector.h>
32 #include <libpspp/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/misc.h>
35 #include <libpspp/pool.h>
36 #include <libpspp/str.h>
39 #define _(msgid) gettext (msgid)
41 /* Parses a name as a variable within VS. Sets *IDX to the
42 variable's index and returns true if successful. On failure
43 emits an error message and returns false. */
45 parse_vs_variable_idx (const struct var_set *vs, size_t *idx)
51 lex_error (_("expecting variable name"));
54 else if (var_set_lookup_var_idx (vs, tokid, idx))
61 msg (SE, _("%s is not a variable name."), tokid);
66 /* Parses a name as a variable within VS and returns the variable
67 if successful. On failure emits an error message and returns
69 static struct variable *
70 parse_vs_variable (const struct var_set *vs)
73 return parse_vs_variable_idx (vs, &idx) ? var_set_get_var (vs, idx) : NULL;
76 /* Parses a variable name in dictionary D and returns the
77 variable if successful. On failure emits an error message and
78 returns a null pointer. */
80 parse_dict_variable (const struct dictionary *d)
82 struct var_set *vs = var_set_create_from_dict (d);
83 struct variable *var = parse_vs_variable (vs);
88 /* Parses a variable name in default_dict and returns the
89 variable if successful. On failure emits an error message and
90 returns a null pointer. */
94 return parse_dict_variable (default_dict);
98 /* Parses a set of variables from dictionary D given options
99 OPTS. Resulting list of variables stored in *VAR and the
100 number of variables into *CNT. Returns nonzero only if
103 parse_variables (const struct dictionary *d, struct variable ***var,
104 size_t *cnt, int opts)
110 assert (var != NULL);
111 assert (cnt != NULL);
113 vs = var_set_create_from_dict (d);
114 success = parse_var_set_vars (vs, var, cnt, opts);
117 var_set_destroy (vs);
121 /* Parses a variable name from VS. If successful, sets *IDX to
122 the variable's index in VS, *CLASS to the variable's
123 dictionary class, and returns nonzero. Returns zero on
126 parse_var_idx_class (const struct var_set *vs, size_t *idx,
127 enum dict_class *class)
129 if (!parse_vs_variable_idx (vs, idx))
132 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
136 /* Add the variable from VS with index IDX to the list of
137 variables V that has *NV elements and room for *MV.
138 Uses and updates INCLUDED to avoid duplicates if indicated by
139 PV_OPTS, which also affects what variables are allowed in
142 add_variable (struct variable ***v, size_t *nv, size_t *mv,
143 char *included, int pv_opts,
144 const struct var_set *vs, size_t idx)
146 struct variable *add = var_set_get_var (vs, idx);
148 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
149 msg (SW, _("%s is not a numeric variable. It will not be "
150 "included in the variable list."), add->name);
151 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
152 msg (SE, _("%s is not a string variable. It will not be "
153 "included in the variable list."), add->name);
154 else if ((pv_opts & PV_NO_SCRATCH)
155 && dict_class_from_id (add->name) == DC_SCRATCH)
156 msg (SE, _("Scratch variables (such as %s) are not allowed "
157 "here."), add->name);
158 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
159 msg (SE, _("%s and %s are not the same type. All variables in "
160 "this variable list must be of the same type. %s "
161 "will be omitted from list."),
162 (*v)[0]->name, add->name, add->name);
163 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
164 msg (SE, _("Variable %s appears twice in variable list."), add->name);
170 *v = xnrealloc (*v, *mv, sizeof **v);
173 if ((pv_opts & PV_DUPLICATE) || !included[idx])
176 if (!(pv_opts & PV_DUPLICATE))
182 /* Adds the variables in VS with indexes FIRST_IDX through
183 LAST_IDX, inclusive, to the list of variables V that has *NV
184 elements and room for *MV. Uses and updates INCLUDED to avoid
185 duplicates if indicated by PV_OPTS, which also affects what
186 variables are allowed in appropriate ways. */
188 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
190 const struct var_set *vs, int first_idx, int last_idx,
191 enum dict_class class)
195 for (i = first_idx; i <= last_idx; i++)
196 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
197 add_variable (v, nv, mv, included, pv_opts, vs, i);
200 /* Note that if parse_variables() returns 0, *v is free()'d.
201 Conversely, if parse_variables() returns non-zero, then *nv is
202 nonzero and *v is non-NULL. */
204 parse_var_set_vars (const struct var_set *vs,
205 struct variable ***v, size_t *nv,
215 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
217 assert ((((pv_opts & PV_NUMERIC) != 0)
218 + ((pv_opts & PV_STRING) != 0)
219 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
221 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
222 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
224 if (!(pv_opts & PV_APPEND))
233 if (!(pv_opts & PV_DUPLICATE))
237 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
238 for (i = 0; i < *nv; i++)
239 included[(*v)[i]->index] = 1;
244 if (lex_match (T_ALL))
245 add_variables (v, nv, &mv, included, pv_opts,
246 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
251 enum dict_class class;
254 if (!parse_var_idx_class (vs, &first_idx, &class))
257 if (!lex_match (T_TO))
258 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
262 enum dict_class last_class;
263 struct variable *first_var, *last_var;
265 if (!parse_var_idx_class (vs, &last_idx, &last_class))
268 first_var = var_set_get_var (vs, first_idx);
269 last_var = var_set_get_var (vs, last_idx);
271 if (last_idx < first_idx)
273 msg (SE, _("%s TO %s is not valid syntax since %s "
274 "precedes %s in the dictionary."),
275 first_var->name, last_var->name,
276 first_var->name, last_var->name);
280 if (class != last_class)
282 msg (SE, _("When using the TO keyword to specify several "
283 "variables, both variables must be from "
284 "the same variable dictionaries, of either "
285 "ordinary, scratch, or system variables. "
286 "%s is a %s variable, whereas %s is %s."),
287 first_var->name, dict_class_to_name (class),
288 last_var->name, dict_class_to_name (last_class));
292 add_variables (v, nv, &mv, included, pv_opts,
293 vs, first_idx, last_idx, class);
295 if (pv_opts & PV_SINGLE)
299 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
316 /* Extracts a numeric suffix from variable name S, copying it
317 into string R. Sets *D to the length of R and *N to its
320 extract_num (char *s, char *r, int *n, int *d)
324 /* Find first digit. */
325 cp = s + strlen (s) - 1;
326 while (isdigit ((unsigned char) *cp) && cp > s)
331 strncpy (r, s, cp - s);
334 /* Count initial zeros. */
343 while (isdigit ((unsigned char) *cp))
346 *n = (*n * 10) + (*cp - '0');
351 if (*n == 0 && *d == 0)
353 msg (SE, _("incorrect use of TO convention"));
359 /* Parses a list of variable names according to the DATA LIST version
360 of the TO convention. */
362 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
368 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
369 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
372 assert (names != NULL);
373 assert (nnames != NULL);
374 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
375 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
376 /* FIXME: PV_NO_DUPLICATE is not implemented. */
378 if (pv_opts & PV_APPEND)
379 nvar = mvar = *nnames;
390 lex_error ("expecting variable name");
393 if (dict_class_from_id (tokid) == DC_SCRATCH
394 && (pv_opts & PV_NO_SCRATCH))
396 msg (SE, _("Scratch variables not allowed here."));
399 strcpy (name1, tokid);
406 lex_error ("expecting variable name");
409 strcpy (name2, tokid);
412 if (!extract_num (name1, root1, &n1, &d1)
413 || !extract_num (name2, root2, &n2, &d2))
416 if (strcasecmp (root1, root2))
418 msg (SE, _("Prefixes don't match in use of TO convention."));
423 msg (SE, _("Bad bounds in use of TO convention."));
429 if (mvar < nvar + (n2 - n1 + 1))
431 mvar += ROUND_UP (n2 - n1 + 1, 16);
432 *names = xnrealloc (*names, mvar, sizeof **names);
435 for (n = n1; n <= n2; n++)
437 char name[LONG_NAME_LEN + 1];
438 sprintf (name, "%s%0*d", root1, d1, n);
439 (*names)[nvar] = xstrdup (name);
448 *names = xnrealloc (*names, mvar, sizeof **names);
450 (*names)[nvar++] = xstrdup (name1);
455 if (pv_opts & PV_SINGLE)
458 while (token == T_ID);
466 for (i = 0; i < nvar; i++)
475 /* Parses a list of variables where some of the variables may be
476 existing and the rest are to be created. Same args as
477 parse_DATA_LIST_vars(). */
479 parse_mixed_vars (char ***names, size_t *nnames, int pv_opts)
483 assert (names != NULL);
484 assert (nnames != NULL);
485 assert ((pv_opts & ~PV_APPEND) == 0);
487 if (!(pv_opts & PV_APPEND))
492 while (token == T_ID || token == T_ALL)
494 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
499 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
501 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
502 for (i = 0; i < nv; i++)
503 (*names)[*nnames + i] = xstrdup (v[i]->name);
507 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
513 for (i = 0; i < *nnames; i++)
521 /* Parses a list of variables where some of the variables may be
522 existing and the rest are to be created. Same args as
523 parse_DATA_LIST_vars(), except that all allocations are taken
524 from the given POOL. */
526 parse_mixed_vars_pool (struct pool *pool,
527 char ***names, size_t *nnames, int pv_opts)
529 int retval = parse_mixed_vars (names, nnames, pv_opts);
534 for (i = 0; i < *nnames; i++)
535 pool_register (pool, free, (*names)[i]);
536 pool_register (pool, free, *names);
542 /* A set of variables. */
545 size_t (*get_cnt) (const struct var_set *);
546 struct variable *(*get_var) (const struct var_set *, size_t idx);
547 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
548 void (*destroy) (struct var_set *);
552 /* Returns the number of variables in VS. */
554 var_set_get_cnt (const struct var_set *vs)
558 return vs->get_cnt (vs);
561 /* Return variable with index IDX in VS.
562 IDX must be less than the number of variables in VS. */
564 var_set_get_var (const struct var_set *vs, size_t idx)
567 assert (idx < var_set_get_cnt (vs));
569 return vs->get_var (vs, idx);
572 /* Returns the variable in VS named NAME, or a null pointer if VS
573 contains no variable with that name. */
575 var_set_lookup_var (const struct var_set *vs, const char *name)
578 return (var_set_lookup_var_idx (vs, name, &idx)
579 ? var_set_get_var (vs, idx)
583 /* If VS contains a variable named NAME, sets *IDX to its index
584 and returns true. Otherwise, returns false. */
586 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
590 assert (name != NULL);
591 assert (strlen (name) <= LONG_NAME_LEN);
593 return vs->lookup_var_idx (vs, name, idx);
598 var_set_destroy (struct var_set *vs)
604 /* Returns the number of variables in VS. */
606 dict_var_set_get_cnt (const struct var_set *vs)
608 struct dictionary *d = vs->aux;
610 return dict_get_var_cnt (d);
613 /* Return variable with index IDX in VS.
614 IDX must be less than the number of variables in VS. */
615 static struct variable *
616 dict_var_set_get_var (const struct var_set *vs, size_t idx)
618 struct dictionary *d = vs->aux;
620 return dict_get_var (d, idx);
623 /* If VS contains a variable named NAME, sets *IDX to its index
624 and returns true. Otherwise, returns false. */
626 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
629 struct dictionary *d = vs->aux;
630 struct variable *v = dict_lookup_var (d, name);
642 dict_var_set_destroy (struct var_set *vs)
647 /* Returns a variable set based on D. */
649 var_set_create_from_dict (const struct dictionary *d)
651 struct var_set *vs = xmalloc (sizeof *vs);
652 vs->get_cnt = dict_var_set_get_cnt;
653 vs->get_var = dict_var_set_get_var;
654 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
655 vs->destroy = dict_var_set_destroy;
656 vs->aux = (void *) d;
660 /* A variable set based on an array. */
663 struct variable *const *var;/* Array of variables. */
664 size_t var_cnt; /* Number of elements in var. */
665 struct hsh_table *name_tab; /* Hash from variable names to variables. */
668 /* Returns the number of variables in VS. */
670 array_var_set_get_cnt (const struct var_set *vs)
672 struct array_var_set *avs = vs->aux;
677 /* Return variable with index IDX in VS.
678 IDX must be less than the number of variables in VS. */
679 static struct variable *
680 array_var_set_get_var (const struct var_set *vs, size_t idx)
682 struct array_var_set *avs = vs->aux;
684 return (struct variable *) avs->var[idx];
687 /* If VS contains a variable named NAME, sets *IDX to its index
688 and returns true. Otherwise, returns false. */
690 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
693 struct array_var_set *avs = vs->aux;
694 struct variable v, *vp, *const *vpp;
696 strcpy (v.name, name);
698 vpp = hsh_find (avs->name_tab, &vp);
701 *idx = vpp - avs->var;
710 array_var_set_destroy (struct var_set *vs)
712 struct array_var_set *avs = vs->aux;
714 hsh_destroy (avs->name_tab);
719 /* Returns a variable set based on the VAR_CNT variables in
722 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
725 struct array_var_set *avs;
728 vs = xmalloc (sizeof *vs);
729 vs->get_cnt = array_var_set_get_cnt;
730 vs->get_var = array_var_set_get_var;
731 vs->lookup_var_idx = array_var_set_lookup_var_idx;
732 vs->destroy = array_var_set_destroy;
733 vs->aux = avs = xmalloc (sizeof *avs);
735 avs->var_cnt = var_cnt;
736 avs->name_tab = hsh_create (2 * var_cnt,
737 compare_var_ptr_names, hash_var_ptr_name, NULL,
739 for (i = 0; i < var_cnt; i++)
740 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
742 var_set_destroy (vs);