Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[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
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <stdlib.h>
22
23 #include <data/any-reader.h>
24 #include <data/dictionary.h>
25 #include <data/file-handle-def.h>
26 #include <data/missing-values.h>
27 #include <data/procedure.h>
28 #include <data/value-labels.h>
29 #include <data/variable.h>
30 #include <language/command.h>
31 #include <language/data-io/file-handle.h>
32 #include <language/lexer/lexer.h>
33 #include <libpspp/hash.h>
34 #include <libpspp/message.h>
35 #include <libpspp/str.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Parses and executes APPLY DICTIONARY. */
41 int
42 cmd_apply_dictionary (struct lexer *lexer, struct dataset *ds)
43 {
44   struct file_handle *handle;
45   struct any_reader *reader;
46   struct dictionary *dict;
47
48   int n_matched = 0;
49
50   int i;
51   
52   lex_match_id (lexer, "FROM");
53   lex_match (lexer, '=');
54   handle = fh_parse (lexer, FH_REF_FILE | FH_REF_SCRATCH);
55   if (!handle)
56     return CMD_FAILURE;
57
58   reader = any_reader_open (handle, &dict);
59   if (dict == NULL)
60     return CMD_FAILURE;
61   any_reader_close (reader);
62
63   for (i = 0; i < dict_get_var_cnt (dict); i++)
64     {
65       struct variable *s = dict_get_var (dict, i);
66       struct variable *t = dict_lookup_var (dataset_dict (ds),
67                                             var_get_name (s));
68       if (t == NULL)
69         continue;
70
71       n_matched++;
72       if (var_get_type (s) != var_get_type (t))
73         {
74           msg (SW, _("Variable %s is %s in target file, but %s in "
75                      "source file."),
76                var_get_name (s),
77                var_is_alpha (t) ? _("string") : _("numeric"),
78                var_is_alpha (s) ? _("string") : _("numeric"));
79           continue;
80         }
81
82       if (var_get_label (s))
83         {
84           const char *label = var_get_label (s);
85           if (strcspn (label, " ") != strlen (label))
86             var_set_label (t, label);
87         }
88       
89       if (var_has_value_labels (s))
90         {
91           if (!var_is_long_string (t))
92             {
93               const struct val_labs *value_labels = var_get_value_labels (s);
94               if (val_labs_can_set_width (value_labels, var_get_width (t)))
95                 var_set_value_labels (s, value_labels);
96             }
97           else
98             msg (SW, _("Cannot add value labels from source file to "
99                        "long string variable %s."),
100                  var_get_name (s));
101         }
102       
103       if (var_has_missing_values (s))
104         {
105           if (!var_is_long_string (t))
106             {
107               const struct missing_values *miss = var_get_missing_values (s);
108               if (mv_is_resizable (miss, var_get_width (t))) 
109                 var_set_missing_values (t, miss);
110             }
111           else
112             msg (SW, _("Cannot apply missing values from source file to "
113                        "long string variable %s."),
114                  var_get_name (s));
115         }
116
117       if (var_is_numeric (s))
118         {
119           var_set_print_format (t, var_get_print_format (s));
120           var_set_write_format (t, var_get_write_format (s));
121         }
122     }
123
124   if (!n_matched)
125     msg (SW, _("No matching variables found between the source "
126                "and target files."));
127       
128   /* Weighting. */
129   if (dict_get_weight (dict) != NULL) 
130     {
131       struct variable *new_weight
132         = dict_lookup_var (dataset_dict (ds),
133                            var_get_name (dict_get_weight (dict)));
134
135       if (new_weight != NULL)
136         dict_set_weight (dataset_dict (ds), new_weight);
137     }
138   
139   any_reader_close (reader);
140
141   return lex_end_of_command (lexer);
142 }