39f544ec4b917fa2b517129a06ad6c252d5cc171
[pspp-builds.git] / src / language / dictionary / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <data/procedure.h>
23 #include <data/value-labels.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/lexer.h>
27 #include <language/lexer/variable-parser.h>
28 #include <libpspp/hash.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
31
32 #include "xalloc.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 \f
37 /* Declarations. */
38
39 static int do_value_labels (struct lexer *,
40                             const struct dictionary *dict, bool);
41 static int verify_val_labs (struct variable **vars, size_t var_cnt);
42 static void erase_labels (struct variable **vars, size_t var_cnt);
43 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
44 \f
45 /* Stubs. */
46
47 int
48 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
49 {
50   return do_value_labels (lexer, dataset_dict (ds), true);
51 }
52
53 int
54 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
55 {
56   return do_value_labels (lexer, dataset_dict (ds), false);
57 }
58 \f
59 /* Do it. */
60
61 static int
62 do_value_labels (struct lexer *lexer, const struct dictionary *dict, bool erase)
63 {
64   struct variable **vars; /* Variable list. */
65   size_t var_cnt;         /* Number of variables. */
66   int parse_err=0;        /* true if error parsing variables */
67
68   lex_match (lexer, '/');
69
70   while (lex_token (lexer) != '.')
71     {
72       parse_err = !parse_variables (lexer, dict, &vars, &var_cnt,
73                                     PV_SAME_TYPE) ;
74       if (var_cnt < 1)
75         {
76           free(vars);
77           return CMD_FAILURE;
78         }
79       if (!verify_val_labs (vars, var_cnt))
80         goto lossage;
81       if (erase)
82         erase_labels (vars, var_cnt);
83       while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
84         if (!get_label (lexer, vars, var_cnt))
85           goto lossage;
86
87       if (lex_token (lexer) != '/')
88         {
89           free (vars);
90           break;
91         }
92
93       lex_get (lexer);
94
95       free (vars);
96     }
97
98   if (parse_err)
99     return CMD_FAILURE;
100
101   return lex_end_of_command (lexer);
102
103  lossage:
104   free (vars);
105   return CMD_FAILURE;
106 }
107
108 /* Verifies that none of the VAR_CNT variables in VARS are long
109    string variables. */
110 static int
111 verify_val_labs (struct variable **vars, size_t var_cnt)
112 {
113   size_t i;
114
115   for (i = 0; i < var_cnt; i++)
116     {
117       const struct variable *vp = vars[i];
118
119       if (var_is_long_string (vp))
120         {
121           msg (SE, _("It is not possible to assign value labels to long "
122                      "string variables such as %s."), var_get_name (vp));
123           return 0;
124         }
125     }
126   return 1;
127 }
128
129 /* Erases all the labels for the VAR_CNT variables in VARS. */
130 static void
131 erase_labels (struct variable **vars, size_t var_cnt)
132 {
133   size_t i;
134
135   /* Erase old value labels if desired. */
136   for (i = 0; i < var_cnt; i++)
137     var_clear_value_labels (vars[i]);
138 }
139
140 /* Parse all the labels for the VAR_CNT variables in VARS and add
141    the specified labels to those variables.  */
142 static int
143 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
144 {
145   /* Parse all the labels and add them to the variables. */
146   do
147     {
148       union value value;
149       struct string label;
150       size_t i;
151
152       /* Set value. */
153       if (var_is_alpha (vars[0]))
154         {
155           if (lex_token (lexer) != T_STRING)
156             {
157               lex_error (lexer, _("expecting string"));
158               return 0;
159             }
160           buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_cstr (lex_tokstr (lexer)));
161         }
162       else
163         {
164           if (!lex_is_number (lexer))
165             {
166               lex_error (lexer, _("expecting number"));
167               return 0;
168             }
169           value.f = lex_tokval (lexer);
170         }
171       lex_get (lexer);
172       lex_match (lexer, ',');
173
174       /* Set label. */
175       if (!lex_force_string (lexer))
176         return 0;
177
178       ds_init_string (&label, lex_tokstr (lexer));
179
180       if (ds_length (&label) > 60)
181         {
182           msg (SW, _("Truncating value label to 60 characters."));
183           ds_truncate (&label, 60);
184         }
185
186       for (i = 0; i < var_cnt; i++)
187         var_replace_value_label (vars[i], &value, ds_cstr (&label));
188
189       ds_destroy (&label);
190
191       lex_get (lexer);
192       lex_match (lexer, ',');
193     }
194   while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
195
196   return 1;
197 }