Fix memory leaks.
[pspp-builds.git] / src / means.q
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 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "error.h"
24 #include "alloc.h"
25 #include "command.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "error.h"
29 #include "magic.h"
30 #include "var.h"
31 /* (headers) */
32
33 #include "debug-print.h"
34
35 /* (specification)
36    means (mns_):
37      *tables=custom;
38      +variables=custom;
39      +crossbreak=custom;
40      +format=lab:!labels/nolabels/nocatlabs,
41             name:!names/nonames,
42             val:!values/novalues,
43             fmt:!table/tree;
44      +missing=miss:!table/include/dependent;
45      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
46      +statistics[st_]=anova,linearity,all,none.
47 */
48 /* (declarations) */
49 /* (functions) */
50
51 /* TABLES: Variable lists for each dimension. */
52 int n_dim;              /* Number of dimensions. */
53 int *nv_dim;            /* Number of variables in each dimension. */
54 struct variable ***v_dim;       /* Variables in each dimension.  */
55
56 /* VARIABLES: List of variables. */
57 int n_var;
58 struct variable **v_var;
59
60 /* Parses and executes the T-TEST procedure. */
61 int
62 cmd_means (void)
63 {
64   struct cmd_means cmd;
65   int success = CMD_FAILURE;
66   
67   n_dim = 0;
68   nv_dim = NULL;
69   v_dim = NULL;
70   v_var = NULL;
71
72   if (!parse_means (&cmd))
73     goto free;
74
75   if (cmd.sbc_cells)
76     {
77       int i;
78       for (i = 0; i < MNS_CL_count; i++)
79         if (cmd.a_cells[i])
80           break;
81       if (i >= MNS_CL_count)
82         cmd.a_cells[MNS_CL_ALL] = 1;
83     }
84   else
85     cmd.a_cells[MNS_CL_DEFAULT] = 1;
86   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
87     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
88   if (cmd.a_cells[MNS_CL_ALL])
89     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
90
91   if (cmd.sbc_statistics)
92     {
93       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
94         cmd.a_statistics[MNS_ST_ANOVA] = 1;
95       if (cmd.a_statistics[MNS_ST_ALL])
96         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
97     }
98
99   if (!cmd.sbc_tables)
100     {
101       msg (SE, _("Missing required subcommand TABLES."));
102       goto free;
103     }
104
105   success = CMD_SUCCESS;
106
107 free:
108   {
109     int i;
110     
111     for (i = 0; i < n_dim; i++)
112       free (v_dim[i]);
113     free (nv_dim);
114     free (v_dim);
115     free (v_var);
116   }
117   
118   return success;
119 }
120
121 /* Parses the TABLES subcommand. */
122 static int
123 mns_custom_tables (struct cmd_means *cmd)
124 {
125   struct var_set *var_set;
126   
127   if (!lex_match_id ("TABLES")
128       && (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
129       && token != T_ALL)
130     return 2;
131   lex_match ('=');
132
133   if (cmd->sbc_tables || cmd->sbc_crossbreak)
134     {
135       msg (SE, _("TABLES or CROSSBREAK subcommand may not appear more "
136                  "than once."));
137       return 0;
138     }
139
140   if (cmd->sbc_variables) 
141     var_set = var_set_create_from_array (v_var, n_var);
142   else
143     var_set = var_set_create_from_dict (default_dict);
144   assert (var_set != NULL);
145
146   do
147     {
148       int nvl;
149       struct variable **vl;
150
151       if (!parse_var_set_vars (var_set, &vl, &nvl,
152                                PV_NO_DUPLICATE | PV_NO_SCRATCH)) 
153         goto lossage;
154       
155       n_dim++;
156       nv_dim = xrealloc (nv_dim, n_dim * sizeof (int));
157       v_dim = xrealloc (v_dim, n_dim * sizeof (struct variable **));
158
159       nv_dim[n_dim - 1] = nvl;
160       v_dim[n_dim - 1] = vl;
161
162       if (cmd->sbc_variables)
163         {
164           int i;
165
166           for (i = 0; i < nv_dim[0]; i++)
167             {
168               struct means_proc *v_inf = &v_dim[0][i]->p.mns;
169
170               if (v_inf->min == SYSMIS)
171                 {
172                   msg (SE, _("Variable %s specified on TABLES or "
173                              "CROSSBREAK, but not specified on "
174                              "VARIABLES."),
175                        v_dim[0][i]->name);
176                   goto lossage;
177                 }
178               
179               if (n_dim == 1)
180                 {
181                   v_inf->min = (int) v_inf->min;
182                   v_inf->max = (int) v_inf->max;
183                 } else {
184                   if (v_inf->min == LOWEST || v_inf->max == HIGHEST)
185                     {
186                       msg (SE, _("LOWEST and HIGHEST may not be used "
187                                  "for independent variables (%s)."),
188                            v_dim[0][i]->name);
189                       goto lossage;
190                     }
191                   if (v_inf->min != (int) v_inf->min
192                       || v_inf->max != (int) v_inf->max)
193                     {
194                       msg (SE, _("Independent variables (%s) may not "
195                                  "have noninteger endpoints in their "
196                                  "ranges."),
197                            v_dim[0][i]->name);
198                       goto lossage;
199                     }
200                 }
201             }
202         }
203     }
204   while (lex_match (T_BY));
205
206   var_set_destroy (var_set);
207   return 1;
208
209  lossage:
210   var_set_destroy (var_set);
211   return 0;
212 }
213
214 /* Parse CROSSBREAK subcommand. */
215 static int
216 mns_custom_crossbreak (struct cmd_means *cmd)
217 {
218   return mns_custom_tables (cmd);
219 }
220
221 /* Parses the VARIABLES subcommand. */
222 static int
223 mns_custom_variables (struct cmd_means *cmd)
224 {
225   if (cmd->sbc_tables)
226     {
227       msg (SE, _("VARIABLES must precede TABLES."));
228       return 0;
229     }
230
231   if (cmd->sbc_variables == 1)
232     {
233       int i;
234       
235       for (i = 0; i < dict_get_var_cnt (default_dict); i++)
236         dict_get_var (default_dict, i)->p.mns.min = SYSMIS;
237     }
238   
239   do
240     {
241       int orig_n = n_var;
242       
243       double min, max;
244       
245       if (!parse_variables (default_dict, &v_var, &n_var,
246                             PV_APPEND | PV_NO_DUPLICATE | PV_NO_SCRATCH))
247         return 0;
248
249       if (!lex_force_match ('('))
250         return 0;
251
252       /* Lower value. */
253       if (token == T_ID
254           && (!strcmp (tokid, "LO") || lex_id_match ("LOWEST", tokid)))
255         min = LOWEST;
256       else
257         {
258           if (!lex_force_num ())
259             return 0;
260           min = tokval;
261         }
262       lex_get ();
263
264       lex_match (',');
265
266       /* Higher value. */
267       if (token == T_ID
268           && (!strcmp (tokid, "HI") || lex_id_match ("HIGHEST", tokid)))
269         max = HIGHEST;
270       else
271         {
272           if (!lex_force_num ())
273             return 0;
274           max = tokval;
275         }
276       lex_get ();
277
278       if (!lex_force_match (')'))
279         return 0;
280
281       /* Range check. */
282       if (max < min)
283         {
284           msg (SE, _("Upper value (%g) is less than lower value "
285                      "(%g) on VARIABLES subcommand."), max, min);
286           return 0;
287         }
288       
289       {
290         int i;
291
292         for (i = orig_n; i < n_var; i++)
293           {
294             struct means_proc *v_inf = &v_var[i]->p.mns;
295
296             v_inf->min = min;
297             v_inf->max = max;
298           }
299       }
300     }
301   while (token != '/' && token != '.');
302   
303   return 1;
304 }
305
306 /* 
307    Local Variables:
308    mode: c
309    End:
310 */