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., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "dictionary.h"
28 #include "expressions/public.h"
29 #include "file-handle.h"
34 #include "value-labels.h"
37 #include "debug-print.h"
40 var_attach_aux (struct variable *v,
41 void *aux, void (*aux_dtor) (struct variable *))
43 assert (v->aux == NULL);
46 v->aux_dtor = aux_dtor;
51 var_detach_aux (struct variable *v)
60 var_clear_aux (struct variable *v)
65 if (v->aux_dtor != NULL)
72 var_dtor_free (struct variable *v)
77 /* Compares A and B, which both have the given WIDTH, and returns
78 a strcmp()-type result. */
80 compare_values (const union value *a, const union value *b, int width)
83 return a->f < b->f ? -1 : a->f > b->f;
85 return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
88 /* Create a hash of v */
90 hash_value(const union value *v, int width)
95 id_hash = hsh_hash_double (v->f);
97 id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
104 /* Discards all the current state in preparation for a data-input
105 command like DATA LIST or GET. */
107 discard_variables (void)
109 dict_clear (default_dict);
110 default_handle = NULL;
114 if (vfm_source != NULL)
116 free_case_source (vfm_source);
120 cancel_transformations ();
124 expr_free (process_if_expr);
125 process_if_expr = NULL;
129 pgm_state = STATE_INIT;
132 /* Return nonzero only if X is a user-missing value for numeric
135 is_num_user_missing (double x, const struct variable *v)
137 switch (v->miss_type)
142 return x == v->missing[0].f;
144 return x == v->missing[0].f || x == v->missing[1].f;
146 return (x == v->missing[0].f || x == v->missing[1].f
147 || x == v->missing[2].f);
149 return x >= v->missing[0].f && x <= v->missing[1].f;
151 return x <= v->missing[0].f;
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);
158 return x <= v->missing[0].f || x == v->missing[1].f;
160 return x >= v->missing[0].f || x == v->missing[1].f;
167 /* Return nonzero only if string S is a user-missing variable for
168 string variable V. */
170 is_str_user_missing (const unsigned char s[], const struct variable *v)
172 /* FIXME: should these be memcmp()? */
173 switch (v->miss_type)
178 return !strncmp (s, v->missing[0].s, v->width);
180 return (!strncmp (s, v->missing[0].s, v->width)
181 || !strncmp (s, v->missing[1].s, v->width));
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));
192 /* Return nonzero only if value VAL is system-missing for variable
195 is_system_missing (const union value *val, const struct variable *v)
197 return v->type == NUMERIC && val->f == SYSMIS;
200 /* Return nonzero only if value VAL is system- or user-missing for
203 is_missing (const union value *val, const struct variable *v)
208 if (val->f == SYSMIS)
210 return is_num_user_missing (val->f, v);
212 return is_str_user_missing (val->s, v);
219 /* Return nonzero only if value VAL is user-missing for variable V. */
221 is_user_missing (const union value *val, const struct variable *v)
226 return is_num_user_missing (val->f, v);
228 return is_str_user_missing (val->s, v);
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. */
239 var_is_valid_name (const char *name, bool issue_error)
243 assert (name != NULL);
245 length = strlen (name);
249 msg (SE, _("Variable names must be at least 1 character long."));
252 else if (length > SHORT_NAME_LEN)
255 msg (SE, _("Variable name %s exceeds %d-character limit."),
256 (int) SHORT_NAME_LEN);
260 for (i = 0; i < length; i++)
261 if (!CHAR_IS_IDN (name[i]))
264 msg (SE, _("Character `%c' (in %s) may not appear in "
270 if (!CHAR_IS_ID1 (name[0]))
273 msg (SE, _("Character `%c' (in %s), may not appear "
274 "as the first character in a variable name."), name);
278 if (lex_id_to_token (name, strlen (name)) != T_ID)
281 msg (SE, _("%s may not be used as a variable name because it "
282 "is a reserved word."), name);
289 /* A hsh_compare_func that orders variables A and B by their
292 compare_var_names (const void *a_, const void *b_, void *foo UNUSED)
294 const struct variable *a = a_;
295 const struct variable *b = b_;
297 return strcmp (a->name, b->name);
300 /* A hsh_hash_func that hashes variable V based on its name. */
302 hash_var_name (const void *v_, void *foo UNUSED)
304 const struct variable *v = v_;
306 return hsh_hash_string (v->name);
309 /* A hsh_compare_func that orders pointers to variables A and B
312 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED)
314 struct variable *const *a = a_;
315 struct variable *const *b = b_;
317 return strcmp ((*a)->name, (*b)->name);
320 /* A hsh_hash_func that hashes pointer to variable V based on its
323 hash_var_ptr_name (const void *v_, void *foo UNUSED)
325 struct variable *const *v = v_;
327 return hsh_hash_string ((*v)->name);