APPLY DICTIONARY: Style improvements.
[pspp] / src / language / dictionary / apply-dictionary.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2012, 2014, 2016 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/any-reader.h"
22 #include "data/casereader.h"
23 #include "data/dataset.h"
24 #include "data/dictionary.h"
25 #include "data/file-handle-def.h"
26 #include "data/missing-values.h"
27 #include "data/value-labels.h"
28 #include "data/variable.h"
29 #include "language/command.h"
30 #include "language/data-io/file-handle.h"
31 #include "language/lexer/lexer.h"
32 #include "libpspp/message.h"
33 #include "libpspp/str.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* Parses and executes APPLY DICTIONARY. */
39 int
40 cmd_apply_dictionary (struct lexer *lexer, struct dataset *ds)
41 {
42   lex_match_id (lexer, "FROM");
43   lex_match (lexer, T_EQUALS);
44
45   struct file_handle *handle = fh_parse (lexer, FH_REF_FILE,
46                                          dataset_session (ds));
47   if (!handle)
48     return CMD_FAILURE;
49
50   struct dictionary *dict;
51   struct casereader *reader = any_reader_open_and_decode (handle, NULL, &dict,
52                                                           NULL);
53   fh_unref (handle);
54   if (!reader)
55     return CMD_FAILURE;
56
57   casereader_destroy (reader);
58
59   size_t n_matched = 0;
60   for (size_t i = 0; i < dict_get_n_vars (dict); i++)
61     {
62       const struct variable *s = dict_get_var (dict, i);
63       struct variable *t = dict_lookup_var (dataset_dict (ds),
64                                             var_get_name (s));
65       if (t == NULL)
66         continue;
67
68       n_matched++;
69       if (var_get_type (s) != var_get_type (t))
70         {
71           msg (SW, _("Variable %s is %s in target file, but %s in "
72                      "source file."),
73                var_get_name (s),
74                var_is_alpha (t) ? _("string") : _("numeric"),
75                var_is_alpha (s) ? _("string") : _("numeric"));
76           continue;
77         }
78
79       if (var_has_label (s))
80         var_set_label (t, var_get_label (s));
81
82       if (var_has_value_labels (s))
83         {
84           const struct val_labs *value_labels = var_get_value_labels (s);
85           if (val_labs_can_set_width (value_labels, var_get_width (t)))
86             var_set_value_labels (t, value_labels);
87         }
88
89       if (var_has_missing_values (s))
90         {
91           const struct missing_values *miss = var_get_missing_values (s);
92           if (mv_is_resizable (miss, var_get_width (t)))
93             var_set_missing_values (t, miss);
94         }
95
96       if (var_is_numeric (s))
97         {
98           var_set_print_format (t, var_get_print_format (s));
99           var_set_write_format (t, var_get_write_format (s));
100         }
101
102       if (var_has_attributes (s))
103         var_set_attributes (t, var_get_attributes (s));
104     }
105
106   if (!n_matched)
107     msg (SW, _("No matching variables found between the source "
108                "and target files."));
109
110   /* Data file attributes. */
111   if (dict_has_attributes (dict))
112     dict_set_attributes (dataset_dict (ds), dict_get_attributes (dict));
113
114   /* Weighting. */
115   if (dict_get_weight (dict) != NULL)
116     {
117       struct variable *new_weight
118         = dict_lookup_var (dataset_dict (ds),
119                            var_get_name (dict_get_weight (dict)));
120
121       if (new_weight != NULL)
122         dict_set_weight (dataset_dict (ds), new_weight);
123     }
124
125   dict_unref (dict);
126
127   return CMD_SUCCESS;
128 }