Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / dictionary / split-file.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include <data/case.h>
22 #include <data/data-out.h>
23 #include <data/dictionary.h>
24 #include <data/format.h>
25 #include <data/procedure.h>
26 #include <data/value-labels.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/dictionary/split-file.h>
30 #include <language/lexer/lexer.h>
31 #include <language/lexer/variable-parser.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35 #include <output/manager.h>
36 #include <output/table.h>
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 int
42 cmd_split_file (struct lexer *lexer, struct dataset *ds)
43 {
44   if (lex_match_id (lexer, "OFF"))
45     dict_set_split_vars (dataset_dict (ds), NULL, 0);
46   else
47     {
48       struct variable **v;
49       size_t n;
50
51       /* For now, ignore SEPARATE and LAYERED. */
52       (void) ( lex_match_id (lexer, "SEPARATE") || lex_match_id (lexer, "LAYERED") );
53
54       lex_match (lexer, T_BY);
55       if (!parse_variables (lexer, dataset_dict (ds), &v, &n, PV_NO_DUPLICATE))
56         return CMD_CASCADING_FAILURE;
57
58       dict_set_split_vars (dataset_dict (ds), v, n);
59       free (v);
60     }
61
62   return lex_end_of_command (lexer);
63 }
64
65 /* Dumps out the values of all the split variables for the case C. */
66 void
67 output_split_file_values (const struct dataset *ds, const struct ccase *c)
68 {
69   const struct dictionary *dict = dataset_dict (ds);
70   const struct variable *const *split;
71   struct tab_table *t;
72   size_t split_cnt;
73   int i;
74
75   split_cnt = dict_get_split_cnt (dict);
76   if (split_cnt == 0)
77     return;
78
79   t = tab_create (3, split_cnt + 1, 0);
80   tab_dim (t, tab_natural_dimensions);
81   tab_vline (t, TAL_GAP, 1, 0, split_cnt);
82   tab_vline (t, TAL_GAP, 2, 0, split_cnt);
83   tab_text (t, 0, 0, TAB_NONE, _("Variable"));
84   tab_text (t, 1, 0, TAB_LEFT, _("Value"));
85   tab_text (t, 2, 0, TAB_LEFT, _("Label"));
86   split = dict_get_split_vars (dict);
87   for (i = 0; i < split_cnt; i++)
88     {
89       const struct variable *v = split[i];
90       char temp_buf[80];
91       const char *val_lab;
92       const struct fmt_spec *print = var_get_print_format (v);
93
94       tab_text (t, 0, i + 1, TAB_LEFT | TAT_PRINTF, "%s", var_get_name (v));
95
96       data_out (case_data (c, v), print, temp_buf);
97       temp_buf[print->w] = 0;
98
99       tab_text (t, 1, i + 1, TAT_PRINTF, "%.*s", print->w, temp_buf);
100
101       val_lab = var_lookup_value_label (v, case_data (c, v));
102       if (val_lab)
103         tab_text (t, 2, i + 1, TAB_LEFT, val_lab);
104     }
105   tab_flags (t, SOMF_NO_TITLE);
106   tab_submit (t);
107 }