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., 59 Temple Place - Suite 330, Boston, MA
25 #include "bitvector.h"
26 #include "dictionary.h"
33 /* Parses a name as a variable within VS and returns the variable
34 if successful. On failure emits an error message and returns
36 static struct variable *
37 parse_vs_variable (const struct var_set *vs)
43 lex_error ("expecting variable name");
47 vp = var_set_lookup_var (vs, tokid);
49 msg (SE, _("%s is not a variable name."), tokid);
55 /* Parses a variable name in dictionary D and returns the
56 variable if successful. On failure emits an error message and
57 returns a null pointer. */
59 parse_dict_variable (const struct dictionary *d)
61 struct var_set *vs = var_set_create_from_dict (d);
62 struct variable *var = parse_vs_variable (vs);
67 /* Parses a variable name in default_dict and returns the
68 variable if successful. On failure emits an error message and
69 returns a null pointer. */
73 return parse_dict_variable (default_dict);
76 /* Returns the dictionary class corresponding to a variable named
79 dict_class_from_id (const char *name)
81 assert (name != NULL);
94 /* Returns the name of dictionary class DICT_CLASS. */
96 dict_class_to_name (enum dict_class dict_class)
101 return _("ordinary");
112 /* Parses a set of variables from dictionary D given options
113 OPTS. Resulting list of variables stored in *VAR and the
114 number of variables into *CNT. Returns nonzero only if
117 parse_variables (const struct dictionary *d, struct variable ***var,
124 assert (var != NULL);
125 assert (cnt != NULL);
127 vs = var_set_create_from_dict (d);
128 success = parse_var_set_vars (vs, var, cnt, opts);
131 var_set_destroy (vs);
135 /* Note that if parse_variables() returns 0, *v is free()'d.
136 Conversely, if parse_variables() returns non-zero, then *nv is
137 nonzero and *v is non-NULL. */
139 parse_var_set_vars (const struct var_set *vs,
140 struct variable ***v, int *nv,
145 char *included = NULL;
147 struct variable *v1, *v2;
149 enum dict_class dict_class;
155 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
157 assert ((((pv_opts & PV_NUMERIC) != 0)
158 + ((pv_opts & PV_STRING) != 0)
159 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
161 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
162 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
164 vs_var_cnt = var_set_get_cnt (vs);
166 if (!(pv_opts & PV_APPEND))
175 if (!(pv_opts & PV_DUPLICATE))
177 included = xmalloc (vs_var_cnt);
178 memset (included, 0, vs_var_cnt);
179 for (i = 0; i < *nv; i++)
180 included[(*v)[i]->index] = 1;
185 if (lex_match (T_ALL))
187 v1 = var_set_get_var (vs, 0);
188 v2 = var_set_get_var (vs, vs_var_cnt - 1);
190 dict_class = DC_ORDINARY;
194 v1 = parse_vs_variable (vs);
198 if (lex_match (T_TO))
200 enum dict_class dict_class_2;
202 v2 = parse_vs_variable (vs);
205 lex_error ("expecting variable name");
209 count = v2->index - v1->index + 1;
212 msg (SE, _("%s TO %s is not valid syntax since %s "
213 "precedes %s in the dictionary."),
214 v1->name, v2->name, v2->name, v1->name);
218 dict_class = dict_class_from_id (v1->name);
219 dict_class_2 = dict_class_from_id (v2->name);
220 if (dict_class != dict_class_2)
222 msg (SE, _("When using the TO keyword to specify several "
223 "variables, both variables must be from "
224 "the same variable dictionaries, of either "
225 "ordinary, scratch, or system variables. "
226 "%s is a %s variable, whereas %s is %s."),
227 v1->name, dict_class_to_name (dict_class),
228 v2->name, dict_class_to_name (dict_class_2));
236 dict_class = dict_class_from_id (v1->name);
238 if (dict_class == DC_SCRATCH && (pv_opts & PV_NO_SCRATCH))
240 msg (SE, _("Scratch variables (such as %s) are not allowed "
246 if (*nv + count > mv)
248 mv += ROUND_UP (count, 16);
249 *v = xrealloc (*v, mv * sizeof **v);
252 /* Add v1...v2 to the list. */
253 for (i = v1->index; i <= v2->index; i++)
255 struct variable *add = var_set_get_var (vs, i);
257 /* Skip over other dictionaries. */
258 if (dict_class != dict_class_from_id (add->name))
261 /* Different kinds of errors. */
262 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
263 msg (SW, _("%s is not a numeric variable. It will not be "
264 "included in the variable list."), add->name);
265 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
266 msg (SE, _("%s is not a string variable. It will not be "
267 "included in the variable list."), add->name);
268 else if ((pv_opts & PV_SAME_TYPE) && *nv
269 && add->type != (*v)[0]->type)
270 msg (SE, _("%s and %s are not the same type. All variables in "
271 "this variable list must be of the same type. %s "
272 "will be omitted from list."),
273 (*v)[0]->name, add->name, add->name);
274 else if ((pv_opts & PV_NO_DUPLICATE) && included[add->index])
275 msg (SE, _("Variable %s appears twice in variable list."),
278 /* Success--add the variable to the list. */
279 if ((pv_opts & PV_DUPLICATE) || !included[add->index])
281 (*v)[(*nv)++] = var_set_get_var (vs, i);
282 if (!(pv_opts & PV_DUPLICATE))
283 included[add->index] = 1;
290 /* Arrive here only on failure. */
291 if (pv_opts & PV_SINGLE)
295 /* We finished adding v1...v2 to the list. */
296 if (pv_opts & PV_SINGLE)
300 while ((token == T_ID && var_set_lookup_var (vs, tokid) != NULL)
303 if (!(pv_opts & PV_DUPLICATE))
313 if (!(pv_opts & PV_DUPLICATE))
318 /* Extracts a numeric suffix from variable name S, copying it
319 into string R. Sets *D to the length of R and *N to its
322 extract_num (char *s, char *r, int *n, int *d)
326 /* Find first digit. */
327 cp = s + strlen (s) - 1;
328 while (isdigit ((unsigned char) *cp) && cp > s)
333 strncpy (r, s, cp - s);
336 /* Count initial zeros. */
345 while (isdigit ((unsigned char) *cp))
348 *n = (*n * 10) + (*cp - '0');
353 if (*n == 0 && *d == 0)
355 msg (SE, _("incorrect use of TO convention"));
361 /* Parses a list of variable names according to the DATA LIST version
362 of the TO convention. */
364 parse_DATA_LIST_vars (char ***names, int *nnames, int pv_opts)
374 assert (names != NULL);
375 assert (nnames != NULL);
376 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
377 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
378 /* FIXME: PV_NO_DUPLICATE is not implemented. */
380 if (pv_opts & PV_APPEND)
381 nvar = mvar = *nnames;
388 name1 = xmalloc (36);
389 name2 = &name1[1 * 9];
390 root1 = &name1[2 * 9];
391 root2 = &name1[3 * 9];
396 lex_error ("expecting variable name");
399 if (dict_class_from_id (tokid) == DC_SCRATCH
400 && (pv_opts & PV_NO_SCRATCH))
402 msg (SE, _("Scratch variables not allowed here."));
405 strcpy (name1, tokid);
412 lex_error ("expecting variable name");
415 strcpy (name2, tokid);
418 if (!extract_num (name1, root1, &n1, &d1)
419 || !extract_num (name2, root2, &n2, &d2))
422 if (strcmp (root1, root2))
424 msg (SE, _("Prefixes don't match in use of TO convention."));
429 msg (SE, _("Bad bounds in use of TO convention."));
435 if (mvar < nvar + (n2 - n1 + 1))
437 mvar += ROUND_UP (n2 - n1 + 1, 16);
438 *names = xrealloc (*names, mvar * sizeof **names);
441 for (n = n1; n <= n2; n++)
443 (*names)[nvar] = xmalloc (9);
444 sprintf ((*names)[nvar], "%s%0*d", root1, d1, n);
453 *names = xrealloc (*names, mvar * sizeof **names);
455 (*names)[nvar++] = xstrdup (name1);
460 if (pv_opts & PV_SINGLE)
463 while (token == T_ID);
472 for (i = 0; i < nvar; i++)
481 /* Parses a list of variables where some of the variables may be
482 existing and the rest are to be created. Same args as
483 parse_DATA_LIST_vars(). */
485 parse_mixed_vars (char ***names, int *nnames, int pv_opts)
489 assert (names != NULL);
490 assert (nnames != NULL);
491 assert ((pv_opts & ~PV_APPEND) == 0);
493 if (!(pv_opts & PV_APPEND))
498 while (token == T_ID || token == T_ALL)
500 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
505 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
507 *names = xrealloc (*names, (*nnames + nv) * sizeof **names);
508 for (i = 0; i < nv; i++)
509 (*names)[*nnames + i] = xstrdup (v[i]->name);
513 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
519 for (i = 0; i < *nnames; i++)
520 free ((*names)[*nnames]);
527 /* A set of variables. */
530 size_t (*get_cnt) (const struct var_set *);
531 struct variable *(*get_var) (const struct var_set *, size_t idx);
532 struct variable *(*lookup_var) (const struct var_set *, const char *);
533 void (*destroy) (struct var_set *);
537 /* Returns the number of variables in VS. */
539 var_set_get_cnt (const struct var_set *vs)
543 return vs->get_cnt (vs);
546 /* Return variable with index IDX in VS.
547 IDX must be less than the number of variables in VS. */
549 var_set_get_var (const struct var_set *vs, size_t idx)
552 assert (idx < var_set_get_cnt (vs));
554 return vs->get_var (vs, idx);
557 /* Returns the variable in VS named NAME, or a null pointer if VS
558 contains no variable with that name. */
560 var_set_lookup_var (const struct var_set *vs, const char *name)
563 assert (name != NULL);
564 assert (strlen (name) < 9);
566 return vs->lookup_var (vs, name);
571 var_set_destroy (struct var_set *vs)
577 /* Returns the number of variables in VS. */
579 dict_var_set_get_cnt (const struct var_set *vs)
581 struct dictionary *d = vs->aux;
583 return dict_get_var_cnt (d);
586 /* Return variable with index IDX in VS.
587 IDX must be less than the number of variables in VS. */
588 static struct variable *
589 dict_var_set_get_var (const struct var_set *vs, size_t idx)
591 struct dictionary *d = vs->aux;
593 return dict_get_var (d, idx);
596 /* Returns the variable in VS named NAME, or a null pointer if VS
597 contains no variable with that name. */
598 static struct variable *
599 dict_var_set_lookup_var (const struct var_set *vs, const char *name)
601 struct dictionary *d = vs->aux;
603 return dict_lookup_var (d, name);
608 dict_var_set_destroy (struct var_set *vs)
613 /* Returns a variable set based on D. */
615 var_set_create_from_dict (const struct dictionary *d)
617 struct var_set *vs = xmalloc (sizeof *vs);
618 vs->get_cnt = dict_var_set_get_cnt;
619 vs->get_var = dict_var_set_get_var;
620 vs->lookup_var = dict_var_set_lookup_var;
621 vs->destroy = dict_var_set_destroy;
622 vs->aux = (void *) d;
626 /* A variable set based on an array. */
629 struct variable *const *var;/* Array of variables. */
630 size_t var_cnt; /* Number of elements in var. */
631 struct hsh_table *name_tab; /* Hash from variable names to variables. */
634 /* Returns the number of variables in VS. */
636 array_var_set_get_cnt (const struct var_set *vs)
638 struct array_var_set *avs = vs->aux;
643 /* Return variable with index IDX in VS.
644 IDX must be less than the number of variables in VS. */
645 static struct variable *
646 array_var_set_get_var (const struct var_set *vs, size_t idx)
648 struct array_var_set *avs = vs->aux;
650 return (struct variable *) avs->var[idx];
653 /* Returns the variable in VS named NAME, or a null pointer if VS
654 contains no variable with that name. */
655 static struct variable *
656 array_var_set_lookup_var (const struct var_set *vs, const char *name)
658 struct array_var_set *avs = vs->aux;
661 strcpy (v.name, name);
663 return hsh_find (avs->name_tab, &v);
668 array_var_set_destroy (struct var_set *vs)
670 struct array_var_set *avs = vs->aux;
672 hsh_destroy (avs->name_tab);
677 /* Returns a variable set based on the VAR_CNT variables in
680 var_set_create_from_array (struct variable *const *var, size_t var_cnt)
683 struct array_var_set *avs;
686 vs = xmalloc (sizeof *vs);
687 vs->get_cnt = array_var_set_get_cnt;
688 vs->get_var = array_var_set_get_var;
689 vs->lookup_var = array_var_set_lookup_var;
690 vs->destroy = array_var_set_destroy;
691 vs->aux = avs = xmalloc (sizeof *avs);
693 avs->var_cnt = var_cnt;
694 avs->name_tab = hsh_create (2 * var_cnt,
695 compare_variables, hash_variable, NULL,
697 for (i = 0; i < var_cnt; i++)
698 if (hsh_insert (avs->name_tab, (void *) var[i]) != NULL)
700 var_set_destroy (vs);