Adopt use of gnulib for portability.
[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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "dictionary.h"
24 #include "error.h"
25 #include "alloc.h"
26 #include "command.h"
27 #include "hash.h"
28 #include "lexer.h"
29 #include "error.h"
30 #include "magic.h"
31 #include "var.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 /* (headers) */
37
38 #include "debug-print.h"
39
40 /* (specification)
41    means (mns_):
42      *tables=custom;
43      +format=lab:!labels/nolabels/nocatlabs,
44             name:!names/nonames,
45             val:!values/novalues,
46             fmt:!table/tree;
47      +missing=miss:!table/include/dependent;
48      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
49      +statistics[st_]=anova,linearity,all,none.
50 */
51 /* (declarations) */
52 /* (functions) */
53
54 /* TABLES: Variable lists for each dimension. */
55 int n_dim;              /* Number of dimensions. */
56 int *nv_dim;            /* Number of variables in each dimension. */
57 struct variable ***v_dim;       /* Variables in each dimension.  */
58
59 /* VARIABLES: List of variables. */
60 int n_var;
61 struct variable **v_var;
62
63 /* Parses and executes the T-TEST procedure. */
64 int
65 cmd_means (void)
66 {
67   struct cmd_means cmd;
68   int success = CMD_FAILURE;
69   
70   n_dim = 0;
71   nv_dim = NULL;
72   v_dim = NULL;
73   v_var = NULL;
74
75   if (!parse_means (&cmd))
76     goto free;
77
78   if (cmd.sbc_cells)
79     {
80       int i;
81       for (i = 0; i < MNS_CL_count; i++)
82         if (cmd.a_cells[i])
83           break;
84       if (i >= MNS_CL_count)
85         cmd.a_cells[MNS_CL_ALL] = 1;
86     }
87   else
88     cmd.a_cells[MNS_CL_DEFAULT] = 1;
89   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
90     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
91   if (cmd.a_cells[MNS_CL_ALL])
92     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
93
94   if (cmd.sbc_statistics)
95     {
96       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
97         cmd.a_statistics[MNS_ST_ANOVA] = 1;
98       if (cmd.a_statistics[MNS_ST_ALL])
99         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
100     }
101
102   if (!cmd.sbc_tables)
103     {
104       msg (SE, _("Missing required subcommand TABLES."));
105       goto free;
106     }
107
108   success = CMD_SUCCESS;
109
110 free:
111   {
112     int i;
113     
114     for (i = 0; i < n_dim; i++)
115       free (v_dim[i]);
116     free (nv_dim);
117     free (v_dim);
118     free (v_var);
119   }
120   
121   return success;
122 }
123
124 /* Parses the TABLES subcommand. */
125 static int
126 mns_custom_tables (struct cmd_means *cmd)
127 {
128   struct var_set *var_set;
129   
130   if (!lex_match_id ("TABLES")
131       && (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
132       && token != T_ALL)
133     return 2;
134   lex_match ('=');
135
136   if (cmd->sbc_tables)
137     {
138       msg (SE, _("TABLES subcommand may not appear more "
139                  "than once."));
140       return 0;
141     }
142
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   while (lex_match (T_BY));
163
164   var_set_destroy (var_set);
165   return 1;
166
167  lossage:
168   var_set_destroy (var_set);
169   return 0;
170 }
171
172 /* 
173    Local Variables:
174    mode: c
175    End:
176 */