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
26 #include "bitvector.h"
33 static struct variable *
34 parse_vs_variable (struct var_set *vs)
40 lex_error ("expecting variable name");
44 vp = var_set_lookup_var (vs, tokid);
46 msg (SE, _("%s is not a variable name."), tokid);
53 parse_dict_variable (struct dictionary *d)
55 struct var_set *vs = var_set_create_from_dict (d);
56 struct variable *var = parse_vs_variable (vs);
64 return parse_dict_variable (default_dict);
69 dict_class_from_id (const char *name)
71 assert (name != NULL);
85 dict_class_to_name (enum dict_class dict_class)
101 parse_variables (struct dictionary *d, struct variable ***var, int *cnt,
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);
113 var_set_destroy (vs);
117 /* Note that if parse_variables() returns 0, *v is free()'d.
118 Conversely, if parse_variables() returns non-zero, then *nv is
119 nonzero and *v is non-NULL. */
121 parse_var_set_vars (struct var_set *vs,
122 struct variable ***v, int *nv,
127 char *included = NULL;
129 struct variable *v1, *v2;
131 enum dict_class dict_class;
137 /* At most one of PV_NUMERIC, PV_STRING, PV_SAME_TYPE may be
139 assert ((((pv_opts & PV_NUMERIC) != 0)
140 + ((pv_opts & PV_STRING) != 0)
141 + ((pv_opts & PV_SAME_TYPE) != 0)) <= 1);
143 /* PV_DUPLICATE and PV_NO_DUPLICATE are incompatible. */
144 assert (!(pv_opts & PV_DUPLICATE) || !(pv_opts & PV_NO_DUPLICATE));
146 vs_var_cnt = var_set_get_cnt (vs);
148 if (!(pv_opts & PV_APPEND))
157 if (!(pv_opts & PV_DUPLICATE))
159 included = xmalloc (vs_var_cnt);
160 memset (included, 0, vs_var_cnt);
161 for (i = 0; i < *nv; i++)
162 included[(*v)[i]->index] = 1;
167 if (lex_match (T_ALL))
169 v1 = var_set_get_var (vs, 0);
170 v2 = var_set_get_var (vs, vs_var_cnt - 1);
172 dict_class = DC_ORDINARY;
176 v1 = parse_vs_variable (vs);
180 if (lex_match (T_TO))
182 enum dict_class dict_class_2;
184 v2 = parse_vs_variable (vs);
187 lex_error ("expecting variable name");
191 count = v2->index - v1->index + 1;
194 msg (SE, _("%s TO %s is not valid syntax since %s "
195 "precedes %s in the dictionary."),
196 v1->name, v2->name, v2->name, v1->name);
200 dict_class = dict_class_from_id (v1->name);
201 dict_class_2 = dict_class_from_id (v2->name);
202 if (dict_class != dict_class_2)
204 msg (SE, _("When using the TO keyword to specify several "
205 "variables, both variables must be from "
206 "the same variable dictionaries, of either "
207 "ordinary, scratch, or system variables. "
208 "%s is a %s variable, whereas %s is %s."),
209 v1->name, dict_class_to_name (dict_class),
210 v2->name, dict_class_to_name (dict_class_2));
218 dict_class = dict_class_from_id (v1->name);
220 if (dict_class == DC_SCRATCH && (pv_opts & PV_NO_SCRATCH))
222 msg (SE, _("Scratch variables (such as %s) are not allowed "
228 if (*nv + count > mv)
230 mv += ROUND_UP (count, 16);
231 *v = xrealloc (*v, mv * sizeof **v);
234 /* Add v1...v2 to the list. */
235 for (i = v1->index; i <= v2->index; i++)
237 struct variable *add = var_set_get_var (vs, i);
239 /* Skip over other dictionaries. */
240 if (dict_class != dict_class_from_id (add->name))
243 /* Different kinds of errors. */
244 if ((pv_opts & PV_NUMERIC) && add->type != NUMERIC)
245 msg (SW, _("%s is not a numeric variable. It will not be "
246 "included in the variable list."), add->name);
247 else if ((pv_opts & PV_STRING) && add->type != ALPHA)
248 msg (SE, _("%s is not a string variable. It will not be "
249 "included in the variable list."), add->name);
250 else if ((pv_opts & PV_SAME_TYPE) && *nv
251 && add->type != (*v)[0]->type)
252 msg (SE, _("%s and %s are not the same type. All variables in "
253 "this variable list must be of the same type. %s "
254 "will be omitted from list."),
255 (*v)[0]->name, add->name, add->name);
256 else if ((pv_opts & PV_NO_DUPLICATE) && included[add->index])
257 msg (SE, _("Variable %s appears twice in variable list."),
260 /* Success--add the variable to the list. */
261 if ((pv_opts & PV_DUPLICATE) || !included[add->index])
263 (*v)[(*nv)++] = var_set_get_var (vs, i);
264 if (!(pv_opts & PV_DUPLICATE))
265 included[add->index] = 1;
272 /* Arrive here only on failure. */
273 if (pv_opts & PV_SINGLE)
277 /* We finished adding v1...v2 to the list. */
278 if (pv_opts & PV_SINGLE)
282 while ((token == T_ID && var_set_lookup_var (vs, tokid) != NULL)
285 if (!(pv_opts & PV_DUPLICATE))
295 if (!(pv_opts & PV_DUPLICATE))
301 extract_num (char *s, char *r, int *n, int *d)
305 /* Find first digit. */
306 cp = s + strlen (s) - 1;
307 while (isdigit ((unsigned char) *cp) && cp > s)
312 strncpy (r, s, cp - s);
315 /* Count initial zeros. */
324 while (isdigit ((unsigned char) *cp))
327 *n = (*n * 10) + (*cp - '0');
332 if (*n == 0 && *d == 0)
334 msg (SE, _("incorrect use of TO convention"));
340 /* Parses a list of variable names according to the DATA LIST version
341 of the TO convention. */
343 parse_DATA_LIST_vars (char ***names, int *nnames, int pv_opts)
353 assert (names != NULL);
354 assert (nnames != NULL);
355 assert ((pv_opts & ~(PV_APPEND | PV_SINGLE
356 | PV_NO_SCRATCH | PV_NO_DUPLICATE)) == 0);
357 /* FIXME: PV_NO_DUPLICATE is not implemented. */
359 if (pv_opts & PV_APPEND)
360 nvar = mvar = *nnames;
367 name1 = xmalloc (36);
368 name2 = &name1[1 * 9];
369 root1 = &name1[2 * 9];
370 root2 = &name1[3 * 9];
375 lex_error ("expecting variable name");
378 if (tokid[0] == '#' && (pv_opts & PV_NO_SCRATCH))
380 msg (SE, _("Scratch variables not allowed here."));
383 strcpy (name1, tokid);
390 lex_error ("expecting variable name");
393 strcpy (name2, tokid);
396 if (!extract_num (name1, root1, &n1, &d1)
397 || !extract_num (name2, root2, &n2, &d2))
400 if (strcmp (root1, root2))
402 msg (SE, _("Prefixes don't match in use of TO convention."));
407 msg (SE, _("Bad bounds in use of TO convention."));
413 if (mvar < nvar + (n2 - n1 + 1))
415 mvar += ROUND_UP (n2 - n1 + 1, 16);
416 *names = xrealloc (*names, mvar * sizeof **names);
419 for (n = n1; n <= n2; n++)
421 (*names)[nvar] = xmalloc (9);
422 sprintf ((*names)[nvar], "%s%0*d", root1, d1, n);
431 *names = xrealloc (*names, mvar * sizeof **names);
433 (*names)[nvar++] = xstrdup (name1);
438 if (pv_opts & PV_SINGLE)
441 while (token == T_ID);
450 for (i = 0; i < nvar; i++)
459 /* Parses a list of variables where some of the variables may be
460 existing and the rest are to be created. Same args as
461 parse_DATA_LIST_vars(). */
463 parse_mixed_vars (char ***names, int *nnames, int pv_opts)
467 assert (names != NULL);
468 assert (nnames != NULL);
469 assert ((pv_opts & ~PV_APPEND) == 0);
471 if (!(pv_opts & PV_APPEND))
476 while (token == T_ID || token == T_ALL)
478 if (token == T_ALL || dict_lookup_var (default_dict, tokid) != NULL)
483 if (!parse_variables (default_dict, &v, &nv, PV_NONE))
485 *names = xrealloc (*names, (*nnames + nv) * sizeof **names);
486 for (i = 0; i < nv; i++)
487 (*names)[*nnames + i] = xstrdup (v[i]->name);
491 else if (!parse_DATA_LIST_vars (names, nnames, PV_APPEND))
497 for (i = 0; i < *nnames; i++)
498 free ((*names)[*nnames]);
507 size_t (*get_cnt) (struct var_set *);
508 struct variable *(*get_var) (struct var_set *, size_t idx);
509 struct variable *(*lookup_var) (struct var_set *, const char *);
510 void (*destroy) (struct var_set *);
515 var_set_get_cnt (struct var_set *vs)
519 return vs->get_cnt (vs);
523 var_set_get_var (struct var_set *vs, size_t idx)
526 assert (idx < var_set_get_cnt (vs));
528 return vs->get_var (vs, idx);
532 var_set_lookup_var (struct var_set *vs, const char *name)
535 assert (name != NULL);
536 assert (strlen (name) < 9);
538 return vs->lookup_var (vs, name);
542 var_set_destroy (struct var_set *vs)
549 dict_var_set_get_cnt (struct var_set *vs)
551 struct dictionary *d = vs->aux;
553 return dict_get_var_cnt (d);
556 static struct variable *
557 dict_var_set_get_var (struct var_set *vs, size_t idx)
559 struct dictionary *d = vs->aux;
561 return dict_get_var (d, idx);
564 static struct variable *
565 dict_var_set_lookup_var (struct var_set *vs, const char *name)
567 struct dictionary *d = vs->aux;
569 return dict_lookup_var (d, name);
573 dict_var_set_destroy (struct var_set *vs)
579 var_set_create_from_dict (struct dictionary *d)
581 struct var_set *vs = xmalloc (sizeof *vs);
582 vs->get_cnt = dict_var_set_get_cnt;
583 vs->get_var = dict_var_set_get_var;
584 vs->lookup_var = dict_var_set_lookup_var;
585 vs->destroy = dict_var_set_destroy;
592 struct variable **var;
594 struct hsh_table *name_tab;
598 array_var_set_get_cnt (struct var_set *vs)
600 struct array_var_set *avs = vs->aux;
605 static struct variable *
606 array_var_set_get_var (struct var_set *vs, size_t idx)
608 struct array_var_set *avs = vs->aux;
610 return avs->var[idx];
613 static struct variable *
614 array_var_set_lookup_var (struct var_set *vs, const char *name)
616 struct array_var_set *avs = vs->aux;
619 strcpy (v.name, name);
621 return hsh_find (avs->name_tab, &v);
625 array_var_set_destroy (struct var_set *vs)
627 struct array_var_set *avs = vs->aux;
629 hsh_destroy (avs->name_tab);
635 var_set_create_from_array (struct variable **var, size_t var_cnt)
638 struct array_var_set *avs;
641 vs = xmalloc (sizeof *vs);
642 vs->get_cnt = array_var_set_get_cnt;
643 vs->get_var = array_var_set_get_var;
644 vs->lookup_var = array_var_set_lookup_var;
645 vs->destroy = array_var_set_destroy;
646 vs->aux = avs = xmalloc (sizeof *avs);
648 avs->var_cnt = var_cnt;
649 avs->name_tab = hsh_create (2 * var_cnt,
650 compare_variables, hash_variable, NULL,
652 for (i = 0; i < var_cnt; i++)
653 if (hsh_insert (avs->name_tab, var[i]) != NULL)
655 var_set_destroy (vs);