checkin of 0.3.0
[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 #undef DEBUGGING
32 /*#define DEBUGGING 1*/
33 #include "debug-print.h"
34
35 /* Parses and executes APPLY DICTIONARY. */
36 int
37 cmd_apply_dictionary (void)
38 {
39   struct file_handle *handle;
40   struct dictionary *dict;
41
42   int n_matched = 0;
43
44   int i;
45   
46   lex_match_id ("APPLY");
47   lex_match_id ("DICTIONARY");
48   
49   lex_match_id ("FROM");
50   lex_match ('=');
51   handle = fh_parse_file_handle ();
52   if (!handle)
53     return CMD_FAILURE;
54
55   dict = sfm_read_dictionary (handle, NULL);
56   if (dict == NULL)
57     return CMD_FAILURE;
58
59   for (i = 0; i < dict->nvar; i++)
60     {
61       struct variable *s = dict->var[i];
62       struct variable *t = find_variable (s->name);
63       if (t == NULL)
64         continue;
65
66       n_matched++;
67       if (s->type != t->type)
68         {
69           msg (SW, _("Variable %s is %s in target file, but %s in "
70                      "source file."),
71                s->name,
72                t->type == ALPHA ? _("string") : _("numeric"),
73                s->type == ALPHA ? _("string") : _("numeric"));
74           continue;
75         }
76
77       if (s->label && strcspn (s->label, " ") != strlen (s->label))
78         {
79           free (t->label);
80           t->label = s->label;
81           s->label = NULL;
82         }
83
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."),
87              s->name);
88       else if (s->val_lab)
89         {
90           if (t->width < s->width)
91             {
92               avl_traverser iter;
93               struct value_label *lab;
94
95               avl_traverser_init (iter);
96               while ((lab = avl_traverse (s->val_lab, &iter)) != NULL)
97                 {
98                   int j;
99
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;
105                 }
106             }
107           else
108             {
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. */
112             }
113           
114           avl_destroy (t->val_lab, free_val_lab);
115           t->val_lab = s->val_lab;
116           s->val_lab = NULL;
117         }
118     skip_value_labels: ;
119
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."),
123              s->name);
124       else if (s->miss_type != MISSING_NONE)
125         {
126           if (t->width < s->width)
127             {
128               static const int miss_count[MISSING_COUNT] = 
129                 {
130                   0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
131                 };
132
133               int j, k;
134               
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;
139             }
140
141           t->miss_type = s->miss_type;
142           memcpy (t->missing, s->missing, sizeof s->missing);
143         }
144
145       if (s->type == NUMERIC)
146         {
147           t->print = s->print;
148           t->write = s->write;
149         }
150     }
151
152   if (!n_matched)
153     msg (SW, _("No matching variables found between the source "
154                "and target files."));
155       
156   /* Weighting. */
157   {
158     const int tfw = find_variable (default_dict.weight_var) != 0;
159     const int sfw = dict->weight_var[0] != 0;
160     struct variable *w;
161
162     switch (10 * tfw + sfw)
163       {
164       case 10:
165         /* The working file retains its weighting variable. */
166         break;
167
168       case 00:
169       case 01:
170         /* Fall through to case 11. */
171
172       case 11:
173         w = find_variable (dict->weight_var);
174         if (w)
175           strcpy (default_dict.weight_var, dict->weight_var);
176         break;
177       }
178   }
179  skip_missing_values: ;
180   
181   sfm_maybe_close (handle);
182
183   return lex_end_of_command ();
184 }