Implemented long variable names a la spss V12.
[pspp-builds.git] / src / vars-atr.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "var.h"
22 #include "error.h"
23 #include <stdlib.h>
24 #include "alloc.h"
25 #include "command.h"
26 #include "dictionary.h"
27 #include "do-ifP.h"
28 #include "expressions/public.h"
29 #include "file-handle.h"
30 #include "hash.h"
31 #include "lexer.h"
32 #include "misc.h"
33 #include "str.h"
34 #include "value-labels.h"
35 #include "vfm.h"
36
37 #include "debug-print.h"
38
39 void *
40 var_attach_aux (struct variable *v,
41                 void *aux, void (*aux_dtor) (struct variable *)) 
42 {
43   assert (v->aux == NULL);
44   assert (aux != NULL);
45   v->aux = aux;
46   v->aux_dtor = aux_dtor;
47   return aux;
48 }
49
50 void *
51 var_detach_aux (struct variable *v) 
52 {
53   void *aux = v->aux;
54   assert (aux != NULL);
55   v->aux = NULL;
56   return aux;
57 }
58
59 void
60 var_clear_aux (struct variable *v) 
61 {
62   assert (v != NULL);
63   if (v->aux != NULL) 
64     {
65       if (v->aux_dtor != NULL)
66         v->aux_dtor (v);
67       v->aux = NULL;
68     }
69 }
70
71 void
72 var_dtor_free (struct variable *v) 
73 {
74   free (v->aux);
75 }
76
77 /* Compares A and B, which both have the given WIDTH, and returns
78    a strcmp()-type result. */
79 int
80 compare_values (const union value *a, const union value *b, int width) 
81 {
82   if (width == 0) 
83     return a->f < b->f ? -1 : a->f > b->f;
84   else
85     return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
86 }
87
88 /* Create a hash of v */
89 unsigned 
90 hash_value(const union value  *v, int width)
91 {
92   unsigned id_hash;
93
94   if ( 0 == width ) 
95     id_hash = hsh_hash_double (v->f);
96   else
97     id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
98
99   return id_hash;
100 }
101
102
103
104 /* Discards all the current state in preparation for a data-input
105    command like DATA LIST or GET. */
106 void
107 discard_variables (void)
108 {
109   dict_clear (default_dict);
110   default_handle = NULL;
111
112   n_lag = 0;
113   
114   if (vfm_source != NULL)
115     {
116       free_case_source (vfm_source);
117       vfm_source = NULL;
118     }
119
120   cancel_transformations ();
121
122   ctl_stack = NULL;
123
124   expr_free (process_if_expr);
125   process_if_expr = NULL;
126
127   cancel_temporary ();
128
129   pgm_state = STATE_INIT;
130 }
131
132 /* Return nonzero only if X is a user-missing value for numeric
133    variable V. */
134 inline int
135 is_num_user_missing (double x, const struct variable *v)
136 {
137   switch (v->miss_type)
138     {
139     case MISSING_NONE:
140       return 0;
141     case MISSING_1:
142       return x == v->missing[0].f;
143     case MISSING_2:
144       return x == v->missing[0].f || x == v->missing[1].f;
145     case MISSING_3:
146       return (x == v->missing[0].f || x == v->missing[1].f
147               || x == v->missing[2].f);
148     case MISSING_RANGE:
149       return x >= v->missing[0].f && x <= v->missing[1].f;
150     case MISSING_LOW:
151       return x <= v->missing[0].f;
152     case MISSING_HIGH:
153       return x >= v->missing[0].f;
154     case MISSING_RANGE_1:
155       return ((x >= v->missing[0].f && x <= v->missing[1].f)
156               || x == v->missing[2].f);
157     case MISSING_LOW_1:
158       return x <= v->missing[0].f || x == v->missing[1].f;
159     case MISSING_HIGH_1:
160       return x >= v->missing[0].f || x == v->missing[1].f;
161     default:
162       assert (0);
163     }
164   abort ();
165 }
166
167 /* Return nonzero only if string S is a user-missing variable for
168    string variable V. */
169 inline int
170 is_str_user_missing (const unsigned char s[], const struct variable *v)
171 {
172   /* FIXME: should these be memcmp()? */
173   switch (v->miss_type)
174     {
175     case MISSING_NONE:
176       return 0;
177     case MISSING_1:
178       return !strncmp (s, v->missing[0].s, v->width);
179     case MISSING_2:
180       return (!strncmp (s, v->missing[0].s, v->width)
181               || !strncmp (s, v->missing[1].s, v->width));
182     case MISSING_3:
183       return (!strncmp (s, v->missing[0].s, v->width)
184               || !strncmp (s, v->missing[1].s, v->width)
185               || !strncmp (s, v->missing[2].s, v->width));
186     default:
187       assert (0);
188     }
189   abort ();
190 }
191
192 /* Return nonzero only if value VAL is system-missing for variable
193    V. */
194 int
195 is_system_missing (const union value *val, const struct variable *v)
196 {
197   return v->type == NUMERIC && val->f == SYSMIS;
198 }
199
200 /* Return nonzero only if value VAL is system- or user-missing for
201    variable V. */
202 int
203 is_missing (const union value *val, const struct variable *v)
204 {
205   switch (v->type)
206     {
207     case NUMERIC:
208       if (val->f == SYSMIS)
209         return 1;
210       return is_num_user_missing (val->f, v);
211     case ALPHA:
212       return is_str_user_missing (val->s, v);
213     default:
214       assert (0);
215     }
216   abort ();
217 }
218
219 /* Return nonzero only if value VAL is user-missing for variable V. */
220 int
221 is_user_missing (const union value *val, const struct variable *v)
222 {
223   switch (v->type)
224     {
225     case NUMERIC:
226       return is_num_user_missing (val->f, v);
227     case ALPHA:
228       return is_str_user_missing (val->s, v);
229     default:
230       assert (0);
231     }
232   abort ();
233 }
234 \f
235 /* Returns true if NAME is an acceptable name for a variable,
236    false otherwise.  If ISSUE_ERROR is true, issues an
237    explanatory error message on failure. */
238 bool
239 var_is_valid_name (const char *name, bool issue_error) 
240 {
241   size_t length, i;
242   
243   assert (name != NULL);
244
245   length = strlen (name);
246   if (length < 1) 
247     {
248       if (issue_error)
249         msg (SE, _("Variable names must be at least 1 character long."));
250       return false;
251     }
252   else if (length > SHORT_NAME_LEN) 
253     {
254       if (issue_error)
255         msg (SE, _("Variable name %s exceeds %d-character limit."),
256              (int) SHORT_NAME_LEN);
257       return false;
258     }
259
260   for (i = 0; i < length; i++)
261     if (!CHAR_IS_IDN (name[i])) 
262       {
263         if (issue_error)
264           msg (SE, _("Character `%c' (in %s) may not appear in "
265                      "a variable name."),
266                name);
267         return false;
268       }
269         
270   if (!CHAR_IS_ID1 (name[0]))
271     {
272       if (issue_error)
273         msg (SE, _("Character `%c' (in %s), may not appear "
274                    "as the first character in a variable name."), name);
275       return false;
276     }
277
278   if (lex_id_to_token (name, strlen (name)) != T_ID) 
279     {
280       if (issue_error)
281         msg (SE, _("%s may not be used as a variable name because it "
282                    "is a reserved word."), name);
283       return false;
284     }
285
286   return true;
287 }
288
289 /* A hsh_compare_func that orders variables A and B by their
290    names. */
291 int
292 compare_var_names (const void *a_, const void *b_, void *foo UNUSED) 
293 {
294   const struct variable *a = a_;
295   const struct variable *b = b_;
296
297   return strcmp (a->name, b->name);
298 }
299
300 /* A hsh_hash_func that hashes variable V based on its name. */
301 unsigned
302 hash_var_name (const void *v_, void *foo UNUSED) 
303 {
304   const struct variable *v = v_;
305
306   return hsh_hash_string (v->name);
307 }
308
309 /* A hsh_compare_func that orders pointers to variables A and B
310    by their names. */
311 int
312 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED) 
313 {
314   struct variable *const *a = a_;
315   struct variable *const *b = b_;
316
317   return strcmp ((*a)->name, (*b)->name);
318 }
319
320 /* A hsh_hash_func that hashes pointer to variable V based on its
321    name. */
322 unsigned
323 hash_var_ptr_name (const void *v_, void *foo UNUSED) 
324 {
325   struct variable *const *v = v_;
326
327   return hsh_hash_string ((*v)->name);
328 }