1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "cat-routines.h"
27 #include "dictionary.h"
28 #include "identifier.h"
29 #include "missing-values.h"
30 #include "value-labels.h"
33 #include <libpspp/alloc.h>
34 #include <libpspp/assertion.h>
35 #include <libpspp/compiler.h>
36 #include <libpspp/hash.h>
37 #include <libpspp/message.h>
38 #include <libpspp/str.h>
41 #define _(msgid) gettext (msgid)
46 /* Dictionary information. */
47 char name[LONG_NAME_LEN + 1]; /* Variable name. Mixed case. */
48 int width; /* 0 for numeric, otherwise string width. */
49 struct missing_values miss; /* Missing values. */
50 struct fmt_spec print; /* Default format for PRINT. */
51 struct fmt_spec write; /* Default format for WRITE. */
52 struct val_labs *val_labs; /* Value labels. */
53 char *label; /* Variable label. */
55 /* GUI information. */
56 enum measure measure; /* Nominal, ordinal, or continuous. */
57 int display_width; /* Width of data editor column. */
58 enum alignment alignment; /* Alignment of data in GUI. */
60 /* Case information. */
61 bool leave; /* Leave value from case to case? */
63 /* Data for use by containing dictionary. */
64 struct vardict_info vardict;
66 /* Short name, used only for system and portable file input
67 and output. Upper case only. Short names are not necessarily
68 unique. Any variable may have no short name, indicated by an
70 char short_name[SHORT_NAME_LEN + 1];
72 /* Each command may use these fields as needed. */
74 void (*aux_dtor) (struct variable *);
76 /* Values of a categorical variable. Procedures need
77 vectors with binary entries, so any variable of type ALPHA will
78 have its values stored here. */
79 struct cat_vals *obs_vals;
82 /* Returns true if VAR_TYPE is a valid variable type. */
84 var_type_is_valid (enum var_type var_type)
86 return var_type == VAR_NUMERIC || var_type == VAR_STRING;
89 /* Returns the variable type for the given width. */
91 var_type_from_width (int width)
93 return width != 0 ? VAR_STRING : VAR_NUMERIC;
96 /* Creates and returns a new variable with the given NAME and
97 WIDTH and other fields initialized to default values. The
98 variable is not added to a dictionary; for that, use
99 dict_create_var instead. */
101 var_create (const char *name, int width)
105 assert (width >= 0 && width <= MAX_STRING);
107 v = xmalloc (sizeof *v);
108 v->vardict.dict_index = v->vardict.case_index = -1;
109 var_set_name (v, name);
111 mv_init (&v->miss, width);
112 v->leave = var_must_leave (v);
113 if (var_is_numeric (v))
115 v->print = fmt_for_output (FMT_F, 8, 2);
116 v->alignment = ALIGN_RIGHT;
117 v->display_width = 8;
118 v->measure = MEASURE_SCALE;
122 v->print = fmt_for_output (FMT_A, var_get_width (v), 0);
123 v->alignment = ALIGN_LEFT;
124 v->display_width = 8;
125 v->measure = MEASURE_NOMINAL;
130 var_clear_short_name (v);
138 /* Creates and returns a clone of OLD_VAR. Most properties of
139 the new variable are copied from OLD_VAR, except:
141 - The variable's short name is not copied, because there is
142 no reason to give a new variable with potentially a new
143 name the same short name.
145 - The new variable is not added to OLD_VAR's dictionary by
146 default. Use dict_clone_var, instead, to do that.
148 - Auxiliary data and obs_vals are not copied. */
150 var_clone (const struct variable *old_var)
152 struct variable *new_var = var_create (var_get_name (old_var),
153 var_get_width (old_var));
155 var_set_missing_values (new_var, var_get_missing_values (old_var));
156 var_set_print_format (new_var, var_get_print_format (old_var));
157 var_set_write_format (new_var, var_get_write_format (old_var));
158 var_set_value_labels (new_var, var_get_value_labels (old_var));
159 var_set_label (new_var, var_get_label (old_var));
160 var_set_measure (new_var, var_get_measure (old_var));
161 var_set_display_width (new_var, var_get_display_width (old_var));
162 var_set_alignment (new_var, var_get_alignment (old_var));
163 var_set_leave (new_var, var_get_leave (old_var));
168 /* Destroys variable V.
169 V must not belong to a dictionary. If it does, use
170 dict_delete_var instead. */
172 var_destroy (struct variable *v)
176 assert (!var_has_vardict (v));
177 cat_stored_values_destroy (v->obs_vals);
179 val_labs_destroy (v->val_labs);
185 /* Variable names. */
187 /* Return variable V's name. */
189 var_get_name (const struct variable *v)
194 /* Sets V's name to NAME.
195 Do not use this function for a variable in a dictionary. Use
196 dict_rename_var instead. */
198 var_set_name (struct variable *v, const char *name)
200 assert (v->vardict.dict_index == -1);
201 assert (var_is_plausible_name (name, false));
203 str_copy_trunc (v->name, sizeof v->name, name);
204 dict_var_changed (v);
207 /* Returns true if NAME is an acceptable name for a variable,
208 false otherwise. If ISSUE_ERROR is true, issues an
209 explanatory error message on failure. */
211 var_is_valid_name (const char *name, bool issue_error)
216 assert (name != NULL);
218 /* Note that strlen returns number of BYTES, not the number of
220 length = strlen (name);
222 plausible = var_is_plausible_name(name, issue_error);
228 if (!lex_is_id1 (name[0]))
231 msg (SE, _("Character `%c' (in %s) may not appear "
232 "as the first character in a variable name."),
238 for (i = 0; i < length; i++)
240 if (!lex_is_idn (name[i]))
243 msg (SE, _("Character `%c' (in %s) may not appear in "
253 /* Returns true if NAME is an plausible name for a variable,
254 false otherwise. If ISSUE_ERROR is true, issues an
255 explanatory error message on failure.
256 This function makes no use of LC_CTYPE.
259 var_is_plausible_name (const char *name, bool issue_error)
263 assert (name != NULL);
265 /* Note that strlen returns number of BYTES, not the number of
267 length = strlen (name);
271 msg (SE, _("Variable name cannot be empty string."));
274 else if (length > LONG_NAME_LEN)
277 msg (SE, _("Variable name %s exceeds %d-character limit."),
278 name, (int) LONG_NAME_LEN);
282 if (lex_id_to_token (ss_cstr (name)) != T_ID)
285 msg (SE, _("`%s' may not be used as a variable name because it "
286 "is a reserved word."), name);
293 /* A hsh_compare_func that orders variables A and B by their
296 compare_vars_by_name (const void *a_, const void *b_, const void *aux UNUSED)
298 const struct variable *a = a_;
299 const struct variable *b = b_;
301 return strcasecmp (a->name, b->name);
304 /* A hsh_hash_func that hashes variable V based on its name. */
306 hash_var_by_name (const void *v_, const void *aux UNUSED)
308 const struct variable *v = v_;
310 return hsh_hash_case_string (v->name);
313 /* A hsh_compare_func that orders pointers to variables A and B
316 compare_var_ptrs_by_name (const void *a_, const void *b_,
317 const void *aux UNUSED)
319 struct variable *const *a = a_;
320 struct variable *const *b = b_;
322 return strcasecmp (var_get_name (*a), var_get_name (*b));
325 /* A hsh_hash_func that hashes pointer to variable V based on its
328 hash_var_ptr_by_name (const void *v_, const void *aux UNUSED)
330 struct variable *const *v = v_;
332 return hsh_hash_case_string (var_get_name (*v));
335 /* Returns the type of variable V. */
337 var_get_type (const struct variable *v)
339 return var_type_from_width (v->width);
342 /* Returns the width of variable V. */
344 var_get_width (const struct variable *v)
349 /* Sets the width of V to WIDTH. */
351 var_set_width (struct variable *v, int new_width)
353 enum var_type new_type = var_type_from_width (new_width);
355 if (mv_is_resizable (&v->miss, new_width))
356 mv_resize (&v->miss, new_width);
358 mv_init (&v->miss, new_width);
360 if (v->val_labs != NULL)
362 if (val_labs_can_set_width (v->val_labs, new_width))
363 val_labs_set_width (v->val_labs, new_width);
366 val_labs_destroy (v->val_labs);
371 if (var_get_type (v) != new_type)
373 v->print = (new_type == VAR_NUMERIC
374 ? fmt_for_output (FMT_F, 8, 2)
375 : fmt_for_output (FMT_A, new_width, 0));
378 else if (new_type == VAR_STRING)
380 v->print.w = v->print.type == FMT_AHEX ? new_width * 2 : new_width;
381 v->write.w = v->write.type == FMT_AHEX ? new_width * 2 : new_width;
384 v->width = new_width;
386 dict_var_changed (v);
389 /* Returns true if variable V is numeric, false otherwise. */
391 var_is_numeric (const struct variable *v)
393 return var_get_type (v) == VAR_NUMERIC;
396 /* Returns true if variable V is a string variable, false
399 var_is_alpha (const struct variable *v)
401 return var_get_type (v) == VAR_STRING;
404 /* Returns true if variable V is a short string variable, false
407 var_is_short_string (const struct variable *v)
409 return v->width > 0 && v->width <= MAX_SHORT_STRING;
412 /* Returns true if variable V is a long string variable, false
415 var_is_long_string (const struct variable *v)
417 return v->width > MAX_SHORT_STRING;
420 /* Returns the number of "union value"s need to store a value of
423 var_get_value_cnt (const struct variable *v)
425 return v->width == 0 ? 1 : DIV_RND_UP (v->width, MAX_SHORT_STRING);
428 /* Returns variable V's missing values. */
429 const struct missing_values *
430 var_get_missing_values (const struct variable *v)
435 /* Sets variable V's missing values to MISS, which must be of V's
436 width or at least resizable to V's width.
437 If MISS is null, then V's missing values, if any, are
440 var_set_missing_values (struct variable *v, const struct missing_values *miss)
444 assert (mv_is_resizable (miss, v->width));
445 mv_copy (&v->miss, miss);
446 mv_resize (&v->miss, v->width);
449 mv_init (&v->miss, v->width);
451 dict_var_changed (v);
454 /* Sets variable V to have no user-missing values. */
456 var_clear_missing_values (struct variable *v)
458 var_set_missing_values (v, NULL);
461 /* Returns true if V has any user-missing values,
464 var_has_missing_values (const struct variable *v)
466 return !mv_is_empty (&v->miss);
469 /* Returns true if VALUE is in the given CLASS of missing values
470 in V, false otherwise. */
472 var_is_value_missing (const struct variable *v, const union value *value,
475 return mv_is_value_missing (&v->miss, value, class);
478 /* Returns true if D is in the given CLASS of missing values in
480 V must be a numeric variable. */
482 var_is_num_missing (const struct variable *v, double d, enum mv_class class)
484 return mv_is_num_missing (&v->miss, d, class);
487 /* Returns true if S[] is a missing value for V, false otherwise.
488 S[] must contain exactly as many characters as V's width.
489 V must be a string variable. */
491 var_is_str_missing (const struct variable *v, const char s[],
494 return mv_is_str_missing (&v->miss, s, class);
497 /* Returns variable V's value labels,
498 possibly a null pointer if it has none. */
499 const struct val_labs *
500 var_get_value_labels (const struct variable *v)
505 /* Returns true if variable V has at least one value label. */
507 var_has_value_labels (const struct variable *v)
509 return val_labs_count (v->val_labs) > 0;
512 /* Sets variable V's value labels to a copy of VLS,
513 which must have a width equal to V's width or one that can be
514 changed to V's width.
515 If VLS is null, then V's value labels, if any, are removed. */
517 var_set_value_labels (struct variable *v, const struct val_labs *vls)
519 val_labs_destroy (v->val_labs);
524 assert (val_labs_can_set_width (vls, v->width));
525 v->val_labs = val_labs_copy (vls);
526 val_labs_set_width (v->val_labs, v->width);
527 dict_var_changed (v);
531 /* Makes sure that V has a set of value labels,
532 by assigning one to it if necessary. */
534 alloc_value_labels (struct variable *v)
536 assert (!var_is_long_string (v));
537 if (v->val_labs == NULL)
538 v->val_labs = val_labs_create (v->width);
541 /* Attempts to add a value label with the given VALUE and LABEL
542 to V. Returns true if successful, false if VALUE has an
544 V must not be a long string variable. */
546 var_add_value_label (struct variable *v,
547 const union value *value, const char *label)
549 alloc_value_labels (v);
550 return val_labs_add (v->val_labs, *value, label);
553 /* Adds or replaces a value label with the given VALUE and LABEL
555 V must not be a long string variable. */
557 var_replace_value_label (struct variable *v,
558 const union value *value, const char *label)
560 alloc_value_labels (v);
561 val_labs_replace (v->val_labs, *value, label);
564 /* Removes V's value labels, if any. */
566 var_clear_value_labels (struct variable *v)
568 var_set_value_labels (v, NULL);
571 /* Returns the label associated with VALUE for variable V,
572 or a null pointer if none. */
574 var_lookup_value_label (const struct variable *v, const union value *value)
576 return val_labs_find (v->val_labs, *value);
579 /* Get a string representing VALUE for variable V.
580 That is, if VALUE has a label, return that label,
581 otherwise format VALUE and return the formatted string. */
583 var_get_value_name (const struct variable *v, const union value *value)
585 const char *name = var_lookup_value_label (v, value);
588 static char buf[MAX_STRING + 1];
589 data_out (value, &v->print, buf);
590 buf[v->print.w] = '\0';
596 /* Print and write formats. */
598 /* Returns V's print format specification. */
599 const struct fmt_spec *
600 var_get_print_format (const struct variable *v)
605 /* Sets V's print format specification to PRINT, which must be a
606 valid format specification for outputting a variable of V's
609 var_set_print_format (struct variable *v, const struct fmt_spec *print)
611 assert (fmt_check_width_compat (print, v->width));
613 dict_var_changed (v);
616 /* Returns V's write format specification. */
617 const struct fmt_spec *
618 var_get_write_format (const struct variable *v)
623 /* Sets V's write format specification to WRITE, which must be a
624 valid format specification for outputting a variable of V's
627 var_set_write_format (struct variable *v, const struct fmt_spec *write)
629 assert (fmt_check_width_compat (write, v->width));
631 dict_var_changed (v);
634 /* Sets V's print and write format specifications to FORMAT,
635 which must be a valid format specification for outputting a
636 variable of V's width. */
638 var_set_both_formats (struct variable *v, const struct fmt_spec *format)
640 var_set_print_format (v, format);
641 var_set_write_format (v, format);
644 /* Return a string representing this variable, in the form most
645 appropriate from a human factors perspective, that is, its
646 variable label if it has one, otherwise its name. */
648 var_to_string (const struct variable *v)
650 return v->label != NULL ? v->label : v->name;
653 /* Returns V's variable label, or a null pointer if it has none. */
655 var_get_label (const struct variable *v)
660 /* Sets V's variable label to LABEL, stripping off leading and
661 trailing white space and truncating to 255 characters.
662 If LABEL is a null pointer or if LABEL is an empty string
663 (after stripping white space), then V's variable label (if
666 var_set_label (struct variable *v, const char *label)
673 struct substring s = ss_cstr (label);
674 ss_trim (&s, ss_cstr (CC_SPACES));
675 ss_truncate (&s, 255);
676 if (!ss_is_empty (s))
677 v->label = ss_xstrdup (s);
678 dict_var_changed (v);
682 /* Removes any variable label from V. */
684 var_clear_label (struct variable *v)
686 var_set_label (v, NULL);
689 /* Returns true if V has a variable V,
692 var_has_label (const struct variable *v)
694 return v->label != NULL;
697 /* Returns true if M is a valid variable measurement level,
700 measure_is_valid (enum measure m)
702 return m == MEASURE_NOMINAL || m == MEASURE_ORDINAL || m == MEASURE_SCALE;
705 /* Returns V's measurement level. */
707 var_get_measure (const struct variable *v)
712 /* Sets V's measurement level to MEASURE. */
714 var_set_measure (struct variable *v, enum measure measure)
716 assert (measure_is_valid (measure));
717 v->measure = measure;
718 dict_var_changed (v);
721 /* Returns V's display width, which applies only to GUIs. */
723 var_get_display_width (const struct variable *v)
725 return v->display_width;
731 /* Sets V's display width to DISPLAY_WIDTH. */
733 var_set_display_width (struct variable *v, int display_width)
735 v->display_width = display_width;
736 dict_var_changed (v);
739 /* Returns true if A is a valid alignment,
742 alignment_is_valid (enum alignment a)
744 return a == ALIGN_LEFT || a == ALIGN_RIGHT || a == ALIGN_CENTRE;
747 /* Returns V's display alignment, which applies only to GUIs. */
749 var_get_alignment (const struct variable *v)
754 /* Sets V's display alignment to ALIGNMENT. */
756 var_set_alignment (struct variable *v, enum alignment alignment)
758 assert (alignment_is_valid (alignment));
759 v->alignment = alignment;
760 dict_var_changed (v);
763 /* Whether variables' values should be preserved from case to
766 /* Returns true if variable V's value should be left from case to
767 case, instead of being reset to 0, system-missing, or blanks. */
769 var_get_leave (const struct variable *v)
774 /* Sets V's leave setting to LEAVE. */
776 var_set_leave (struct variable *v, bool leave)
778 assert (leave || !var_must_leave (v));
780 dict_var_changed (v);
783 /* Returns true if V must be left from case to case,
784 false if it can be set either way. */
786 var_must_leave (const struct variable *v)
788 return dict_class_from_id (v->name) == DC_SCRATCH;
791 /* Returns V's short name, if it has one, or a null pointer
794 Short names are used only for system and portable file input
795 and output. They are upper-case only, not necessarily unique,
796 and limited to SHORT_NAME_LEN characters (plus a null
797 terminator). Any variable may have no short name, indicated
798 by returning a null pointer. */
800 var_get_short_name (const struct variable *v)
802 return v->short_name[0] != '\0' ? v->short_name : NULL;
805 /* Sets V's short_name to SHORT_NAME, truncating it to
806 SHORT_NAME_LEN characters and converting it to uppercase in
807 the process. Specifying a null pointer for SHORT_NAME clears
808 the variable's short name. */
810 var_set_short_name (struct variable *v, const char *short_name)
813 assert (short_name == NULL || var_is_plausible_name (short_name, false));
815 if (short_name != NULL)
817 str_copy_trunc (v->short_name, sizeof v->short_name, short_name);
818 str_uppercase (v->short_name);
821 v->short_name[0] = '\0';
822 dict_var_changed (v);
825 /* Clears V's short name. */
827 var_clear_short_name (struct variable *v)
831 v->short_name[0] = '\0';
834 /* Relationship with dictionary. */
836 /* Returns V's index within its dictionary, the value
837 for which "dict_get_var (dict, index)" will return V.
838 V must be in a dictionary. */
840 var_get_dict_index (const struct variable *v)
842 assert (v->vardict.dict_index != -1);
843 return v->vardict.dict_index;
846 /* Returns V's index within the case represented by its
847 dictionary, that is, the value for which "case_data_idx (case,
848 index)" will return the data for V in that case.
849 V must be in a dictionary. */
851 var_get_case_index (const struct variable *v)
853 assert (v->vardict.case_index != -1);
854 return v->vardict.case_index;
857 /* Returns V's auxiliary data, or a null pointer if none has been
860 var_get_aux (const struct variable *v)
865 /* Assign auxiliary data AUX to variable V, which must not
866 already have auxiliary data. Before V's auxiliary data is
867 cleared, AUX_DTOR(V) will be called. (var_dtor_free, below,
868 may be appropriate for use as AUX_DTOR.) */
870 var_attach_aux (struct variable *v,
871 void *aux, void (*aux_dtor) (struct variable *))
873 assert (v->aux == NULL);
874 assert (aux != NULL);
876 v->aux_dtor = aux_dtor;
880 /* Remove auxiliary data, if any, from V, and return it, without
881 calling any associated destructor. */
883 var_detach_aux (struct variable *v)
886 assert (aux != NULL);
891 /* Clears auxiliary data, if any, from V, and calls any
892 associated destructor. */
894 var_clear_aux (struct variable *v)
899 if (v->aux_dtor != NULL)
905 /* This function is appropriate for use an auxiliary data
906 destructor (passed as AUX_DTOR to var_attach_aux()) for the
907 case where the auxiliary data should be passed to free(). */
909 var_dtor_free (struct variable *v)
914 /* Observed categorical values. */
916 /* Returns V's observed categorical values,
917 which V must have. */
919 var_get_obs_vals (const struct variable *v)
921 assert (v->obs_vals != NULL);
925 /* Sets V's observed categorical values to CAT_VALS. */
927 var_set_obs_vals (struct variable *v, struct cat_vals *cat_vals)
929 cat_stored_values_destroy (v->obs_vals);
930 v->obs_vals = cat_vals;
933 /* Returns true if V has observed categorical values,
936 var_has_obs_vals (const struct variable *v)
938 return v->obs_vals != NULL;
941 /* Returns the dictionary class corresponding to a variable named
944 dict_class_from_id (const char *name)
957 /* Returns the name of dictionary class DICT_CLASS. */
959 dict_class_to_name (enum dict_class dict_class)
964 return _("ordinary");
974 /* Returns V's vardict structure. */
975 const struct vardict_info *
976 var_get_vardict (const struct variable *v)
978 assert (var_has_vardict (v));
982 /* Sets V's vardict data to VARDICT. */
984 var_set_vardict (struct variable *v, const struct vardict_info *vardict)
986 assert (vardict->dict_index >= 0);
987 assert (vardict->case_index >= 0);
988 v->vardict = *vardict;
991 /* Returns true if V has vardict data. */
993 var_has_vardict (const struct variable *v)
995 return v->vardict.dict_index != -1;
998 /* Clears V's vardict data. */
1000 var_clear_vardict (struct variable *v)
1002 v->vardict.dict_index = v->vardict.case_index = -1;