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