variable: Make var_set_label() use the variable's own encoding.
[pspp-builds.git] / src / data / variable.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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
26 union value;
27
28 /* Variables.
29    These functions should rarely be called directly: use
30    dict_create_var, dict_clone_var, or dict_delete_var
31    instead. */
32 struct variable *var_create (const char *name, int width);
33 struct variable *var_clone (const struct variable *);
34 void var_destroy (struct variable *);
35
36 /* Variable names. */
37 const char *var_get_name (const struct variable *);
38 void var_set_name (struct variable *, const char *);
39 enum dict_class var_get_dict_class (const struct variable *);
40
41 int compare_vars_by_name (const void *, const void *, const void *);
42 unsigned hash_var_by_name (const void *, const void *);
43
44 int compare_var_ptrs_by_name (const void *, const void *, const void *);
45 unsigned hash_var_ptr_by_name (const void *, const void *);
46
47 int compare_var_ptrs_by_dict_index (const void *, const void *, const void *);
48
49 /* Types and widths of values associated with a variable. */
50 enum val_type var_get_type (const struct variable *);
51 int var_get_width (const struct variable *);
52 void var_set_width (struct variable *, int width);
53
54 bool var_is_numeric (const struct variable *);
55 bool var_is_alpha (const struct variable *);
56
57 /* Variables' missing values. */
58 const struct missing_values *var_get_missing_values (const struct variable *);
59 void var_set_missing_values (struct variable *, const struct missing_values *);
60 void var_clear_missing_values (struct variable *);
61 bool var_has_missing_values (const struct variable *);
62
63 bool var_is_value_missing (const struct variable *, const union value *,
64                            enum mv_class);
65 bool var_is_num_missing (const struct variable *, double, enum mv_class);
66 bool var_is_str_missing (const struct variable *, const uint8_t[], enum mv_class);
67
68 /* Value labels. */
69 const char *var_lookup_value_label (const struct variable *,
70                                     const union value *);
71 struct string;
72 void var_append_value_name (const struct variable *, const union value *,
73                             struct string *);
74
75 const char *
76 var_get_value_name (const struct variable *v, const union value *value);
77
78
79 bool var_has_value_labels (const struct variable *);
80 const struct val_labs *var_get_value_labels (const struct variable *);
81 void var_set_value_labels (struct variable *, const struct val_labs *);
82
83 bool var_add_value_label (struct variable *,
84                           const union value *, const char *);
85 void var_replace_value_label (struct variable *,
86                               const union value *, const char *);
87 void var_clear_value_labels (struct variable *);
88
89 /* Print and write formats. */
90 const struct fmt_spec *var_get_print_format (const struct variable *);
91 void var_set_print_format (struct variable *, const struct fmt_spec *);
92 const struct fmt_spec *var_get_write_format (const struct variable *);
93 void var_set_write_format (struct variable *, const struct fmt_spec *);
94 void var_set_both_formats (struct variable *, const struct fmt_spec *);
95
96 struct fmt_spec var_default_formats (int width);
97
98 /* Variable labels. */
99 const char *var_to_string (const struct variable *);
100 const char *var_get_label (const struct variable *);
101 bool var_set_label (struct variable *, const char *label, bool issue_warning);
102 void var_clear_label (struct variable *);
103 bool var_has_label (const struct variable *);
104
105 /* How data is measured. */
106 enum measure
107   {
108     MEASURE_NOMINAL = 0,
109     MEASURE_ORDINAL = 1,
110     MEASURE_SCALE = 2,
111     n_MEASURES
112   };
113
114 bool measure_is_valid (enum measure);
115 enum measure var_get_measure (const struct variable *);
116 void var_set_measure (struct variable *, enum measure);
117
118 enum measure var_default_measure (enum val_type);
119
120 /* GUI display width. */
121 int var_get_display_width (const struct variable *);
122 void var_set_display_width (struct variable *, int display_width);
123
124 int var_default_display_width (int width);
125
126 /* Alignment of data for display. */
127 enum alignment
128   {
129     ALIGN_LEFT = 0,
130     ALIGN_RIGHT = 1,
131     ALIGN_CENTRE = 2,
132     n_ALIGN
133   };
134
135 bool alignment_is_valid (enum alignment);
136 enum alignment var_get_alignment (const struct variable *);
137 void var_set_alignment (struct variable *, enum alignment);
138
139 enum alignment var_default_alignment (enum val_type);
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 /* Custom attributes. */
166 struct attrset *var_get_attributes (const struct variable *);
167 void var_set_attributes (struct variable *, const struct attrset *);
168 bool var_has_attributes (const struct variable *);
169
170 /* Encoding. */
171 const char *var_get_encoding (const struct variable *);
172
173 /* Function types. */
174 typedef bool var_predicate_func (const struct variable *);
175
176 #endif /* data/variable.h */