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