Continue reforming procedure execution. In this phase, get rid of
[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 <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 (void)
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 ("FROM");
53   lex_match ('=');
54   handle = fh_parse (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 (default_dict, s->name);
67       if (t == NULL)
68         continue;
69
70       n_matched++;
71       if (s->type != t->type)
72         {
73           msg (SW, _("Variable %s is %s in target file, but %s in "
74                      "source file."),
75                s->name,
76                t->type == ALPHA ? _("string") : _("numeric"),
77                s->type == ALPHA ? _("string") : _("numeric"));
78           continue;
79         }
80
81       if (s->label && strcspn (s->label, " ") != strlen (s->label))
82         {
83           free (t->label);
84           t->label = s->label;
85           s->label = NULL;
86         }
87
88       if (val_labs_count (s->val_labs) && t->width > MAX_SHORT_STRING)
89         msg (SW, _("Cannot add value labels from source file to "
90                    "long string variable %s."),
91              s->name);
92       else if (val_labs_count (s->val_labs))
93         {
94           /* Whether to apply the value labels. */
95           int apply = 1;
96           
97           if (t->width < s->width)
98             {
99               struct val_labs_iterator *i;
100               struct val_lab *lab;
101
102               for (lab = val_labs_first (s->val_labs, &i); lab != NULL;
103                    lab = val_labs_next (s->val_labs, &i))
104                 {
105                   int j;
106
107                   /* We will apply the value labels only if all
108                      the truncated characters are blanks. */
109                   for (j = t->width; j < s->width; j++)
110                     if (lab->value.s[j] != ' ') 
111                       {
112                         val_labs_done (&i);
113                         apply = 0;
114                         break; 
115                       }
116                 }
117             }
118           else
119             {
120               /* Fortunately, we follow the convention that all value
121                  label values are right-padded with spaces, so it is
122                  unnecessary to bother padding values here. */
123             }
124
125           if (apply) 
126             {
127               val_labs_destroy (t->val_labs);
128               t->val_labs = s->val_labs;
129               val_labs_set_width (t->val_labs, t->width);
130               s->val_labs = val_labs_create (s->width);
131             }
132         }
133
134       if (!mv_is_empty (&s->miss) && t->width > MAX_SHORT_STRING)
135         msg (SW, _("Cannot apply missing values from source file to "
136                    "long string variable %s."),
137              s->name);
138       else if (!mv_is_empty (&s->miss))
139         {
140           if (mv_is_resizable (&s->miss, t->width)) 
141             {
142               mv_copy (&t->miss, &s->miss);
143               mv_resize (&t->miss, t->width); 
144             }
145         }
146
147       if (s->type == NUMERIC)
148         {
149           t->print = s->print;
150           t->write = s->write;
151         }
152     }
153
154   if (!n_matched)
155     msg (SW, _("No matching variables found between the source "
156                "and target files."));
157       
158   /* Weighting. */
159   if (dict_get_weight (dict) != NULL) 
160     {
161       struct variable *new_weight
162         = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
163
164       if (new_weight != NULL)
165         dict_set_weight (default_dict, new_weight);
166     }
167   
168   any_reader_close (reader);
169
170   return lex_end_of_command ();
171 }