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