Add scratch file handles.
[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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include "any-reader.h"
23 #include "command.h"
24 #include "dictionary.h"
25 #include "error.h"
26 #include "file-handle.h"
27 #include "hash.h"
28 #include "lexer.h"
29 #include "str.h"
30 #include "value-labels.h"
31 #include "var.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 #include "debug-print.h"
37
38 /* Parses and executes APPLY DICTIONARY. */
39 int
40 cmd_apply_dictionary (void)
41 {
42   struct file_handle *handle;
43   struct any_reader *reader;
44   struct dictionary *dict;
45
46   int n_matched = 0;
47
48   int i;
49   
50   lex_match_id ("FROM");
51   lex_match ('=');
52   handle = fh_parse (FH_REF_FILE | FH_REF_SCRATCH);
53   if (!handle)
54     return CMD_FAILURE;
55
56   reader = any_reader_open (handle, &dict);
57   if (dict == NULL)
58     return CMD_FAILURE;
59   any_reader_close (reader);
60
61   for (i = 0; i < dict_get_var_cnt (dict); i++)
62     {
63       struct variable *s = dict_get_var (dict, i);
64       struct variable *t = dict_lookup_var (default_dict, s->name);
65       if (t == NULL)
66         continue;
67
68       n_matched++;
69       if (s->type != t->type)
70         {
71           msg (SW, _("Variable %s is %s in target file, but %s in "
72                      "source file."),
73                s->name,
74                t->type == ALPHA ? _("string") : _("numeric"),
75                s->type == ALPHA ? _("string") : _("numeric"));
76           continue;
77         }
78
79       if (s->label && strcspn (s->label, " ") != strlen (s->label))
80         {
81           free (t->label);
82           t->label = s->label;
83           s->label = NULL;
84         }
85
86       if (val_labs_count (s->val_labs) && t->width > MAX_SHORT_STRING)
87         msg (SW, _("Cannot add value labels from source file to "
88                    "long string variable %s."),
89              s->name);
90       else if (val_labs_count (s->val_labs))
91         {
92           /* Whether to apply the value labels. */
93           int apply = 1;
94           
95           if (t->width < s->width)
96             {
97               struct val_labs_iterator *i;
98               struct val_lab *lab;
99
100               for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
101                    lab = val_labs_next (s->val_labs, &i))
102                 {
103                   int j;
104
105                   /* We will apply the value labels only if all
106                      the truncated characters are blanks. */
107                   for (j = t->width; j < s->width; j++)
108                     if (lab->value.s[j] != ' ') 
109                       {
110                         val_labs_done (&i);
111                         apply = 0;
112                         break; 
113                       }
114                 }
115             }
116           else
117             {
118               /* Fortunately, we follow the convention that all value
119                  label values are right-padded with spaces, so it is
120                  unnecessary to bother padding values here. */
121             }
122
123           if (apply) 
124             {
125               val_labs_destroy (t->val_labs);
126               t->val_labs = s->val_labs;
127               val_labs_set_width (t->val_labs, t->width);
128               s->val_labs = val_labs_create (s->width);
129             }
130         }
131
132       if (!mv_is_empty (&s->miss) && t->width > MAX_SHORT_STRING)
133         msg (SW, _("Cannot apply missing values from source file to "
134                    "long string variable %s."),
135              s->name);
136       else if (!mv_is_empty (&s->miss))
137         {
138           if (mv_is_resizable (&s->miss, t->width)) 
139             {
140               mv_copy (&t->miss, &s->miss);
141               mv_resize (&t->miss, t->width); 
142             }
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   if (dict_get_weight (dict) != NULL) 
158     {
159       struct variable *new_weight
160         = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
161
162       if (new_weight != NULL)
163         dict_set_weight (default_dict, new_weight);
164     }
165   
166   any_reader_close (reader);
167
168   return lex_end_of_command ();
169 }