Fully implement arbitrary delimiters on DATA LIST, extending the half
[pspp-builds.git] / src / val-labs.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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "error.h"
26 #include "hash.h"
27 #include "lexer.h"
28 #include "str.h"
29 #include "value-labels.h"
30 #include "var.h"
31 \f
32 /* Declarations. */
33
34 static int do_value_labels (int);
35 static int verify_val_labs (struct variable **vars, int var_cnt);
36 static void erase_labels (struct variable **vars, int var_cnt);
37 static int get_label (struct variable **vars, int var_cnt);
38 \f
39 /* Stubs. */
40
41 int
42 cmd_value_labels (void)
43 {
44   return do_value_labels (1);
45 }
46
47 int
48 cmd_add_value_labels (void)
49 {
50   return do_value_labels (0);
51 }
52 \f
53 /* Do it. */
54
55 static int
56 do_value_labels (int erase)
57 {
58   struct variable **vars; /* Variable list. */
59   int var_cnt;            /* Number of variables. */
60   int parse_err=0;        /* true if error parsing variables */
61
62   lex_match ('/');
63   
64   while (token != '.')
65     {
66       parse_err = !parse_variables (default_dict, &vars, &var_cnt, 
67                                     PV_SAME_TYPE) ;
68       if (var_cnt < 1)
69         {
70           free(vars);
71           return CMD_FAILURE;
72         }
73       if (!verify_val_labs (vars, var_cnt))
74         goto lossage;
75       if (erase)
76         erase_labels (vars, var_cnt);
77       while (token != '/' && token != '.')
78         if (!get_label (vars, var_cnt))
79           goto lossage;
80
81       if (token != '/')
82         break;
83       lex_get ();
84
85       free (vars);
86     }
87   free (vars);
88
89   if (token != '.')
90     {
91       lex_error (NULL);
92       return CMD_TRAILING_GARBAGE;
93     }
94
95   return parse_err ? CMD_PART_SUCCESS_MAYBE : CMD_SUCCESS;
96
97  lossage:
98   free (vars);
99   return CMD_PART_SUCCESS_MAYBE;
100 }
101
102 /* Verifies that none of the VAR_CNT variables in VARS are long
103    string variables. */
104 static int
105 verify_val_labs (struct variable **vars, int var_cnt)
106 {
107   int i;
108
109   for (i = 0; i < var_cnt; i++)
110     {
111       struct variable *vp = vars[i];
112
113       if (vp->type == ALPHA && vp->width > 8)
114         {
115           msg (SE, _("It is not possible to assign value labels to long "
116                      "string variables such as %s."), vp->name);
117           return 0;
118         }
119     }
120   return 1;
121 }
122
123 /* Erases all the labels for the VAR_CNT variables in VARS. */
124 static void
125 erase_labels (struct variable **vars, int var_cnt) 
126 {
127   int i;
128
129   /* Erase old value labels if desired. */
130   for (i = 0; i < var_cnt; i++)
131     val_labs_clear (vars[i]->val_labs);
132 }
133
134 /* Parse all the labels for the VAR_CNT variables in VARS and add
135    the specified labels to those variables.  */
136 static int
137 get_label (struct variable **vars, int var_cnt)
138 {
139   /* Parse all the labels and add them to the variables. */
140   do
141     {
142       union value value;
143       char *label;
144       int i;
145
146       /* Set value. */
147       if (vars[0]->type == ALPHA)
148         {
149           if (token != T_STRING)
150             {
151               lex_error (_("expecting string"));
152               return 0;
153             }
154           st_bare_pad_copy (value.s, ds_c_str (&tokstr), MAX_SHORT_STRING);
155         }
156       else
157         {
158           if (token != T_NUM)
159             {
160               lex_error (_("expecting integer"));
161               return 0;
162             }
163           if (!lex_integer_p ())
164             msg (SW, _("Value label `%g' is not integer."), tokval);
165           value.f = tokval;
166         }
167       lex_get ();
168
169       /* Set label. */
170       if (!lex_force_string ())
171         return 0;
172       if (ds_length (&tokstr) > 60)
173         {
174           msg (SW, _("Truncating value label to 60 characters."));
175           ds_truncate (&tokstr, 60);
176         }
177       label = ds_c_str (&tokstr);
178
179       for (i = 0; i < var_cnt; i++)
180         val_labs_replace (vars[i]->val_labs, value, label);
181
182       lex_get ();
183     }
184   while (token != '/' && token != '.');
185
186   return 1;
187 }