76820b7f9c41d2f0e717d7658ef65a48fa3d30f1
[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 "command.h"
23 #include "error.h"
24 #include "file-handle.h"
25 #include "hash.h"
26 #include "lexer.h"
27 #include "sfm.h"
28 #include "str.h"
29 #include "value-labels.h"
30 #include "var.h"
31
32 #include "debug-print.h"
33
34 /* Parses and executes APPLY DICTIONARY. */
35 int
36 cmd_apply_dictionary (void)
37 {
38   struct file_handle *handle;
39   struct dictionary *dict;
40
41   int n_matched = 0;
42
43   int i;
44   
45   lex_match_id ("APPLY");
46   lex_match_id ("DICTIONARY");
47   
48   lex_match_id ("FROM");
49   lex_match ('=');
50   handle = fh_parse_file_handle ();
51   if (!handle)
52     return CMD_FAILURE;
53
54   dict = sfm_read_dictionary (handle, NULL);
55   if (dict == NULL)
56     return CMD_FAILURE;
57
58   for (i = 0; i < dict->nvar; i++)
59     {
60       struct variable *s = dict->var[i];
61       struct variable *t = find_variable (s->name);
62       if (t == NULL)
63         continue;
64
65       n_matched++;
66       if (s->type != t->type)
67         {
68           msg (SW, _("Variable %s is %s in target file, but %s in "
69                      "source file."),
70                s->name,
71                t->type == ALPHA ? _("string") : _("numeric"),
72                s->type == ALPHA ? _("string") : _("numeric"));
73           continue;
74         }
75
76       if (s->label && strcspn (s->label, " ") != strlen (s->label))
77         {
78           free (t->label);
79           t->label = s->label;
80           s->label = NULL;
81         }
82
83       if (val_labs_count (s->val_labs) && t->width > MAX_SHORT_STRING)
84         msg (SW, _("Cannot add value labels from source file to "
85                    "long string variable %s."),
86              s->name);
87       else if (val_labs_count (s->val_labs))
88         {
89           /* Whether to apply the value labels. */
90           int apply = 1;
91           
92           if (t->width < s->width)
93             {
94               struct val_labs_iterator *i;
95               struct val_lab *lab;
96
97               for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
98                    lab = val_labs_next (s->val_labs, &i))
99                 {
100                   int j;
101
102                   /* We will apply the value labels only if all
103                      the truncated characters are blanks. */
104                   for (j = t->width; j < s->width; j++)
105                     if (lab->value.s[j] != ' ') 
106                       {
107                         val_labs_done (&i);
108                         apply = 0;
109                         break; 
110                       }
111                 }
112             }
113           else
114             {
115               /* Fortunately, we follow the convention that all value
116                  label values are right-padded with spaces, so it is
117                  unnecessary to bother padding values here. */
118             }
119
120           if (apply) 
121             {
122               val_labs_destroy (t->val_labs);
123               t->val_labs = s->val_labs;
124               val_labs_set_width (t->val_labs, t->width);
125               s->val_labs = val_labs_create (s->width);
126             }
127         }
128
129       if (s->miss_type != MISSING_NONE && t->width > MAX_SHORT_STRING)
130         msg (SW, _("Cannot apply missing values from source file to "
131                    "long string variable %s."),
132              s->name);
133       else if (s->miss_type != MISSING_NONE)
134         {
135           if (t->width < s->width)
136             {
137               static const int miss_count[MISSING_COUNT] = 
138                 {
139                   0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
140                 };
141
142               int j, k;
143               
144               for (j = 0; j < miss_count[s->miss_type]; j++)
145                 for (k = t->width; k < s->width; k++)
146                   if (s->missing[j].s[k] != ' ')
147                     goto skip_missing_values;
148             }
149
150           t->miss_type = s->miss_type;
151           memcpy (t->missing, s->missing, sizeof s->missing);
152         }
153
154       if (s->type == NUMERIC)
155         {
156           t->print = s->print;
157           t->write = s->write;
158         }
159     }
160
161   if (!n_matched)
162     msg (SW, _("No matching variables found between the source "
163                "and target files."));
164       
165   /* Weighting. */
166   {
167     const int tfw = find_variable (default_dict.weight_var) != 0;
168     const int sfw = dict->weight_var[0] != 0;
169     struct variable *w;
170
171     switch (10 * tfw + sfw)
172       {
173       case 10:
174         /* The working file retains its weighting variable. */
175         break;
176
177       case 00:
178       case 01:
179         /* Fall through to case 11. */
180
181       case 11:
182         w = find_variable (dict->weight_var);
183         if (w)
184           strcpy (default_dict.weight_var, dict->weight_var);
185         break;
186       }
187   }
188  skip_missing_values: ;
189   
190   sfm_maybe_close (handle);
191
192   return lex_end_of_command ();
193 }