Change license from GPLv2+ to GPLv3+.
[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/alloc.h>
30 #include <libpspp/compiler.h>
31
32 /* (headers) */
33
34 struct cor_set
35   {
36     struct cor_set *next;
37     const struct variable **v1, **v2;
38     size_t nv1, nv2;
39   };
40
41 static struct cor_set *cor_list, *cor_last;
42
43 static struct file_handle *matrix_file;
44
45 static void free_correlations_state (void);
46 static int internal_cmd_correlations (struct lexer *lexer, struct dataset *ds);
47
48 int
49 cmd_correlations (struct lexer *lexer, struct dataset *ds)
50 {
51   int result = internal_cmd_correlations (lexer, ds);
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 (struct lexer *lexer, struct dataset *ds)
72 {
73   struct cmd_correlations cmd;
74
75   cor_list = cor_last = NULL;
76   matrix_file = NULL;
77
78   if (!parse_correlations (lexer, ds, &cmd, NULL))
79     return CMD_FAILURE;
80
81   free_correlations (&cmd);
82
83   return CMD_SUCCESS;
84 }
85
86 static int
87 cor_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
88 {
89   const struct variable **v1, **v2;
90   size_t nv1, nv2;
91   struct cor_set *cor;
92
93   /* Ensure that this is a VARIABLES subcommand. */
94   if (!lex_match_id (lexer, "VARIABLES")
95       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) != NULL)
96       && lex_token (lexer) != T_ALL)
97     return 2;
98   lex_match (lexer, '=');
99
100   if (!parse_variables_const (lexer, dataset_dict (ds), &v1, &nv1,
101                         PV_NO_DUPLICATE | PV_NUMERIC))
102     return 0;
103
104   if (lex_match (lexer, T_WITH))
105     {
106       if (!parse_variables_const (lexer, dataset_dict (ds), &v2, &nv2,
107                             PV_NO_DUPLICATE | PV_NUMERIC))
108         {
109           free (v1);
110           return 0;
111         }
112     }
113   else
114     {
115       nv2 = nv1;
116       v2 = v1;
117     }
118
119   cor = xmalloc (sizeof *cor);
120   cor->next = NULL;
121   cor->v1 = v1;
122   cor->v2 = v2;
123   cor->nv1 = nv1;
124   cor->nv2 = nv2;
125   if (cor_list)
126     cor_last = cor_last->next = cor;
127   else
128     cor_list = cor_last = cor;
129
130   return 1;
131 }
132
133 static int
134 cor_custom_matrix (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
135 {
136   if (!lex_force_match (lexer, '('))
137     return 0;
138
139   if (lex_match (lexer, '*'))
140     matrix_file = NULL;
141   else
142     {
143       matrix_file = fh_parse (lexer, FH_REF_FILE);
144       if (matrix_file == NULL)
145         return 0;
146     }
147
148   if (!lex_force_match (lexer, ')'))
149     return 0;
150
151   return 1;
152 }
153
154 static void
155 free_correlations_state (void)
156 {
157   struct cor_set *cor, *next;
158
159   for (cor = cor_list; cor != NULL; cor = next)
160     {
161       next = cor->next;
162       if (cor->v1 != cor->v2)
163         free (cor->v2);
164       free (cor->v1);
165       free (cor);
166     }
167 }
168
169 /*
170   Local Variables:
171   mode: c
172   End:
173 */