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