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
25 #include "dictionary.h"
30 #include "value-labels.h"
33 #define _(msgid) gettext (msgid)
36 /* Assign auxiliary data AUX to variable V, which must not
37 already have auxiliary data. Before V's auxiliary data is
38 cleared, AUX_DTOR(V) will be called. */
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;
50 /* Remove auxiliary data, if any, from V, and returns it, without
51 calling any associated destructor. */
53 var_detach_aux (struct variable *v)
61 /* Clears auxiliary data, if any, from V, and calls any
62 associated destructor. */
64 var_clear_aux (struct variable *v)
69 if (v->aux_dtor != NULL)
75 /* This function is appropriate for use an auxiliary data
76 destructor (passed as AUX_DTOR to var_attach_aux()) for the
77 case where the auxiliary data should be passed to free(). */
79 var_dtor_free (struct variable *v)
84 /* Compares A and B, which both have the given WIDTH, and returns
85 a strcmp()-type result. */
87 compare_values (const union value *a, const union value *b, int width)
90 return a->f < b->f ? -1 : a->f > b->f;
92 return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
95 /* Create a hash of v */
97 hash_value(const union value *v, int width)
102 id_hash = hsh_hash_double (v->f);
104 id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
112 /* Returns true if NAME is an acceptable name for a variable,
113 false otherwise. If ISSUE_ERROR is true, issues an
114 explanatory error message on failure. */
116 var_is_valid_name (const char *name, bool issue_error)
120 assert (name != NULL);
122 length = strlen (name);
126 msg (SE, _("Variable name cannot be empty string."));
129 else if (length > LONG_NAME_LEN)
132 msg (SE, _("Variable name %s exceeds %d-character limit."),
133 name, (int) LONG_NAME_LEN);
137 for (i = 0; i < length; i++)
138 if (!CHAR_IS_IDN (name[i]))
141 msg (SE, _("Character `%c' (in %s) may not appear in "
147 if (!CHAR_IS_ID1 (name[0]))
150 msg (SE, _("Character `%c' (in %s), may not appear "
151 "as the first character in a variable name."),
156 if (lex_id_to_token (name, strlen (name)) != T_ID)
159 msg (SE, _("%s may not be used as a variable name because it "
160 "is a reserved word."), name);
167 /* A hsh_compare_func that orders variables A and B by their
170 compare_var_names (const void *a_, const void *b_, void *foo UNUSED)
172 const struct variable *a = a_;
173 const struct variable *b = b_;
175 return strcasecmp (a->name, b->name);
178 /* A hsh_hash_func that hashes variable V based on its name. */
180 hash_var_name (const void *v_, void *foo UNUSED)
182 const struct variable *v = v_;
184 return hsh_hash_case_string (v->name);
187 /* A hsh_compare_func that orders pointers to variables A and B
190 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED)
192 struct variable *const *a = a_;
193 struct variable *const *b = b_;
195 return strcasecmp ((*a)->name, (*b)->name);
198 /* A hsh_hash_func that hashes pointer to variable V based on its
201 hash_var_ptr_name (const void *v_, void *foo UNUSED)
203 struct variable *const *v = v_;
205 return hsh_hash_case_string ((*v)->name);
208 /* Sets V's short_name to SHORT_NAME, truncating it to
209 SHORT_NAME_LEN characters and converting it to uppercase in
212 var_set_short_name (struct variable *v, const char *short_name)
215 assert (short_name[0] == '\0' || var_is_valid_name (short_name, false));
217 str_copy_trunc (v->short_name, sizeof v->short_name, short_name);
218 str_uppercase (v->short_name);
221 /* Clears V's short name. */
223 var_clear_short_name (struct variable *v)
227 v->short_name[0] = '\0';
230 /* Sets V's short name to BASE, followed by a suffix of the form
231 _A, _B, _C, ..., _AA, _AB, etc. according to the value of
232 SUFFIX. Truncates BASE as necessary to fit. */
234 var_set_short_name_suffix (struct variable *v, const char *base, int suffix)
236 char string[SHORT_NAME_LEN + 1];
241 assert (suffix >= 0);
242 assert (strlen (v->short_name) > 0);
245 var_set_short_name (v, base);
247 /* Compose suffix_string. */
248 start = end = string + sizeof string - 1;
252 *--start = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[suffix % 26];
253 if (start <= string + 1)
254 msg (SE, _("Variable suffix too large."));
260 /* Append suffix_string to V's short name. */
262 if (len + strlen (v->short_name) > SHORT_NAME_LEN)
263 ofs = SHORT_NAME_LEN - len;
265 ofs = strlen (v->short_name);
266 strcpy (v->short_name + ofs, start);
270 /* Returns the dictionary class corresponding to a variable named
273 dict_class_from_id (const char *name)
275 assert (name != NULL);
288 /* Returns the name of dictionary class DICT_CLASS. */
290 dict_class_to_name (enum dict_class dict_class)
295 return _("ordinary");