Added new files resulting from directory restructuring.
[pspp-builds.git] / src / data / variable.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "variable.h"
22 #include "message.h"
23 #include <stdlib.h>
24 #include "alloc.h"
25 #include "dictionary.h"
26 #include "hash.h"
27 #include "identifier.h"
28 #include "misc.h"
29 #include "str.h"
30 #include "value-labels.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* Returns an adjective describing the given variable TYPE,
36    suitable for use in phrases like "numeric variable". */
37 const char *
38 var_type_adj (enum var_type type) 
39 {
40   return type == NUMERIC ? _("numeric") : _("string");
41 }
42
43 /* Returns a noun describing a value of the given variable TYPE,
44    suitable for use in phrases like "a number". */
45 const char *
46 var_type_noun (enum var_type type) 
47 {
48   return type == NUMERIC ? _("number") : _("string");
49 }
50
51 /* Assign auxiliary data AUX to variable V, which must not
52    already have auxiliary data.  Before V's auxiliary data is
53    cleared, AUX_DTOR(V) will be called. */
54 void *
55 var_attach_aux (struct variable *v,
56                 void *aux, void (*aux_dtor) (struct variable *)) 
57 {
58   assert (v->aux == NULL);
59   assert (aux != NULL);
60   v->aux = aux;
61   v->aux_dtor = aux_dtor;
62   return aux;
63 }
64
65 /* Remove auxiliary data, if any, from V, and returns it, without
66    calling any associated destructor. */
67 void *
68 var_detach_aux (struct variable *v) 
69 {
70   void *aux = v->aux;
71   assert (aux != NULL);
72   v->aux = NULL;
73   return aux;
74 }
75
76 /* Clears auxiliary data, if any, from V, and calls any
77    associated destructor. */
78 void
79 var_clear_aux (struct variable *v) 
80 {
81   assert (v != NULL);
82   if (v->aux != NULL) 
83     {
84       if (v->aux_dtor != NULL)
85         v->aux_dtor (v);
86       v->aux = NULL;
87     }
88 }
89
90 /* This function is appropriate for use an auxiliary data
91    destructor (passed as AUX_DTOR to var_attach_aux()) for the
92    case where the auxiliary data should be passed to free(). */
93 void
94 var_dtor_free (struct variable *v) 
95 {
96   free (v->aux);
97 }
98
99 /* Compares A and B, which both have the given WIDTH, and returns
100    a strcmp()-type result. */
101 int
102 compare_values (const union value *a, const union value *b, int width) 
103 {
104   if (width == 0) 
105     return a->f < b->f ? -1 : a->f > b->f;
106   else
107     return memcmp (a->s, b->s, min(MAX_SHORT_STRING, width));
108 }
109
110 /* Create a hash of v */
111 unsigned 
112 hash_value(const union value  *v, int width)
113 {
114   unsigned id_hash;
115
116   if ( 0 == width ) 
117     id_hash = hsh_hash_double (v->f);
118   else
119     id_hash = hsh_hash_bytes (v->s, min(MAX_SHORT_STRING, width));
120
121   return id_hash;
122 }
123
124
125
126 \f
127 /* Returns true if NAME is an acceptable name for a variable,
128    false otherwise.  If ISSUE_ERROR is true, issues an
129    explanatory error message on failure. */
130 bool
131 var_is_valid_name (const char *name, bool issue_error) 
132 {
133   size_t length, i;
134   
135   assert (name != NULL);
136
137   length = strlen (name);
138   if (length < 1) 
139     {
140       if (issue_error)
141         msg (SE, _("Variable name cannot be empty string."));
142       return false;
143     }
144   else if (length > LONG_NAME_LEN) 
145     {
146       if (issue_error)
147         msg (SE, _("Variable name %s exceeds %d-character limit."),
148              name, (int) LONG_NAME_LEN);
149       return false;
150     }
151
152   for (i = 0; i < length; i++)
153     if (!lex_is_idn (name[i])) 
154       {
155         if (issue_error)
156           msg (SE, _("Character `%c' (in %s) may not appear in "
157                      "a variable name."),
158                name[i], name);
159         return false;
160       }
161         
162   if (!lex_is_id1 (name[0]))
163     {
164       if (issue_error)
165         msg (SE, _("Character `%c' (in %s), may not appear "
166                    "as the first character in a variable name."),
167              name[0], name);
168       return false;
169     }
170
171   if (lex_id_to_token (name, strlen (name)) != T_ID) 
172     {
173       if (issue_error)
174         msg (SE, _("`%s' may not be used as a variable name because it "
175                    "is a reserved word."), name);
176       return false;
177     }
178
179   return true;
180 }
181
182 /* A hsh_compare_func that orders variables A and B by their
183    names. */
184 int
185 compare_var_names (const void *a_, const void *b_, void *foo UNUSED) 
186 {
187   const struct variable *a = a_;
188   const struct variable *b = b_;
189
190   return strcasecmp (a->name, b->name);
191 }
192
193 /* A hsh_hash_func that hashes variable V based on its name. */
194 unsigned
195 hash_var_name (const void *v_, void *foo UNUSED) 
196 {
197   const struct variable *v = v_;
198
199   return hsh_hash_case_string (v->name);
200 }
201
202 /* A hsh_compare_func that orders pointers to variables A and B
203    by their names. */
204 int
205 compare_var_ptr_names (const void *a_, const void *b_, void *foo UNUSED) 
206 {
207   struct variable *const *a = a_;
208   struct variable *const *b = b_;
209
210   return strcasecmp ((*a)->name, (*b)->name);
211 }
212
213 /* A hsh_hash_func that hashes pointer to variable V based on its
214    name. */
215 unsigned
216 hash_var_ptr_name (const void *v_, void *foo UNUSED) 
217 {
218   struct variable *const *v = v_;
219
220   return hsh_hash_case_string ((*v)->name);
221 }
222 \f
223 /* Sets V's short_name to SHORT_NAME, truncating it to
224    SHORT_NAME_LEN characters and converting it to uppercase in
225    the process. */
226 void
227 var_set_short_name (struct variable *v, const char *short_name) 
228 {
229   assert (v != NULL);
230   assert (short_name[0] == '\0' || var_is_valid_name (short_name, false));
231   
232   str_copy_trunc (v->short_name, sizeof v->short_name, short_name);
233   str_uppercase (v->short_name);
234 }
235
236 /* Clears V's short name. */
237 void
238 var_clear_short_name (struct variable *v) 
239 {
240   assert (v != NULL);
241
242   v->short_name[0] = '\0';
243 }
244
245 /* Sets V's short name to BASE, followed by a suffix of the form
246    _A, _B, _C, ..., _AA, _AB, etc. according to the value of
247    SUFFIX.  Truncates BASE as necessary to fit. */
248 void
249 var_set_short_name_suffix (struct variable *v, const char *base, int suffix)
250 {
251   char string[SHORT_NAME_LEN + 1];
252   char *start, *end;
253   int len, ofs;
254
255   assert (v != NULL);
256   assert (suffix >= 0);
257   assert (strlen (v->short_name) > 0);
258
259   /* Set base name. */
260   var_set_short_name (v, base);
261
262   /* Compose suffix_string. */
263   start = end = string + sizeof string - 1;
264   *end = '\0';
265   do 
266     {
267       *--start = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[suffix % 26];
268       if (start <= string + 1)
269         msg (SE, _("Variable suffix too large."));
270       suffix /= 26;
271     }
272   while (suffix > 0);
273   *--start = '_';
274
275   /* Append suffix_string to V's short name. */
276   len = end - start;
277   if (len + strlen (v->short_name) > SHORT_NAME_LEN)
278     ofs = SHORT_NAME_LEN - len;
279   else
280     ofs = strlen (v->short_name);
281   strcpy (v->short_name + ofs, start);
282 }
283
284
285 /* Returns the dictionary class corresponding to a variable named
286    NAME. */
287 enum dict_class
288 dict_class_from_id (const char *name) 
289 {
290   assert (name != NULL);
291
292   switch (name[0]) 
293     {
294     default:
295       return DC_ORDINARY;
296     case '$':
297       return DC_SYSTEM;
298     case '#':
299       return DC_SCRATCH;
300     }
301 }
302
303 /* Returns the name of dictionary class DICT_CLASS. */
304 const char *
305 dict_class_to_name (enum dict_class dict_class) 
306 {
307   switch (dict_class) 
308     {
309     case DC_ORDINARY:
310       return _("ordinary");
311     case DC_SYSTEM:
312       return _("system");
313     case DC_SCRATCH:
314       return _("scratch");
315     default:
316       assert (0);
317       abort ();
318     }
319 }