1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
21 #include "dictionary.h"
24 #include "algorithm.h"
31 #include "value-labels.h"
37 struct variable **var; /* Variables. */
38 size_t var_cnt, var_cap; /* Number of variables, capacity. */
39 struct hsh_table *name_tab; /* Variable index by name. */
40 struct hsh_table *long_name_tab; /* Variable indexed by long name */
41 int next_value_idx; /* Index of next `union value' to allocate. */
42 struct variable **split; /* SPLIT FILE vars. */
43 size_t split_cnt; /* SPLIT FILE count. */
44 struct variable *weight; /* WEIGHT variable. */
45 struct variable *filter; /* FILTER variable. */
46 int case_limit; /* Current case limit (N command). */
47 char *label; /* File label. */
48 char *documents; /* Documents, as a string. */
49 struct vector **vector; /* Vectors of variables. */
50 size_t vector_cnt; /* Number of vectors. */
58 compare_long_names(const void *a_, const void *b_, void *aux UNUSED)
60 const struct name_table_entry *a = a_;
61 const struct name_table_entry *b = b_;
63 return strcasecmp(a->longname, b->longname);
67 /* Long names use case insensitive comparison */
69 hash_long_name (const void *e_, void *aux UNUSED)
71 const struct name_table_entry *e = e_;
75 char *s = strdup(e->longname);
77 for ( i = 0 ; i < strlen(s) ; ++i )
80 hash = hsh_hash_string (s);
90 static char *make_short_name(struct dictionary *dict, const char *longname) ;
93 /* Creates and returns a new dictionary. */
97 struct dictionary *d = xmalloc (sizeof *d);
100 d->var_cnt = d->var_cap = 0;
101 d->name_tab = hsh_create (8, compare_var_names, hash_var_name, NULL, NULL);
102 d->long_name_tab = hsh_create (8, compare_long_names, hash_long_name,
103 (hsh_free_func *) free_nte, NULL);
104 d->next_value_idx = 0;
118 /* Creates and returns a (deep) copy of an existing
121 dict_clone (const struct dictionary *s)
123 struct dictionary *d;
130 for (i = 0; i < s->var_cnt; i++)
131 dict_clone_var (d, s->var[i], s->var[i]->name, s->var[i]->longname);
133 d->next_value_idx = s->next_value_idx;
135 d->split_cnt = s->split_cnt;
136 if (d->split_cnt > 0)
138 d->split = xmalloc (d->split_cnt * sizeof *d->split);
139 for (i = 0; i < d->split_cnt; i++)
140 d->split[i] = dict_lookup_var_assert (d, s->split[i]->name);
143 if (s->weight != NULL)
144 d->weight = dict_lookup_var_assert (d, s->weight->name);
146 if (s->filter != NULL)
147 d->filter = dict_lookup_var_assert (d, s->filter->name);
149 d->case_limit = s->case_limit;
150 dict_set_label (d, dict_get_label (s));
151 dict_set_documents (d, dict_get_documents (s));
153 for (i = 0; i < s->vector_cnt; i++)
154 dict_create_vector (d, s->vector[i]->name,
155 s->vector[i]->var, s->vector[i]->cnt);
160 /* Clears the contents from a dictionary without destroying the
161 dictionary itself. */
163 dict_clear (struct dictionary *d)
165 /* FIXME? Should we really clear case_limit, label, documents?
166 Others are necessarily cleared by deleting all the variables.*/
171 for (i = 0; i < d->var_cnt; i++)
173 struct variable *v = d->var[i];
175 val_labs_destroy (v->val_labs);
181 d->var_cnt = d->var_cap = 0;
182 hsh_clear (d->name_tab);
183 if ( d->long_name_tab)
184 hsh_clear (d->long_name_tab);
185 d->next_value_idx = 0;
196 dict_clear_vectors (d);
199 /* Allocate the pointer TEXT and fill it with text representing the
200 long variable name buffer. SIZE will contain the size of TEXT.
201 TEXT must be freed by the caller when no longer required.
204 dict_get_varname_block(const struct dictionary *dict, char **text, int *size)
208 struct hsh_iterator hi;
209 struct name_table_entry *nte;
212 for ( nte = hsh_first(dict->long_name_tab, &hi);
214 nte = hsh_next(dict->long_name_tab, &hi))
216 bufsize += strlen(nte->name) + strlen(nte->longname) + 2;
217 buf = xrealloc(buf, bufsize + 1);
222 strcat(buf, nte->name);
224 strcat(buf, nte->longname);
230 /* Loose the final delimiting TAB */
239 /* Add a new entry into the dictionary's long name table, and update the
240 corresponding variable with the relevant long name.
243 dict_add_longvar_entry(struct dictionary *d,
245 const char *longname)
250 struct name_table_entry *nte = xmalloc (sizeof (struct name_table_entry));
251 nte->longname = strdup(longname);
252 nte->name = strdup(name);
254 /* Look up the name in name_tab */
255 v = hsh_find ( d->name_tab, name);
258 msg (FE, _("The entry \"%s\" in the variable name map, has no corresponding variable"), name);
261 assert ( 0 == strcmp(v->name, name) );
262 v->longname = nte->longname;
264 hsh_insert(d->long_name_tab, nte);
267 /* Destroy and free up an nte */
269 free_nte(struct name_table_entry *nte)
278 /* Destroys the aux data for every variable in D, by calling
279 var_clear_aux() for each variable. */
281 dict_clear_aux (struct dictionary *d)
287 for (i = 0; i < d->var_cnt; i++)
288 var_clear_aux (d->var[i]);
291 /* Clears a dictionary and destroys it. */
293 dict_destroy (struct dictionary *d)
298 hsh_destroy (d->name_tab);
299 hsh_destroy (d->long_name_tab);
304 /* Returns the number of variables in D. */
306 dict_get_var_cnt (const struct dictionary *d)
313 /* Returns the variable in D with index IDX, which must be
314 between 0 and the count returned by dict_get_var_cnt(),
317 dict_get_var (const struct dictionary *d, size_t idx)
320 assert (idx < d->var_cnt);
325 /* Sets *VARS to an array of pointers to variables in D and *CNT
326 to the number of variables in *D. By default all variables
327 are returned, but bits may be set in EXCLUDE_CLASSES to
328 exclude ordinary, system, and/or scratch variables. */
330 dict_get_vars (const struct dictionary *d, struct variable ***vars,
331 size_t *cnt, unsigned exclude_classes)
337 assert (vars != NULL);
338 assert (cnt != NULL);
339 assert ((exclude_classes & ~((1u << DC_ORDINARY)
341 | (1u << DC_SCRATCH))) == 0);
344 for (i = 0; i < d->var_cnt; i++)
345 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
348 *vars = xmalloc (count * sizeof **vars);
350 for (i = 0; i < d->var_cnt; i++)
351 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
352 (*vars)[(*cnt)++] = d->var[i];
353 assert (*cnt == count);
357 static struct variable * dict_create_var_x (struct dictionary *d,
358 const char *name, int width,
359 short name_is_short) ;
361 /* Creates and returns a new variable in D with the given LONGNAME
362 and WIDTH. Returns a null pointer if the given LONGNAME would
363 duplicate that of an existing variable in the dictionary.
366 dict_create_var (struct dictionary *d, const char *longname, int width)
368 return dict_create_var_x(d, longname, width, 0);
372 /* Creates and returns a new variable in D with the given SHORTNAME and
373 WIDTH. The long name table is not updated */
375 dict_create_var_from_short (struct dictionary *d, const char *shortname,
378 return dict_create_var_x(d, shortname, width, 1);
383 /* Creates and returns a new variable in D with the given NAME
385 If NAME_IS_SHORT, assume NAME is the short name. Otherwise assumes
386 NAME is the long name, and creates the corresponding entry in the
387 Dictionary's lookup name table .
388 Returns a null pointer if the given NAME would
389 duplicate that of an existing variable in the dictionary.
392 static struct variable *
393 dict_create_var_x (struct dictionary *d, const char *name, int width,
399 assert (name != NULL);
401 assert (strlen (name) >= 1);
403 assert (width >= 0 && width < 256);
406 assert(strlen (name) <= SHORT_NAME_LEN);
408 assert(strlen (name) <= LONG_NAME_LEN);
410 /* Make sure there's not already a variable by that name. */
411 if (dict_lookup_var (d, name) != NULL)
414 /* Allocate and initialize variable. */
415 v = xmalloc (sizeof *v);
419 strncpy (v->name, name, sizeof v->name);
420 v->name[SHORT_NAME_LEN] = '\0';
424 const char *sn = make_short_name(d, name);
425 strncpy(v->name, sn, SHORT_NAME_LEN + 1);
430 v->index = d->var_cnt;
431 v->type = width == 0 ? NUMERIC : ALPHA;
433 v->fv = d->next_value_idx;
434 v->nv = width == 0 ? 1 : DIV_RND_UP (width, 8);
436 v->reinit = dict_class_from_id (v->name) != DC_SCRATCH;
437 v->miss_type = MISSING_NONE;
438 if (v->type == NUMERIC)
440 v->print.type = FMT_F;
444 v->alignment = ALIGN_RIGHT;
445 v->display_width = 8;
446 v->measure = MEASURE_SCALE;
450 v->print.type = FMT_A;
451 v->print.w = v->width;
454 v->alignment = ALIGN_LEFT;
455 v->display_width = 8;
456 v->measure = MEASURE_NOMINAL;
459 v->val_labs = val_labs_create (v->width);
464 /* Update dictionary. */
465 if (d->var_cnt >= d->var_cap)
467 d->var_cap = 8 + 2 * d->var_cap;
468 d->var = xrealloc (d->var, d->var_cap * sizeof *d->var);
470 d->var[v->index] = v;
472 hsh_force_insert (d->name_tab, v);
474 if ( ! name_is_short)
475 dict_add_longvar_entry(d, v->name, name);
477 d->next_value_idx += v->nv;
482 /* Creates and returns a new variable in D with the given NAME
483 and WIDTH. Assert-fails if the given NAME would duplicate
484 that of an existing variable in the dictionary. */
486 dict_create_var_assert (struct dictionary *d, const char *longname, int width)
488 struct variable *v = dict_create_var (d, longname, width);
493 /* Creates a new variable in D with longname LONGNAME, as a copy of
494 existing variable OV, which need not be in D or in any
496 If SHORTNAME is non null, it will be used as the short name
497 otherwise a new short name will be generated.
500 dict_clone_var (struct dictionary *d, const struct variable *ov,
501 const char *name, const char *longname)
507 assert (strlen (longname) <= LONG_NAME_LEN);
509 struct name_table_entry *nte = xmalloc (sizeof (struct name_table_entry));
511 nte->longname = strdup(longname);
514 assert (strlen (name) >= 1);
515 assert (strlen (name) <= SHORT_NAME_LEN);
516 nte->name = strdup(name);
519 nte->name = make_short_name(d, longname);
522 nv = dict_create_var_from_short (d, nte->name, ov->width);
526 hsh_insert(d->long_name_tab, nte);
527 nv->longname = nte->longname;
530 nv->reinit = ov->reinit;
531 nv->miss_type = ov->miss_type;
532 memcpy (nv->missing, ov->missing, sizeof nv->missing);
533 nv->print = ov->print;
534 nv->write = ov->write;
535 val_labs_destroy (nv->val_labs);
536 nv->val_labs = val_labs_copy (ov->val_labs);
537 if (ov->label != NULL)
538 nv->label = xstrdup (ov->label);
540 nv->alignment = ov->alignment;
541 nv->measure = ov->measure;
542 nv->display_width = ov->display_width;
547 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
548 a variable named NEW_NAME is already in D, except that
549 NEW_NAME may be the same as V's existing name. */
551 dict_rename_var (struct dictionary *d, struct variable *v,
552 const char *new_name)
556 assert (new_name != NULL);
557 assert (strlen (new_name) >= 1 && strlen (new_name) <= SHORT_NAME_LEN);
558 assert (dict_contains_var (d, v));
560 if (!strcmp (v->name, new_name))
563 assert (dict_lookup_var (d, new_name) == NULL);
565 hsh_force_delete (d->name_tab, v);
566 strncpy (v->name, new_name, sizeof v->name);
567 v->name[SHORT_NAME_LEN] = '\0';
568 hsh_force_insert (d->name_tab, v);
569 dict_add_longvar_entry (d, new_name, new_name);
572 /* Returns the variable named NAME in D, or a null pointer if no
573 variable has that name. */
575 dict_lookup_var (const struct dictionary *d, const char *name)
581 struct name_table_entry key;
582 struct name_table_entry *nte;
585 assert (name != NULL);
586 assert (strlen (name) >= 1 && strlen (name) <= LONG_NAME_LEN);
589 nte = hsh_find (d->long_name_tab, &key);
596 short_name = nte->name ;
598 strncpy (v.name, short_name, sizeof v.name);
599 v.name[SHORT_NAME_LEN] = '\0';
601 vr = hsh_find (d->name_tab, &v);
607 /* Returns the variable named NAME in D. Assert-fails if no
608 variable has that name. */
610 dict_lookup_var_assert (const struct dictionary *d, const char *name)
612 struct variable *v = dict_lookup_var (d, name);
617 /* Returns nonzero if variable V is in dictionary D. */
619 dict_contains_var (const struct dictionary *d, const struct variable *v)
624 return v->index >= 0 && v->index < d->var_cnt && d->var[v->index] == v;
627 /* Compares two double pointers to variables, which should point
628 to elements of a struct dictionary's `var' member array. */
630 compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED)
632 struct variable *const *a = a_;
633 struct variable *const *b = b_;
635 return *a < *b ? -1 : *a > *b;
638 /* Deletes variable V from dictionary D and frees V.
640 This is a very bad idea if there might be any pointers to V
641 from outside D. In general, no variable in default_dict
642 should be deleted when any transformations are active, because
643 those transformations might reference the deleted variable.
644 The safest time to delete a variable is just after a procedure
645 has been executed, as done by MODIFY VARS.
647 Pointers to V within D are not a problem, because
648 dict_delete_var() knows to remove V from split variables,
649 weights, filters, etc. */
651 dict_delete_var (struct dictionary *d, struct variable *v)
657 assert (dict_contains_var (d, v));
658 assert (d->var[v->index] == v);
660 /* Delete aux data. */
663 /* Remove V from splits, weight, filter variables. */
664 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
665 &v, compare_var_ptrs, NULL);
670 dict_clear_vectors (d);
672 /* Remove V from var array. */
673 remove_element (d->var, d->var_cnt, sizeof *d->var, v->index);
677 for (i = v->index; i < d->var_cnt; i++)
678 d->var[i]->index = i;
680 /* Update name hash. */
681 hsh_force_delete (d->name_tab, v);
684 val_labs_destroy (v->val_labs);
689 /* Deletes the COUNT variables listed in VARS from D. This is
690 unsafe; see the comment on dict_delete_var() for details. */
692 dict_delete_vars (struct dictionary *d,
693 struct variable *const *vars, size_t count)
695 /* FIXME: this can be done in O(count) time, but this algorithm
698 assert (count == 0 || vars != NULL);
701 dict_delete_var (d, *vars++);
704 /* Reorders the variables in D, placing the COUNT variables
705 listed in ORDER in that order at the beginning of D. The
706 other variables in D, if any, retain their relative
709 dict_reorder_vars (struct dictionary *d,
710 struct variable *const *order, size_t count)
712 struct variable **new_var;
716 assert (count == 0 || order != NULL);
717 assert (count <= d->var_cnt);
719 new_var = xmalloc (d->var_cnt * sizeof *new_var);
720 memcpy (new_var, order, count * sizeof *new_var);
721 for (i = 0; i < count; i++)
723 assert (d->var[order[i]->index] != NULL);
724 d->var[order[i]->index] = NULL;
727 for (i = 0; i < d->var_cnt; i++)
728 if (d->var[i] != NULL)
730 assert (count < d->var_cnt);
731 new_var[count] = d->var[i];
732 new_var[count]->index = count;
739 /* Renames COUNT variables specified in VARS to the names given
740 in NEW_NAMES within dictionary D. If the renaming would
741 result in a duplicate variable name, returns zero and stores a
742 name that would be duplicated into *ERR_NAME (if ERR_NAME is
743 non-null). Otherwise, the renaming is successful, and nonzero
746 dict_rename_vars (struct dictionary *d,
747 struct variable **vars, char **new_names,
748 size_t count, char **err_name)
756 assert (count == 0 || vars != NULL);
757 assert (count == 0 || new_names != NULL);
760 old_names = xmalloc (count * sizeof *old_names);
761 for (i = 0; i < count; i++)
763 assert (d->var[vars[i]->index] == vars[i]);
764 hsh_force_delete (d->name_tab, vars[i]);
765 old_names[i] = xstrdup (vars[i]->name);
768 for (i = 0; i < count; i++)
771 struct name_table_entry key;
772 struct name_table_entry *nte;
773 assert (new_names[i] != NULL);
774 assert (*new_names[i] != '\0');
775 assert (strlen (new_names[i]) <= LONG_NAME_LEN );
777 sn = make_short_name(d, new_names[i]);
778 strncpy(vars[i]->name, sn, SHORT_NAME_LEN + 1);
783 key.longname = vars[i]->longname;
784 nte = hsh_find (d->long_name_tab, &key);
786 free( nte->longname ) ;
787 nte->longname = strdup ( new_names[i]);
788 vars[i]->longname = nte->longname;
790 if (hsh_insert (d->name_tab, vars[i]) != NULL )
793 if (err_name != NULL)
794 *err_name = new_names[i];
796 for (i = 0; i < fail_idx; i++)
797 hsh_force_delete (d->name_tab, vars[i]);
799 for (i = 0; i < count; i++)
801 strcpy (vars[i]->name, old_names[i]);
802 hsh_force_insert (d->name_tab, vars[i]);
804 key.longname = vars[i]->longname;
805 nte = hsh_find (d->long_name_tab, &key);
807 free( nte->longname ) ;
808 nte->longname = strdup ( old_names[i]);
809 vars[i]->longname = nte->longname;
818 for (i = 0; i < count; i++)
825 /* Returns the weighting variable in dictionary D, or a null
826 pointer if the dictionary is unweighted. */
828 dict_get_weight (const struct dictionary *d)
831 assert (d->weight == NULL || dict_contains_var (d, d->weight));
836 /* Returns the value of D's weighting variable in case C, except that a
837 negative weight is returned as 0. Returns 1 if the dictionary is
838 unweighted. Will warn about missing, negative, or zero values if
839 warn_on_invalid is nonzero. The function will set warn_on_invalid to zero
840 if an invalid weight is found. */
842 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
843 int *warn_on_invalid)
848 if (d->weight == NULL)
852 double w = case_num (c, d->weight->fv);
853 if ( w < 0.0 || w == SYSMIS || is_num_user_missing(w, d->weight) )
855 if ( w == 0.0 && *warn_on_invalid ) {
856 *warn_on_invalid = 0;
857 msg (SW, _("At least one case in the data file had a weight value "
858 "that was user-missing, system-missing, zero, or "
859 "negative. These case(s) were ignored."));
865 /* Sets the weighting variable of D to V, or turning off
866 weighting if V is a null pointer. */
868 dict_set_weight (struct dictionary *d, struct variable *v)
871 assert (v == NULL || dict_contains_var (d, v));
872 assert (v == NULL || v->type == NUMERIC);
877 /* Returns the filter variable in dictionary D (see cmd_filter())
878 or a null pointer if the dictionary is unfiltered. */
880 dict_get_filter (const struct dictionary *d)
883 assert (d->filter == NULL || dict_contains_var (d, d->filter));
888 /* Sets V as the filter variable for dictionary D. Passing a
889 null pointer for V turn off filtering. */
891 dict_set_filter (struct dictionary *d, struct variable *v)
894 assert (v == NULL || dict_contains_var (d, v));
899 /* Returns the case limit for dictionary D, or zero if the number
900 of cases is unlimited (see cmd_n()). */
902 dict_get_case_limit (const struct dictionary *d)
906 return d->case_limit;
909 /* Sets CASE_LIMIT as the case limit for dictionary D. Zero for
910 CASE_LIMIT indicates no limit. */
912 dict_set_case_limit (struct dictionary *d, int case_limit)
915 assert (case_limit >= 0);
917 d->case_limit = case_limit;
920 /* Returns the index of the next value to be added to D. This
921 value is the number of `union value's that need to be
922 allocated to store a case for dictionary D. */
924 dict_get_next_value_idx (const struct dictionary *d)
928 return d->next_value_idx;
931 /* Returns the number of bytes needed to store a case for
934 dict_get_case_size (const struct dictionary *d)
938 return sizeof (union value) * dict_get_next_value_idx (d);
941 /* Deletes scratch variables in dictionary D and reassigns values
942 so that fragmentation is eliminated. */
944 dict_compact_values (struct dictionary *d)
948 d->next_value_idx = 0;
949 for (i = 0; i < d->var_cnt; )
951 struct variable *v = d->var[i];
953 if (dict_class_from_id (v->name) != DC_SCRATCH)
955 v->fv = d->next_value_idx;
956 d->next_value_idx += v->nv;
960 dict_delete_var (default_dict, v);
964 /* Copies values from SRC, which represents a case arranged
965 according to dictionary D, to DST, which represents a case
966 arranged according to the dictionary that will be produced by
967 dict_compact_values(D). */
969 dict_compact_case (const struct dictionary *d,
970 struct ccase *dst, const struct ccase *src)
976 for (i = 0; i < d->var_cnt; i++)
978 struct variable *v = d->var[i];
980 if (dict_class_from_id (v->name) != DC_SCRATCH)
982 case_copy (dst, value_idx, src, v->fv, v->nv);
988 /* Returns the number of values that would be used by a case if
989 dict_compact_values() were called. */
991 dict_get_compacted_value_cnt (const struct dictionary *d)
997 for (i = 0; i < d->var_cnt; i++)
998 if (dict_class_from_id (d->var[i]->name) != DC_SCRATCH)
999 cnt += d->var[i]->nv;
1003 /* Creates and returns an array mapping from a dictionary index
1004 to the `fv' that the corresponding variable will have after
1005 calling dict_compact_values(). Scratch variables receive -1
1006 for `fv' because dict_compact_values() will delete them. */
1008 dict_get_compacted_idx_to_fv (const struct dictionary *d)
1011 size_t next_value_idx;
1014 idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv);
1016 for (i = 0; i < d->var_cnt; i++)
1018 struct variable *v = d->var[i];
1020 if (dict_class_from_id (v->name) != DC_SCRATCH)
1022 idx_to_fv[i] = next_value_idx;
1023 next_value_idx += v->nv;
1031 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1032 dict_get_split_cnt() to determine how many SPLIT FILE vars
1033 there are. Returns a null pointer if and only if there are no
1035 struct variable *const *
1036 dict_get_split_vars (const struct dictionary *d)
1043 /* Returns the number of SPLIT FILE vars. */
1045 dict_get_split_cnt (const struct dictionary *d)
1049 return d->split_cnt;
1052 /* Sets CNT split vars SPLIT in dictionary D. */
1054 dict_set_split_vars (struct dictionary *d,
1055 struct variable *const *split, size_t cnt)
1058 assert (cnt == 0 || split != NULL);
1061 d->split = xrealloc (d->split, cnt * sizeof *d->split);
1062 memcpy (d->split, split, cnt * sizeof *d->split);
1065 /* Returns the file label for D, or a null pointer if D is
1066 unlabeled (see cmd_file_label()). */
1068 dict_get_label (const struct dictionary *d)
1075 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1078 dict_set_label (struct dictionary *d, const char *label)
1085 else if (strlen (label) < 60)
1086 d->label = xstrdup (label);
1089 d->label = xmalloc (61);
1090 memcpy (d->label, label, 60);
1091 d->label[60] = '\0';
1095 /* Returns the documents for D, or a null pointer if D has no
1096 documents (see cmd_document()).. */
1098 dict_get_documents (const struct dictionary *d)
1102 return d->documents;
1105 /* Sets the documents for D to DOCUMENTS, or removes D's
1106 documents if DOCUMENT is a null pointer. */
1108 dict_set_documents (struct dictionary *d, const char *documents)
1112 free (d->documents);
1113 if (documents == NULL)
1114 d->documents = NULL;
1116 d->documents = xstrdup (documents);
1119 /* Creates in D a vector named NAME that contains CNT variables
1120 VAR (see cmd_vector()). Returns nonzero if successful, or
1121 zero if a vector named NAME already exists in D. */
1123 dict_create_vector (struct dictionary *d,
1125 struct variable **var, size_t cnt)
1127 struct vector *vector;
1130 assert (name != NULL);
1131 assert (strlen (name) > 0 && strlen (name) <= SHORT_NAME_LEN );
1132 assert (var != NULL);
1135 if (dict_lookup_vector (d, name) != NULL)
1138 d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector);
1139 vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector);
1140 vector->idx = d->vector_cnt++;
1141 strncpy (vector->name, name, SHORT_NAME_LEN);
1142 vector->name[SHORT_NAME_LEN] = '\0';
1143 vector->var = xmalloc (cnt * sizeof *var);
1144 memcpy (vector->var, var, cnt * sizeof *var);
1150 /* Returns the vector in D with index IDX, which must be less
1151 than dict_get_vector_cnt (D). */
1152 const struct vector *
1153 dict_get_vector (const struct dictionary *d, size_t idx)
1156 assert (idx < d->vector_cnt);
1158 return d->vector[idx];
1161 /* Returns the number of vectors in D. */
1163 dict_get_vector_cnt (const struct dictionary *d)
1167 return d->vector_cnt;
1170 /* Looks up and returns the vector within D with the given
1172 const struct vector *
1173 dict_lookup_vector (const struct dictionary *d, const char *name)
1178 assert (name != NULL);
1180 for (i = 0; i < d->vector_cnt; i++)
1181 if (!strcmp (d->vector[i]->name, name))
1182 return d->vector[i];
1186 /* Deletes all vectors from D. */
1188 dict_clear_vectors (struct dictionary *d)
1194 for (i = 0; i < d->vector_cnt; i++)
1196 free (d->vector[i]->var);
1197 free (d->vector[i]);
1205 static const char * quasi_base27(int i);
1208 /* Convert I to quasi base 27
1209 The result is a staticly allocated string.
1214 static char result[SHORT_NAME_LEN + 1];
1215 static char reverse[SHORT_NAME_LEN + 1];
1217 /* FIXME: check the result cant overflow these arrays */
1220 const int radix = 27;
1223 /* and here's the quasi-ness of this routine */
1224 i = i + ( i / radix );
1229 *s++ = (units > 0 ) ? units + 'A' - 1 : 'A';
1234 /* Reverse the result */
1245 /* Generate a short name, given a long name.
1246 The return value of this function must be freed by the caller.
1249 make_short_name(struct dictionary *dict, const char *longname)
1255 char *d = xmalloc ( SHORT_NAME_LEN + 1);
1257 /* Truncate the name */
1258 strncpy(d, longname, SHORT_NAME_LEN);
1259 d[SHORT_NAME_LEN] = '\0';
1261 /* Convert to upper case */
1262 for ( p = d; *p ; ++p )
1265 /* If a variable with that name already exists, then munge it
1266 until there's no conflict */
1267 while (0 != hsh_find (dict->name_tab, d))
1269 const char *suffix = quasi_base27(i++);
1271 d[SHORT_NAME_LEN - strlen(suffix) - 1 ] = '_';
1272 d[SHORT_NAME_LEN - strlen(suffix) ] = '\0';