variable: New function var_append_value_name__().
[pspp] / src / data / variable.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2013, 2014 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 #ifndef DATA_VARIABLE_H
18 #define DATA_VARIABLE_H 1
19
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include "data/dict-class.h"
23 #include "data/missing-values.h"
24 #include "data/val-type.h"
25 #include "data/settings.h"
26
27 /* Bitfields to identify traits of a variable */
28
29 #define VAR_TRAIT_NAME             0x0001
30 #define VAR_TRAIT_WIDTH            0x0002
31 #define VAR_TRAIT_ROLE             0x0004
32 #define VAR_TRAIT_LABEL            0x0008
33 #define VAR_TRAIT_VALUE_LABELS     0x0010
34 #define VAR_TRAIT_MISSING_VALUES   0x0020
35 #define VAR_TRAIT_ALIGNMENT        0x0040
36 #define VAR_TRAIT_MEASURE          0x0080
37 #define VAR_TRAIT_DISPLAY_WIDTH    0x0100
38 #define VAR_TRAIT_LEAVE            0x0200
39 #define VAR_TRAIT_POSITION         0x0400
40 #define VAR_TRAIT_ATTRIBUTES       0x0800
41 #define VAR_TRAIT_PRINT_FORMAT     0x1000
42 #define VAR_TRAIT_WRITE_FORMAT     0x2000
43
44
45 union value;
46
47 /* Variables.
48    These functions should rarely be called directly: use
49    dict_create_var, dict_clone_var, or dict_delete_var
50    instead. */
51 struct variable *var_create (const char *name, int width);
52 struct variable *var_clone (const struct variable *);
53 void var_destroy (struct variable *);
54
55 /* Variable names. */
56 const char *var_get_name (const struct variable *);
57 void var_set_name (struct variable *, const char *);
58 enum dict_class var_get_dict_class (const struct variable *);
59
60 int compare_vars_by_name (const void *, const void *, const void *);
61 unsigned hash_var_by_name (const void *, const void *);
62
63 int compare_var_ptrs_by_name (const void *, const void *, const void *);
64 unsigned hash_var_ptr_by_name (const void *, const void *);
65
66 int compare_var_ptrs_by_dict_index (const void *, const void *, const void *);
67
68 struct fmt_spec;
69
70 /* Types and widths of values associated with a variable. */
71 enum val_type var_get_type (const struct variable *);
72 int var_get_width (const struct variable *);
73 void var_set_width (struct variable *, int width);
74 void var_set_width_and_formats (struct variable *v, int new_width,
75                                 const struct fmt_spec *print, const struct fmt_spec *write);
76
77 bool var_is_numeric (const struct variable *);
78 bool var_is_alpha (const struct variable *);
79
80 /* Variables' missing values. */
81 const struct missing_values *var_get_missing_values (const struct variable *);
82 void var_set_missing_values (struct variable *, const struct missing_values *);
83 void var_clear_missing_values (struct variable *);
84 bool var_has_missing_values (const struct variable *);
85
86 bool var_is_value_missing (const struct variable *, const union value *,
87                            enum mv_class);
88 bool var_is_num_missing (const struct variable *, double, enum mv_class);
89 bool var_is_str_missing (const struct variable *, const uint8_t[], enum mv_class);
90
91 /* Value labels. */
92 const char *var_lookup_value_label (const struct variable *,
93                                     const union value *);
94 struct string;
95 void var_append_value_name (const struct variable *, const union value *,
96                             struct string *);
97 void var_append_value_name__ (const struct variable *, const union value *,
98                               enum settings_value_show, struct string *);
99
100 bool var_has_value_labels (const struct variable *);
101 const struct val_labs *var_get_value_labels (const struct variable *);
102 void var_set_value_labels (struct variable *, const struct val_labs *);
103
104 bool var_add_value_label (struct variable *,
105                           const union value *, const char *);
106 void var_replace_value_label (struct variable *,
107                               const union value *, const char *);
108 void var_clear_value_labels (struct variable *);
109
110 /* Print and write formats. */
111 const struct fmt_spec *var_get_print_format (const struct variable *);
112 void var_set_print_format (struct variable *, const struct fmt_spec *);
113 const struct fmt_spec *var_get_write_format (const struct variable *);
114 void var_set_write_format (struct variable *, const struct fmt_spec *);
115 void var_set_both_formats (struct variable *, const struct fmt_spec *);
116
117 struct fmt_spec var_default_formats (int width);
118
119 /* Variable labels. */
120 const char *var_to_string (const struct variable *);
121 const char *var_get_label (const struct variable *);
122 void var_set_label (struct variable *, const char *label);
123 void var_clear_label (struct variable *);
124 bool var_has_label (const struct variable *);
125
126 /* How data is measured. */
127 enum measure
128   {
129     MEASURE_NOMINAL = 0,
130     MEASURE_ORDINAL = 1,
131     MEASURE_SCALE = 2,
132     n_MEASURES
133   };
134
135 bool measure_is_valid (enum measure);
136 const char *measure_to_string (enum measure);
137 const char *measure_to_syntax (enum measure);
138
139 enum measure var_get_measure (const struct variable *);
140 void var_set_measure (struct variable *, enum measure);
141
142 enum measure var_default_measure (enum val_type);
143
144 /* Intended usage of a variable, for populating dialogs. */
145 enum var_role
146   {
147     ROLE_INPUT,
148     ROLE_TARGET,
149     ROLE_BOTH,
150     ROLE_NONE,
151     ROLE_PARTITION,
152     ROLE_SPLIT
153   };
154
155 bool var_role_is_valid (enum var_role);
156 const char *var_role_to_string (enum var_role);
157 const char *var_role_to_syntax (enum var_role);
158
159 enum var_role var_get_role (const struct variable *);
160 void var_set_role (struct variable *, enum var_role);
161
162 /* GUI display width. */
163 int var_get_display_width (const struct variable *);
164 void var_set_display_width (struct variable *, int display_width);
165
166 int var_default_display_width (int width);
167
168 /* Alignment of data for display. */
169 enum alignment
170   {
171     ALIGN_LEFT = 0,
172     ALIGN_RIGHT = 1,
173     ALIGN_CENTRE = 2
174   };
175
176 bool alignment_is_valid (enum alignment);
177 const char *alignment_to_string (enum alignment);
178 const char *alignment_to_syntax (enum alignment);
179
180 enum alignment var_get_alignment (const struct variable *);
181 void var_set_alignment (struct variable *, enum alignment);
182
183 enum alignment var_default_alignment (enum val_type);
184
185 /* Whether variables' values should be preserved from case to
186    case. */
187 bool var_get_leave (const struct variable *);
188 void var_set_leave (struct variable *, bool leave);
189 bool var_must_leave (const struct variable *);
190
191 /* Short names. */
192 size_t var_get_short_name_cnt (const struct variable *);
193 const char *var_get_short_name (const struct variable *, size_t idx);
194 void var_set_short_name (struct variable *, size_t, const char *);
195 void var_clear_short_names (struct variable *);
196
197 /* Relationship with dictionary. */
198 size_t var_get_dict_index (const struct variable *);
199 size_t var_get_case_index (const struct variable *);
200
201 /* Custom attributes. */
202 struct attrset *var_get_attributes (const struct variable *);
203 void var_set_attributes (struct variable *, const struct attrset *);
204 bool var_has_attributes (const struct variable *);
205
206 /* Encoding. */
207 const char *var_get_encoding (const struct variable *);
208
209 /* Function types. */
210 typedef bool var_predicate_func (const struct variable *);
211
212 double var_force_valid_weight (const struct variable *wv, double w,
213                                bool *warn_on_invalid);
214
215 #endif /* data/variable.h */