treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / language / dictionary / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009, 2010, 2011 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/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/value-labels.h"
25 #include "data/variable.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/value-parser.h"
29 #include "language/lexer/variable-parser.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "libpspp/str.h"
33
34 #include "gl/xalloc.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, bool);
43 static void erase_labels (struct variable **vars, size_t n_vars);
44 static int get_label (struct lexer *, struct variable **vars, size_t n_vars,
45                       const char *dict_encoding);
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), true);
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), false);
59 }
60 \f
61 /* Do it. */
62
63 static int
64 do_value_labels (struct lexer *lexer, const struct dictionary *dict, bool erase)
65 {
66   struct variable **vars; /* Variable list. */
67   size_t n_vars;         /* Number of variables. */
68   int parse_err=0;        /* true if error parsing variables */
69
70   lex_match (lexer, T_SLASH);
71
72   while (lex_token (lexer) != T_ENDCMD)
73     {
74       parse_err = !parse_variables (lexer, dict, &vars, &n_vars,
75                                     PV_SAME_WIDTH);
76       if (n_vars < 1)
77         {
78           free(vars);
79           return CMD_FAILURE;
80         }
81       if (erase)
82         erase_labels (vars, n_vars);
83       while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
84         if (!get_label (lexer, vars, n_vars, dict_get_encoding (dict)))
85           goto lossage;
86
87       if (lex_token (lexer) != T_SLASH)
88         {
89           free (vars);
90           break;
91         }
92
93       lex_get (lexer);
94
95       free (vars);
96     }
97
98   return parse_err ? CMD_FAILURE : CMD_SUCCESS;
99
100  lossage:
101   free (vars);
102   return CMD_FAILURE;
103 }
104
105 /* Erases all the labels for the N_VARS variables in VARS. */
106 static void
107 erase_labels (struct variable **vars, size_t n_vars)
108 {
109   /* Erase old value labels if desired. */
110   for (size_t i = 0; i < n_vars; i++)
111     var_clear_value_labels (vars[i]);
112 }
113
114 /* Parse all the labels for the N_VARS variables in VARS and add
115    the specified labels to those variables.  */
116 static int
117 get_label (struct lexer *lexer, struct variable **vars, size_t n_vars,
118            const char *dict_encoding)
119 {
120   /* Parse all the labels and add them to the variables. */
121   do
122     {
123       enum { MAX_LABEL_LEN = 255 };
124       int width = var_get_width (vars[0]);
125       union value value;
126       struct string label;
127       size_t trunc_len;
128       size_t i;
129
130       /* Set value. */
131       value_init (&value, width);
132       if (!parse_value (lexer, &value, vars[0]))
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       trunc_len = utf8_encoding_trunc_len (ds_cstr (&label), dict_encoding,
149                                            MAX_LABEL_LEN);
150       if (ds_length (&label) > trunc_len)
151         {
152           msg (SW, _("Truncating value label to %d bytes."), MAX_LABEL_LEN);
153           ds_truncate (&label, trunc_len);
154         }
155
156       for (i = 0; i < n_vars; i++)
157         var_replace_value_label (vars[i], &value, ds_cstr (&label));
158
159       ds_destroy (&label);
160       value_destroy (&value, width);
161
162       lex_get (lexer);
163       lex_match (lexer, T_COMMA);
164     }
165   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD);
166
167   return 1;
168 }