8c312ab8cee23ebec567ac3e1a8189285aa80e47
[pspp-builds.git] / src / language / dictionary / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010 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/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 void erase_labels (struct variable **vars, size_t var_cnt);
42 static int get_label (struct lexer *, struct variable **vars, size_t var_cnt);
43 \f
44 /* Stubs. */
45
46 int
47 cmd_value_labels (struct lexer *lexer, struct dataset *ds)
48 {
49   return do_value_labels (lexer, dataset_dict (ds), true);
50 }
51
52 int
53 cmd_add_value_labels (struct lexer *lexer, struct dataset *ds)
54 {
55   return do_value_labels (lexer, dataset_dict (ds), false);
56 }
57 \f
58 /* Do it. */
59
60 static int
61 do_value_labels (struct lexer *lexer, const struct dictionary *dict, bool erase)
62 {
63   struct variable **vars; /* Variable list. */
64   size_t var_cnt;         /* Number of variables. */
65   int parse_err=0;        /* true if error parsing variables */
66
67   lex_match (lexer, T_SLASH);
68
69   while (lex_token (lexer) != T_ENDCMD)
70     {
71       parse_err = !parse_variables (lexer, dict, &vars, &var_cnt,
72                                     PV_SAME_WIDTH);
73       if (var_cnt < 1)
74         {
75           free(vars);
76           return CMD_FAILURE;
77         }
78       if (erase)
79         erase_labels (vars, var_cnt);
80       while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
81         if (!get_label (lexer, vars, var_cnt))
82           goto lossage;
83
84       if (lex_token (lexer) != T_SLASH)
85         {
86           free (vars);
87           break;
88         }
89
90       lex_get (lexer);
91
92       free (vars);
93     }
94
95   if (parse_err)
96     return CMD_FAILURE;
97
98   return lex_end_of_command (lexer);
99
100  lossage:
101   free (vars);
102   return CMD_FAILURE;
103 }
104
105 /* Erases all the labels for the VAR_CNT variables in VARS. */
106 static void
107 erase_labels (struct variable **vars, size_t var_cnt)
108 {
109   size_t i;
110
111   /* Erase old value labels if desired. */
112   for (i = 0; i < var_cnt; i++)
113     var_clear_value_labels (vars[i]);
114 }
115
116 /* Parse all the labels for the VAR_CNT variables in VARS and add
117    the specified labels to those variables.  */
118 static int
119 get_label (struct lexer *lexer, struct variable **vars, size_t var_cnt)
120 {
121   /* Parse all the labels and add them to the variables. */
122   do
123     {
124       enum { MAX_LABEL_LEN = 255 };
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, T_COMMA);
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_substring (&label, lex_tokss (lexer));
147
148       if (ds_length (&label) > MAX_LABEL_LEN)
149         {
150           msg (SW, _("Truncating value label to %d bytes."), MAX_LABEL_LEN);
151           ds_truncate (&label, MAX_LABEL_LEN);
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, T_COMMA);
162     }
163   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
164
165   return 1;
166 }