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