Adopt use of gnulib for portability.
[pspp] / 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 "command.h"
23 #include "dictionary.h"
24 #include "error.h"
25 #include "file-handle.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "sfm-read.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 sfm_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 ();
53   if (!handle)
54     return CMD_FAILURE;
55
56   reader = sfm_open_reader (handle, &dict, NULL);
57   if (dict == NULL)
58     return CMD_FAILURE;
59   sfm_close_reader (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 (s->miss_type != MISSING_NONE && 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 (s->miss_type != MISSING_NONE)
137         {
138           if (t->width < s->width)
139             {
140               static const int miss_count[MISSING_COUNT] = 
141                 {
142                   0, 1, 2, 3, 2, 1, 1, 3, 2, 2,
143                 };
144
145               int j, k;
146               
147               for (j = 0; j < miss_count[s->miss_type]; j++)
148                 for (k = t->width; k < s->width; k++)
149                   if (s->missing[j].s[k] != ' ')
150                     goto skip_missing_values;
151             }
152
153           t->miss_type = s->miss_type;
154           memcpy (t->missing, s->missing, sizeof s->missing);
155         }
156     skip_missing_values: ;
157
158       if (s->type == NUMERIC)
159         {
160           t->print = s->print;
161           t->write = s->write;
162         }
163     }
164
165   if (!n_matched)
166     msg (SW, _("No matching variables found between the source "
167                "and target files."));
168       
169   /* Weighting. */
170   if (dict_get_weight (dict) != NULL) 
171     {
172       struct variable *new_weight
173         = dict_lookup_var (default_dict, dict_get_weight (dict)->name);
174
175       if (new_weight != NULL)
176         dict_set_weight (default_dict, new_weight);
177     }
178   
179   sfm_close_reader (reader);
180
181   return lex_end_of_command ();
182 }