540024dbd1f6ee15c98a9deca208b5060b450978
[pspp-builds.git] / src / language / stats / correlations.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include <data/dictionary.h>
22 #include <data/file-handle-def.h>
23 #include <data/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/data-io/file-handle.h>
27 #include <language/lexer/lexer.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/compiler.h>
30
31 #include "xalloc.h"
32
33 /* (headers) */
34
35 struct cor_set
36   {
37     struct cor_set *next;
38     const struct variable **v1, **v2;
39     size_t nv1, nv2;
40   };
41
42 static struct cor_set *cor_list, *cor_last;
43
44 static struct file_handle *matrix_file;
45
46 static void free_correlations_state (void);
47 static int internal_cmd_correlations (struct lexer *lexer, struct dataset *ds);
48
49 int
50 cmd_correlations (struct lexer *lexer, struct dataset *ds)
51 {
52   int result = internal_cmd_correlations (lexer, ds);
53   free_correlations_state ();
54   return result;
55 }
56
57 /* (specification)
58    "CORRELATIONS" (cor_):
59      *variables=custom;
60      missing=miss:!pairwise/listwise,
61              inc:include/exclude;
62      +print=tail:!twotail/onetail,
63             sig:!sig/nosig;
64      +format=fmt:!matrix/serial;
65      +matrix=custom;
66      +statistics[st_]=descriptives,xprod,all.
67 */
68 /* (declarations) */
69 /* (functions) */
70
71 int
72 internal_cmd_correlations (struct lexer *lexer, struct dataset *ds)
73 {
74   struct cmd_correlations cmd;
75
76   cor_list = cor_last = NULL;
77   matrix_file = NULL;
78
79   if (!parse_correlations (lexer, ds, &cmd, NULL))
80     return CMD_FAILURE;
81
82   free_correlations (&cmd);
83
84   return CMD_SUCCESS;
85 }
86
87 static int
88 cor_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
89 {
90   const struct variable **v1, **v2;
91   size_t nv1, nv2;
92   struct cor_set *cor;
93
94   /* Ensure that this is a VARIABLES subcommand. */
95   if (!lex_match_id (lexer, "VARIABLES")
96       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) != NULL)
97       && lex_token (lexer) != T_ALL)
98     return 2;
99   lex_match (lexer, '=');
100
101   if (!parse_variables_const (lexer, dataset_dict (ds), &v1, &nv1,
102                         PV_NO_DUPLICATE | PV_NUMERIC))
103     return 0;
104
105   if (lex_match (lexer, T_WITH))
106     {
107       if (!parse_variables_const (lexer, dataset_dict (ds), &v2, &nv2,
108                             PV_NO_DUPLICATE | PV_NUMERIC))
109         {
110           free (v1);
111           return 0;
112         }
113     }
114   else
115     {
116       nv2 = nv1;
117       v2 = v1;
118     }
119
120   cor = xmalloc (sizeof *cor);
121   cor->next = NULL;
122   cor->v1 = v1;
123   cor->v2 = v2;
124   cor->nv1 = nv1;
125   cor->nv2 = nv2;
126   if (cor_list)
127     cor_last = cor_last->next = cor;
128   else
129     cor_list = cor_last = cor;
130
131   return 1;
132 }
133
134 static int
135 cor_custom_matrix (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
136 {
137   if (!lex_force_match (lexer, '('))
138     return 0;
139
140   if (lex_match (lexer, '*'))
141     matrix_file = NULL;
142   else
143     {
144       matrix_file = fh_parse (lexer, FH_REF_FILE);
145       if (matrix_file == NULL)
146         return 0;
147     }
148
149   if (!lex_force_match (lexer, ')'))
150     return 0;
151
152   return 1;
153 }
154
155 static void
156 free_correlations_state (void)
157 {
158   struct cor_set *cor, *next;
159
160   for (cor = cor_list; cor != NULL; cor = next)
161     {
162       next = cor->next;
163       if (cor->v1 != cor->v2)
164         free (cor->v2);
165       free (cor->v1);
166       free (cor);
167     }
168 }
169
170 /*
171   Local Variables:
172   mode: c
173   End:
174 */