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