1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
24 #include <data/any-reader.h>
25 #include <data/dictionary.h>
26 #include <data/file-handle-def.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/data-io/file-handle.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/hash.h>
34 #include <libpspp/message.h>
35 #include <libpspp/str.h>
38 #define _(msgid) gettext (msgid)
40 /* Parses and executes APPLY DICTIONARY. */
42 cmd_apply_dictionary (struct lexer *lexer, struct dataset *ds)
44 struct file_handle *handle;
45 struct any_reader *reader;
46 struct dictionary *dict;
52 lex_match_id (lexer, "FROM");
53 lex_match (lexer, '=');
54 handle = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
58 reader = any_reader_open (handle, &dict);
61 any_reader_close (reader);
63 for (i = 0; i < dict_get_var_cnt (dict); i++)
65 struct variable *s = dict_get_var (dict, i);
66 struct variable *t = dict_lookup_var (dataset_dict (ds),
72 if (var_get_type (s) != var_get_type (t))
74 msg (SW, _("Variable %s is %s in target file, but %s in "
77 var_is_alpha (t) ? _("string") : _("numeric"),
78 var_is_alpha (s) ? _("string") : _("numeric"));
82 if (var_get_label (s))
84 const char *label = var_get_label (s);
85 if (strcspn (label, " ") != strlen (label))
86 var_set_label (t, label);
89 if (val_labs_count (s->val_labs) && var_is_long_string (t))
90 msg (SW, _("Cannot add value labels from source file to "
91 "long string variable %s."),
93 else if (val_labs_count (s->val_labs))
95 if (val_labs_can_set_width (s->val_labs, var_get_width (t)))
97 val_labs_destroy (t->val_labs);
98 t->val_labs = s->val_labs;
99 val_labs_set_width (t->val_labs, var_get_width (t));
100 s->val_labs = val_labs_create (var_get_width (s));
104 if (var_has_missing_values (s))
106 if (!var_is_long_string (t))
108 struct missing_values miss;
109 mv_copy (&miss, var_get_missing_values (s));
110 if (mv_is_resizable (&miss, var_get_width (t)))
112 mv_resize (&miss, var_get_width (t));
113 var_set_missing_values (t, &miss);
117 msg (SW, _("Cannot apply missing values from source file to "
118 "long string variable %s."),
122 if (var_is_numeric (s))
124 var_set_print_format (t, var_get_print_format (s));
125 var_set_write_format (t, var_get_write_format (s));
130 msg (SW, _("No matching variables found between the source "
131 "and target files."));
134 if (dict_get_weight (dict) != NULL)
136 struct variable *new_weight
137 = dict_lookup_var (dataset_dict (ds),
138 var_get_name (dict_get_weight (dict)));
140 if (new_weight != NULL)
141 dict_set_weight (dataset_dict (ds), new_weight);
144 any_reader_close (reader);
146 return lex_end_of_command (lexer);