You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA. */
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
#include <config.h>
#include <stdio.h>
\f
/* Declarations. */
-#include "debug-print.h"
-
-/* Variable list. */
-static struct variable **v;
-
-/* Number of variables. */
-static int nv;
-
static int do_value_labels (int);
-static int verify_val_labs (int erase);
-static int get_label (void);
-
-#if DEBUGGING
-static void debug_print (void);
-#endif
+static int verify_val_labs (struct variable **vars, int var_cnt);
+static void erase_labels (struct variable **vars, int var_cnt);
+static int get_label (struct variable **vars, int var_cnt);
\f
/* Stubs. */
-static void
-init (void)
-{
- v = NULL;
-}
-
-static void
-done (void)
-{
- free (v);
-}
-
int
cmd_value_labels (void)
{
- int code;
- init ();
- lex_match_id ("VALUE");
- lex_match_id ("LABELS");
- code = do_value_labels (1);
- done ();
- return code;
+ return do_value_labels (1);
}
int
cmd_add_value_labels (void)
{
- int code;
- lex_match_id ("ADD");
- lex_match_id ("VALUE");
- lex_match_id ("LABELS");
- code = do_value_labels (0);
- done ();
- return code;
+ return do_value_labels (0);
}
\f
/* Do it. */
static int
do_value_labels (int erase)
{
+ struct variable **vars; /* Variable list. */
+ int var_cnt; /* Number of variables. */
+ int parse_err=0; /* true if error parsing variables */
+
lex_match ('/');
while (token != '.')
{
- parse_variables (default_dict, &v, &nv, PV_SAME_TYPE);
- if (!verify_val_labs (erase))
- return CMD_PART_SUCCESS_MAYBE;
+ parse_err = !parse_variables (default_dict, &vars, &var_cnt,
+ PV_SAME_TYPE) ;
+ if (var_cnt < 1)
+ {
+ free(vars);
+ return CMD_FAILURE;
+ }
+ if (!verify_val_labs (vars, var_cnt))
+ goto lossage;
+ if (erase)
+ erase_labels (vars, var_cnt);
while (token != '/' && token != '.')
- if (!get_label ())
- return CMD_PART_SUCCESS_MAYBE;
+ if (!get_label (vars, var_cnt))
+ goto lossage;
if (token != '/')
+ {
+ free (vars);
break;
+ }
+
lex_get ();
- free (v);
- v = NULL;
+ free (vars);
}
if (token != '.')
return CMD_TRAILING_GARBAGE;
}
-#if 0 && DEBUGGING
- debug_print ();
-#endif
- return CMD_SUCCESS;
+ return parse_err ? CMD_PART_SUCCESS_MAYBE : CMD_SUCCESS;
+
+ lossage:
+ free (vars);
+ return CMD_PART_SUCCESS_MAYBE;
}
+/* Verifies that none of the VAR_CNT variables in VARS are long
+ string variables. */
static int
-verify_val_labs (int erase)
+verify_val_labs (struct variable **vars, int var_cnt)
{
int i;
- if (!nv)
- return 1;
-
- for (i = 0; i < nv; i++)
+ for (i = 0; i < var_cnt; i++)
{
- struct variable *vp = v[i];
+ struct variable *vp = vars[i];
- if (vp->type == ALPHA && vp->width > 8)
+ if (vp->type == ALPHA && vp->width > MAX_SHORT_STRING)
{
msg (SE, _("It is not possible to assign value labels to long "
"string variables such as %s."), vp->name);
return 0;
}
-
- if (erase)
- val_labs_clear (vp->val_labs);
}
return 1;
}
-/* Parse all the labels for a particular set of variables and add the
- specified labels to those variables. */
-static int
-get_label (void)
+/* Erases all the labels for the VAR_CNT variables in VARS. */
+static void
+erase_labels (struct variable **vars, int var_cnt)
{
int i;
- /* Make sure there's some variables. */
- if (!nv)
- {
- if (token != T_STRING && token != T_NUM)
- return 0;
- lex_get ();
- return 1;
- }
+ /* Erase old value labels if desired. */
+ for (i = 0; i < var_cnt; i++)
+ val_labs_clear (vars[i]->val_labs);
+}
+/* Parse all the labels for the VAR_CNT variables in VARS and add
+ the specified labels to those variables. */
+static int
+get_label (struct variable **vars, int var_cnt)
+{
/* Parse all the labels and add them to the variables. */
do
{
union value value;
char *label;
+ int i;
/* Set value. */
- if (v[0]->type == ALPHA)
+ if (vars[0]->type == ALPHA)
{
if (token != T_STRING)
{
- msg (SE, _("String expected for value."));
+ lex_error (_("expecting string"));
return 0;
}
- st_bare_pad_copy (value.s, ds_value (&tokstr), MAX_SHORT_STRING);
+ st_bare_pad_copy (value.s, ds_c_str (&tokstr), MAX_SHORT_STRING);
}
else
{
- if (token != T_NUM)
+ if (!lex_is_number ())
{
- msg (SE, _("Number expected for value."));
+ lex_error (_("expecting integer"));
return 0;
}
- if (!lex_integer_p ())
+ if (!lex_is_integer ())
msg (SW, _("Value label `%g' is not integer."), tokval);
value.f = tokval;
}
+ lex_get ();
/* Set label. */
- lex_get ();
if (!lex_force_string ())
return 0;
if (ds_length (&tokstr) > 60)
msg (SW, _("Truncating value label to 60 characters."));
ds_truncate (&tokstr, 60);
}
- label = ds_value (&tokstr);
+ label = ds_c_str (&tokstr);
- for (i = 0; i < nv; i++)
- val_labs_replace (v[i]->val_labs, value, label);
+ for (i = 0; i < var_cnt; i++)
+ val_labs_replace (vars[i]->val_labs, value, label);
lex_get ();
}
return 1;
}
-
-#if 0 && DEBUGGING
-static void
-debug_print ()
-{
- int i;
-
- puts (_("Value labels:"));
- for (i = 0; i < nvar; i++)
- {
- struct hsh_iterator i;
- struct value_label *val;
-
- printf (" %s\n", var[i]->name);
- if (var[i]->val_lab)
- {
- for (val = hsh_first (var[i]->val_lab, &i); val != NULL;
- val = hsh_next (var[i]->val_lab, &i))
- if (var[i]->type == NUMERIC)
- printf (" %g: `%s'\n", val->v.f, val->s);
- else
- printf (" `%.8s': `%s'\n", val->v.s, val->s);
- }
- else
- printf (_(" (no value labels)\n"));
- }
-}
-#endif /* DEBUGGING */