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