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
23 #include "dictionary.h"
25 #include "file-handle.h"
30 #include "value-labels.h"
34 #define _(msgid) gettext (msgid)
36 #include "debug-print.h"
38 /* Parses and executes APPLY DICTIONARY. */
40 cmd_apply_dictionary (void)
42 struct file_handle *handle;
43 struct sfm_reader *reader;
44 struct dictionary *dict;
50 lex_match_id ("FROM");
56 reader = sfm_open_reader (handle, &dict, NULL);
59 sfm_close_reader (reader);
61 for (i = 0; i < dict_get_var_cnt (dict); i++)
63 struct variable *s = dict_get_var (dict, i);
64 struct variable *t = dict_lookup_var (default_dict, s->name);
69 if (s->type != t->type)
71 msg (SW, _("Variable %s is %s in target file, but %s in "
74 t->type == ALPHA ? _("string") : _("numeric"),
75 s->type == ALPHA ? _("string") : _("numeric"));
79 if (s->label && strcspn (s->label, " ") != strlen (s->label))
86 if (val_labs_count (s->val_labs) && t->width > MAX_SHORT_STRING)
87 msg (SW, _("Cannot add value labels from source file to "
88 "long string variable %s."),
90 else if (val_labs_count (s->val_labs))
92 /* Whether to apply the value labels. */
95 if (t->width < s->width)
97 struct val_labs_iterator *i;
100 for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
101 lab = val_labs_next (s->val_labs, &i))
105 /* We will apply the value labels only if all
106 the truncated characters are blanks. */
107 for (j = t->width; j < s->width; j++)
108 if (lab->value.s[j] != ' ')
118 /* Fortunately, we follow the convention that all value
119 label values are right-padded with spaces, so it is
120 unnecessary to bother padding values here. */
125 val_labs_destroy (t->val_labs);
126 t->val_labs = s->val_labs;
127 val_labs_set_width (t->val_labs, t->width);
128 s->val_labs = val_labs_create (s->width);
132 if (!mv_is_empty (&s->miss) && t->width > MAX_SHORT_STRING)
133 msg (SW, _("Cannot apply missing values from source file to "
134 "long string variable %s."),
136 else if (!mv_is_empty (&s->miss))
138 if (mv_is_resizable (&s->miss, t->width))
140 mv_copy (&t->miss, &s->miss);
141 mv_resize (&t->miss, t->width);
145 if (s->type == NUMERIC)
153 msg (SW, _("No matching variables found between the source "
154 "and target files."));
157 if (dict_get_weight (dict) != NULL)
159 struct variable *new_weight
160 = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
162 if (new_weight != NULL)
163 dict_set_weight (default_dict, new_weight);
166 sfm_close_reader (reader);
168 return lex_end_of_command ();