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
31 #include "value-labels.h"
34 #include "debug-print.h"
37 struct dictionary *temp_dict;
40 /* Parses the TEMPORARY command. */
44 lex_match_id ("TEMPORARY");
46 /* TEMPORARY is not allowed inside DO IF or LOOP. */
49 msg (SE, _("This command is not valid inside DO IF or LOOP."));
53 /* TEMPORARY can only appear once! */
56 msg (SE, _("This command may only appear once between "
57 "procedures and procedure-like commands."));
61 /* Everything is temporary, even if we think it'll last forever.
64 temp_dict = save_dictionary ();
69 debug_printf (("TEMPORARY: temp_trns=%d\n", temp_trns));
71 return lex_end_of_command ();
74 /* Copies a variable structure. */
76 copy_variable (struct variable *dest, const struct variable *src)
81 dest->type = src->type;
82 dest->left = src->left;
83 dest->width = src->width;
86 dest->miss_type = src->miss_type;
88 switch (src->miss_type)
101 case MISSING_RANGE_1:
109 for (i = 0; i < n; i++)
110 dest->missing[i] = src->missing[i];
111 dest->print = src->print;
112 dest->write = src->write;
114 dest->val_labs = val_labs_copy (src->val_labs);
115 dest->label = src->label ? xstrdup (src->label) : NULL;
118 /* Returns a newly created empty dictionary. The file label and
119 documents are copied from default_dict if COPY is nonzero. */
121 new_dictionary (int copy)
123 struct dictionary *d = xmalloc (sizeof *d);
126 d->name_tab = hsh_create (8, compare_variables, hash_variable, NULL, NULL);
136 if (default_dict.label && copy)
137 d->label = xstrdup (default_dict.label);
141 if (default_dict.n_documents && copy)
143 d->n_documents = default_dict.n_documents;
146 d->documents = malloc (default_dict.n_documents * 80);
147 memcpy (d->documents, default_dict.documents,
148 default_dict.n_documents * 80);
157 d->weight_index = -1;
158 d->weight_var[0] = 0;
160 d->filter_var[0] = 0;
165 /* Copies the current dictionary info into a newly allocated
166 dictionary structure, which is returned. */
168 save_dictionary (void)
170 /* Dictionary being created. */
171 struct dictionary *d;
175 d = xmalloc (sizeof *d);
177 /* First the easy stuff. */
179 d->label = default_dict.label ? xstrdup (default_dict.label) : NULL;
180 if (default_dict.n_documents)
182 d->documents = malloc (default_dict.n_documents * 80);
183 memcpy (d->documents, default_dict.documents,
184 default_dict.n_documents * 80);
186 else d->documents = NULL;
188 /* Then the variables. */
189 d->name_tab = hsh_create (8, compare_variables, hash_variable, NULL, NULL);
190 d->var = xmalloc (default_dict.nvar * sizeof *d->var);
191 for (i = 0; i < default_dict.nvar; i++)
193 d->var[i] = xmalloc (sizeof *d->var[i]);
194 copy_variable (d->var[i], default_dict.var[i]);
195 strcpy (d->var[i]->name, default_dict.var[i]->name);
196 d->var[i]->index = i;
197 hsh_force_insert (d->name_tab, d->var[i]);
200 /* Then the SPLIT FILE variables. */
201 if (default_dict.splits)
205 d->n_splits = default_dict.n_splits;
206 d->splits = xmalloc ((default_dict.n_splits + 1) * sizeof *d->splits);
207 for (i = 0; i < default_dict.n_splits; i++)
208 d->splits[i] = d->var[default_dict.splits[i]->index];
209 d->splits[default_dict.n_splits] = NULL;
220 /* Copies dictionary D into the active file dictionary. Deletes
223 restore_dictionary (struct dictionary * d)
227 /* 1. Delete the current dictionary. */
228 default_dict.n_splits = 0;
229 free (default_dict.splits);
230 default_dict.splits = NULL;
232 hsh_destroy (default_dict.name_tab);
233 default_dict.name_tab = NULL;
235 for (i = 0; i < default_dict.nvar; i++)
237 clear_variable (&default_dict, default_dict.var[i]);
238 free (default_dict.var[i]);
241 free (default_dict.var);
242 free (default_dict.label);
243 free (default_dict.documents);
245 /* 2. Copy dictionary D into the active file dictionary. */
247 if (default_dict.name_tab == NULL)
249 default_dict.name_tab = hsh_create (8, compare_variables, hash_variable,
252 for (i = 0; i < default_dict.nvar; i++)
253 hsh_force_insert (default_dict.name_tab, default_dict.var[i]);
256 /* 3. Destroy dictionary D. */
260 /* Destroys dictionary D. */
262 free_dictionary (struct dictionary * d)
271 hsh_destroy (d->name_tab);
273 for (i = 0; i < d->nvar; i++)
275 struct variable *v = d->var[i];
277 val_labs_destroy (v->val_labs);
293 /* Cancels the temporary transformation, if any. */
295 cancel_temporary (void)
300 free_dictionary (temp_dict);