Move var_set and variable parsing declarations into new header.
[pspp-builds.git] / src / language / dictionary / split-file.c
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
22 #include <stdlib.h>
23
24 #include <data/case.h>
25 #include <data/dictionary.h>
26 #include <data/format.h>
27 #include <data/procedure.h>
28 #include <data/value-labels.h>
29 #include <data/variable.h>
30 #include <language/command.h>
31 #include <language/dictionary/split-file.h>
32 #include <language/lexer/lexer.h>
33 #include <language/lexer/variable-parser.h>
34 #include <libpspp/alloc.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37 #include <output/manager.h>
38 #include <output/table.h>
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 int
44 cmd_split_file (void)
45 {
46   if (lex_match_id ("OFF"))
47     dict_set_split_vars (default_dict, NULL, 0);
48   else
49     {
50       struct variable **v;
51       size_t n;
52
53       /* For now, ignore SEPARATE and LAYERED. */
54       lex_match_id ("SEPARATE") || lex_match_id ("LAYERED");
55       
56       lex_match (T_BY);
57       if (!parse_variables (default_dict, &v, &n, PV_NO_DUPLICATE))
58         return CMD_CASCADING_FAILURE;
59
60       dict_set_split_vars (default_dict, v, n);
61       free (v);
62     }
63
64   return lex_end_of_command ();
65 }
66
67 /* Dumps out the values of all the split variables for the case C. */
68 void
69 output_split_file_values (const struct ccase *c)
70 {
71   struct variable *const *split;
72   struct tab_table *t;
73   size_t split_cnt;
74   int i;
75
76   split_cnt = dict_get_split_cnt (default_dict);
77   if (split_cnt == 0)
78     return;
79
80   t = tab_create (3, split_cnt + 1, 0);
81   tab_dim (t, tab_natural_dimensions);
82   tab_vline (t, TAL_GAP, 1, 0, split_cnt);
83   tab_vline (t, TAL_GAP, 2, 0, split_cnt);
84   tab_text (t, 0, 0, TAB_NONE, _("Variable"));
85   tab_text (t, 1, 0, TAB_LEFT, _("Value"));
86   tab_text (t, 2, 0, TAB_LEFT, _("Label"));
87   split = dict_get_split_vars (default_dict);
88   for (i = 0; i < split_cnt; i++)
89     {
90       struct variable *v = split[i];
91       char temp_buf[80];
92       const char *val_lab;
93
94       assert (v->type == NUMERIC || v->type == ALPHA);
95       tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", v->name);
96       
97       data_out (temp_buf, &v->print, case_data (c, v->fv));
98       
99       temp_buf[v->print.w] = 0;
100       tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", v->print.w, temp_buf);
101
102       val_lab = val_labs_find (v->val_labs, *case_data (c, v->fv));
103       if (val_lab)
104         tab_text (t, 2, i + 1, TAB_LEFT, val_lab);
105     }
106   tab_flags (t, SOMF_NO_TITLE);
107   tab_submit (t);
108 }