Rewrite portable file reader code and incidentally clean up code for
[pspp] / 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   switch (v->miss_type)
173     {
174     case MISSING_NONE:
175       return 0;
176     case MISSING_1:
177       return !strncmp (s, v->missing[0].s, v->width);
178     case MISSING_2:
179       return (!strncmp (s, v->missing[0].s, v->width)
180               || !strncmp (s, v->missing[1].s, v->width));
181     case MISSING_3:
182       return (!strncmp (s, v->missing[0].s, v->width)
183               || !strncmp (s, v->missing[1].s, v->width)
184               || !strncmp (s, v->missing[2].s, v->width));
185     default:
186       assert (0);
187     }
188   abort ();
189 }
190
191 /* Return nonzero only if value VAL is system-missing for variable
192    V. */
193 int
194 is_system_missing (const union value *val, const struct variable *v)
195 {
196   return v->type == NUMERIC && val->f == SYSMIS;
197 }
198
199 /* Return nonzero only if value VAL is system- or user-missing for
200    variable V. */
201 int
202 is_missing (const union value *val, const struct variable *v)
203 {
204   switch (v->type)
205     {
206     case NUMERIC:
207       if (val->f == SYSMIS)
208         return 1;
209       return is_num_user_missing (val->f, v);
210     case ALPHA:
211       return is_str_user_missing (val->s, v);
212     default:
213       assert (0);
214     }
215   abort ();
216 }
217
218 /* Return nonzero only if value VAL is user-missing for variable V. */
219 int
220 is_user_missing (const union value *val, const struct variable *v)
221 {
222   switch (v->type)
223     {
224     case NUMERIC:
225       return is_num_user_missing (val->f, v);
226     case ALPHA:
227       return is_str_user_missing (val->s, v);
228     default:
229       assert (0);
230     }
231   abort ();
232 }
233 \f
234 /* Returns true if NAME is an acceptable name for a variable,
235    false otherwise.  If ISSUE_ERROR is true, issues an
236    explanatory error message on failure. */
237 bool
238 var_is_valid_name (const char *name, bool issue_error) 
239 {
240   size_t length, i;
241   
242   assert (name != NULL);
243
244   length = strlen (name);
245   if (length < 1) 
246     {
247       if (issue_error)
248         msg (SE, _("Variable names must be at least 1 character long."));
249       return false;
250     }
251   else if (length > MAX_VAR_NAME_LEN) 
252     {
253       if (issue_error)
254         msg (SE, _("Variable name %s exceeds %d-character limit."),
255              (int) MAX_VAR_NAME_LEN);
256       return false;
257     }
258
259   for (i = 0; i < length; i++)
260     if (!CHAR_IS_IDN (name[i])) 
261       {
262         if (issue_error)
263           msg (SE, _("Character `%c' (in %s) may not appear in "
264                      "a variable name."),
265                name);
266         return false;
267       }
268         
269   if (!CHAR_IS_ID1 (name[0]))
270     {
271       if (issue_error)
272         msg (SE, _("Character `%c' (in %s), may not appear "
273                    "as the first character in a variable name."), name);
274       return false;
275     }
276
277   if (lex_id_to_token (name, strlen (name)) != T_ID) 
278     {
279       if (issue_error)
280         msg (SE, _("%s may not be used as a variable name because it "
281                    "is a reserved word."), name);
282       return false;
283     }
284
285   return true;
286 }
287
288 /* A hsh_compare_func that orders variables A and B by their
289    names. */
290 int
291 compare_var_names (const void *a_, const void *b_, void *foo UNUSED) 
292 {
293   const struct variable *a = a_;
294   const struct variable *b = b_;
295
296   return strcmp (a->name, b->name);
297 }
298
299 /* A hsh_hash_func that hashes variable V based on its name. */
300 unsigned
301 hash_var_name (const void *v_, void *foo UNUSED) 
302 {
303   const struct variable *v = v_;
304
305   return hsh_hash_string (v->name);
306 }
307
308 /* A hsh_compare_func that orders pointers to variables A and B
309    by their names. */
310 int
311 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED) 
312 {
313   struct variable *const *a = a_;
314   struct variable *const *b = b_;
315
316   return strcmp ((*a)->name, (*b)->name);
317 }
318
319 /* A hsh_hash_func that hashes pointer to variable V based on its
320    name. */
321 unsigned
322 hash_var_ptr_name (const void *v_, void *foo UNUSED) 
323 {
324   struct variable *const *v = v_;
325
326   return hsh_hash_string ((*v)->name);
327 }