Add support for value labels on long string variables.
[pspp-builds.git] / src / language / dictionary / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 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/value-parser.h>
28 #include <language/lexer/variable-parser.h>
29 #include <libpspp/hash.h>
30 #include <libpspp/message.h>
31 #include <libpspp/str.h>
32
33 #include "xalloc.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, bool);
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_WIDTH);
74       if (var_cnt < 1)
75         {
76           free(vars);
77           return CMD_FAILURE;
78         }
79       if (erase)
80         erase_labels (vars, var_cnt);
81       while (lex_token (lexer) != '/' && lex_token (lexer) != '.')
82         if (!get_label (lexer, vars, var_cnt))
83           goto lossage;
84
85       if (lex_token (lexer) != '/')
86         {
87           free (vars);
88           break;
89         }
90
91       lex_get (lexer);
92
93       free (vars);
94     }
95
96   if (parse_err)
97     return CMD_FAILURE;
98
99   return lex_end_of_command (lexer);
100
101  lossage:
102   free (vars);
103   return CMD_FAILURE;
104 }
105
106 /* Erases all the labels for the VAR_CNT variables in VARS. */
107 static void
108 erase_labels (struct variable **vars, size_t var_cnt)
109 {
110   size_t i;
111
112   /* Erase old value labels if desired. */
113   for (i = 0; i < var_cnt; i++)
114     var_clear_value_labels (vars[i]);
115 }
116
117 /* Parse all the labels for the VAR_CNT variables in VARS and add
118    the specified labels to those variables.  */
119 static int
120 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
121 {
122   /* Parse all the labels and add them to the variables. */
123   do
124     {
125       int width = var_get_width (vars[0]);
126       union value value;
127       struct string label;
128       size_t i;
129
130       /* Set value. */
131       value_init (&value, width);
132       if (!parse_value (lexer, &value, width))
133         {
134           value_destroy (&value, width);
135           return 0;
136         }
137       lex_match (lexer, ',');
138
139       /* Set label. */
140       if (lex_token (lexer) != T_ID && !lex_force_string (lexer))
141         {
142           value_destroy (&value, width);
143           return 0;
144         }
145
146       ds_init_string (&label, lex_tokstr (lexer));
147
148       if (ds_length (&label) > 60)
149         {
150           msg (SW, _("Truncating value label to 60 characters."));
151           ds_truncate (&label, 60);
152         }
153
154       for (i = 0; i < var_cnt; i++)
155         var_replace_value_label (vars[i], &value, ds_cstr (&label));
156
157       ds_destroy (&label);
158       value_destroy (&value, width);
159
160       lex_get (lexer);
161       lex_match (lexer, ',');
162     }
163   while (lex_token (lexer) != '/' && lex_token (lexer) != '.');
164
165   return 1;
166 }