pool: New function pool_strdup0.
[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     {
81       fh_unref (matrix_file);
82       return CMD_FAILURE;
83     }
84
85   free_correlations (&cmd);
86   fh_unref (matrix_file);
87
88   return CMD_SUCCESS;
89 }
90
91 static int
92 cor_custom_variables (struct lexer *lexer, struct dataset *ds, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
93 {
94   const struct variable **v1, **v2;
95   size_t nv1, nv2;
96   struct cor_set *cor;
97
98   /* Ensure that this is a VARIABLES subcommand. */
99   if (!lex_match_id (lexer, "VARIABLES")
100       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) != NULL)
101       && lex_token (lexer) != T_ALL)
102     return 2;
103   lex_match (lexer, '=');
104
105   if (!parse_variables_const (lexer, dataset_dict (ds), &v1, &nv1,
106                         PV_NO_DUPLICATE | PV_NUMERIC))
107     return 0;
108
109   if (lex_match (lexer, T_WITH))
110     {
111       if (!parse_variables_const (lexer, dataset_dict (ds), &v2, &nv2,
112                             PV_NO_DUPLICATE | PV_NUMERIC))
113         {
114           free (v1);
115           return 0;
116         }
117     }
118   else
119     {
120       nv2 = nv1;
121       v2 = v1;
122     }
123
124   cor = xmalloc (sizeof *cor);
125   cor->next = NULL;
126   cor->v1 = v1;
127   cor->v2 = v2;
128   cor->nv1 = nv1;
129   cor->nv2 = nv2;
130   if (cor_list)
131     cor_last = cor_last->next = cor;
132   else
133     cor_list = cor_last = cor;
134
135   return 1;
136 }
137
138 static int
139 cor_custom_matrix (struct lexer *lexer, struct dataset *ds UNUSED, struct cmd_correlations *cmd UNUSED, void *aux UNUSED)
140 {
141   if (!lex_force_match (lexer, '('))
142     return 0;
143
144   if (lex_match (lexer, '*'))
145     matrix_file = NULL;
146   else
147     {
148       fh_unref (matrix_file);
149       matrix_file = fh_parse (lexer, FH_REF_FILE);
150       if (matrix_file == NULL)
151         return 0;
152     }
153
154   if (!lex_force_match (lexer, ')'))
155     return 0;
156
157   return 1;
158 }
159
160 static void
161 free_correlations_state (void)
162 {
163   struct cor_set *cor, *next;
164
165   for (cor = cor_list; cor != NULL; cor = next)
166     {
167       next = cor->next;
168       if (cor->v1 != cor->v2)
169         free (cor->v2);
170       free (cor->v1);
171       free (cor);
172     }
173 }
174
175 /*
176   Local Variables:
177   mode: c
178   End:
179 */