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
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 switch (v->miss_type)
177 return !strncmp (s, v->missing[0].s, v->width);
179 return (!strncmp (s, v->missing[0].s, v->width)
180 || !strncmp (s, v->missing[1].s, v->width));
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));
191 /* Return nonzero only if value VAL is system-missing for variable
194 is_system_missing (const union value *val, const struct variable *v)
196 return v->type == NUMERIC && val->f == SYSMIS;
199 /* Return nonzero only if value VAL is system- or user-missing for
202 is_missing (const union value *val, const struct variable *v)
207 if (val->f == SYSMIS)
209 return is_num_user_missing (val->f, v);
211 return is_str_user_missing (val->s, v);
218 /* Return nonzero only if value VAL is user-missing for variable V. */
220 is_user_missing (const union value *val, const struct variable *v)
225 return is_num_user_missing (val->f, v);
227 return is_str_user_missing (val->s, v);
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. */
238 var_is_valid_name (const char *name, bool issue_error)
242 assert (name != NULL);
244 length = strlen (name);
248 msg (SE, _("Variable names must be at least 1 character long."));
251 else if (length > MAX_VAR_NAME_LEN)
254 msg (SE, _("Variable name %s exceeds %d-character limit."),
255 (int) MAX_VAR_NAME_LEN);
259 for (i = 0; i < length; i++)
260 if (!CHAR_IS_IDN (name[i]))
263 msg (SE, _("Character `%c' (in %s) may not appear in "
269 if (!CHAR_IS_ID1 (name[0]))
272 msg (SE, _("Character `%c' (in %s), may not appear "
273 "as the first character in a variable name."), name);
277 if (lex_id_to_token (name, strlen (name)) != T_ID)
280 msg (SE, _("%s may not be used as a variable name because it "
281 "is a reserved word."), name);
288 /* A hsh_compare_func that orders variables A and B by their
291 compare_var_names (const void *a_, const void *b_, void *foo UNUSED)
293 const struct variable *a = a_;
294 const struct variable *b = b_;
296 return strcmp (a->name, b->name);
299 /* A hsh_hash_func that hashes variable V based on its name. */
301 hash_var_name (const void *v_, void *foo UNUSED)
303 const struct variable *v = v_;
305 return hsh_hash_string (v->name);
308 /* A hsh_compare_func that orders pointers to variables A and B
311 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED)
313 struct variable *const *a = a_;
314 struct variable *const *b = b_;
316 return strcmp ((*a)->name, (*b)->name);
319 /* A hsh_hash_func that hashes pointer to variable V based on its
322 hash_var_ptr_name (const void *v_, void *foo UNUSED)
324 struct variable *const *v = v_;
326 return hsh_hash_string ((*v)->name);