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"
38 #define _(msgid) gettext (msgid)
40 #include "debug-print.h"
42 /* Assign auxiliary data AUX to variable V, which must not
43 already have auxiliary data. Before V's auxiliary data is
44 cleared, AUX_DTOR(V) will be called. */
46 var_attach_aux (struct variable *v,
47 void *aux, void (*aux_dtor) (struct variable *))
49 assert (v->aux == NULL);
52 v->aux_dtor = aux_dtor;
56 /* Remove auxiliary data, if any, from V, and returns it, without
57 calling any associated destructor. */
59 var_detach_aux (struct variable *v)
67 /* Clears auxiliary data, if any, from V, and calls any
68 associated destructor. */
70 var_clear_aux (struct variable *v)
75 if (v->aux_dtor != NULL)
81 /* This function is appropriate for use an auxiliary data
82 destructor (passed as AUX_DTOR to var_attach_aux()) for the
83 case where the auxiliary data should be passed to free(). */
85 var_dtor_free (struct variable *v)
90 /* Compares A and B, which both have the given WIDTH, and returns
91 a strcmp()-type result. */
93 compare_values (const union value *a, const union value *b, int width)
96 return a->f < b->f ? -1 : a->f > b->f;
98 return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
101 /* Create a hash of v */
103 hash_value(const union value *v, int width)
108 id_hash = hsh_hash_double (v->f);
110 id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
117 /* Discards all the current state in preparation for a data-input
118 command like DATA LIST or GET. */
120 discard_variables (void)
122 dict_clear (default_dict);
123 default_handle = NULL;
127 if (vfm_source != NULL)
129 free_case_source (vfm_source);
133 cancel_transformations ();
137 expr_free (process_if_expr);
138 process_if_expr = NULL;
142 pgm_state = STATE_INIT;
145 /* Return nonzero only if X is a user-missing value for numeric
148 is_num_user_missing (double x, const struct variable *v)
150 switch (v->miss_type)
155 return x == v->missing[0].f;
157 return x == v->missing[0].f || x == v->missing[1].f;
159 return (x == v->missing[0].f || x == v->missing[1].f
160 || x == v->missing[2].f);
162 return x >= v->missing[0].f && x <= v->missing[1].f;
164 return x <= v->missing[0].f;
166 return x >= v->missing[0].f;
167 case MISSING_RANGE_1:
168 return ((x >= v->missing[0].f && x <= v->missing[1].f)
169 || x == v->missing[2].f);
171 return x <= v->missing[0].f || x == v->missing[1].f;
173 return x >= v->missing[0].f || x == v->missing[1].f;
180 /* Return nonzero only if string S is a user-missing variable for
181 string variable V. */
183 is_str_user_missing (const unsigned char s[], const struct variable *v)
185 /* FIXME: should these be memcmp()? */
186 switch (v->miss_type)
191 return !strncmp (s, v->missing[0].s, v->width);
193 return (!strncmp (s, v->missing[0].s, v->width)
194 || !strncmp (s, v->missing[1].s, v->width));
196 return (!strncmp (s, v->missing[0].s, v->width)
197 || !strncmp (s, v->missing[1].s, v->width)
198 || !strncmp (s, v->missing[2].s, v->width));
205 /* Return nonzero only if value VAL is system-missing for variable
208 is_system_missing (const union value *val, const struct variable *v)
210 return v->type == NUMERIC && val->f == SYSMIS;
213 /* Return nonzero only if value VAL is system- or user-missing for
216 is_missing (const union value *val, const struct variable *v)
221 if (val->f == SYSMIS)
223 return is_num_user_missing (val->f, v);
225 return is_str_user_missing (val->s, v);
232 /* Return nonzero only if value VAL is user-missing for variable V. */
234 is_user_missing (const union value *val, const struct variable *v)
239 return is_num_user_missing (val->f, v);
241 return is_str_user_missing (val->s, v);
248 /* Returns true if NAME is an acceptable name for a variable,
249 false otherwise. If ISSUE_ERROR is true, issues an
250 explanatory error message on failure. */
252 var_is_valid_name (const char *name, bool issue_error)
256 assert (name != NULL);
258 length = strlen (name);
262 msg (SE, _("Variable name cannot be empty string."));
265 else if (length > LONG_NAME_LEN)
268 msg (SE, _("Variable name %s exceeds %d-character limit."),
269 name, (int) LONG_NAME_LEN);
273 for (i = 0; i < length; i++)
274 if (!CHAR_IS_IDN (name[i]))
277 msg (SE, _("Character `%c' (in %s) may not appear in "
283 if (!CHAR_IS_ID1 (name[0]))
286 msg (SE, _("Character `%c' (in %s), may not appear "
287 "as the first character in a variable name."),
292 if (lex_id_to_token (name, strlen (name)) != T_ID)
295 msg (SE, _("%s may not be used as a variable name because it "
296 "is a reserved word."), name);
303 /* A hsh_compare_func that orders variables A and B by their
306 compare_var_names (const void *a_, const void *b_, void *foo UNUSED)
308 const struct variable *a = a_;
309 const struct variable *b = b_;
311 return strcasecmp (a->name, b->name);
314 /* A hsh_hash_func that hashes variable V based on its name. */
316 hash_var_name (const void *v_, void *foo UNUSED)
318 const struct variable *v = v_;
320 return hsh_hash_case_string (v->name);
323 /* A hsh_compare_func that orders pointers to variables A and B
326 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED)
328 struct variable *const *a = a_;
329 struct variable *const *b = b_;
331 return strcasecmp ((*a)->name, (*b)->name);
334 /* A hsh_hash_func that hashes pointer to variable V based on its
337 hash_var_ptr_name (const void *v_, void *foo UNUSED)
339 struct variable *const *v = v_;
341 return hsh_hash_case_string ((*v)->name);
344 /* Sets V's short_name to SHORT_NAME, truncating it to
345 SHORT_NAME_LEN characters and converting it to uppercase in
348 var_set_short_name (struct variable *v, const char *short_name)
351 assert (short_name[0] == '\0' || var_is_valid_name (short_name, false));
353 str_copy_trunc (v->short_name, sizeof v->short_name, short_name);
354 str_uppercase (v->short_name);
357 /* Clears V's short name. */
359 var_clear_short_name (struct variable *v)
363 v->short_name[0] = '\0';
366 /* Sets V's short name to BASE, followed by a suffix of the form
367 _A, _B, _C, ..., _AA, _AB, etc. according to the value of
368 SUFFIX. Truncates BASE as necessary to fit. */
370 var_set_short_name_suffix (struct variable *v, const char *base, int suffix)
372 char string[SHORT_NAME_LEN + 1];
377 assert (suffix >= 0);
378 assert (strlen (v->short_name) > 0);
381 var_set_short_name (v, base);
383 /* Compose suffix_string. */
384 start = end = string + sizeof string - 1;
388 *--start = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[suffix % 26];
389 if (start <= string + 1)
390 msg (SE, _("Variable suffix too large."));
396 /* Append suffix_string to V's short name. */
398 if (len + strlen (v->short_name) > SHORT_NAME_LEN)
399 ofs = SHORT_NAME_LEN - len;
401 ofs = strlen (v->short_name);
402 strcpy (v->short_name + ofs, start);