Fixed reading of system files with non-ascii characters in variable names.
[pspp-builds.git] / src / data / variable.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 !variable_h
21 #define variable_h 1
22
23
24 #include <stddef.h>
25 #include "config.h"
26 #include <stdbool.h>
27 #include "category.h"
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 /* Maximum variable name lengths. */
44 #define SHORT_NAME_LEN 8        /* Short name length. */
45 #define LONG_NAME_LEN 64        /* Long name length. */
46
47 /* A variable's dictionary entry.  */
48 struct variable
49   {
50     /* Dictionary information. */
51     char name[LONG_NAME_LEN + 1]; /* Variable name.  Mixed case. */
52     enum var_type type;         /* NUMERIC or ALPHA. */
53     int width;                  /* Size of string variables in chars. */
54     struct missing_values miss; /* Missing values. */
55     struct fmt_spec print;      /* Default format for PRINT. */
56     struct fmt_spec write;      /* Default format for WRITE. */
57     struct val_labs *val_labs;  /* Value labels. */
58     char *label;                /* Variable label. */
59     enum measure measure;       /* Nominal, ordinal, or continuous. */
60     int display_width;          /* Width of data editor column. */
61     enum alignment alignment;   /* Alignment of data in GUI. */
62
63     /* Case information. */
64     int fv, nv;                 /* Index into `value's, number of values. */
65     bool init;                  /* True if needs init and possibly reinit. */
66     bool reinit;                /* True: reinitialize; false: leave. */
67
68     /* Data for use by containing dictionary. */
69     int index;                  /* Dictionary index. */
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     /* Each command may use these fields as needed. */
79     void *aux;
80     void (*aux_dtor) (struct variable *);
81
82     /* Values of a categorical variable.  Procedures need
83        vectors with binary entries, so any variable of type ALPHA will
84        have its values stored here. */
85     struct cat_vals *obs_vals;
86   };
87
88 /* Variable names. */
89 bool var_is_valid_name (const char *, bool issue_error);
90 bool var_is_plausible_name (const char *name, bool issue_error);
91 int compare_var_names (const void *, const void *, void *);
92 unsigned hash_var_name (const void *, void *);
93
94 /* Short names. */
95 void var_set_short_name (struct variable *, const char *);
96 void var_set_short_name_suffix (struct variable *, const char *, int suffix);
97 void var_clear_short_name (struct variable *);
98
99 /* Pointers to `struct variable', by name. */
100 int compare_var_ptr_names (const void *, const void *, void *);
101 unsigned hash_var_ptr_name (const void *, void *);
102
103 /* Variable auxiliary data. */
104 void *var_attach_aux (struct variable *,
105                       void *aux, void (*aux_dtor) (struct variable *));
106 void var_clear_aux (struct variable *);
107 void *var_detach_aux (struct variable *);
108 void var_dtor_free (struct variable *);
109
110 /* Classes of variables. */
111 enum dict_class 
112   {
113     DC_ORDINARY,                /* Ordinary identifier. */
114     DC_SYSTEM,                  /* System variable. */
115     DC_SCRATCH                  /* Scratch variable. */
116   };
117
118 enum dict_class dict_class_from_id (const char *name);
119 const char *dict_class_to_name (enum dict_class dict_class);
120 \f
121 /* Vector of variables. */
122 struct vector
123   {
124     int idx;                    /* Index for dict_get_vector(). */
125     char name[LONG_NAME_LEN + 1]; /* Name. */
126     struct variable **var;      /* Vector of variables. */
127     int cnt;                    /* Number of variables. */
128   };
129 \f
130 void discard_variables (void);
131
132 /* This is the active file dictionary. */
133 extern struct dictionary *default_dict;
134 \f
135 /* Transformation state. */
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 void cancel_temporary (void);
155 \f
156 struct ccase;
157 void dump_split_vars (const struct ccase *);
158 \f
159 /* Transformations. */
160
161 /* trns_proc_func return values. */
162 #define TRNS_CONTINUE   -1 /* Continue to next transformation. */
163 #define TRNS_DROP_CASE  -2 /* Drop this case. */
164 #define TRNS_ERROR      -3 /* A serious error, so stop the procedure. */
165 #define TRNS_NEXT_CASE  -4 /* Skip to next case.  INPUT PROGRAM only. */
166 #define TRNS_END_FILE   -5 /* End of input.  INPUT PROGRAM only. */
167
168 typedef int trns_proc_func (void *, struct ccase *, int);
169 typedef bool trns_free_func (void *);
170
171 /* A transformation. */
172 struct transformation
173   {
174     trns_proc_func *proc;       /* Transformation proc. */
175     trns_free_func *free;       /* Garbage collector proc. */
176     void *private;              /* Private data. */
177   };
178
179 /* Array of transformations */
180 extern struct transformation *t_trns;
181
182 /* Number of transformations, maximum number in array currently. */
183 extern size_t n_trns, m_trns;
184
185 /* Index of first transformation that is really a transformation.  Any
186    transformations before this belong to INPUT PROGRAM. */
187 extern size_t f_trns;
188
189 void add_transformation (trns_proc_func *, trns_free_func *, void *);
190 size_t next_transformation (void);
191 bool cancel_transformations (void);
192 \f
193 struct var_set;
194
195 struct var_set *var_set_create_from_dict (const struct dictionary *d);
196 struct var_set *var_set_create_from_array (struct variable *const *var,
197                                            size_t);
198
199 size_t var_set_get_cnt (const struct var_set *vs);
200 struct variable *var_set_get_var (const struct var_set *vs, size_t idx);
201 struct variable *var_set_lookup_var (const struct var_set *vs,
202                                      const char *name);
203 bool var_set_lookup_var_idx (const struct var_set *vs, const char *name,
204                              size_t *idx);
205 void var_set_destroy (struct var_set *vs);
206 \f
207 /* Variable parsers. */
208
209 enum
210   {
211     PV_NONE = 0,                /* No options. */
212     PV_SINGLE = 0001,           /* Restrict to a single name or TO use. */
213     PV_DUPLICATE = 0002,        /* Don't merge duplicates. */
214     PV_APPEND = 0004,           /* Append to existing list. */
215     PV_NO_DUPLICATE = 0010,     /* Error on duplicates. */
216     PV_NUMERIC = 0020,          /* Vars must be numeric. */
217     PV_STRING = 0040,           /* Vars must be string. */
218     PV_SAME_TYPE = 00100,       /* All vars must be the same type. */
219     PV_NO_SCRATCH = 00200       /* Disallow scratch variables. */
220   };
221
222 struct pool;
223 struct variable *parse_variable (void);
224 struct variable *parse_dict_variable (const struct dictionary *);
225 int parse_variables (const struct dictionary *, struct variable ***, size_t *,
226                      int opts);
227 int parse_var_set_vars (const struct var_set *, struct variable ***, size_t *,
228                         int opts);
229 int parse_DATA_LIST_vars (char ***names, size_t *cnt, int opts);
230 int parse_mixed_vars (char ***names, size_t *cnt, int opts);
231 int parse_mixed_vars_pool (struct pool *,
232                            char ***names, size_t *cnt, int opts);
233
234
235 /* Return a string representing this variable, in the form most 
236    appropriate from a human factors perspective.
237    (IE: the label if it has one, otherwise the name )
238 */
239 const char * var_to_string(const struct variable *var);
240
241
242 #endif /* !variable.h */