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