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"
32 /*#define DEBUGGING 1*/
33 #include "debug-print.h"
35 /* Parses and executes APPLY DICTIONARY. */
37 cmd_apply_dictionary (void)
39 struct file_handle *handle;
40 struct dictionary *dict;
46 lex_match_id ("APPLY");
47 lex_match_id ("DICTIONARY");
49 lex_match_id ("FROM");
51 handle = fh_parse_file_handle ();
55 dict = sfm_read_dictionary (handle, NULL);
59 for (i = 0; i < dict->nvar; i++)
61 struct variable *s = dict->var[i];
62 struct variable *t = find_variable (s->name);
67 if (s->type != t->type)
69 msg (SW, _("Variable %s is %s in target file, but %s in "
72 t->type == ALPHA ? _("string") : _("numeric"),
73 s->type == ALPHA ? _("string") : _("numeric"));
77 if (s->label && strcspn (s->label, " ") != strlen (s->label))
84 if (s->val_lab && t->width > MAX_SHORT_STRING)
85 msg (SW, _("Cannot add value labels from source file to "
86 "long string variable %s."),
90 if (t->width < s->width)
93 struct value_label *lab;
95 avl_traverser_init (iter);
96 while ((lab = avl_traverse (s->val_lab, &iter)) != NULL)
100 /* If the truncated characters aren't all blanks
101 anyway, then don't apply the value labels. */
102 for (j = t->width; j < s->width; j++)
103 if (lab->v.s[j] != ' ')
104 goto skip_value_labels;
109 /* Fortunately, we follow the convention that all value
110 label values are right-padded with spaces, so it is
111 unnecessary to bother padding values here. */
114 avl_destroy (t->val_lab, free_val_lab);
115 t->val_lab = s->val_lab;
120 if (s->miss_type != MISSING_NONE && t->width > MAX_SHORT_STRING)
121 msg (SW, _("Cannot apply missing values from source file to "
122 "long string variable %s."),
124 else if (s->miss_type != MISSING_NONE)
126 if (t->width < s->width)
128 static const int miss_count[MISSING_COUNT] =
130 0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
135 for (j = 0; j < miss_count[s->miss_type]; j++)
136 for (k = t->width; k < s->width; k++)
137 if (s->missing[j].s[k] != ' ')
138 goto skip_missing_values;
141 t->miss_type = s->miss_type;
142 memcpy (t->missing, s->missing, sizeof s->missing);
145 if (s->type == NUMERIC)
153 msg (SW, _("No matching variables found between the source "
154 "and target files."));
158 const int tfw = find_variable (default_dict.weight_var) != 0;
159 const int sfw = dict->weight_var[0] != 0;
162 switch (10 * tfw + sfw)
165 /* The working file retains its weighting variable. */
170 /* Fall through to case 11. */
173 w = find_variable (dict->weight_var);
175 strcpy (default_dict.weight_var, dict->weight_var);
179 skip_missing_values: ;
181 sfm_maybe_close (handle);
183 return lex_end_of_command ();