Fix some warnings.
[pspp-builds.git] / src / language / stats / correlations.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 <libpspp/alloc.h>
23 #include <libpspp/compiler.h>
24 #include <data/dictionary.h>
25 #include <data/file-handle-def.h>
26 #include <language/data-io/file-handle.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <data/variable.h>
30 /* (headers) */
31
32 #include <libpspp/debug-print.h>
33
34 struct cor_set
35   {
36     struct cor_set *next;
37     struct variable **v1, **v2;
38     size_t nv1, nv2;
39   };
40
41 struct cor_set *cor_list, *cor_last;
42
43 struct file_handle *matrix_file;
44
45 static void free_correlations_state (void);
46 static int internal_cmd_correlations (void);
47
48 int
49 cmd_correlations (void)
50 {
51   int result = internal_cmd_correlations ();
52   free_correlations_state ();
53   return result;
54 }
55
56 /* (specification)
57    "CORRELATIONS" (cor_):
58      *variables=custom;
59      +missing=miss:!pairwise/listwise,
60               inc:include/exclude;
61      +print=tail:!twotail/onetail,
62             sig:!sig/nosig;
63      +format=fmt:!matrix/serial;
64      +matrix=custom;
65      +statistics[st_]=descriptives,xprod,all.
66 */
67 /* (declarations) */
68 /* (functions) */
69
70 int
71 internal_cmd_correlations (void)
72 {
73   struct cmd_correlations cmd;
74
75   cor_list = cor_last = NULL;
76   matrix_file = NULL;
77
78   if (!parse_correlations (&cmd))
79     return CMD_FAILURE;
80   free_correlations (&cmd);
81
82   return CMD_SUCCESS;
83 }
84
85 static int
86 cor_custom_variables (struct cmd_correlations *cmd UNUSED)
87 {
88   struct variable **v1, **v2;
89   size_t nv1, nv2;
90   struct cor_set *cor;
91
92   /* Ensure that this is a VARIABLES subcommand. */
93   if (!lex_match_id ("VARIABLES")
94       && (token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
95       && token != T_ALL)
96     return 2;
97   lex_match ('=');
98
99   if (!parse_variables (default_dict, &v1, &nv1,
100                         PV_NO_DUPLICATE | PV_NUMERIC))
101     return 0;
102   
103   if (lex_match (T_WITH))
104     {
105       if (!parse_variables (default_dict, &v2, &nv2,
106                             PV_NO_DUPLICATE | PV_NUMERIC))
107         {
108           free (v1);
109           return 0;
110         }
111     }
112   else
113     {
114       nv2 = nv1;
115       v2 = v1;
116     }
117
118   cor = xmalloc (sizeof *cor);
119   cor->next = NULL;
120   cor->v1 = v1;
121   cor->v2 = v2;
122   cor->nv1 = nv1;
123   cor->nv2 = nv2;
124   if (cor_list)
125     cor_last = cor_last->next = cor;
126   else
127     cor_list = cor_last = cor;
128   
129   return 1;
130 }
131
132 static int
133 cor_custom_matrix (struct cmd_correlations *cmd UNUSED)
134 {
135   if (!lex_force_match ('('))
136     return 0;
137   
138   if (lex_match ('*'))
139     matrix_file = NULL;
140   else 
141     {
142       matrix_file = fh_parse (FH_REF_FILE);
143       if (matrix_file == NULL)
144         return 0; 
145     }
146
147   if (!lex_force_match (')'))
148     return 0;
149
150   return 1;
151 }
152
153 static void
154 free_correlations_state (void)
155 {
156   struct cor_set *cor, *next;
157
158   for (cor = cor_list; cor != NULL; cor = next)
159     {
160       next = cor->next;
161       if (cor->v1 != cor->v2)
162         free (cor->v2);
163       free (cor->v1);
164       free (cor);
165     }
166 }
167
168 /*
169   Local Variables:
170   mode: c
171   End:
172 */