/* PSPP - a program for statistical analysis.
- Copyright (C) 2005, 2009, 2011 Free Software Foundation, Inc.
+ Copyright (C) 2005, 2009, 2011, 2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "data/variable.h"
#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
#include "libpspp/str.h"
/* Types of user-missing values.
NOT_REACHED ();
}
-/* Attempts to add S to the set of string missing values MV. S
- must contain exactly as many characters as MV's width.
- Returns true if successful, false if MV has no more room for
+/* Attempts to add S, which is LEN bytes long, to the set of string missing
+ values MV. Returns true if successful, false if MV has no more room for
missing values or if S is not an acceptable missing value. */
bool
-mv_add_str (struct missing_values *mv, const uint8_t s[])
+mv_add_str (struct missing_values *mv, const uint8_t s[], size_t len)
{
union value v;
bool ok;
assert (mv->width > 0);
+ while (len > mv->width)
+ if (s[--len] != ' ')
+ return false;
+
value_init (&v, mv->width);
- memcpy (value_str_rw (&v, mv->width), s, mv->width);
+ buf_copy_rpad (CHAR_CAST (char *, value_str_rw (&v, mv->width)), mv->width,
+ CHAR_CAST (char *, s), len, ' ');
ok = mv_add_value (mv, &v);
value_destroy (&v, mv->width);
/* PSPP - a program for statistical analysis.
- Copyright (C) 2005, 2009 Free Software Foundation, Inc.
+ Copyright (C) 2005, 2009, 2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
/* Adding and modifying discrete values. */
bool mv_add_value (struct missing_values *, const union value *);
-bool mv_add_str (struct missing_values *, const uint8_t[]);
+bool mv_add_str (struct missing_values *, const uint8_t[], size_t len);
bool mv_add_num (struct missing_values *, double);
void mv_pop_value (struct missing_values *, union value *);
bool mv_replace_value (struct missing_values *, const union value *, int idx);
value_init_pool (r->pool, &value, width);
value_set_missing (&value, width);
for (i = 0; i < rec->missing_value_code; i++)
- {
- uint8_t *s = value_str_rw (&value, width);
- memcpy (s, rec->missing + 8 * i, MIN (width, 8));
- mv_add_str (&mv, s);
- }
+ mv_add_str (&mv, rec->missing + 8 * i, MIN (width, 8));
}
var_set_missing_values (var, &mv);
}
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
}
else
{
+ const char *encoding = dict_get_encoding (dict);
+
mv_init (&mv, MV_MAX_STRING);
while (!lex_match (lexer, T_RPAREN))
{
- uint8_t value[MV_MAX_STRING];
- char *dict_mv;
- size_t length;
+ const char *utf8_s;
+ size_t utf8_trunc_len;
+ size_t utf8_len;
+
+ char *raw_s;
if (!lex_force_string (lexer))
{
break;
}
- dict_mv = recode_string (dict_get_encoding (dict), "UTF-8",
- lex_tokcstr (lexer),
- ss_length (lex_tokss (lexer)));
- length = strlen (dict_mv);
- if (length > MV_MAX_STRING)
- {
- /* XXX truncate graphemes not bytes */
- msg (SE, _("Truncating missing value to maximum "
- "acceptable length (%d bytes)."),
- MV_MAX_STRING);
- length = MV_MAX_STRING;
- }
- memset (value, ' ', MV_MAX_STRING);
- memcpy (value, dict_mv, length);
- free (dict_mv);
-
- if (!mv_add_str (&mv, value))
+ /* Truncate the string to fit in 8 bytes in the dictionary
+ encoding. */
+ utf8_s = lex_tokcstr (lexer);
+ utf8_len = ss_length (lex_tokss (lexer));
+ utf8_trunc_len = utf8_encoding_trunc_len (utf8_s, encoding,
+ MV_MAX_STRING);
+ if (utf8_trunc_len < utf8_len)
+ msg (SE, _("Truncating missing value to maximum "
+ "acceptable length (%d bytes)."),
+ MV_MAX_STRING);
+
+ /* Recode to dictionary encoding and add. */
+ raw_s = recode_string (encoding, "UTF-8",
+ utf8_s, utf8_trunc_len);
+ if (!mv_add_str (&mv, CHAR_CAST (const uint8_t *, raw_s),
+ strlen (raw_s)))
ok = false;
+ free (raw_s);
lex_get (lexer);
lex_match (lexer, T_COMMA);