44cfba703fe56a162f9bd6f418cb311df95c27d1
[pspp-builds.git] / src / data / variable.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #if !variable_h
18 #define variable_h 1
19
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <data/missing-values.h>
23
24 union value;
25
26 /* Variable type. */
27 enum var_type
28   {
29     VAR_NUMERIC,                /* A numeric variable. */
30     VAR_STRING                  /* A string variable. */
31   };
32
33 bool var_type_is_valid (enum var_type);
34 enum var_type var_type_from_width (int width);
35
36 /* Variables. */
37 struct variable *var_create (const char *name, int width);
38 struct variable *var_clone (const struct variable *);
39 void var_destroy (struct variable *);
40
41 /* Variable names.
42    Long variable names can be used in most contexts, but a few
43    procedures and file formats are limited to short names. */
44 #define SHORT_NAME_LEN 8
45 #define LONG_NAME_LEN 64
46
47 const char *var_get_name (const struct variable *);
48 void var_set_name (struct variable *, const char *);
49 bool var_is_valid_name (const char *, bool issue_error);
50 bool var_is_plausible_name (const char *name, bool issue_error);
51
52 int compare_vars_by_name (const void *, const void *, const void *);
53 unsigned hash_var_by_name (const void *, const void *);
54
55 int compare_var_ptrs_by_name (const void *, const void *, const void *);
56 unsigned hash_var_ptr_by_name (const void *, const void *);
57
58 /* Variable types and widths. */
59 enum var_type var_get_type (const struct variable *);
60 int var_get_width (const struct variable *);
61 void var_set_width (struct variable *, int width);
62
63 typedef bool var_predicate_func (const struct variable *);
64
65 bool var_is_numeric (const struct variable *);
66 bool var_is_alpha (const struct variable *);
67 bool var_is_short_string (const struct variable *);
68 bool var_is_long_string (const struct variable *);
69 size_t var_get_value_cnt (const struct variable *);
70
71 /* Variables' missing values. */
72 const struct missing_values *var_get_missing_values (const struct variable *);
73 void var_set_missing_values (struct variable *, const struct missing_values *);
74 void var_clear_missing_values (struct variable *);
75 bool var_has_missing_values (const struct variable *);
76
77 bool var_is_value_missing (const struct variable *, const union value *,
78                            enum mv_class);
79 bool var_is_num_missing (const struct variable *, double, enum mv_class);
80 bool var_is_str_missing (const struct variable *, const char[], enum mv_class);
81
82 /* Value labels. */
83 const struct val_labs *var_get_value_labels (const struct variable *);
84 bool var_has_value_labels (const struct variable *);
85 void var_set_value_labels (struct variable *, const struct val_labs *);
86 bool var_add_value_label (struct variable *,
87                           const union value *, const char *);
88 void var_replace_value_label (struct variable *,
89                               const union value *, const char *);
90 void var_clear_value_labels (struct variable *);
91 const char *var_lookup_value_label (const struct variable *,
92                                     const union value *);
93 const char *var_get_value_name (const struct variable *, const union value *);
94
95 /* Print and write formats. */
96 const struct fmt_spec *var_get_print_format (const struct variable *);
97 void var_set_print_format (struct variable *, const struct fmt_spec *);
98 const struct fmt_spec *var_get_write_format (const struct variable *);
99 void var_set_write_format (struct variable *, const struct fmt_spec *);
100 void var_set_both_formats (struct variable *, const struct fmt_spec *);
101
102 /* Variable labels. */
103 const char *var_to_string (const struct variable *);
104 const char *var_get_label (const struct variable *);
105 void var_set_label (struct variable *, const char *);
106 void var_clear_label (struct variable *);
107 bool var_has_label (const struct variable *);
108
109 /* How data is measured. */
110 enum measure
111   {
112     MEASURE_NOMINAL = 0,
113     MEASURE_ORDINAL = 1,
114     MEASURE_SCALE = 2,
115     n_MEASURES
116   };
117
118 bool measure_is_valid (enum measure);
119 enum measure var_get_measure (const struct variable *);
120 void var_set_measure (struct variable *, enum measure);
121
122 /* GUI display width. */
123 int var_get_display_width (const struct variable *);
124 void var_set_display_width (struct variable *, int display_width);
125
126 int var_default_display_width (int width);
127
128 /* Alignment of data for display. */
129 enum alignment
130   {
131     ALIGN_LEFT = 0,
132     ALIGN_RIGHT = 1,
133     ALIGN_CENTRE = 2,
134     n_ALIGN
135   };
136
137 bool alignment_is_valid (enum alignment);
138 enum alignment var_get_alignment (const struct variable *);
139 void var_set_alignment (struct variable *, enum alignment);
140
141 /* Whether variables' values should be preserved from case to
142    case. */
143 bool var_get_leave (const struct variable *);
144 void var_set_leave (struct variable *, bool leave);
145 bool var_must_leave (const struct variable *);
146
147 /* Short names. */
148 size_t var_get_short_name_cnt (const struct variable *);
149 const char *var_get_short_name (const struct variable *, size_t idx);
150 void var_set_short_name (struct variable *, size_t, const char *);
151 void var_clear_short_names (struct variable *);
152
153 /* Relationship with dictionary. */
154 size_t var_get_dict_index (const struct variable *);
155 size_t var_get_case_index (const struct variable *);
156
157 /* Variable auxiliary data. */
158 void *var_get_aux (const struct variable *);
159 void *var_attach_aux (const struct variable *,
160                       void *aux, void (*aux_dtor) (struct variable *));
161 void var_clear_aux (struct variable *);
162 void *var_detach_aux (struct variable *);
163 void var_dtor_free (struct variable *);
164
165 /* Observed categorical values. */
166 struct cat_vals *var_get_obs_vals (const struct variable *);
167 void var_set_obs_vals (const struct variable *, struct cat_vals *);
168 bool var_has_obs_vals (const struct variable *);
169
170 /* Classes of variables. */
171 enum dict_class
172   {
173     DC_ORDINARY,                /* Ordinary identifier. */
174     DC_SYSTEM,                  /* System variable. */
175     DC_SCRATCH                  /* Scratch variable. */
176   };
177
178 enum dict_class dict_class_from_id (const char *name);
179 const char *dict_class_to_name (enum dict_class dict_class);
180
181 #endif /* !variable.h */