static void set_map_out_str (struct map_out *, struct pool *,
struct substring);
-static void enlarge_dst_widths (struct recode_trns *);
+static bool enlarge_dst_widths (struct recode_trns *);
static void create_dst_vars (struct recode_trns *, struct dictionary *);
static trns_proc_func recode_trns_proc;
/* Ensure that all the output strings are at least as wide
as the widest destination variable. */
if (trns->dst_type == VAL_STRING)
- enlarge_dst_widths (trns);
+ {
+ if ( ! enlarge_dst_widths (trns))
+ {
+ recode_trns_free (trns);
+ return CMD_FAILURE;
+ }
+ }
/* Create destination variables, if needed.
This must be the final step; otherwise we'd have to
set_map_in_generic (in, MAP_MISSING);
else if (!lex_force_string (lexer))
return false;
- else
+ else
{
set_map_in_str (in, pool, lex_tokss (lexer), max_src_width,
dict_encoding);
if (lex_token (lexer) == T_ID
&& lex_id_match (ss_cstr ("THRU"), lex_tokss (lexer)))
{
- msg (SE, _("THRU is not allowed with string variables."));
+ msg (SE, _("%s is not allowed with string variables."), "THRU");
return false;
}
}
set_map_out_str (out, pool, lex_tokss (lexer));
lex_get (lexer);
}
- else if (lex_match_id (lexer, "COPY"))
+ else if (lex_match_id (lexer, "COPY"))
{
out->copy_input = true;
- out->width = 0;
+ out->width = 0;
}
else
{
out->copy_input = false;
value_init_pool (pool, &out->value, length);
- memcpy (value_str_rw (&out->value, length), string, length);
+ memcpy (out->value.s, string, length);
out->width = length;
}
/* Ensures that all the output values in TRNS are as wide as the
widest destination variable. */
-static void
+static bool
enlarge_dst_widths (struct recode_trns *trns)
{
size_t i;
-
+ const struct variable *narrow_var = NULL;
+ int min_dst_width = INT_MAX;
trns->max_dst_width = 0;
+
for (i = 0; i < trns->var_cnt; i++)
{
const struct variable *v = trns->dst_vars[i];
if (var_get_width (v) > trns->max_dst_width)
trns->max_dst_width = var_get_width (v);
+
+ if (var_get_width (v) < min_dst_width)
+ {
+ min_dst_width = var_get_width (v);
+ narrow_var = v;
+ }
}
for (i = 0; i < trns->map_cnt; i++)
{
struct map_out *out = &trns->mappings[i].out;
if (!out->copy_input)
- value_resize_pool (trns->pool, &out->value,
- out->width, trns->max_dst_width);
+ {
+ if (out->width > min_dst_width)
+ {
+ msg (ME,
+ _("Cannot recode because the variable %s would require a width of %d bytes or greater, but it has a width of only %d bytes."),
+ var_get_name (narrow_var), out->width, min_dst_width);
+ return false;
+ }
+
+ value_resize_pool (trns->pool, &out->value,
+ out->width, trns->max_dst_width);
+ }
}
+
+ return true;
}
/* Creates destination variables that don't already exist. */
switch (in->type)
{
case MAP_SINGLE:
- match = !memcmp (value, value_str (&in->x, trns->max_src_width),
- width);
+ match = !memcmp (value, in->x.s, width);
break;
case MAP_ELSE:
match = true;
if (out != NULL)
{
if (!out->copy_input)
- memcpy (dst, value_str (&out->value, trns->max_dst_width),
- var_get_width (dst_var));
+ memcpy (dst, out->value.s, var_get_width (dst_var));
else if (trns->src_vars != trns->dst_vars)
{
union value *dst_data = case_data_rw (*c, dst_var);