Changed DFM from open-at-first-access to explicit-open. Before,
[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 ("FROM");
46   lex_match ('=');
47   handle = fh_parse_file_handle ();
48   if (!handle)
49     return CMD_FAILURE;
50
51   dict = sfm_read_dictionary (handle, NULL);
52   if (dict == NULL)
53     return CMD_FAILURE;
54
55   for (i = 0; i < dict_get_var_cnt (dict); i++)
56     {
57       struct variable *s = dict_get_var (dict, i);
58       struct variable *t = dict_lookup_var (default_dict, s->name);
59       if (t == NULL)
60         continue;
61
62       n_matched++;
63       if (s->type != t->type)
64         {
65           msg (SW, _("Variable %s is %s in target file, but %s in "
66                      "source file."),
67                s->name,
68                t->type == ALPHA ? _("string") : _("numeric"),
69                s->type == ALPHA ? _("string") : _("numeric"));
70           continue;
71         }
72
73       if (s->label && strcspn (s->label, " ") != strlen (s->label))
74         {
75           free (t->label);
76           t->label = s->label;
77           s->label = NULL;
78         }
79
80       if (val_labs_count (s->val_labs) && t->width > MAX_SHORT_STRING)
81         msg (SW, _("Cannot add value labels from source file to "
82                    "long string variable %s."),
83              s->name);
84       else if (val_labs_count (s->val_labs))
85         {
86           /* Whether to apply the value labels. */
87           int apply = 1;
88           
89           if (t->width < s->width)
90             {
91               struct val_labs_iterator *i;
92               struct val_lab *lab;
93
94               for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
95                    lab = val_labs_next (s->val_labs, &i))
96                 {
97                   int j;
98
99                   /* We will apply the value labels only if all
100                      the truncated characters are blanks. */
101                   for (j = t->width; j < s->width; j++)
102                     if (lab->value.s[j] != ' ') 
103                       {
104                         val_labs_done (&i);
105                         apply = 0;
106                         break; 
107                       }
108                 }
109             }
110           else
111             {
112               /* Fortunately, we follow the convention that all value
113                  label values are right-padded with spaces, so it is
114                  unnecessary to bother padding values here. */
115             }
116
117           if (apply) 
118             {
119               val_labs_destroy (t->val_labs);
120               t->val_labs = s->val_labs;
121               val_labs_set_width (t->val_labs, t->width);
122               s->val_labs = val_labs_create (s->width);
123             }
124         }
125
126       if (s->miss_type != MISSING_NONE && t->width > MAX_SHORT_STRING)
127         msg (SW, _("Cannot apply missing values from source file to "
128                    "long string variable %s."),
129              s->name);
130       else if (s->miss_type != MISSING_NONE)
131         {
132           if (t->width < s->width)
133             {
134               static const int miss_count[MISSING_COUNT] = 
135                 {
136                   0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
137                 };
138
139               int j, k;
140               
141               for (j = 0; j < miss_count[s->miss_type]; j++)
142                 for (k = t->width; k < s->width; k++)
143                   if (s->missing[j].s[k] != ' ')
144                     goto skip_missing_values;
145             }
146
147           t->miss_type = s->miss_type;
148           memcpy (t->missing, s->missing, sizeof s->missing);
149         }
150     skip_missing_values: ;
151
152       if (s->type == NUMERIC)
153         {
154           t->print = s->print;
155           t->write = s->write;
156         }
157     }
158
159   if (!n_matched)
160     msg (SW, _("No matching variables found between the source "
161                "and target files."));
162       
163   /* Weighting. */
164   if (dict_get_weight (dict) != NULL) 
165     {
166       struct variable *new_weight
167         = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
168
169       if (new_weight != NULL)
170         dict_set_weight (default_dict, new_weight);
171     }
172   
173   sfm_maybe_close (handle);
174
175   return lex_end_of_command ();
176 }