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