4a2b9cb64cde5d41303f5f11ef51ae39d6e7c0c9
[pspp-builds.git] / src / language / dictionary / apply-dictionary.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
22 #include <stdlib.h>
23
24 #include <data/any-reader.h>
25 #include <data/dictionary.h>
26 #include <data/file-handle-def.h>
27 #include <data/missing-values.h>
28 #include <data/procedure.h>
29 #include <data/value-labels.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/file-handle.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/hash.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* Parses and executes APPLY DICTIONARY. */
42 int
43 cmd_apply_dictionary (struct lexer *lexer, struct dataset *ds)
44 {
45   struct file_handle *handle;
46   struct any_reader *reader;
47   struct dictionary *dict;
48
49   int n_matched = 0;
50
51   int i;
52   
53   lex_match_id (lexer, "FROM");
54   lex_match (lexer, '=');
55   handle = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
56   if (!handle)
57     return CMD_FAILURE;
58
59   reader = any_reader_open (handle, &dict);
60   if (dict == NULL)
61     return CMD_FAILURE;
62   any_reader_close (reader);
63
64   for (i = 0; i < dict_get_var_cnt (dict); i++)
65     {
66       struct variable *s = dict_get_var (dict, i);
67       struct variable *t = dict_lookup_var (dataset_dict (ds),
68                                             var_get_name (s));
69       if (t == NULL)
70         continue;
71
72       n_matched++;
73       if (var_get_type (s) != var_get_type (t))
74         {
75           msg (SW, _("Variable %s is %s in target file, but %s in "
76                      "source file."),
77                var_get_name (s),
78                var_is_alpha (t) ? _("string") : _("numeric"),
79                var_is_alpha (s) ? _("string") : _("numeric"));
80           continue;
81         }
82
83       if (var_get_label (s))
84         {
85           const char *label = var_get_label (s);
86           if (strcspn (label, " ") != strlen (label))
87             var_set_label (t, label);
88         }
89       
90       if (var_has_value_labels (s))
91         {
92           if (!var_is_long_string (t))
93             {
94               const struct val_labs *value_labels = var_get_value_labels (s);
95               if (val_labs_can_set_width (value_labels, var_get_width (t)))
96                 var_set_value_labels (s, value_labels);
97             }
98           else
99             msg (SW, _("Cannot add value labels from source file to "
100                        "long string variable %s."),
101                  var_get_name (s));
102         }
103       
104       if (var_has_missing_values (s))
105         {
106           if (!var_is_long_string (t))
107             {
108               const struct missing_values *miss = var_get_missing_values (s);
109               if (mv_is_resizable (miss, var_get_width (t))) 
110                 var_set_missing_values (t, miss);
111             }
112           else
113             msg (SW, _("Cannot apply missing values from source file to "
114                        "long string variable %s."),
115                  var_get_name (s));
116         }
117
118       if (var_is_numeric (s))
119         {
120           var_set_print_format (t, var_get_print_format (s));
121           var_set_write_format (t, var_get_write_format (s));
122         }
123     }
124
125   if (!n_matched)
126     msg (SW, _("No matching variables found between the source "
127                "and target files."));
128       
129   /* Weighting. */
130   if (dict_get_weight (dict) != NULL) 
131     {
132       struct variable *new_weight
133         = dict_lookup_var (dataset_dict (ds),
134                            var_get_name (dict_get_weight (dict)));
135
136       if (new_weight != NULL)
137         dict_set_weight (dataset_dict (ds), new_weight);
138     }
139   
140   any_reader_close (reader);
141
142   return lex_end_of_command (lexer);
143 }