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