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
34 /*#define DEBUGGING 1*/
35 #include "debug-print.h"
38 struct dictionary *temp_dict;
42 /* Displays all the value labels in TREE, with label S. */
44 display_tree (char *s, avl_tree *tree)
47 avl_traverser *trav = NULL;
49 printf("%s tree:\n", s);
51 while ((iter = avl_traverse (tree, &trav)) != NULL)
52 printf (" %g: %s\n", iter->v.f, iter->s);
56 /* Parses the TEMPORARY command. */
60 lex_match_id ("TEMPORARY");
62 /* TEMPORARY is not allowed inside DO IF or LOOP. */
65 msg (SE, _("This command is not valid inside DO IF or LOOP."));
69 /* TEMPORARY can only appear once! */
72 msg (SE, _("This command may only appear once between "
73 "procedures and procedure-like commands."));
77 /* Everything is temporary, even if we think it'll last forever.
80 temp_dict = save_dictionary ();
85 debug_printf (("TEMPORARY: temp_trns=%d\n", temp_trns));
87 return lex_end_of_command ();
90 /* Copies a variable structure. */
92 copy_variable (struct variable *dest, const struct variable *src)
97 dest->type = src->type;
98 dest->left = src->left;
99 dest->width = src->width;
102 dest->miss_type = src->miss_type;
104 switch (src->miss_type)
117 case MISSING_RANGE_1:
125 for (i = 0; i < n; i++)
126 dest->missing[i] = src->missing[i];
127 dest->print = src->print;
128 dest->write = src->write;
130 dest->val_lab = copy_value_labels (src->val_lab);
131 dest->label = src->label ? xstrdup (src->label) : NULL;
134 /* Returns a newly created empty dictionary. The file label and
135 documents are copied from default_dict if COPY is nonzero. */
137 new_dictionary (int copy)
139 struct dictionary *d = xmalloc (sizeof *d);
142 d->var_by_name = avl_create (NULL, cmp_variable, NULL);
152 if (default_dict.label && copy)
153 d->label = xstrdup (default_dict.label);
157 if (default_dict.n_documents && copy)
159 d->n_documents = default_dict.n_documents;
162 d->documents = malloc (default_dict.n_documents * 80);
163 memcpy (d->documents, default_dict.documents,
164 default_dict.n_documents * 80);
173 d->weight_index = -1;
174 d->weight_var[0] = 0;
176 d->filter_var[0] = 0;
181 /* Copies the current dictionary info into a newly allocated
182 dictionary structure, which is returned. */
184 save_dictionary (void)
186 /* Dictionary being created. */
187 struct dictionary *d;
191 d = xmalloc (sizeof *d);
193 /* First the easy stuff. */
195 d->label = default_dict.label ? xstrdup (default_dict.label) : NULL;
196 if (default_dict.n_documents)
198 d->documents = malloc (default_dict.n_documents * 80);
199 memcpy (d->documents, default_dict.documents,
200 default_dict.n_documents * 80);
202 else d->documents = NULL;
204 /* Then the variables. */
205 d->var_by_name = avl_create (NULL, cmp_variable, NULL);
206 d->var = xmalloc (default_dict.nvar * sizeof *d->var);
207 for (i = 0; i < default_dict.nvar; i++)
209 d->var[i] = xmalloc (sizeof *d->var[i]);
210 copy_variable (d->var[i], default_dict.var[i]);
211 strcpy (d->var[i]->name, default_dict.var[i]->name);
212 d->var[i]->index = i;
213 avl_force_insert (d->var_by_name, d->var[i]);
216 /* Then the SPLIT FILE variables. */
217 if (default_dict.splits)
221 d->n_splits = default_dict.n_splits;
222 d->splits = xmalloc ((default_dict.n_splits + 1) * sizeof *d->splits);
223 for (i = 0; i < default_dict.n_splits; i++)
224 d->splits[i] = d->var[default_dict.splits[i]->index];
225 d->splits[default_dict.n_splits] = NULL;
236 /* Copies dictionary D into the active file dictionary. Deletes
239 restore_dictionary (struct dictionary * d)
243 /* 1. Delete the current dictionary. */
244 default_dict.n_splits = 0;
245 free (default_dict.splits);
246 default_dict.splits = NULL;
248 avl_destroy (default_dict.var_by_name, NULL);
249 default_dict.var_by_name = NULL;
251 for (i = 0; i < default_dict.nvar; i++)
253 clear_variable (&default_dict, default_dict.var[i]);
254 free (default_dict.var[i]);
257 free (default_dict.var);
258 free (default_dict.label);
259 free (default_dict.documents);
261 /* 2. Copy dictionary D into the active file dictionary. */
263 if (!default_dict.var_by_name)
265 default_dict.var_by_name = avl_create (NULL, cmp_variable, NULL);
267 for (i = 0; i < default_dict.nvar; i++)
268 avl_force_insert (default_dict.var_by_name, default_dict.var[i]);
271 /* 3. Destroy dictionary D. */
275 /* Destroys dictionary D. */
277 free_dictionary (struct dictionary * d)
286 avl_destroy (d->var_by_name, NULL);
288 for (i = 0; i < d->nvar; i++)
290 struct variable *v = d->var[i];
294 avl_destroy (v->val_lab, free_val_lab);
312 /* Cancels the temporary transformation, if any. */
314 cancel_temporary (void)
319 free_dictionary (temp_dict);