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., 59 Temple Place - Suite 330, 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);
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);
571 /* Returns the variable named NAME in D, or a null pointer if no
572 variable has that name. */
574 dict_lookup_var (const struct dictionary *d, const char *name)
580 struct name_table_entry key;
581 struct name_table_entry *nte;
584 assert (name != NULL);
585 assert (strlen (name) >= 1 && strlen (name) <= LONG_NAME_LEN);
588 nte = hsh_find (d->long_name_tab, &key);
595 short_name = nte->name ;
597 strncpy (v.name, short_name, sizeof v.name);
598 v.name[SHORT_NAME_LEN] = '\0';
600 vr = hsh_find (d->name_tab, &v);
606 /* Returns the variable named NAME in D. Assert-fails if no
607 variable has that name. */
609 dict_lookup_var_assert (const struct dictionary *d, const char *name)
611 struct variable *v = dict_lookup_var (d, name);
616 /* Returns nonzero if variable V is in dictionary D. */
618 dict_contains_var (const struct dictionary *d, const struct variable *v)
623 return v->index >= 0 && v->index < d->var_cnt && d->var[v->index] == v;
626 /* Compares two double pointers to variables, which should point
627 to elements of a struct dictionary's `var' member array. */
629 compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED)
631 struct variable *const *a = a_;
632 struct variable *const *b = b_;
634 return *a < *b ? -1 : *a > *b;
637 /* Deletes variable V from dictionary D and frees V.
639 This is a very bad idea if there might be any pointers to V
640 from outside D. In general, no variable in default_dict
641 should be deleted when any transformations are active, because
642 those transformations might reference the deleted variable.
643 The safest time to delete a variable is just after a procedure
644 has been executed, as done by MODIFY VARS.
646 Pointers to V within D are not a problem, because
647 dict_delete_var() knows to remove V from split variables,
648 weights, filters, etc. */
650 dict_delete_var (struct dictionary *d, struct variable *v)
656 assert (dict_contains_var (d, v));
657 assert (d->var[v->index] == v);
659 /* Delete aux data. */
662 /* Remove V from splits, weight, filter variables. */
663 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
664 &v, compare_var_ptrs, NULL);
669 dict_clear_vectors (d);
671 /* Remove V from var array. */
672 remove_element (d->var, d->var_cnt, sizeof *d->var, v->index);
676 for (i = v->index; i < d->var_cnt; i++)
677 d->var[i]->index = i;
679 /* Update name hash. */
680 hsh_force_delete (d->name_tab, v);
683 val_labs_destroy (v->val_labs);
688 /* Deletes the COUNT variables listed in VARS from D. This is
689 unsafe; see the comment on dict_delete_var() for details. */
691 dict_delete_vars (struct dictionary *d,
692 struct variable *const *vars, size_t count)
694 /* FIXME: this can be done in O(count) time, but this algorithm
697 assert (count == 0 || vars != NULL);
700 dict_delete_var (d, *vars++);
703 /* Reorders the variables in D, placing the COUNT variables
704 listed in ORDER in that order at the beginning of D. The
705 other variables in D, if any, retain their relative
708 dict_reorder_vars (struct dictionary *d,
709 struct variable *const *order, size_t count)
711 struct variable **new_var;
715 assert (count == 0 || order != NULL);
716 assert (count <= d->var_cnt);
718 new_var = xmalloc (d->var_cnt * sizeof *new_var);
719 memcpy (new_var, order, count * sizeof *new_var);
720 for (i = 0; i < count; i++)
722 assert (d->var[order[i]->index] != NULL);
723 d->var[order[i]->index] = NULL;
726 for (i = 0; i < d->var_cnt; i++)
727 if (d->var[i] != NULL)
729 assert (count < d->var_cnt);
730 new_var[count] = d->var[i];
731 new_var[count]->index = count;
738 /* Renames COUNT variables specified in VARS to the names given
739 in NEW_NAMES within dictionary D. If the renaming would
740 result in a duplicate variable name, returns zero and stores a
741 name that would be duplicated into *ERR_NAME (if ERR_NAME is
742 non-null). Otherwise, the renaming is successful, and nonzero
745 dict_rename_vars (struct dictionary *d,
746 struct variable **vars, char **new_names,
747 size_t count, char **err_name)
755 assert (count == 0 || vars != NULL);
756 assert (count == 0 || new_names != NULL);
759 old_names = xmalloc (count * sizeof *old_names);
760 for (i = 0; i < count; i++)
762 assert (d->var[vars[i]->index] == vars[i]);
763 hsh_force_delete (d->name_tab, vars[i]);
764 old_names[i] = xstrdup (vars[i]->name);
767 for (i = 0; i < count; i++)
770 struct name_table_entry key;
771 struct name_table_entry *nte;
772 assert (new_names[i] != NULL);
773 assert (*new_names[i] != '\0');
774 assert (strlen (new_names[i]) <= LONG_NAME_LEN );
776 sn = make_short_name(d, new_names[i]);
777 strncpy(vars[i]->name, sn, SHORT_NAME_LEN);
782 key.longname = vars[i]->longname;
783 nte = hsh_find (d->long_name_tab, &key);
785 free( nte->longname ) ;
786 nte->longname = strdup ( new_names[i]);
787 vars[i]->longname = nte->longname;
789 if (hsh_insert (d->name_tab, vars[i]) != NULL )
792 if (err_name != NULL)
793 *err_name = new_names[i];
795 for (i = 0; i < fail_idx; i++)
796 hsh_force_delete (d->name_tab, vars[i]);
798 for (i = 0; i < count; i++)
800 strcpy (vars[i]->name, old_names[i]);
801 hsh_force_insert (d->name_tab, vars[i]);
803 key.longname = vars[i]->longname;
804 nte = hsh_find (d->long_name_tab, &key);
806 free( nte->longname ) ;
807 nte->longname = strdup ( old_names[i]);
808 vars[i]->longname = nte->longname;
817 for (i = 0; i < count; i++)
824 /* Returns the weighting variable in dictionary D, or a null
825 pointer if the dictionary is unweighted. */
827 dict_get_weight (const struct dictionary *d)
830 assert (d->weight == NULL || dict_contains_var (d, d->weight));
835 /* Returns the value of D's weighting variable in case C, except that a
836 negative weight is returned as 0. Returns 1 if the dictionary is
837 unweighted. Will warn about missing, negative, or zero values if
838 warn_on_invalid is nonzero. The function will set warn_on_invalid to zero
839 if an invalid weight is found. */
841 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
842 int *warn_on_invalid)
847 if (d->weight == NULL)
851 double w = case_num (c, d->weight->fv);
852 if ( w < 0.0 || w == SYSMIS || is_num_user_missing(w, d->weight) )
854 if ( w == 0.0 && *warn_on_invalid ) {
855 *warn_on_invalid = 0;
856 msg (SW, _("At least one case in the data file had a weight value "
857 "that was user-missing, system-missing, zero, or "
858 "negative. These case(s) were ignored."));
864 /* Sets the weighting variable of D to V, or turning off
865 weighting if V is a null pointer. */
867 dict_set_weight (struct dictionary *d, struct variable *v)
870 assert (v == NULL || dict_contains_var (d, v));
871 assert (v == NULL || v->type == NUMERIC);
876 /* Returns the filter variable in dictionary D (see cmd_filter())
877 or a null pointer if the dictionary is unfiltered. */
879 dict_get_filter (const struct dictionary *d)
882 assert (d->filter == NULL || dict_contains_var (d, d->filter));
887 /* Sets V as the filter variable for dictionary D. Passing a
888 null pointer for V turn off filtering. */
890 dict_set_filter (struct dictionary *d, struct variable *v)
893 assert (v == NULL || dict_contains_var (d, v));
898 /* Returns the case limit for dictionary D, or zero if the number
899 of cases is unlimited (see cmd_n()). */
901 dict_get_case_limit (const struct dictionary *d)
905 return d->case_limit;
908 /* Sets CASE_LIMIT as the case limit for dictionary D. Zero for
909 CASE_LIMIT indicates no limit. */
911 dict_set_case_limit (struct dictionary *d, int case_limit)
914 assert (case_limit >= 0);
916 d->case_limit = case_limit;
919 /* Returns the index of the next value to be added to D. This
920 value is the number of `union value's that need to be
921 allocated to store a case for dictionary D. */
923 dict_get_next_value_idx (const struct dictionary *d)
927 return d->next_value_idx;
930 /* Returns the number of bytes needed to store a case for
933 dict_get_case_size (const struct dictionary *d)
937 return sizeof (union value) * dict_get_next_value_idx (d);
940 /* Deletes scratch variables in dictionary D and reassigns values
941 so that fragmentation is eliminated. */
943 dict_compact_values (struct dictionary *d)
947 d->next_value_idx = 0;
948 for (i = 0; i < d->var_cnt; )
950 struct variable *v = d->var[i];
952 if (dict_class_from_id (v->name) != DC_SCRATCH)
954 v->fv = d->next_value_idx;
955 d->next_value_idx += v->nv;
959 dict_delete_var (default_dict, v);
963 /* Copies values from SRC, which represents a case arranged
964 according to dictionary D, to DST, which represents a case
965 arranged according to the dictionary that will be produced by
966 dict_compact_values(D). */
968 dict_compact_case (const struct dictionary *d,
969 struct ccase *dst, const struct ccase *src)
975 for (i = 0; i < d->var_cnt; i++)
977 struct variable *v = d->var[i];
979 if (dict_class_from_id (v->name) != DC_SCRATCH)
981 case_copy (dst, value_idx, src, v->fv, v->nv);
987 /* Returns the number of values that would be used by a case if
988 dict_compact_values() were called. */
990 dict_get_compacted_value_cnt (const struct dictionary *d)
996 for (i = 0; i < d->var_cnt; i++)
997 if (dict_class_from_id (d->var[i]->name) != DC_SCRATCH)
998 cnt += d->var[i]->nv;
1002 /* Creates and returns an array mapping from a dictionary index
1003 to the `fv' that the corresponding variable will have after
1004 calling dict_compact_values(). Scratch variables receive -1
1005 for `fv' because dict_compact_values() will delete them. */
1007 dict_get_compacted_idx_to_fv (const struct dictionary *d)
1010 size_t next_value_idx;
1013 idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv);
1015 for (i = 0; i < d->var_cnt; i++)
1017 struct variable *v = d->var[i];
1019 if (dict_class_from_id (v->name) != DC_SCRATCH)
1021 idx_to_fv[i] = next_value_idx;
1022 next_value_idx += v->nv;
1030 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1031 dict_get_split_cnt() to determine how many SPLIT FILE vars
1032 there are. Returns a null pointer if and only if there are no
1034 struct variable *const *
1035 dict_get_split_vars (const struct dictionary *d)
1042 /* Returns the number of SPLIT FILE vars. */
1044 dict_get_split_cnt (const struct dictionary *d)
1048 return d->split_cnt;
1051 /* Sets CNT split vars SPLIT in dictionary D. */
1053 dict_set_split_vars (struct dictionary *d,
1054 struct variable *const *split, size_t cnt)
1057 assert (cnt == 0 || split != NULL);
1060 d->split = xrealloc (d->split, cnt * sizeof *d->split);
1061 memcpy (d->split, split, cnt * sizeof *d->split);
1064 /* Returns the file label for D, or a null pointer if D is
1065 unlabeled (see cmd_file_label()). */
1067 dict_get_label (const struct dictionary *d)
1074 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1077 dict_set_label (struct dictionary *d, const char *label)
1084 else if (strlen (label) < 60)
1085 d->label = xstrdup (label);
1088 d->label = xmalloc (61);
1089 memcpy (d->label, label, 60);
1090 d->label[60] = '\0';
1094 /* Returns the documents for D, or a null pointer if D has no
1095 documents (see cmd_document()).. */
1097 dict_get_documents (const struct dictionary *d)
1101 return d->documents;
1104 /* Sets the documents for D to DOCUMENTS, or removes D's
1105 documents if DOCUMENT is a null pointer. */
1107 dict_set_documents (struct dictionary *d, const char *documents)
1111 free (d->documents);
1112 if (documents == NULL)
1113 d->documents = NULL;
1115 d->documents = xstrdup (documents);
1118 /* Creates in D a vector named NAME that contains CNT variables
1119 VAR (see cmd_vector()). Returns nonzero if successful, or
1120 zero if a vector named NAME already exists in D. */
1122 dict_create_vector (struct dictionary *d,
1124 struct variable **var, size_t cnt)
1126 struct vector *vector;
1129 assert (name != NULL);
1130 assert (strlen (name) > 0 && strlen (name) <= SHORT_NAME_LEN );
1131 assert (var != NULL);
1134 if (dict_lookup_vector (d, name) != NULL)
1137 d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector);
1138 vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector);
1139 vector->idx = d->vector_cnt++;
1140 strncpy (vector->name, name, SHORT_NAME_LEN);
1141 vector->name[SHORT_NAME_LEN] = '\0';
1142 vector->var = xmalloc (cnt * sizeof *var);
1143 memcpy (vector->var, var, cnt * sizeof *var);
1149 /* Returns the vector in D with index IDX, which must be less
1150 than dict_get_vector_cnt (D). */
1151 const struct vector *
1152 dict_get_vector (const struct dictionary *d, size_t idx)
1155 assert (idx < d->vector_cnt);
1157 return d->vector[idx];
1160 /* Returns the number of vectors in D. */
1162 dict_get_vector_cnt (const struct dictionary *d)
1166 return d->vector_cnt;
1169 /* Looks up and returns the vector within D with the given
1171 const struct vector *
1172 dict_lookup_vector (const struct dictionary *d, const char *name)
1177 assert (name != NULL);
1179 for (i = 0; i < d->vector_cnt; i++)
1180 if (!strcmp (d->vector[i]->name, name))
1181 return d->vector[i];
1185 /* Deletes all vectors from D. */
1187 dict_clear_vectors (struct dictionary *d)
1193 for (i = 0; i < d->vector_cnt; i++)
1195 free (d->vector[i]->var);
1196 free (d->vector[i]);
1204 static const char * quasi_base27(int i);
1207 /* Convert I to quasi base 27
1208 The result is a staticly allocated string.
1213 static char result[SHORT_NAME_LEN + 1];
1214 static char reverse[SHORT_NAME_LEN + 1];
1216 /* FIXME: check the result cant overflow these arrays */
1219 const int radix = 27;
1222 /* and here's the quasi-ness of this routine */
1223 i = i + ( i / radix );
1228 *s++ = (units > 0 ) ? units + 'A' - 1 : 'A';
1233 /* Reverse the result */
1244 /* Generate a short name, given a long name.
1245 The return value of this function must be freed by the caller.
1248 make_short_name(struct dictionary *dict, const char *longname)
1254 char *d = xmalloc ( SHORT_NAME_LEN + 1);
1256 /* Truncate the name */
1257 strncpy(d, longname, SHORT_NAME_LEN);
1258 d[SHORT_NAME_LEN] = '\0';
1260 /* Convert to upper case */
1261 for ( p = d; *p ; ++p )
1264 /* If a variable with that name already exists, then munge it
1265 until there's no conflict */
1266 while (0 != hsh_find (dict->name_tab, d))
1268 const char *suffix = quasi_base27(i++);
1270 d[SHORT_NAME_LEN - strlen(suffix) - 1 ] = '_';
1271 d[SHORT_NAME_LEN - strlen(suffix) ] = '\0';