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);
87 static char *make_short_name(struct dictionary *dict, const char *longname) ;
90 /* Creates and returns a new dictionary. */
94 struct dictionary *d = xmalloc (sizeof *d);
97 d->var_cnt = d->var_cap = 0;
98 d->name_tab = hsh_create (8, compare_var_names, hash_var_name, NULL, NULL);
99 d->long_name_tab = hsh_create (8, compare_long_names, hash_long_name, NULL, NULL);
100 d->next_value_idx = 0;
114 /* Creates and returns a (deep) copy of an existing
117 dict_clone (const struct dictionary *s)
119 struct dictionary *d;
126 for (i = 0; i < s->var_cnt; i++)
127 dict_clone_var (d, s->var[i], s->var[i]->name, s->var[i]->longname);
129 d->next_value_idx = s->next_value_idx;
131 d->split_cnt = s->split_cnt;
132 if (d->split_cnt > 0)
134 d->split = xmalloc (d->split_cnt * sizeof *d->split);
135 for (i = 0; i < d->split_cnt; i++)
136 d->split[i] = dict_lookup_var_assert (d, s->split[i]->name);
139 if (s->weight != NULL)
140 d->weight = dict_lookup_var_assert (d, s->weight->name);
142 if (s->filter != NULL)
143 d->filter = dict_lookup_var_assert (d, s->filter->name);
145 d->case_limit = s->case_limit;
146 dict_set_label (d, dict_get_label (s));
147 dict_set_documents (d, dict_get_documents (s));
149 for (i = 0; i < s->vector_cnt; i++)
150 dict_create_vector (d, s->vector[i]->name,
151 s->vector[i]->var, s->vector[i]->cnt);
156 /* Clears the contents from a dictionary without destroying the
157 dictionary itself. */
159 dict_clear (struct dictionary *d)
161 /* FIXME? Should we really clear case_limit, label, documents?
162 Others are necessarily cleared by deleting all the variables.*/
167 for (i = 0; i < d->var_cnt; i++)
169 struct variable *v = d->var[i];
171 val_labs_destroy (v->val_labs);
177 d->var_cnt = d->var_cap = 0;
178 hsh_clear (d->name_tab);
179 if ( d->long_name_tab)
180 hsh_clear (d->long_name_tab);
181 d->next_value_idx = 0;
192 dict_clear_vectors (d);
195 /* Allocate the pointer TEXT and fill it with text representing the
196 long variable name buffer. SIZE will contain the size of TEXT.
197 TEXT must be freed by the caller when no longer required.
200 dict_get_varname_block(const struct dictionary *dict, char **text, int *size)
204 struct hsh_iterator hi;
205 struct name_table_entry *nte;
208 for ( nte = hsh_first(dict->long_name_tab, &hi);
210 nte = hsh_next(dict->long_name_tab, &hi))
212 bufsize += strlen(nte->name) + strlen(nte->longname) + 2;
213 buf = xrealloc(buf, bufsize + 1);
218 strcat(buf, nte->name);
220 strcat(buf, nte->longname);
226 /* Loose the final delimiting TAB */
236 /* Add a new entry into the dictionary's long name table, and update the
237 corresponding varible with the relevant long name.
240 dict_add_longvar_entry(struct dictionary *d,
242 const char *longname)
247 struct name_table_entry *nte = xmalloc (sizeof (struct name_table_entry));
248 nte->longname = strdup(longname);
249 nte->name = strdup(name);
252 /* Look up the name in name_tab */
253 v = hsh_find ( d->name_tab, name);
256 msg (FE, _("The entry \"%s\" in the variable name map, has no corresponding variable"), name);
259 assert ( 0 == strcmp(v->name, name) );
260 v->longname = nte->longname;
262 hsh_insert(d->long_name_tab, nte);
267 /* Destroys the aux data for every variable in D, by calling
268 var_clear_aux() for each variable. */
270 dict_clear_aux (struct dictionary *d)
276 for (i = 0; i < d->var_cnt; i++)
277 var_clear_aux (d->var[i]);
280 /* Clears a dictionary and destroys it. */
282 dict_destroy (struct dictionary *d)
287 hsh_destroy (d->name_tab);
288 hsh_destroy (d->long_name_tab);
293 /* Returns the number of variables in D. */
295 dict_get_var_cnt (const struct dictionary *d)
302 /* Returns the variable in D with index IDX, which must be
303 between 0 and the count returned by dict_get_var_cnt(),
306 dict_get_var (const struct dictionary *d, size_t idx)
309 assert (idx < d->var_cnt);
314 /* Sets *VARS to an array of pointers to variables in D and *CNT
315 to the number of variables in *D. By default all variables
316 are returned, but bits may be set in EXCLUDE_CLASSES to
317 exclude ordinary, system, and/or scratch variables. */
319 dict_get_vars (const struct dictionary *d, struct variable ***vars,
320 size_t *cnt, unsigned exclude_classes)
326 assert (vars != NULL);
327 assert (cnt != NULL);
328 assert ((exclude_classes & ~((1u << DC_ORDINARY)
330 | (1u << DC_SCRATCH))) == 0);
333 for (i = 0; i < d->var_cnt; i++)
334 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
337 *vars = xmalloc (count * sizeof **vars);
339 for (i = 0; i < d->var_cnt; i++)
340 if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
341 (*vars)[(*cnt)++] = d->var[i];
342 assert (*cnt == count);
346 static struct variable * dict_create_var_x (struct dictionary *d,
347 const char *name, int width,
348 short name_is_short) ;
350 /* Creates and returns a new variable in D with the given LONGNAME
351 and WIDTH. Returns a null pointer if the given LONGNAME would
352 duplicate that of an existing variable in the dictionary.
355 dict_create_var (struct dictionary *d, const char *longname, int width)
357 return dict_create_var_x(d, longname, width, 0);
361 /* Creates and returns a new variable in D with the given SHORTNAME and
362 WIDTH. The long name table is not updated */
364 dict_create_var_from_short (struct dictionary *d, const char *shortname,
367 return dict_create_var_x(d, shortname, width, 1);
372 /* Creates and returns a new variable in D with the given NAME
374 If NAME_IS_SHORT, assume NAME is the short name. Otherwise assumes
375 NAME is the long name, and creates the corresponding entry in the
376 Dictionary's lookup name table .
377 Returns a null pointer if the given NAME would
378 duplicate that of an existing variable in the dictionary.
381 static struct variable *
382 dict_create_var_x (struct dictionary *d, const char *name, int width,
388 assert (name != NULL);
390 assert (strlen (name) >= 1);
392 assert (width >= 0 && width < 256);
395 assert(strlen (name) <= SHORT_NAME_LEN);
397 assert(strlen (name) <= LONG_NAME_LEN);
399 /* Make sure there's not already a variable by that name. */
400 if (dict_lookup_var (d, name) != NULL)
403 /* Allocate and initialize variable. */
404 v = xmalloc (sizeof *v);
408 strncpy (v->name, name, sizeof v->name);
409 v->name[SHORT_NAME_LEN] = '\0';
412 strcpy(v->name,make_short_name(d, name));
415 v->index = d->var_cnt;
416 v->type = width == 0 ? NUMERIC : ALPHA;
418 v->fv = d->next_value_idx;
419 v->nv = width == 0 ? 1 : DIV_RND_UP (width, 8);
421 v->reinit = dict_class_from_id (v->name) != DC_SCRATCH;
422 v->miss_type = MISSING_NONE;
423 if (v->type == NUMERIC)
425 v->print.type = FMT_F;
429 v->alignment = ALIGN_RIGHT;
430 v->display_width = 8;
431 v->measure = MEASURE_SCALE;
435 v->print.type = FMT_A;
436 v->print.w = v->width;
439 v->alignment = ALIGN_LEFT;
440 v->display_width = 8;
441 v->measure = MEASURE_NOMINAL;
444 v->val_labs = val_labs_create (v->width);
449 /* Update dictionary. */
450 if (d->var_cnt >= d->var_cap)
452 d->var_cap = 8 + 2 * d->var_cap;
453 d->var = xrealloc (d->var, d->var_cap * sizeof *d->var);
455 d->var[v->index] = v;
457 hsh_force_insert (d->name_tab, v);
459 if ( ! name_is_short)
460 dict_add_longvar_entry(d, v->name, name);
462 d->next_value_idx += v->nv;
467 /* Creates and returns a new variable in D with the given NAME
468 and WIDTH. Assert-fails if the given NAME would duplicate
469 that of an existing variable in the dictionary. */
471 dict_create_var_assert (struct dictionary *d, const char *longname, int width)
473 struct variable *v = dict_create_var (d, longname, width);
478 /* Creates a new variable in D with longname LONGNAME, as a copy of
479 existing variable OV, which need not be in D or in any
481 If SHORTNAME is non null, it will be used as the short name
482 otherwise a new short name will be generated.
485 dict_clone_var (struct dictionary *d, const struct variable *ov,
486 const char *name, const char *longname)
492 assert (strlen (longname) <= LONG_NAME_LEN);
494 struct name_table_entry *nte = xmalloc (sizeof (struct name_table_entry));
496 nte->longname = strdup(longname);
499 assert (strlen (name) >= 1);
500 assert (strlen (name) <= SHORT_NAME_LEN);
501 nte->name = strdup(name);
504 nte->name = make_short_name(d, longname);
507 nv = dict_create_var_from_short (d, nte->name, ov->width);
511 hsh_insert(d->long_name_tab, nte);
512 nv->longname = nte->longname;
515 nv->reinit = ov->reinit;
516 nv->miss_type = ov->miss_type;
517 memcpy (nv->missing, ov->missing, sizeof nv->missing);
518 nv->print = ov->print;
519 nv->write = ov->write;
520 val_labs_destroy (nv->val_labs);
521 nv->val_labs = val_labs_copy (ov->val_labs);
522 if (ov->label != NULL)
523 nv->label = xstrdup (ov->label);
525 nv->alignment = ov->alignment;
526 nv->measure = ov->measure;
527 nv->display_width = ov->display_width;
532 /* Changes the name of V in D to name NEW_NAME. Assert-fails if
533 a variable named NEW_NAME is already in D, except that
534 NEW_NAME may be the same as V's existing name. */
536 dict_rename_var (struct dictionary *d, struct variable *v,
537 const char *new_name)
541 assert (new_name != NULL);
542 assert (strlen (new_name) >= 1 && strlen (new_name) <= SHORT_NAME_LEN);
543 assert (dict_contains_var (d, v));
545 if (!strcmp (v->name, new_name))
548 assert (dict_lookup_var (d, new_name) == NULL);
550 hsh_force_delete (d->name_tab, v);
551 strncpy (v->name, new_name, sizeof v->name);
552 v->name[SHORT_NAME_LEN] = '\0';
553 hsh_force_insert (d->name_tab, v);
556 /* Returns the variable named NAME in D, or a null pointer if no
557 variable has that name. */
559 dict_lookup_var (const struct dictionary *d, const char *name)
565 struct name_table_entry key;
566 struct name_table_entry *nte;
569 assert (name != NULL);
570 assert (strlen (name) >= 1 && strlen (name) <= LONG_NAME_LEN);
573 nte = hsh_find (d->long_name_tab, &key);
580 short_name = nte->name ;
582 strncpy (v.name, short_name, sizeof v.name);
583 v.name[SHORT_NAME_LEN] = '\0';
585 vr = hsh_find (d->name_tab, &v);
591 /* Returns the variable named NAME in D. Assert-fails if no
592 variable has that name. */
594 dict_lookup_var_assert (const struct dictionary *d, const char *name)
596 struct variable *v = dict_lookup_var (d, name);
601 /* Returns nonzero if variable V is in dictionary D. */
603 dict_contains_var (const struct dictionary *d, const struct variable *v)
608 return v->index >= 0 && v->index < d->var_cnt && d->var[v->index] == v;
611 /* Compares two double pointers to variables, which should point
612 to elements of a struct dictionary's `var' member array. */
614 compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED)
616 struct variable *const *a = a_;
617 struct variable *const *b = b_;
619 return *a < *b ? -1 : *a > *b;
622 /* Deletes variable V from dictionary D and frees V.
624 This is a very bad idea if there might be any pointers to V
625 from outside D. In general, no variable in default_dict
626 should be deleted when any transformations are active, because
627 those transformations might reference the deleted variable.
628 The safest time to delete a variable is just after a procedure
629 has been executed, as done by MODIFY VARS.
631 Pointers to V within D are not a problem, because
632 dict_delete_var() knows to remove V from split variables,
633 weights, filters, etc. */
635 dict_delete_var (struct dictionary *d, struct variable *v)
641 assert (dict_contains_var (d, v));
642 assert (d->var[v->index] == v);
644 /* Delete aux data. */
647 /* Remove V from splits, weight, filter variables. */
648 d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
649 &v, compare_var_ptrs, NULL);
654 dict_clear_vectors (d);
656 /* Remove V from var array. */
657 remove_element (d->var, d->var_cnt, sizeof *d->var, v->index);
661 for (i = v->index; i < d->var_cnt; i++)
662 d->var[i]->index = i;
664 /* Update name hash. */
665 hsh_force_delete (d->name_tab, v);
668 val_labs_destroy (v->val_labs);
673 /* Deletes the COUNT variables listed in VARS from D. This is
674 unsafe; see the comment on dict_delete_var() for details. */
676 dict_delete_vars (struct dictionary *d,
677 struct variable *const *vars, size_t count)
679 /* FIXME: this can be done in O(count) time, but this algorithm
682 assert (count == 0 || vars != NULL);
685 dict_delete_var (d, *vars++);
688 /* Reorders the variables in D, placing the COUNT variables
689 listed in ORDER in that order at the beginning of D. The
690 other variables in D, if any, retain their relative
693 dict_reorder_vars (struct dictionary *d,
694 struct variable *const *order, size_t count)
696 struct variable **new_var;
700 assert (count == 0 || order != NULL);
701 assert (count <= d->var_cnt);
703 new_var = xmalloc (d->var_cnt * sizeof *new_var);
704 memcpy (new_var, order, count * sizeof *new_var);
705 for (i = 0; i < count; i++)
707 assert (d->var[order[i]->index] != NULL);
708 d->var[order[i]->index] = NULL;
711 for (i = 0; i < d->var_cnt; i++)
712 if (d->var[i] != NULL)
714 assert (count < d->var_cnt);
715 new_var[count] = d->var[i];
716 new_var[count]->index = count;
723 /* Renames COUNT variables specified in VARS to the names given
724 in NEW_NAMES within dictionary D. If the renaming would
725 result in a duplicate variable name, returns zero and stores a
726 name that would be duplicated into *ERR_NAME (if ERR_NAME is
727 non-null). Otherwise, the renaming is successful, and nonzero
730 dict_rename_vars (struct dictionary *d,
731 struct variable **vars, char **new_names,
732 size_t count, char **err_name)
740 assert (count == 0 || vars != NULL);
741 assert (count == 0 || new_names != NULL);
744 old_names = xmalloc (count * sizeof *old_names);
745 for (i = 0; i < count; i++)
747 assert (d->var[vars[i]->index] == vars[i]);
748 hsh_force_delete (d->name_tab, vars[i]);
749 old_names[i] = xstrdup (vars[i]->name);
752 for (i = 0; i < count; i++)
754 struct name_table_entry key;
755 struct name_table_entry *nte;
756 assert (new_names[i] != NULL);
757 assert (*new_names[i] != '\0');
758 assert (strlen (new_names[i]) <= LONG_NAME_LEN );
759 strcpy (vars[i]->name, make_short_name(d, new_names[i]));
762 key.longname = vars[i]->longname;
763 nte = hsh_find (d->long_name_tab, &key);
765 free( nte->longname ) ;
766 nte->longname = strdup ( new_names[i]);
767 vars[i]->longname = nte->longname;
769 if (hsh_insert (d->name_tab, vars[i]) != NULL )
772 if (err_name != NULL)
773 *err_name = new_names[i];
775 for (i = 0; i < fail_idx; i++)
776 hsh_force_delete (d->name_tab, vars[i]);
778 for (i = 0; i < count; i++)
780 strcpy (vars[i]->name, old_names[i]);
781 hsh_force_insert (d->name_tab, vars[i]);
783 key.longname = vars[i]->longname;
784 nte = hsh_find (d->long_name_tab, &key);
786 free( nte->longname ) ;
787 nte->longname = strdup ( old_names[i]);
788 vars[i]->longname = nte->longname;
797 for (i = 0; i < count; i++)
804 /* Returns the weighting variable in dictionary D, or a null
805 pointer if the dictionary is unweighted. */
807 dict_get_weight (const struct dictionary *d)
810 assert (d->weight == NULL || dict_contains_var (d, d->weight));
815 /* Returns the value of D's weighting variable in case C, except that a
816 negative weight is returned as 0. Returns 1 if the dictionary is
817 unweighted. Will warn about missing, negative, or zero values if
818 warn_on_invalid is nonzero. The function will set warn_on_invalid to zero
819 if an invalid weight is found. */
821 dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
822 int *warn_on_invalid)
827 if (d->weight == NULL)
831 double w = case_num (c, d->weight->fv);
832 if ( w < 0.0 || w == SYSMIS || is_num_user_missing(w, d->weight) )
834 if ( w == 0.0 && *warn_on_invalid ) {
835 *warn_on_invalid = 0;
836 msg (SW, _("At least one case in the data file had a weight value "
837 "that was user-missing, system-missing, zero, or "
838 "negative. These case(s) were ignored."));
844 /* Sets the weighting variable of D to V, or turning off
845 weighting if V is a null pointer. */
847 dict_set_weight (struct dictionary *d, struct variable *v)
850 assert (v == NULL || dict_contains_var (d, v));
851 assert (v == NULL || v->type == NUMERIC);
856 /* Returns the filter variable in dictionary D (see cmd_filter())
857 or a null pointer if the dictionary is unfiltered. */
859 dict_get_filter (const struct dictionary *d)
862 assert (d->filter == NULL || dict_contains_var (d, d->filter));
867 /* Sets V as the filter variable for dictionary D. Passing a
868 null pointer for V turn off filtering. */
870 dict_set_filter (struct dictionary *d, struct variable *v)
873 assert (v == NULL || dict_contains_var (d, v));
878 /* Returns the case limit for dictionary D, or zero if the number
879 of cases is unlimited (see cmd_n()). */
881 dict_get_case_limit (const struct dictionary *d)
885 return d->case_limit;
888 /* Sets CASE_LIMIT as the case limit for dictionary D. Zero for
889 CASE_LIMIT indicates no limit. */
891 dict_set_case_limit (struct dictionary *d, int case_limit)
894 assert (case_limit >= 0);
896 d->case_limit = case_limit;
899 /* Returns the index of the next value to be added to D. This
900 value is the number of `union value's that need to be
901 allocated to store a case for dictionary D. */
903 dict_get_next_value_idx (const struct dictionary *d)
907 return d->next_value_idx;
910 /* Returns the number of bytes needed to store a case for
913 dict_get_case_size (const struct dictionary *d)
917 return sizeof (union value) * dict_get_next_value_idx (d);
920 /* Deletes scratch variables in dictionary D and reassigns values
921 so that fragmentation is eliminated. */
923 dict_compact_values (struct dictionary *d)
927 d->next_value_idx = 0;
928 for (i = 0; i < d->var_cnt; )
930 struct variable *v = d->var[i];
932 if (dict_class_from_id (v->name) != DC_SCRATCH)
934 v->fv = d->next_value_idx;
935 d->next_value_idx += v->nv;
939 dict_delete_var (default_dict, v);
943 /* Copies values from SRC, which represents a case arranged
944 according to dictionary D, to DST, which represents a case
945 arranged according to the dictionary that will be produced by
946 dict_compact_values(D). */
948 dict_compact_case (const struct dictionary *d,
949 struct ccase *dst, const struct ccase *src)
955 for (i = 0; i < d->var_cnt; i++)
957 struct variable *v = d->var[i];
959 if (dict_class_from_id (v->name) != DC_SCRATCH)
961 case_copy (dst, value_idx, src, v->fv, v->nv);
967 /* Returns the number of values that would be used by a case if
968 dict_compact_values() were called. */
970 dict_get_compacted_value_cnt (const struct dictionary *d)
976 for (i = 0; i < d->var_cnt; i++)
977 if (dict_class_from_id (d->var[i]->name) != DC_SCRATCH)
978 cnt += d->var[i]->nv;
982 /* Creates and returns an array mapping from a dictionary index
983 to the `fv' that the corresponding variable will have after
984 calling dict_compact_values(). Scratch variables receive -1
985 for `fv' because dict_compact_values() will delete them. */
987 dict_get_compacted_idx_to_fv (const struct dictionary *d)
990 size_t next_value_idx;
993 idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv);
995 for (i = 0; i < d->var_cnt; i++)
997 struct variable *v = d->var[i];
999 if (dict_class_from_id (v->name) != DC_SCRATCH)
1001 idx_to_fv[i] = next_value_idx;
1002 next_value_idx += v->nv;
1010 /* Returns the SPLIT FILE vars (see cmd_split_file()). Call
1011 dict_get_split_cnt() to determine how many SPLIT FILE vars
1012 there are. Returns a null pointer if and only if there are no
1014 struct variable *const *
1015 dict_get_split_vars (const struct dictionary *d)
1022 /* Returns the number of SPLIT FILE vars. */
1024 dict_get_split_cnt (const struct dictionary *d)
1028 return d->split_cnt;
1031 /* Sets CNT split vars SPLIT in dictionary D. */
1033 dict_set_split_vars (struct dictionary *d,
1034 struct variable *const *split, size_t cnt)
1037 assert (cnt == 0 || split != NULL);
1040 d->split = xrealloc (d->split, cnt * sizeof *d->split);
1041 memcpy (d->split, split, cnt * sizeof *d->split);
1044 /* Returns the file label for D, or a null pointer if D is
1045 unlabeled (see cmd_file_label()). */
1047 dict_get_label (const struct dictionary *d)
1054 /* Sets D's file label to LABEL, truncating it to a maximum of 60
1057 dict_set_label (struct dictionary *d, const char *label)
1064 else if (strlen (label) < 60)
1065 d->label = xstrdup (label);
1068 d->label = xmalloc (61);
1069 memcpy (d->label, label, 60);
1070 d->label[60] = '\0';
1074 /* Returns the documents for D, or a null pointer if D has no
1075 documents (see cmd_document()).. */
1077 dict_get_documents (const struct dictionary *d)
1081 return d->documents;
1084 /* Sets the documents for D to DOCUMENTS, or removes D's
1085 documents if DOCUMENT is a null pointer. */
1087 dict_set_documents (struct dictionary *d, const char *documents)
1091 free (d->documents);
1092 if (documents == NULL)
1093 d->documents = NULL;
1095 d->documents = xstrdup (documents);
1098 /* Creates in D a vector named NAME that contains CNT variables
1099 VAR (see cmd_vector()). Returns nonzero if successful, or
1100 zero if a vector named NAME already exists in D. */
1102 dict_create_vector (struct dictionary *d,
1104 struct variable **var, size_t cnt)
1106 struct vector *vector;
1109 assert (name != NULL);
1110 assert (strlen (name) > 0 && strlen (name) <= SHORT_NAME_LEN );
1111 assert (var != NULL);
1114 if (dict_lookup_vector (d, name) != NULL)
1117 d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector);
1118 vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector);
1119 vector->idx = d->vector_cnt++;
1120 strncpy (vector->name, name, SHORT_NAME_LEN);
1121 vector->name[SHORT_NAME_LEN] = '\0';
1122 vector->var = xmalloc (cnt * sizeof *var);
1123 memcpy (vector->var, var, cnt * sizeof *var);
1129 /* Returns the vector in D with index IDX, which must be less
1130 than dict_get_vector_cnt (D). */
1131 const struct vector *
1132 dict_get_vector (const struct dictionary *d, size_t idx)
1135 assert (idx < d->vector_cnt);
1137 return d->vector[idx];
1140 /* Returns the number of vectors in D. */
1142 dict_get_vector_cnt (const struct dictionary *d)
1146 return d->vector_cnt;
1149 /* Looks up and returns the vector within D with the given
1151 const struct vector *
1152 dict_lookup_vector (const struct dictionary *d, const char *name)
1157 assert (name != NULL);
1159 for (i = 0; i < d->vector_cnt; i++)
1160 if (!strcmp (d->vector[i]->name, name))
1161 return d->vector[i];
1165 /* Deletes all vectors from D. */
1167 dict_clear_vectors (struct dictionary *d)
1173 for (i = 0; i < d->vector_cnt; i++)
1175 free (d->vector[i]->var);
1176 free (d->vector[i]);
1184 static const char * quasi_base27(int i);
1187 /* Convert I to quasi base 27
1188 The result is a staticly allocated string.
1193 static char result[SHORT_NAME_LEN + 1];
1194 static char reverse[SHORT_NAME_LEN + 1];
1196 /* FIXME: check the result cant overflow these arrays */
1199 const int radix = 27;
1202 /* and here's the quasi-ness of this routine */
1203 i = i + ( i / radix );
1208 *s++ = (units > 0 ) ? units + 'A' - 1 : 'A';
1213 /* Reverse the result */
1224 /* Generate a short name, given a long name.
1225 The return value of this function must be freed by the caller.
1228 make_short_name(struct dictionary *dict, const char *longname)
1234 char *d = xmalloc ( SHORT_NAME_LEN + 1);
1236 /* Truncate the name */
1237 strncpy(d, longname, SHORT_NAME_LEN);
1238 d[SHORT_NAME_LEN] = '\0';
1240 /* Convert to upper case */
1241 for ( p = d; *p ; ++p )
1244 /* If a variable with that name already exists, then munge it
1245 until there's no conflict */
1246 while (0 != hsh_find (dict->name_tab, d))
1248 const char *suffix = quasi_base27(i++);
1250 d[SHORT_NAME_LEN - strlen(suffix) - 1 ] = '_';
1251 d[SHORT_NAME_LEN - strlen(suffix) ] = '\0';