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
33 #include "debug-print.h"
36 struct dictionary *temp_dict;
40 /* Displays all the value labels in TREE, with label S. */
42 display_tree (char *s, avl_tree *tree)
45 avl_traverser *trav = NULL;
47 printf("%s tree:\n", s);
49 while ((iter = avl_traverse (tree, &trav)) != NULL)
50 printf (" %g: %s\n", iter->v.f, iter->s);
54 /* Parses the TEMPORARY command. */
58 lex_match_id ("TEMPORARY");
60 /* TEMPORARY is not allowed inside DO IF or LOOP. */
63 msg (SE, _("This command is not valid inside DO IF or LOOP."));
67 /* TEMPORARY can only appear once! */
70 msg (SE, _("This command may only appear once between "
71 "procedures and procedure-like commands."));
75 /* Everything is temporary, even if we think it'll last forever.
78 temp_dict = save_dictionary ();
83 debug_printf (("TEMPORARY: temp_trns=%d\n", temp_trns));
85 return lex_end_of_command ();
88 /* Copies a variable structure. */
90 copy_variable (struct variable *dest, const struct variable *src)
95 dest->type = src->type;
96 dest->left = src->left;
97 dest->width = src->width;
100 dest->miss_type = src->miss_type;
102 switch (src->miss_type)
115 case MISSING_RANGE_1:
123 for (i = 0; i < n; i++)
124 dest->missing[i] = src->missing[i];
125 dest->print = src->print;
126 dest->write = src->write;
128 dest->val_lab = copy_value_labels (src->val_lab);
129 dest->label = src->label ? xstrdup (src->label) : NULL;
132 /* Returns a newly created empty dictionary. The file label and
133 documents are copied from default_dict if COPY is nonzero. */
135 new_dictionary (int copy)
137 struct dictionary *d = xmalloc (sizeof *d);
140 d->var_by_name = avl_create (NULL, cmp_variable, NULL);
150 if (default_dict.label && copy)
151 d->label = xstrdup (default_dict.label);
155 if (default_dict.n_documents && copy)
157 d->n_documents = default_dict.n_documents;
160 d->documents = malloc (default_dict.n_documents * 80);
161 memcpy (d->documents, default_dict.documents,
162 default_dict.n_documents * 80);
171 d->weight_index = -1;
172 d->weight_var[0] = 0;
174 d->filter_var[0] = 0;
179 /* Copies the current dictionary info into a newly allocated
180 dictionary structure, which is returned. */
182 save_dictionary (void)
184 /* Dictionary being created. */
185 struct dictionary *d;
189 d = xmalloc (sizeof *d);
191 /* First the easy stuff. */
193 d->label = default_dict.label ? xstrdup (default_dict.label) : NULL;
194 if (default_dict.n_documents)
196 d->documents = malloc (default_dict.n_documents * 80);
197 memcpy (d->documents, default_dict.documents,
198 default_dict.n_documents * 80);
200 else d->documents = NULL;
202 /* Then the variables. */
203 d->var_by_name = avl_create (NULL, cmp_variable, NULL);
204 d->var = xmalloc (default_dict.nvar * sizeof *d->var);
205 for (i = 0; i < default_dict.nvar; i++)
207 d->var[i] = xmalloc (sizeof *d->var[i]);
208 copy_variable (d->var[i], default_dict.var[i]);
209 strcpy (d->var[i]->name, default_dict.var[i]->name);
210 d->var[i]->index = i;
211 avl_force_insert (d->var_by_name, d->var[i]);
214 /* Then the SPLIT FILE variables. */
215 if (default_dict.splits)
219 d->n_splits = default_dict.n_splits;
220 d->splits = xmalloc ((default_dict.n_splits + 1) * sizeof *d->splits);
221 for (i = 0; i < default_dict.n_splits; i++)
222 d->splits[i] = d->var[default_dict.splits[i]->index];
223 d->splits[default_dict.n_splits] = NULL;
234 /* Copies dictionary D into the active file dictionary. Deletes
237 restore_dictionary (struct dictionary * d)
241 /* 1. Delete the current dictionary. */
242 default_dict.n_splits = 0;
243 free (default_dict.splits);
244 default_dict.splits = NULL;
246 avl_destroy (default_dict.var_by_name, NULL);
247 default_dict.var_by_name = NULL;
249 for (i = 0; i < default_dict.nvar; i++)
251 clear_variable (&default_dict, default_dict.var[i]);
252 free (default_dict.var[i]);
255 free (default_dict.var);
256 free (default_dict.label);
257 free (default_dict.documents);
259 /* 2. Copy dictionary D into the active file dictionary. */
261 if (!default_dict.var_by_name)
263 default_dict.var_by_name = avl_create (NULL, cmp_variable, NULL);
265 for (i = 0; i < default_dict.nvar; i++)
266 avl_force_insert (default_dict.var_by_name, default_dict.var[i]);
269 /* 3. Destroy dictionary D. */
273 /* Destroys dictionary D. */
275 free_dictionary (struct dictionary * d)
284 avl_destroy (d->var_by_name, NULL);
286 for (i = 0; i < d->nvar; i++)
288 struct variable *v = d->var[i];
292 avl_destroy (v->val_lab, free_val_lab);
310 /* Cancels the temporary transformation, if any. */
312 cancel_temporary (void)
317 free_dictionary (temp_dict);