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);
95 /* Parses a set of variables from dictionary D given options
96 OPTS. Resulting list of variables stored in *VAR and the
97 number of variables into *CNT. Returns nonzero only if
100 parse_variables (const struct dictionary *d, struct variable ***var,
101 size_t *cnt, int opts)
107 assert (var != NULL);
108 assert (cnt != NULL);
110 vs = var_set_create_from_dict (d);
111 success = parse_var_set_vars (vs, var, cnt, opts);
114 var_set_destroy (vs);
118 /* Parses a variable name from VS. If successful, sets *IDX to
119 the variable's index in VS, *CLASS to the variable's
120 dictionary class, and returns nonzero. Returns zero on
123 parse_var_idx_class (const struct var_set *vs, size_t *idx,
124 enum dict_class *class)
126 if (!parse_vs_variable_idx (vs, idx))
129 *class = dict_class_from_id (var_set_get_var (vs, *idx)->name);
133 /* Add the variable from VS with index IDX to the list of
134 variables V that has *NV elements and room for *MV.
135 Uses and updates INCLUDED to avoid duplicates if indicated by
136 PV_OPTS, which also affects what variables are allowed in
139 add_variable (struct variable ***v, size_t *nv, size_t *mv,
140 char *included, int pv_opts,
141 const struct var_set *vs, size_t idx)
143 struct variable *add = var_set_get_var (vs, idx);
145 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
146 msg (SW, _("%s is not a numeric variable. It will not be "
147 "included in the variable list."), add->name);
148 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
149 msg (SE, _("%s is not a string variable. It will not be "
150 "included in the variable list."), add->name);
151 else if ((pv_opts & PV_NO_SCRATCH)
152 && dict_class_from_id (add->name) == DC_SCRATCH)
153 msg (SE, _("Scratch variables (such as %s) are not allowed "
154 "here."), add->name);
155 else if ((pv_opts & PV_SAME_TYPE) && *nv && add->type != (*v)[0]->type)
156 msg (SE, _("%s and %s are not the same type. All variables in "
157 "this variable list must be of the same type. %s "
158 "will be omitted from list."),
159 (*v)[0]->name, add->name, add->name);
160 else if ((pv_opts & PV_NO_DUPLICATE) && included[idx])
161 msg (SE, _("Variable %s appears twice in variable list."), add->name);
167 *v = xnrealloc (*v, *mv, sizeof **v);
170 if ((pv_opts & PV_DUPLICATE) || !included[idx])
173 if (!(pv_opts & PV_DUPLICATE))
179 /* Adds the variables in VS with indexes FIRST_IDX through
180 LAST_IDX, inclusive, to the list of variables V that has *NV
181 elements and room for *MV. Uses and updates INCLUDED to avoid
182 duplicates if indicated by PV_OPTS, which also affects what
183 variables are allowed in appropriate ways. */
185 add_variables (struct variable ***v, size_t *nv, size_t *mv, char *included,
187 const struct var_set *vs, int first_idx, int last_idx,
188 enum dict_class class)
192 for (i = first_idx; i <= last_idx; i++)
193 if (dict_class_from_id (var_set_get_var (vs, i)->name) == class)
194 add_variable (v, nv, mv, included, pv_opts, vs, i);
197 /* Note that if parse_variables() returns 0, *v is free()'d.
198 Conversely, if parse_variables() returns non-zero, then *nv is
199 nonzero and *v is non-NULL. */
201 parse_var_set_vars (const struct var_set *vs,
202 struct variable ***v, size_t *nv,
212 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
214 assert ((((pv_opts & PV_NUMERIC) != 0)
215 + ((pv_opts & PV_STRING) != 0)
216 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
218 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
219 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
221 if (!(pv_opts & PV_APPEND))
230 if (!(pv_opts & PV_DUPLICATE))
234 included = xcalloc (var_set_get_cnt (vs), sizeof *included);
235 for (i = 0; i < *nv; i++)
236 included[(*v)[i]->index] = 1;
241 if (lex_match (T_ALL))
242 add_variables (v, nv, &mv, included, pv_opts,
243 vs, 0, var_set_get_cnt (vs) - 1, DC_ORDINARY);
248 enum dict_class class;
251 if (!parse_var_idx_class (vs, &first_idx, &class))
254 if (!lex_match (T_TO))
255 add_variable (v, nv, &mv, included, pv_opts, vs, first_idx);
259 enum dict_class last_class;
260 struct variable *first_var, *last_var;
262 if (!parse_var_idx_class (vs, &last_idx, &last_class))
265 first_var = var_set_get_var (vs, first_idx);
266 last_var = var_set_get_var (vs, last_idx);
268 if (last_idx < first_idx)
270 msg (SE, _("%s TO %s is not valid syntax since %s "
271 "precedes %s in the dictionary."),
272 first_var->name, last_var->name,
273 first_var->name, last_var->name);
277 if (class != last_class)
279 msg (SE, _("When using the TO keyword to specify several "
280 "variables, both variables must be from "
281 "the same variable dictionaries, of either "
282 "ordinary, scratch, or system variables. "
283 "%s is a %s variable, whereas %s is %s."),
284 first_var->name, dict_class_to_name (class),
285 last_var->name, dict_class_to_name (last_class));
289 add_variables (v, nv, &mv, included, pv_opts,
290 vs, first_idx, last_idx, class);
292 if (pv_opts & PV_SINGLE)
296 while (token == T_ID && var_set_lookup_var (vs, tokid) != NULL);
313 /* Extracts a numeric suffix from variable name S, copying it
314 into string R. Sets *D to the length of R and *N to its
317 extract_num (char *s, char *r, int *n, int *d)
321 /* Find first digit. */
322 cp = s + strlen (s) - 1;
323 while (isdigit ((unsigned char) *cp) && cp > s)
328 strncpy (r, s, cp - s);
331 /* Count initial zeros. */
340 while (isdigit ((unsigned char) *cp))
343 *n = (*n * 10) + (*cp - '0');
348 if (*n == 0 && *d == 0)
350 msg (SE, _("incorrect use of TO convention"));
356 /* Parses a list of variable names according to the DATA LIST version
357 of the TO convention. */
359 parse_DATA_LIST_vars (char ***names, size_t *nnames, int pv_opts)
365 char name1[LONG_NAME_LEN + 1], name2[LONG_NAME_LEN + 1];
366 char root1[LONG_NAME_LEN + 1], root2[LONG_NAME_LEN + 1];
369 assert (names != NULL);
370 assert (nnames != NULL);
371 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
372 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
373 /* FIXME: PV_NO_DUPLICATE is not implemented. */
375 if (pv_opts & PV_APPEND)
376 nvar = mvar = *nnames;
387 lex_error ("expecting variable name");
390 if (dict_class_from_id (tokid) == DC_SCRATCH
391 && (pv_opts & PV_NO_SCRATCH))
393 msg (SE, _("Scratch variables not allowed here."));
396 strcpy (name1, tokid);
403 lex_error ("expecting variable name");
406 strcpy (name2, tokid);
409 if (!extract_num (name1, root1, &n1, &d1)
410 || !extract_num (name2, root2, &n2, &d2))
413 if (strcasecmp (root1, root2))
415 msg (SE, _("Prefixes don't match in use of TO convention."));
420 msg (SE, _("Bad bounds in use of TO convention."));
426 if (mvar < nvar + (n2 - n1 + 1))
428 mvar += ROUND_UP (n2 - n1 + 1, 16);
429 *names = xnrealloc (*names, mvar, sizeof **names);
432 for (n = n1; n <= n2; n++)
434 char name[LONG_NAME_LEN + 1];
435 sprintf (name, "%s%0*d", root1, d1, n);
436 (*names)[nvar] = xstrdup (name);
445 *names = xnrealloc (*names, mvar, sizeof **names);
447 (*names)[nvar++] = xstrdup (name1);
452 if (pv_opts & PV_SINGLE)
455 while (token == T_ID);
463 for (i = 0; i < nvar; i++)
472 /* Parses a list of variables where some of the variables may be
473 existing and the rest are to be created. Same args as
474 parse_DATA_LIST_vars(). */
476 parse_mixed_vars (char ***names, size_t *nnames, int pv_opts)
480 assert (names != NULL);
481 assert (nnames != NULL);
482 assert ((pv_opts & ~PV_APPEND) == 0);
484 if (!(pv_opts & PV_APPEND))
489 while (token == T_ID || token == T_ALL)
491 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
496 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
498 *names = xnrealloc (*names, *nnames + nv, sizeof **names);
499 for (i = 0; i < nv; i++)
500 (*names)[*nnames + i] = xstrdup (v[i]->name);
504 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
510 for (i = 0; i < *nnames; i++)
511 free ((*names)[*nnames]);
518 /* A set of variables. */
521 size_t (*get_cnt) (const struct var_set *);
522 struct variable *(*get_var) (const struct var_set *, size_t idx);
523 bool (*lookup_var_idx) (const struct var_set *, const char *, size_t *);
524 void (*destroy) (struct var_set *);
528 /* Returns the number of variables in VS. */
530 var_set_get_cnt (const struct var_set *vs)
534 return vs->get_cnt (vs);
537 /* Return variable with index IDX in VS.
538 IDX must be less than the number of variables in VS. */
540 var_set_get_var (const struct var_set *vs, size_t idx)
543 assert (idx < var_set_get_cnt (vs));
545 return vs->get_var (vs, idx);
548 /* Returns the variable in VS named NAME, or a null pointer if VS
549 contains no variable with that name. */
551 var_set_lookup_var (const struct var_set *vs, const char *name)
554 return (var_set_lookup_var_idx (vs, name, &idx)
555 ? var_set_get_var (vs, idx)
559 /* If VS contains a variable named NAME, sets *IDX to its index
560 and returns true. Otherwise, returns false. */
562 var_set_lookup_var_idx (const struct var_set *vs, const char *name,
566 assert (name != NULL);
567 assert (strlen (name) <= LONG_NAME_LEN);
569 return vs->lookup_var_idx (vs, name, idx);
574 var_set_destroy (struct var_set *vs)
580 /* Returns the number of variables in VS. */
582 dict_var_set_get_cnt (const struct var_set *vs)
584 struct dictionary *d = vs->aux;
586 return dict_get_var_cnt (d);
589 /* Return variable with index IDX in VS.
590 IDX must be less than the number of variables in VS. */
591 static struct variable *
592 dict_var_set_get_var (const struct var_set *vs, size_t idx)
594 struct dictionary *d = vs->aux;
596 return dict_get_var (d, idx);
599 /* If VS contains a variable named NAME, sets *IDX to its index
600 and returns true. Otherwise, returns false. */
602 dict_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
605 struct dictionary *d = vs->aux;
606 struct variable *v = dict_lookup_var (d, name);
618 dict_var_set_destroy (struct var_set *vs)
623 /* Returns a variable set based on D. */
625 var_set_create_from_dict (const struct dictionary *d)
627 struct var_set *vs = xmalloc (sizeof *vs);
628 vs->get_cnt = dict_var_set_get_cnt;
629 vs->get_var = dict_var_set_get_var;
630 vs->lookup_var_idx = dict_var_set_lookup_var_idx;
631 vs->destroy = dict_var_set_destroy;
632 vs->aux = (void *) d;
636 /* A variable set based on an array. */
639 struct variable *const *var;/* Array of variables. */
640 size_t var_cnt; /* Number of elements in var. */
641 struct hsh_table *name_tab; /* Hash from variable names to variables. */
644 /* Returns the number of variables in VS. */
646 array_var_set_get_cnt (const struct var_set *vs)
648 struct array_var_set *avs = vs->aux;
653 /* Return variable with index IDX in VS.
654 IDX must be less than the number of variables in VS. */
655 static struct variable *
656 array_var_set_get_var (const struct var_set *vs, size_t idx)
658 struct array_var_set *avs = vs->aux;
660 return (struct variable *) avs->var[idx];
663 /* If VS contains a variable named NAME, sets *IDX to its index
664 and returns true. Otherwise, returns false. */
666 array_var_set_lookup_var_idx (const struct var_set *vs, const char *name,
669 struct array_var_set *avs = vs->aux;
670 struct variable v, *vp, *const *vpp;
672 strcpy (v.name, name);
674 vpp = hsh_find (avs->name_tab, &vp);
677 *idx = vpp - avs->var;
686 array_var_set_destroy (struct var_set *vs)
688 struct array_var_set *avs = vs->aux;
690 hsh_destroy (avs->name_tab);
695 /* Returns a variable set based on the VAR_CNT variables in
698 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
701 struct array_var_set *avs;
704 vs = xmalloc (sizeof *vs);
705 vs->get_cnt = array_var_set_get_cnt;
706 vs->get_var = array_var_set_get_var;
707 vs->lookup_var_idx = array_var_set_lookup_var_idx;
708 vs->destroy = array_var_set_destroy;
709 vs->aux = avs = xmalloc (sizeof *avs);
711 avs->var_cnt = var_cnt;
712 avs->name_tab = hsh_create (2 * var_cnt,
713 compare_var_ptr_names, hash_var_ptr_name, NULL,
715 for (i = 0; i < var_cnt; i++)
716 if (hsh_insert (avs->name_tab, (void *) &var[i]) != NULL)
718 var_set_destroy (vs);