1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
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.
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.
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/>. */
21 #include <data/any-reader.h>
22 #include <data/casereader.h>
23 #include <data/dictionary.h>
24 #include <data/file-handle-def.h>
25 #include <data/missing-values.h>
26 #include <data/procedure.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/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
39 /* Parses and executes APPLY DICTIONARY. */
41 cmd_apply_dictionary (struct lexer *lexer, struct dataset *ds)
43 struct file_handle *handle;
44 struct casereader *reader;
45 struct dictionary *dict;
51 lex_match_id (lexer, "FROM");
52 lex_match (lexer, '=');
54 handle = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
57 reader = any_reader_open (handle, &dict);
62 casereader_destroy (reader);
64 for (i = 0; i < dict_get_var_cnt (dict); i++)
66 struct variable *s = dict_get_var (dict, i);
67 struct variable *t = dict_lookup_var (dataset_dict (ds),
73 if (var_get_type (s) != var_get_type (t))
75 msg (SW, _("Variable %s is %s in target file, but %s in "
78 var_is_alpha (t) ? _("string") : _("numeric"),
79 var_is_alpha (s) ? _("string") : _("numeric"));
83 if (var_get_label (s))
85 const char *label = var_get_label (s);
86 if (strcspn (label, " ") != strlen (label))
87 var_set_label (t, label);
90 if (var_has_value_labels (s))
92 if (!var_is_long_string (t))
94 const struct val_labs *value_labels = var_get_value_labels (s);
95 if (val_labs_can_set_width (value_labels, var_get_width (t)))
96 var_set_value_labels (s, value_labels);
99 msg (SW, _("Cannot add value labels from source file to "
100 "long string variable %s."),
104 if (var_has_missing_values (s))
106 if (!var_is_long_string (t))
108 const struct missing_values *miss = var_get_missing_values (s);
109 if (mv_is_resizable (miss, var_get_width (t)))
110 var_set_missing_values (t, miss);
113 msg (SW, _("Cannot apply missing values from source file to "
114 "long string variable %s."),
118 if (var_is_numeric (s))
120 var_set_print_format (t, var_get_print_format (s));
121 var_set_write_format (t, var_get_write_format (s));
126 msg (SW, _("No matching variables found between the source "
127 "and target files."));
130 if (dict_get_weight (dict) != NULL)
132 struct variable *new_weight
133 = dict_lookup_var (dataset_dict (ds),
134 var_get_name (dict_get_weight (dict)));
136 if (new_weight != NULL)
137 dict_set_weight (dataset_dict (ds), new_weight);
140 return lex_end_of_command (lexer);