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