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