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
22 #include <libpspp/message.h>
24 #include <libpspp/alloc.h>
25 #include <libpspp/compiler.h>
26 #include "dictionary.h"
27 #include <libpspp/hash.h>
28 #include "identifier.h"
29 #include <libpspp/misc.h>
30 #include <libpspp/str.h>
31 #include "value-labels.h"
34 #define _(msgid) gettext (msgid)
36 /* Returns an adjective describing the given variable TYPE,
37 suitable for use in phrases like "numeric variable". */
39 var_type_adj (enum var_type type)
41 return type == NUMERIC ? _("numeric") : _("string");
44 /* Returns a noun describing a value of the given variable TYPE,
45 suitable for use in phrases like "a number". */
47 var_type_noun (enum var_type type)
49 return type == NUMERIC ? _("number") : _("string");
52 /* Assign auxiliary data AUX to variable V, which must not
53 already have auxiliary data. Before V's auxiliary data is
54 cleared, AUX_DTOR(V) will be called. */
56 var_attach_aux (struct variable *v,
57 void *aux, void (*aux_dtor) (struct variable *))
59 assert (v->aux == NULL);
62 v->aux_dtor = aux_dtor;
66 /* Remove auxiliary data, if any, from V, and returns it, without
67 calling any associated destructor. */
69 var_detach_aux (struct variable *v)
77 /* Clears auxiliary data, if any, from V, and calls any
78 associated destructor. */
80 var_clear_aux (struct variable *v)
85 if (v->aux_dtor != NULL)
91 /* This function is appropriate for use an auxiliary data
92 destructor (passed as AUX_DTOR to var_attach_aux()) for the
93 case where the auxiliary data should be passed to free(). */
95 var_dtor_free (struct variable *v)
100 /* Compares A and B, which both have the given WIDTH, and returns
101 a strcmp()-type result. */
103 compare_values (const union value *a, const union value *b, int width)
106 return a->f < b->f ? -1 : a->f > b->f;
108 return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
111 /* Create a hash of v */
113 hash_value(const union value *v, int width)
118 id_hash = hsh_hash_double (v->f);
120 id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
128 /* Returns true if NAME is an acceptable name for a variable,
129 false otherwise. If ISSUE_ERROR is true, issues an
130 explanatory error message on failure. */
132 var_is_valid_name (const char *name, bool issue_error)
136 assert (name != NULL);
138 /* Note that strlen returns number of BYTES, not the number of
140 length = strlen (name);
142 bool plausible = var_is_plausible_name(name, issue_error);
148 if (!lex_is_id1 (name[0]))
151 msg (SE, _("Character `%c' (in %s), may not appear "
152 "as the first character in a variable name."),
158 for (i = 0; i < length; i++)
160 if (!lex_is_idn (name[i]))
163 msg (SE, _("Character `%c' (in %s) may not appear in "
174 Returns true if NAME is an plausible name for a variable,
175 false otherwise. If ISSUE_ERROR is true, issues an
176 explanatory error message on failure.
177 This function makes no use of LC_CTYPE.
180 var_is_plausible_name (const char *name, bool issue_error)
184 assert (name != NULL);
186 /* Note that strlen returns number of BYTES, not the number of
188 length = strlen (name);
192 msg (SE, _("Variable name cannot be empty string."));
195 else if (length > LONG_NAME_LEN)
198 msg (SE, _("Variable name %s exceeds %d-character limit."),
199 name, (int) LONG_NAME_LEN);
203 if (lex_id_to_token (name, strlen (name)) != T_ID)
206 msg (SE, _("`%s' may not be used as a variable name because it "
207 "is a reserved word."), name);
214 /* A hsh_compare_func that orders variables A and B by their
217 compare_var_names (const void *a_, const void *b_, void *foo UNUSED)
219 const struct variable *a = a_;
220 const struct variable *b = b_;
222 return strcasecmp (a->name, b->name);
225 /* A hsh_hash_func that hashes variable V based on its name. */
227 hash_var_name (const void *v_, void *foo UNUSED)
229 const struct variable *v = v_;
231 return hsh_hash_case_string (v->name);
234 /* A hsh_compare_func that orders pointers to variables A and B
237 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED)
239 struct variable *const *a = a_;
240 struct variable *const *b = b_;
242 return strcasecmp ((*a)->name, (*b)->name);
245 /* A hsh_hash_func that hashes pointer to variable V based on its
248 hash_var_ptr_name (const void *v_, void *foo UNUSED)
250 struct variable *const *v = v_;
252 return hsh_hash_case_string ((*v)->name);
255 /* Sets V's short_name to SHORT_NAME, truncating it to
256 SHORT_NAME_LEN characters and converting it to uppercase in
259 var_set_short_name (struct variable *v, const char *short_name)
262 assert (short_name[0] == '\0' || var_is_plausible_name (short_name, false));
264 str_copy_trunc (v->short_name, sizeof v->short_name, short_name);
265 str_uppercase (v->short_name);
268 /* Clears V's short name. */
270 var_clear_short_name (struct variable *v)
274 v->short_name[0] = '\0';
277 /* Sets V's short name to BASE, followed by a suffix of the form
278 _A, _B, _C, ..., _AA, _AB, etc. according to the value of
279 SUFFIX. Truncates BASE as necessary to fit. */
281 var_set_short_name_suffix (struct variable *v, const char *base, int suffix)
283 char string[SHORT_NAME_LEN + 1];
288 assert (suffix >= 0);
289 assert (strlen (v->short_name) > 0);
292 var_set_short_name (v, base);
294 /* Compose suffix_string. */
295 start = end = string + sizeof string - 1;
299 *--start = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[suffix % 26];
300 if (start <= string + 1)
301 msg (SE, _("Variable suffix too large."));
307 /* Append suffix_string to V's short name. */
309 if (len + strlen (v->short_name) > SHORT_NAME_LEN)
310 ofs = SHORT_NAME_LEN - len;
312 ofs = strlen (v->short_name);
313 strcpy (v->short_name + ofs, start);
317 /* Returns the dictionary class corresponding to a variable named
320 dict_class_from_id (const char *name)
322 assert (name != NULL);
335 /* Returns the name of dictionary class DICT_CLASS. */
337 dict_class_to_name (enum dict_class dict_class)
342 return _("ordinary");