fa7c0d6df0dff6410e1e7f0d0aece0c959171a96
[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
24 #include <stddef.h>
25 #include "config.h"
26 #include <stdbool.h>
27 #include "category.h"
28 #include "format.h"
29 #include "missing-values.h"
30
31 /* Script variables. */
32
33 /* Variable type. */
34 enum var_type
35   {
36     NUMERIC,                    /* A numeric variable. */
37     ALPHA                       /* A string variable. */
38   };
39
40 const char *var_type_adj (enum var_type);
41 const char *var_type_noun (enum var_type);
42
43 /* Maximum lengths of short and long variable names.
44    Most operations support long variable names,
45    but some file formats are limited to short names. */
46 #define SHORT_NAME_LEN 8        /* Short name length. */
47 #define LONG_NAME_LEN 64        /* Long name length. */
48
49 /* A variable's dictionary entry.  */
50 struct variable
51   {
52     /* Dictionary information. */
53     char name[LONG_NAME_LEN + 1]; /* Variable name.  Mixed case. */
54     enum var_type type;         /* NUMERIC or ALPHA. */
55     int width;                  /* Size of string variables in chars. */
56     struct missing_values miss; /* Missing values. */
57     struct fmt_spec print;      /* Default format for PRINT. */
58     struct fmt_spec write;      /* Default format for WRITE. */
59     struct val_labs *val_labs;  /* Value labels. */
60     char *label;                /* Variable label. */
61     enum measure measure;       /* Nominal, ordinal, or continuous. */
62     int display_width;          /* Width of data editor column. */
63     enum alignment alignment;   /* Alignment of data in GUI. */
64
65     /* Case information. */
66     int fv, nv;                 /* Index into `value's, number of values. */
67     bool leave;                 /* Leave value from case to case? */
68
69     /* Data for use by containing dictionary. */
70     int index;                  /* Dictionary index. */
71
72     /* Short name, used only for system and portable file input
73        and output.  Upper case only.  There is no index for short
74        names.  Short names are not necessarily unique.  Any
75        variable may have no short name, indicated by an empty
76        string. */
77     char short_name[SHORT_NAME_LEN + 1];
78
79     /* Each command may use these fields as needed. */
80     void *aux;
81     void (*aux_dtor) (struct variable *);
82
83     /* Values of a categorical variable.  Procedures need
84        vectors with binary entries, so any variable of type ALPHA will
85        have its values stored here. */
86     struct cat_vals *obs_vals;
87   };
88
89 /* Variable names. */
90 bool var_is_valid_name (const char *, bool issue_error);
91 bool var_is_plausible_name (const char *name, bool issue_error);
92 int compare_var_names (const void *, const void *, void *);
93 unsigned hash_var_name (const void *, void *);
94
95 /* Short names. */
96 void var_set_short_name (struct variable *, const char *);
97 void var_set_short_name_suffix (struct variable *, const char *, int suffix);
98 void var_clear_short_name (struct variable *);
99
100 /* Pointers to `struct variable', by name. */
101 int compare_var_ptr_names (const void *, const void *, void *);
102 unsigned hash_var_ptr_name (const void *, void *);
103
104 /* Variable auxiliary data. */
105 void *var_attach_aux (struct variable *,
106                       void *aux, void (*aux_dtor) (struct variable *));
107 void var_clear_aux (struct variable *);
108 void *var_detach_aux (struct variable *);
109 void var_dtor_free (struct variable *);
110
111 /* Classes of variables. */
112 enum dict_class 
113   {
114     DC_ORDINARY,                /* Ordinary identifier. */
115     DC_SYSTEM,                  /* System variable. */
116     DC_SCRATCH                  /* Scratch variable. */
117   };
118
119 enum dict_class dict_class_from_id (const char *name);
120 const char *dict_class_to_name (enum dict_class dict_class);
121 \f
122 /* Vector of variables. */
123 struct vector
124   {
125     int idx;                    /* Index for dict_get_vector(). */
126     char name[LONG_NAME_LEN + 1]; /* Name. */
127     struct variable **var;      /* Vector of variables. */
128     int cnt;                    /* Number of variables. */
129   };
130 \f
131 void discard_variables (void);
132
133 /* This is the active file dictionary. */
134 extern struct dictionary *default_dict;
135 \f
136 /* Transformation state. */
137
138 /* PROCESS IF expression. */
139 extern struct expression *process_if_expr;
140 \f
141 /* TEMPORARY support. */
142
143 /* 1=TEMPORARY has been executed at some point. */
144 extern int temporary;
145
146 /* If temporary!=0, the saved dictionary. */
147 extern struct dictionary *temp_dict;
148
149 /* If temporary!=0, index into t_trns[] (declared far below) that
150    gives the point at which data should be written out.  -1 means that
151    the data shouldn't be changed since all transformations are
152    temporary. */
153 extern size_t temp_trns;
154
155 void cancel_temporary (void);
156 \f
157 struct ccase;
158 void dump_split_vars (const struct ccase *);
159 \f
160 /* Transformations. */
161
162 /* trns_proc_func return values. */
163 #define TRNS_CONTINUE   -1 /* Continue to next transformation. */
164 #define TRNS_DROP_CASE  -2 /* Drop this case. */
165 #define TRNS_ERROR      -3 /* A serious error, so stop the procedure. */
166 #define TRNS_NEXT_CASE  -4 /* Skip to next case.  INPUT PROGRAM only. */
167 #define TRNS_END_FILE   -5 /* End of input.  INPUT PROGRAM only. */
168
169 typedef int trns_proc_func (void *, struct ccase *, int);
170 typedef bool trns_free_func (void *);
171
172 /* A transformation. */
173 struct transformation
174   {
175     trns_proc_func *proc;       /* Transformation proc. */
176     trns_free_func *free;       /* Garbage collector proc. */
177     void *private;              /* Private data. */
178   };
179
180 /* Array of transformations */
181 extern struct transformation *t_trns;
182
183 /* Number of transformations, maximum number in array currently. */
184 extern size_t n_trns, m_trns;
185
186 /* Index of first transformation that is really a transformation.  Any
187    transformations before this belong to INPUT PROGRAM. */
188 extern size_t f_trns;
189
190 void add_transformation (trns_proc_func *, trns_free_func *, void *);
191 size_t next_transformation (void);
192 bool cancel_transformations (void);
193 \f
194 struct var_set;
195
196 struct var_set *var_set_create_from_dict (const struct dictionary *d);
197 struct var_set *var_set_create_from_array (struct variable *const *var,
198                                            size_t);
199
200 size_t var_set_get_cnt (const struct var_set *vs);
201 struct variable *var_set_get_var (const struct var_set *vs, size_t idx);
202 struct variable *var_set_lookup_var (const struct var_set *vs,
203                                      const char *name);
204 bool var_set_lookup_var_idx (const struct var_set *vs, const char *name,
205                              size_t *idx);
206 void var_set_destroy (struct var_set *vs);
207 \f
208 /* Variable parsers. */
209
210 enum
211   {
212     PV_NONE = 0,                /* No options. */
213     PV_SINGLE = 0001,           /* Restrict to a single name or TO use. */
214     PV_DUPLICATE = 0002,        /* Don't merge duplicates. */
215     PV_APPEND = 0004,           /* Append to existing list. */
216     PV_NO_DUPLICATE = 0010,     /* Error on duplicates. */
217     PV_NUMERIC = 0020,          /* Vars must be numeric. */
218     PV_STRING = 0040,           /* Vars must be string. */
219     PV_SAME_TYPE = 00100,       /* All vars must be the same type. */
220     PV_NO_SCRATCH = 00200       /* Disallow scratch variables. */
221   };
222
223 struct pool;
224 struct variable *parse_variable (void);
225 struct variable *parse_dict_variable (const struct dictionary *);
226 int parse_variables (const struct dictionary *, struct variable ***, size_t *,
227                      int opts);
228 int parse_var_set_vars (const struct var_set *, struct variable ***, size_t *,
229                         int opts);
230 int parse_DATA_LIST_vars (char ***names, size_t *cnt, int opts);
231 int parse_mixed_vars (char ***names, size_t *cnt, int opts);
232 int parse_mixed_vars_pool (struct pool *,
233                            char ***names, size_t *cnt, int opts);
234
235
236 /* Return a string representing this variable, in the form most 
237    appropriate from a human factors perspective.
238    (IE: the label if it has one, otherwise the name )
239 */
240 const char * var_to_string(const struct variable *var);
241
242
243 #endif /* !variable.h */