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., 59 Temple Place - Suite 330, Boston, MA
25 #include "file-handle.h"
31 #include "debug-print.h"
33 /* Parses and executes APPLY DICTIONARY. */
35 cmd_apply_dictionary (void)
37 struct file_handle *handle;
38 struct dictionary *dict;
44 lex_match_id ("APPLY");
45 lex_match_id ("DICTIONARY");
47 lex_match_id ("FROM");
49 handle = fh_parse_file_handle ();
53 dict = sfm_read_dictionary (handle, NULL);
57 for (i = 0; i < dict->nvar; i++)
59 struct variable *s = dict->var[i];
60 struct variable *t = find_variable (s->name);
65 if (s->type != t->type)
67 msg (SW, _("Variable %s is %s in target file, but %s in "
70 t->type == ALPHA ? _("string") : _("numeric"),
71 s->type == ALPHA ? _("string") : _("numeric"));
75 if (s->label && strcspn (s->label, " ") != strlen (s->label))
82 if (s->val_lab && t->width > MAX_SHORT_STRING)
83 msg (SW, _("Cannot add value labels from source file to "
84 "long string variable %s."),
88 if (t->width < s->width)
91 struct value_label *lab;
93 avl_traverser_init (iter);
94 while ((lab = avl_traverse (s->val_lab, &iter)) != NULL)
98 /* If the truncated characters aren't all blanks
99 anyway, then don't apply the value labels. */
100 for (j = t->width; j < s->width; j++)
101 if (lab->v.s[j] != ' ')
102 goto skip_value_labels;
107 /* Fortunately, we follow the convention that all value
108 label values are right-padded with spaces, so it is
109 unnecessary to bother padding values here. */
112 avl_destroy (t->val_lab, free_val_lab);
113 t->val_lab = s->val_lab;
118 if (s->miss_type != MISSING_NONE && t->width > MAX_SHORT_STRING)
119 msg (SW, _("Cannot apply missing values from source file to "
120 "long string variable %s."),
122 else if (s->miss_type != MISSING_NONE)
124 if (t->width < s->width)
126 static const int miss_count[MISSING_COUNT] =
128 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
133 for (j = 0; j < miss_count[s->miss_type]; j++)
134 for (k = t->width; k < s->width; k++)
135 if (s->missing[j].s[k] != ' ')
136 goto skip_missing_values;
139 t->miss_type = s->miss_type;
140 memcpy (t->missing, s->missing, sizeof s->missing);
143 if (s->type == NUMERIC)
151 msg (SW, _("No matching variables found between the source "
152 "and target files."));
156 const int tfw = find_variable (default_dict.weight_var) != 0;
157 const int sfw = dict->weight_var[0] != 0;
160 switch (10 * tfw + sfw)
163 /* The working file retains its weighting variable. */
168 /* Fall through to case 11. */
171 w = find_variable (dict->weight_var);
173 strcpy (default_dict.weight_var, dict->weight_var);
177 skip_missing_values: ;
179 sfm_maybe_close (handle);
181 return lex_end_of_command ();