0259d69b3ee8b32847afd2fa9245e6eac69ead3f
[pspp-builds.git] / src / language / dictionary / value-labels.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 <stdio.h>
23 #include <stdlib.h>
24
25 #include <data/procedure.h>
26 #include <data/value-labels.h>
27 #include <data/variable.h>
28 #include <language/command.h>
29 #include <language/lexer/lexer.h>
30 #include <language/lexer/variable-parser.h>
31 #include <libpspp/alloc.h>
32 #include <libpspp/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 \f
39 /* Declarations. */
40
41 static int do_value_labels (struct lexer *, 
42                             const struct dictionary *dict, int);
43 static int verify_val_labs (struct variable **vars, size_t var_cnt);
44 static void erase_labels (struct variable **vars, size_t var_cnt);
45 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
46 \f
47 /* Stubs. */
48
49 int
50 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
51 {
52   return do_value_labels (lexer, dataset_dict (ds), 1);
53 }
54
55 int
56 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
57 {
58   return do_value_labels (lexer, dataset_dict (ds), 0);
59 }
60 \f
61 /* Do it. */
62
63 static int
64 do_value_labels (struct lexer *lexer, const struct dictionary *dict, int erase)
65 {
66   struct variable **vars; /* Variable list. */
67   size_t var_cnt;         /* Number of variables. */
68   int parse_err=0;        /* true if error parsing variables */
69
70   lex_match (lexer, '/');
71   
72   while (lex_token (lexer) != '.')
73     {
74       parse_err = !parse_variables (lexer, dict, &vars, &var_cnt, 
75                                     PV_SAME_TYPE) ;
76       if (var_cnt < 1)
77         {
78           free(vars);
79           return CMD_FAILURE;
80         }
81       if (!verify_val_labs (vars, var_cnt))
82         goto lossage;
83       if (erase)
84         erase_labels (vars, var_cnt);
85       while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
86         if (!get_label (lexer, vars, var_cnt))
87           goto lossage;
88
89       if (lex_token (lexer) != '/')
90         {
91           free (vars);
92           break;
93         }
94
95       lex_get (lexer);
96
97       free (vars);
98     }
99
100   if (parse_err)
101     return CMD_FAILURE;
102
103   return lex_end_of_command (lexer);
104
105  lossage:
106   free (vars);
107   return CMD_FAILURE;
108 }
109
110 /* Verifies that none of the VAR_CNT variables in VARS are long
111    string variables. */
112 static int
113 verify_val_labs (struct variable **vars, size_t var_cnt)
114 {
115   size_t i;
116
117   for (i = 0; i < var_cnt; i++)
118     {
119       struct variable *vp = vars[i];
120
121       if (var_is_long_string (vp))
122         {
123           msg (SE, _("It is not possible to assign value labels to long "
124                      "string variables such as %s."), var_get_name (vp));
125           return 0;
126         }
127     }
128   return 1;
129 }
130
131 /* Erases all the labels for the VAR_CNT variables in VARS. */
132 static void
133 erase_labels (struct variable **vars, size_t var_cnt) 
134 {
135   size_t i;
136
137   /* Erase old value labels if desired. */
138   for (i = 0; i < var_cnt; i++)
139     var_clear_value_labels (vars[i]);
140 }
141
142 /* Parse all the labels for the VAR_CNT variables in VARS and add
143    the specified labels to those variables.  */
144 static int
145 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
146 {
147   /* Parse all the labels and add them to the variables. */
148   do
149     {
150       union value value;
151       struct string label;
152       size_t i;
153
154       /* Set value. */
155       if (var_is_alpha (vars[0]))
156         {
157           if (lex_token (lexer) != T_STRING)
158             {
159               lex_error (lexer, _("expecting string"));
160               return 0;
161             }
162           buf_copy_str_rpad (value.s, MAX_SHORT_STRING, ds_cstr (lex_tokstr (lexer)));
163         }
164       else
165         {
166           if (!lex_is_number (lexer))
167             {
168               lex_error (lexer, _("expecting integer"));
169               return 0;
170             }
171           if (!lex_is_integer (lexer))
172             msg (SW, _("Value label `%g' is not integer."), lex_tokval (lexer));
173           value.f = lex_tokval (lexer);
174         }
175       lex_get (lexer);
176       lex_match (lexer, ',');
177
178       /* Set label. */
179       if (!lex_force_string (lexer))
180         return 0;
181       
182       ds_init_string (&label, lex_tokstr (lexer));
183
184       if (ds_length (&label) > 60)
185         {
186           msg (SW, _("Truncating value label to 60 characters."));
187           ds_truncate (&label, 60);
188         }
189
190       for (i = 0; i < var_cnt; i++)
191         var_replace_value_label (vars[i], &value, ds_cstr (&label));
192
193       ds_destroy (&label);
194
195       lex_get (lexer);
196       lex_match (lexer, ',');
197     }
198   while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
199
200   return 1;
201 }