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