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