60b374ecfc0c16cdc33b32df80409a02e45fd157
[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 /* Script variables. */
28
29 /* Variable type. */
30 enum
31   {
32     NUMERIC,                    /* A numeric variable. */
33     ALPHA                       /* A string variable.  (STRING is pre-empted by lexer.h) */
34   };
35
36 /* Types of missing values.  Order is significant, see
37    mis-val.c:parse_numeric(), sfm-read.c, sfm-write.c,
38    sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
39    pfm-read.c:read_variables(), pfm-write.c:write_variables(),
40    apply-dict.c:cmd_apply_dictionary(), and more (?). */
41 enum
42   {
43     MISSING_NONE,               /* No user-missing values. */
44     MISSING_1,                  /* One user-missing value. */
45     MISSING_2,                  /* Two user-missing values. */
46     MISSING_3,                  /* Three user-missing values. */
47     MISSING_RANGE,              /* [a,b]. */
48     MISSING_LOW,                /* (-inf,a]. */
49     MISSING_HIGH,               /* (a,+inf]. */
50     MISSING_RANGE_1,            /* [a,b], c. */
51     MISSING_LOW_1,              /* (-inf,a], b. */
52     MISSING_HIGH_1,             /* (a,+inf), b. */
53     MISSING_COUNT
54   };
55
56 /* A variable's dictionary entry.  */
57 struct variable
58   {
59     char name[9];               /* As a string. */
60     int index;                  /* Index into its dictionary's var[]. */
61     int type;                   /* NUMERIC or ALPHA. */
62
63     int width;                  /* Size of string variables in chars. */
64     int fv, nv;                 /* Index into `value's, number of values. */
65     unsigned init : 1;          /* 1=VFM must init and possibly reinit. */
66     unsigned reinit : 1;        /* Cases are: 1=reinitialized; 0=left. */
67
68     /* Missing values. */
69     int miss_type;              /* One of the MISSING_* constants. */
70     union value missing[3];     /* User-missing value. */
71
72     /* Display formats. */
73     struct fmt_spec print;      /* Default format for PRINT. */
74     struct fmt_spec write;      /* Default format for WRITE. */
75
76     /* Labels. */
77     struct val_labs *val_labs;  /* Value labels. */
78     char *label;                /* Variable label. */
79
80     /* Per-command info. */
81     void *aux;
82     void (*aux_dtor) (struct variable *);
83   };
84
85 int compare_var_names (const void *, const void *, void *);
86 unsigned hash_var_name (const void *, void *);
87 int compare_var_ptr_names (const void *, const void *, void *);
88 unsigned hash_var_ptr_name (const void *, void *);
89
90 void *var_attach_aux (struct variable *,
91                       void *aux, void (*aux_dtor) (struct variable *));
92 void var_clear_aux (struct variable *);
93 void *var_detach_aux (struct variable *);
94 void var_dtor_free (struct variable *);
95
96 /* Classes of variables. */
97 enum dict_class 
98   {
99     DC_ORDINARY,                /* Ordinary identifier. */
100     DC_SYSTEM,                  /* System variable. */
101     DC_SCRATCH                  /* Scratch variable. */
102   };
103
104 enum dict_class dict_class_from_id (const char *name);
105 const char *dict_class_to_name (enum dict_class dict_class);
106 \f
107 /* Vector of variables. */
108 struct vector
109   {
110     int idx;                    /* Index for dict_get_vector(). */
111     char name[9];               /* Name. */
112     struct variable **var;      /* Vector of variables. */
113     int cnt;                    /* Number of variables. */
114   };
115 \f
116 \f
117 void discard_variables (void);
118
119 /* This is the active file dictionary. */
120 extern struct dictionary *default_dict;
121 \f
122 /* Transformation state. */
123
124 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
125    commands. */
126 extern struct file_handle *default_handle;
127
128 /* PROCESS IF expression. */
129 extern struct expression *process_if_expr;
130 \f
131 /* TEMPORARY support. */
132
133 /* 1=TEMPORARY has been executed at some point. */
134 extern int temporary;
135
136 /* If temporary!=0, the saved dictionary. */
137 extern struct dictionary *temp_dict;
138
139 /* If temporary!=0, index into t_trns[] (declared far below) that
140    gives the point at which data should be written out.  -1 means that
141    the data shouldn't be changed since all transformations are
142    temporary. */
143 extern int temp_trns;
144
145 /* If FILTER is active, whether it was executed before or after
146    TEMPORARY. */
147 extern int FILTER_before_TEMPORARY;
148
149 void cancel_temporary (void);
150 \f
151 /* Functions. */
152
153 struct ccase;
154 void dump_split_vars (const struct ccase *);
155 typedef int (* is_missing_func )(const union value *, const struct variable *);
156
157 int is_num_user_missing (double, const struct variable *);
158 int is_str_user_missing (const unsigned char[], const struct variable *);
159 int is_missing (const union value *, const struct variable *);
160 int is_system_missing (const union value *, const struct variable *);
161 int is_user_missing (const union value *, const struct variable *);
162 void copy_missing_values (struct variable *dest, const struct variable *src);
163 \f
164 /* Transformations. */
165
166 struct trns_header;
167 typedef int trns_proc_func (struct trns_header *, struct ccase *, int);
168 typedef void trns_free_func (struct trns_header *);
169
170 /* Header for all transformations. */
171 struct trns_header
172   {
173     int index;                  /* Index into t_trns[]. */
174     trns_proc_func *proc;       /* Transformation proc. */
175     trns_free_func *free;       /* Garbage collector proc. */
176   };
177
178 /* Array of transformations */
179 extern struct trns_header **t_trns;
180
181 /* Number of transformations, maximum number in array currently. */
182 extern int n_trns, m_trns;
183
184 /* Index of first transformation that is really a transformation.  Any
185    transformations before this belong to INPUT PROGRAM. */
186 extern int f_trns;
187
188 void add_transformation (struct trns_header *trns);
189 void cancel_transformations (void);
190 \f
191 struct var_set;
192
193 struct var_set *var_set_create_from_dict (const struct dictionary *d);
194 struct var_set *var_set_create_from_array (struct variable *const *var,
195                                            size_t);
196
197 size_t var_set_get_cnt (const struct var_set *vs);
198 struct variable *var_set_get_var (const struct var_set *vs, size_t idx);
199 struct variable *var_set_lookup_var (const struct var_set *vs,
200                                      const char *name);
201 int var_set_lookup_var_idx (const struct var_set *vs, const char *name);
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 variable *parse_variable (void);
220 struct variable *parse_dict_variable (const struct dictionary *);
221 int parse_variables (const struct dictionary *, struct variable ***, int *,
222                      int opts);
223 int parse_var_set_vars (const struct var_set *, struct variable ***, int *,
224                         int opts);
225 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
226 int parse_mixed_vars (char ***names, int *cnt, int opts);
227
228
229
230 /* Return a string representing this variable, in the form most 
231    appropriate from a human factors perspective.
232    (IE: the label if it has one, otherwise the name )
233 */
234 const char * var_to_string(const struct variable *var);
235
236
237 #endif /* !var_h */