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