Instead of making system or portable file readers responsible for
[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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #if !var_h
21 #define var_h 1
22
23 #include <stddef.h>
24 #include "format.h"
25 #include "val.h"
26
27 /* Frequency tables. */
28
29 /* Frequency table entry. */
30 struct freq
31   {
32     union value v;              /* The value. */
33     double c;                   /* The number of occurrences of the value. */
34   };
35
36 /* Types of frequency tables. */
37 enum
38   {
39     FRQM_GENERAL,
40     FRQM_INTEGER
41   };
42
43 /* Entire frequency table. */
44 struct freq_tab
45   {
46     int mode;                   /* FRQM_GENERAL or FRQM_INTEGER. */
47
48     /* General mode. */
49     struct hsh_table *data;     /* Undifferentiated data. */
50
51     /* Integer mode. */
52     double *vector;             /* Frequencies proper. */
53     int min, max;               /* The boundaries of the table. */
54     double out_of_range;        /* Sum of weights of out-of-range values. */
55     double sysmis;              /* Sum of weights of SYSMIS values. */
56
57     /* All modes. */
58     struct freq *valid;         /* Valid freqs. */
59     int n_valid;                /* Number of total freqs. */
60
61     struct freq *missing;       /* Missing freqs. */
62     int n_missing;              /* Number of missing freqs. */
63
64     /* Statistics. */
65     double total_cases;         /* Sum of weights of all cases. */
66     double valid_cases;         /* Sum of weights of valid cases. */
67   };
68 \f
69 /* Script variables. */
70
71 /* Variable type. */
72 enum
73   {
74     NUMERIC,                    /* A numeric variable. */
75     ALPHA                       /* A string variable.  (STRING is pre-empted by lexer.h) */
76   };
77
78 /* Types of missing values.  Order is significant, see
79    mis-val.c:parse_numeric(), sfm-read.c, sfm-write.c,
80    sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
81    pfm-read.c:read_variables(), pfm-write.c:write_variables(),
82    apply-dict.c:cmd_apply_dictionary(), and more (?). */
83 enum
84   {
85     MISSING_NONE,               /* No user-missing values. */
86     MISSING_1,                  /* One user-missing value. */
87     MISSING_2,                  /* Two user-missing values. */
88     MISSING_3,                  /* Three user-missing values. */
89     MISSING_RANGE,              /* [a,b]. */
90     MISSING_LOW,                /* (-inf,a]. */
91     MISSING_HIGH,               /* (a,+inf]. */
92     MISSING_RANGE_1,            /* [a,b], c. */
93     MISSING_LOW_1,              /* (-inf,a], b. */
94     MISSING_HIGH_1,             /* (a,+inf), b. */
95     MISSING_COUNT
96   };
97
98 /* A variable's dictionary entry.  */
99 struct variable
100   {
101     char name[9];               /* As a string. */
102     int index;                  /* Index into its dictionary's var[]. */
103     int type;                   /* NUMERIC or ALPHA. */
104
105     int width;                  /* Size of string variables in chars. */
106     int fv, nv;                 /* Index into `value's, number of values. */
107     unsigned init : 1;          /* 1=VFM must init and possibly reinit. */
108     unsigned reinit : 1;        /* Cases are: 1=reinitialized; 0=left. */
109
110     /* Missing values. */
111     int miss_type;              /* One of the MISSING_* constants. */
112     union value missing[3];     /* User-missing value. */
113
114     /* Display formats. */
115     struct fmt_spec print;      /* Default format for PRINT. */
116     struct fmt_spec write;      /* Default format for WRITE. */
117
118     /* Labels. */
119     struct val_labs *val_labs;  /* Value labels. */
120     char *label;                /* Variable label. */
121
122     /* Per-command info. */
123     void *aux;
124     void (*aux_dtor) (struct variable *);
125   };
126
127 int compare_variables (const void *, const void *, void *);
128 unsigned hash_variable (const void *, void *);
129
130 void *var_attach_aux (struct variable *,
131                       void *aux, void (*aux_dtor) (struct variable *));
132 void var_clear_aux (struct variable *);
133 void *var_detach_aux (struct variable *);
134 void var_dtor_free (struct variable *);
135
136 /* Classes of variables. */
137 enum dict_class 
138   {
139     DC_ORDINARY,                /* Ordinary identifier. */
140     DC_SYSTEM,                  /* System variable. */
141     DC_SCRATCH                  /* Scratch variable. */
142   };
143
144 enum dict_class dict_class_from_id (const char *name);
145 const char *dict_class_to_name (enum dict_class dict_class);
146 \f
147 /* Vector of variables. */
148 struct vector
149   {
150     int idx;                    /* Index for dict_get_vector(). */
151     char name[9];               /* Name. */
152     struct variable **var;      /* Vector of variables. */
153     int cnt;                    /* Number of variables. */
154   };
155 \f
156 \f
157 void discard_variables (void);
158
159 /* This is the active file dictionary. */
160 extern struct dictionary *default_dict;
161 \f
162 /* Transformation state. */
163
164 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
165    commands. */
166 extern struct file_handle *default_handle;
167
168 /* PROCESS IF expression. */
169 extern struct expression *process_if_expr;
170 \f
171 /* TEMPORARY support. */
172
173 /* 1=TEMPORARY has been executed at some point. */
174 extern int temporary;
175
176 /* If temporary!=0, the saved dictionary. */
177 extern struct dictionary *temp_dict;
178
179 /* If temporary!=0, index into t_trns[] (declared far below) that
180    gives the point at which data should be written out.  -1 means that
181    the data shouldn't be changed since all transformations are
182    temporary. */
183 extern int temp_trns;
184
185 /* If FILTER is active, whether it was executed before or after
186    TEMPORARY. */
187 extern int FILTER_before_TEMPORARY;
188
189 void cancel_temporary (void);
190 \f
191 /* Functions. */
192
193 struct ccase;
194 void dump_split_vars (const struct ccase *);
195 typedef int (* is_missing_func )(const union value *, const struct variable *);
196
197 int is_num_user_missing (double, const struct variable *);
198 int is_str_user_missing (const unsigned char[], const struct variable *);
199 int is_missing (const union value *, const struct variable *);
200 int is_system_missing (const union value *, const struct variable *);
201 int is_user_missing (const union value *, const struct variable *);
202 void copy_missing_values (struct variable *dest, const struct variable *src);
203 \f
204 /* Transformations. */
205
206 struct trns_header;
207 typedef int trns_proc_func (struct trns_header *, struct ccase *, int);
208 typedef void trns_free_func (struct trns_header *);
209
210 /* Header for all transformations. */
211 struct trns_header
212   {
213     int index;                  /* Index into t_trns[]. */
214     trns_proc_func *proc;       /* Transformation proc. */
215     trns_free_func *free;       /* Garbage collector proc. */
216   };
217
218 /* Array of transformations */
219 extern struct trns_header **t_trns;
220
221 /* Number of transformations, maximum number in array currently. */
222 extern int n_trns, m_trns;
223
224 /* Index of first transformation that is really a transformation.  Any
225    transformations before this belong to INPUT PROGRAM. */
226 extern int f_trns;
227
228 void add_transformation (struct trns_header *trns);
229 void cancel_transformations (void);
230 \f
231 struct var_set;
232
233 struct var_set *var_set_create_from_dict (const struct dictionary *d);
234 struct var_set *var_set_create_from_array (struct variable *const *var,
235                                            size_t);
236
237 size_t var_set_get_cnt (const struct var_set *vs);
238 struct variable *var_set_get_var (const struct var_set *vs, size_t idx);
239 struct variable *var_set_lookup_var (const struct var_set *vs,
240                                      const char *name);
241 void var_set_destroy (struct var_set *vs);
242 \f
243 /* Variable parsers. */
244
245 enum
246   {
247     PV_NONE = 0,                /* No options. */
248     PV_SINGLE = 0001,           /* Restrict to a single name or TO use. */
249     PV_DUPLICATE = 0002,        /* Don't merge duplicates. */
250     PV_APPEND = 0004,           /* Append to existing list. */
251     PV_NO_DUPLICATE = 0010,     /* Error on duplicates. */
252     PV_NUMERIC = 0020,          /* Vars must be numeric. */
253     PV_STRING = 0040,           /* Vars must be string. */
254     PV_SAME_TYPE = 00100,       /* All vars must be the same type. */
255     PV_NO_SCRATCH = 00200       /* Disallow scratch variables. */
256   };
257
258 struct variable *parse_variable (void);
259 struct variable *parse_dict_variable (const struct dictionary *);
260 int parse_variables (const struct dictionary *, struct variable ***, int *,
261                      int opts);
262 int parse_var_set_vars (const struct var_set *, struct variable ***, int *,
263                         int opts);
264 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
265 int parse_mixed_vars (char ***names, int *cnt, int opts);
266
267
268
269 /* Return a string representing this variable, in the form most 
270    appropriate from a human factors perspective.
271    (IE: the label if it has one, otherwise the name )
272 */
273 const char * var_to_string(const struct variable *var);
274
275
276 #endif /* !var_h */