Added a --enable-debug option to configure and
[pspp-builds.git] / src / apply-dict.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include "avl.h"
23 #include "command.h"
24 #include "error.h"
25 #include "file-handle.h"
26 #include "lexer.h"
27 #include "sfm.h"
28 #include "str.h"
29 #include "var.h"
30
31 #include "debug-print.h"
32
33 /* Parses and executes APPLY DICTIONARY. */
34 int
35 cmd_apply_dictionary (void)
36 {
37   struct file_handle *handle;
38   struct dictionary *dict;
39
40   int n_matched = 0;
41
42   int i;
43   
44   lex_match_id ("APPLY");
45   lex_match_id ("DICTIONARY");
46   
47   lex_match_id ("FROM");
48   lex_match ('=');
49   handle = fh_parse_file_handle ();
50   if (!handle)
51     return CMD_FAILURE;
52
53   dict = sfm_read_dictionary (handle, NULL);
54   if (dict == NULL)
55     return CMD_FAILURE;
56
57   for (i = 0; i < dict->nvar; i++)
58     {
59       struct variable *s = dict->var[i];
60       struct variable *t = find_variable (s->name);
61       if (t == NULL)
62         continue;
63
64       n_matched++;
65       if (s->type != t->type)
66         {
67           msg (SW, _("Variable %s is %s in target file, but %s in "
68                      "source file."),
69                s->name,
70                t->type == ALPHA ? _("string") : _("numeric"),
71                s->type == ALPHA ? _("string") : _("numeric"));
72           continue;
73         }
74
75       if (s->label && strcspn (s->label, " ") != strlen (s->label))
76         {
77           free (t->label);
78           t->label = s->label;
79           s->label = NULL;
80         }
81
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."),
85              s->name);
86       else if (s->val_lab)
87         {
88           if (t->width < s->width)
89             {
90               avl_traverser iter;
91               struct value_label *lab;
92
93               avl_traverser_init (iter);
94               while ((lab = avl_traverse (s->val_lab, &iter)) != NULL)
95                 {
96                   int j;
97
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;
103                 }
104             }
105           else
106             {
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. */
110             }
111           
112           avl_destroy (t->val_lab, free_val_lab);
113           t->val_lab = s->val_lab;
114           s->val_lab = NULL;
115         }
116     skip_value_labels: ;
117
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."),
121              s->name);
122       else if (s->miss_type != MISSING_NONE)
123         {
124           if (t->width < s->width)
125             {
126               static const int miss_count[MISSING_COUNT] = 
127                 {
128                   0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
129                 };
130
131               int j, k;
132               
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;
137             }
138
139           t->miss_type = s->miss_type;
140           memcpy (t->missing, s->missing, sizeof s->missing);
141         }
142
143       if (s->type == NUMERIC)
144         {
145           t->print = s->print;
146           t->write = s->write;
147         }
148     }
149
150   if (!n_matched)
151     msg (SW, _("No matching variables found between the source "
152                "and target files."));
153       
154   /* Weighting. */
155   {
156     const int tfw = find_variable (default_dict.weight_var) != 0;
157     const int sfw = dict->weight_var[0] != 0;
158     struct variable *w;
159
160     switch (10 * tfw + sfw)
161       {
162       case 10:
163         /* The working file retains its weighting variable. */
164         break;
165
166       case 00:
167       case 01:
168         /* Fall through to case 11. */
169
170       case 11:
171         w = find_variable (dict->weight_var);
172         if (w)
173           strcpy (default_dict.weight_var, dict->weight_var);
174         break;
175       }
176   }
177  skip_missing_values: ;
178   
179   sfm_maybe_close (handle);
180
181   return lex_end_of_command ();
182 }