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