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