#include "libpspp/i18n.h"
#include "libpspp/integer-format.h"
#include "libpspp/legacy-encoding.h"
-#include "libpspp/message.h"
#include "libpspp/misc.h"
#include "libpspp/str.h"
#include "settings.h"
union value *output; /* Destination. */
int width; /* Output width. */
-
- int first_column; /* First column of field; 0 if inapplicable. */
- int last_column; /* Last column. */
};
-typedef bool data_in_parser_func (struct data_in *);
+typedef char *data_in_parser_func (struct data_in *);
#define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
static data_in_parser_func parse_##METHOD;
#include "format.def"
-static void data_warning (const struct data_in *, const char *, ...)
- PRINTF_FORMAT (2, 3);
-
static void default_result (struct data_in *);
static bool trim_spaces_and_check_missing (struct data_in *);
initialized with the given WIDTH (0 for a numeric field, otherwise the
string width). If FORMAT is FMT_A, then OUTPUT_ENCODING must specify the
correct encoding for OUTPUT (normally obtained via dict_get_encoding()). */
-bool
+char *
data_in (struct substring input, const char *input_encoding,
- enum fmt_type format, int first_column, int last_column,
+ enum fmt_type format,
union value *output, int width, const char *output_encoding)
{
static data_in_parser_func *const handlers[FMT_NUMBER_OF_FORMATS] =
enum fmt_category cat;
const char *dest_encoding;
char *s;
- bool ok;
+ char *error;
assert ((width != 0) == fmt_is_string (format));
i.output = output;
i.width = width;
- i.first_column = first_column;
- i.last_column = last_column;
-
if (ss_is_empty (input))
{
default_result (&i);
- return true;
+ return NULL;
}
cat = fmt_get_category (format);
s = NULL;
}
- ok = handlers[i.format] (&i);
- if (!ok)
+ error = handlers[i.format] (&i);
+ if (error != NULL)
default_result (&i);
free (s);
- return ok;
+ return error;
+}
+
+bool
+data_in_msg (struct substring input, const char *input_encoding,
+ enum fmt_type format,
+ union value *output, int width, const char *output_encoding)
+{
+ char *error = data_in (input, input_encoding, format,
+ output, width, output_encoding);
+ if (error != NULL)
+ {
+ msg (SW,_("Data is not valid as format %s: %s"),
+ fmt_name (format), error);
+ free (error);
+ return false;
+ }
+ else
+ return true;
}
static bool
/* Format parsers. */
/* Parses F, COMMA, DOT, DOLLAR, PCT, and E input formats. */
-static bool
+static char *
parse_number (struct data_in *i)
{
const struct fmt_number_style *style =
/* Trim spaces and check for missing value representation. */
if (trim_spaces_and_check_missing (i))
- return true;
+ return NULL;
ds_init_empty (&tmp);
ds_extend (&tmp, 64);
if (!ss_is_empty (i->input))
{
+ char *error;
if (ds_is_empty (&tmp))
- data_warning (i, _("Field contents are not numeric."));
+ error = xstrdup (_("Field contents are not numeric."));
else
- data_warning (i, _("Number followed by garbage."));
+ error = xstrdup (_("Number followed by garbage."));
ds_destroy (&tmp);
- return false;
+ return error;
}
/* Let c_strtod() do the conversion. */
i->output->f = c_strtod (ds_cstr (&tmp), &tail);
if (*tail != '\0')
{
- data_warning (i, _("Invalid numeric syntax."));
errno = save_errno;
ds_destroy (&tmp);
- return false;
+ return xstrdup (_("Invalid numeric syntax."));
}
else if (errno == ERANGE)
{
if (fabs (i->output->f) > 1)
{
- data_warning (i, _("Too-large number set to system-missing."));
i->output->f = SYSMIS;
+ ds_destroy (&tmp);
+ return xstrdup (_("Too-large number set to system-missing."));
}
else
{
- data_warning (i, _("Too-small number set to zero."));
i->output->f = 0.0;
+ ds_destroy (&tmp);
+ return xstrdup (_("Too-small number set to zero."));
}
}
else
- {
- errno = save_errno;
- }
+ errno = save_errno;
ds_destroy (&tmp);
- return true;
+ return NULL;
}
/* Parses N format. */
-static bool
+static char *
parse_N (struct data_in *i)
{
int c;
while ((c = ss_get_char (&i->input)) != EOF)
{
if (!c_isdigit (c))
- {
- data_warning (i, _("All characters in field must be digits."));
- return false;
- }
+ return xstrdup (_("All characters in field must be digits."));
i->output->f = i->output->f * 10.0 + (c - '0');
}
- return true;
+ return NULL;
}
/* Parses PIBHEX format. */
-static bool
+static char *
parse_PIBHEX (struct data_in *i)
{
double n;
while ((c = ss_get_char (&i->input)) != EOF)
{
if (!c_isxdigit (c))
- {
- data_warning (i, _("Unrecognized character in field."));
- return false;
- }
+ return xstrdup (_("Unrecognized character in field."));
n = n * 16.0 + hexit_value (c);
}
i->output->f = n;
- return true;
+ return NULL;
}
/* Parses RBHEX format. */
-static bool
+static char *
parse_RBHEX (struct data_in *i)
{
double d;
int hi = ss_get_char (&i->input);
int lo = ss_get_char (&i->input);
if (lo == EOF)
- {
- data_warning (i, _("Field must have even length."));
- return false;
- }
+ return xstrdup (_("Field must have even length."));
else if (!c_isxdigit (hi) || !c_isxdigit (lo))
- {
- data_warning (i, _("Field must contain only hex digits."));
- return false;
- }
+ return xstrdup (_("Field must contain only hex digits."));
((unsigned char *) &d)[j] = 16 * hexit_value (hi) + hexit_value (lo);
}
i->output->f = d;
- return true;
+ return NULL;
}
/* Digits for Z format. */
}
/* Parses Z format. */
-static bool
+static char *
parse_Z (struct data_in *i)
{
struct string tmp;
/* Trim spaces and check for missing value representation. */
if (trim_spaces_and_check_missing (i))
- return true;
+ return NULL;
ds_init_empty (&tmp);
ds_extend (&tmp, 64);
else
{
ds_destroy (&tmp);
- return false;
+ return xstrdup (_("Invalid zoned decimal syntax."));
}
}
if (!ss_is_empty (i->input))
{
+ char *error;
+
if (ds_length (&tmp) == 1)
- data_warning (i, _("Field contents are not numeric."));
+ error = xstrdup (_("Field contents are not numeric."));
else
- data_warning (i, _("Number followed by garbage."));
+ error = xstrdup (_("Number followed by garbage."));
+
ds_destroy (&tmp);
- return false;
+ return error;
}
/* Let c_strtod() do the conversion. */
{
if (fabs (i->output->f) > 1)
{
- data_warning (i, _("Too-large number set to system-missing."));
i->output->f = SYSMIS;
+ ds_destroy (&tmp);
+ return xstrdup (_("Too-large number set to system-missing."));
}
else
{
- data_warning (i, _("Too-small number set to zero."));
i->output->f = 0.0;
+ ds_destroy (&tmp);
+ return xstrdup (_("Too-small number set to zero."));
}
}
else
errno = save_errno;
ds_destroy (&tmp);
- return true;
+ return NULL;
}
/* Parses IB format. */
-static bool
+static char *
parse_IB (struct data_in *i)
{
size_t bytes;
i->output->f = -(double) -value;
}
- return true;
+ return NULL;
}
/* Parses PIB format. */
-static bool
+static char *
parse_PIB (struct data_in *i)
{
i->output->f = integer_get (settings_get_input_integer_format (), ss_data (i->input),
MIN (8, ss_length (i->input)));
- return true;
+ return NULL;
}
/* Consumes the first character of S. Stores its high 4 bits in
}
/* Parses P format. */
-static bool
+static char *
parse_P (struct data_in *i)
{
int high_nibble, low_nibble;
{
get_nibbles (&i->input, &high_nibble, &low_nibble);
if (high_nibble > 9 || low_nibble > 9)
- return false;
+ return xstrdup (_("Invalid syntax for P field."));
i->output->f = (100 * i->output->f) + (10 * high_nibble) + low_nibble;
}
get_nibbles (&i->input, &high_nibble, &low_nibble);
if (high_nibble > 9)
- return false;
+ return xstrdup (_("Invalid syntax for P field."));
i->output->f = (10 * i->output->f) + high_nibble;
if (low_nibble < 10)
i->output->f = (10 * i->output->f) + low_nibble;
else if (low_nibble == 0xb || low_nibble == 0xd)
i->output->f = -i->output->f;
- return true;
+ return NULL;
}
/* Parses PK format. */
-static bool
+static char *
parse_PK (struct data_in *i)
{
i->output->f = 0.0;
if (high_nibble > 9 || low_nibble > 9)
{
i->output->f = SYSMIS;
- return true;
+ return NULL;
}
i->output->f = (100 * i->output->f) + (10 * high_nibble) + low_nibble;
}
- return true;
+ return NULL;
}
/* Parses RB format. */
-static bool
+static char *
parse_RB (struct data_in *i)
{
enum float_format ff = settings_get_input_float_format ();
else
i->output->f = SYSMIS;
- return true;
+ return NULL;
}
/* Parses A format. */
-static bool
+static char *
parse_A (struct data_in *i)
{
/* This is equivalent to buf_copy_rpad, except that we posibly
if (dst_size > src_size)
memset (&dst[src_size], ' ', dst_size - src_size);
- return true;
+ return NULL;
}
/* Parses AHEX format. */
-static bool
+static char *
parse_AHEX (struct data_in *i)
{
uint8_t *s = value_str_rw (i->output, i->width);
if (hi == EOF)
break;
else if (lo == EOF)
- {
- data_warning (i, _("Field must have even length."));
- return false;
- }
+ return xstrdup (_("Field must have even length."));
if (!c_isxdigit (hi) || !c_isxdigit (lo))
- {
- data_warning (i, _("Field must contain only hex digits."));
- return false;
- }
+ return xstrdup (_("Field must contain only hex digits."));
if (j < i->width)
s[j] = hexit_value (hi) * 16 + hexit_value (lo);
memset (&s[j], ' ', i->width - j);
- return true;
+ return NULL;
}
\f
/* Date & time format components. */
MAX_DIGITS characters in I, storing the result into *RESULT.
Returns true if successful, false if no integer was
present. */
-static bool
+static char * WARN_UNUSED_RESULT
parse_int (struct data_in *i, long *result, size_t max_digits)
{
struct substring head = ss_head (i->input, max_digits);
if (n)
{
ss_advance (&i->input, n);
- return true;
+ return NULL;
}
else
- {
- data_warning (i, _("Syntax error in date field."));
- return false;
- }
+ return xstrdup (_("Syntax error in date field."));
}
/* Parses a date integer between 1 and 31 from I, storing it into
*DAY.
Returns true if successful, false if no date was present. */
-static bool
+static char *
parse_day (struct data_in *i, long *day)
{
- if (!parse_int (i, day, SIZE_MAX))
- return false;
+ char *error = parse_int (i, day, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (*day >= 1 && *day <= 31)
- return true;
+ return NULL;
- data_warning (i, _("Day (%ld) must be between 1 and 31."), *day);
- return false;
+ return xasprintf (_("Day (%ld) must be between 1 and 31."), *day);
}
/* Parses an integer from the beginning of I.
If *TIME_SIGN is SIGN_NO_TIME, allows a sign to precede the
time and sets *TIME_SIGN. Otherwise, does not allow a sign.
Returns true if successful, false if no integer was present. */
-static bool
+static char *
parse_time_units (struct data_in *i, double seconds_per_unit,
enum time_sign *time_sign, double *time)
{
+ char *error;
long units;
if (*time_sign == SIGN_NO_TIME)
*time_sign = SIGN_POSITIVE;
}
}
- if (!parse_int (i, &units, SIZE_MAX))
- return false;
+ error = parse_int (i, &units, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (units < 0)
- {
- data_warning (i, _("Syntax error in date field."));
- return false;
- }
+ return xstrdup (_("Syntax error in date field."));
*time += units * seconds_per_unit;
- return true;
+ return NULL;
}
/* Parses a data delimiter from the beginning of I.
Returns true if successful, false if no delimiter was
present. */
-static bool
+static char *
parse_date_delimiter (struct data_in *i)
{
if (ss_ltrim (&i->input, ss_cstr ("-/.," CC_SPACES)))
- return true;
+ return NULL;
- data_warning (i, _("Delimiter expected between fields in date."));
- return false;
+ return xstrdup (_("Delimiter expected between fields in date."));
}
/* Parses spaces at the beginning of I. */
/* Parses a month name or number from the beginning of I,
storing the month (in range 1...12) into *MONTH.
Returns true if successful, false if no month was present. */
-static bool
+static char *
parse_month (struct data_in *i, long *month)
{
if (c_isdigit (ss_first (i->input)))
{
- if (!parse_int (i, month, SIZE_MAX))
- return false;
+ char *error = parse_int (i, month, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (*month >= 1 && *month <= 12)
- return true;
+ return NULL;
}
else
{
struct substring token = parse_name_token (i);
if (match_name (ss_head (token, 3), english_names, month)
|| match_name (ss_head (token, 4), roman_names, month))
- return true;
+ return NULL;
}
- data_warning (i, _("Unrecognized month format. Months may be specified "
- "as Arabic or Roman numerals or as at least 3 letters "
- "of their English names."));
- return false;
+ return xstrdup (_("Unrecognized month format. Months may be specified "
+ "as Arabic or Roman numerals or as at least 3 letters "
+ "of their English names."));
}
/* Parses a year of at most MAX_DIGITS from the beginning of I,
storing a "4-digit" year into *YEAR. */
-static bool
+static char *
parse_year (struct data_in *i, long *year, size_t max_digits)
{
- if (!parse_int (i, year, max_digits))
- return false;
+ char *error = parse_int (i, year, max_digits);
+ if (error != NULL)
+ return error;
if (*year >= 0 && *year <= 99)
{
*year += epoch_century + 100;
}
if (*year >= 1582 || *year <= 19999)
- return true;
+ return NULL;
- data_warning (i, _("Year (%ld) must be between 1582 and 19999."), *year);
- return false;
+ return xasprintf (_("Year (%ld) must be between 1582 and 19999."), *year);
}
/* Returns true if input in I has been exhausted,
false otherwise. */
-static bool
+static char *
parse_trailer (struct data_in *i)
{
if (ss_is_empty (i->input))
- return true;
+ return NULL;
- data_warning (i, _("Trailing garbage `%.*s' following date."),
- (int) ss_length (i->input), ss_data (i->input));
- return false;
+ return xasprintf (_("Trailing garbage `%.*s' following date."),
+ (int) ss_length (i->input), ss_data (i->input));
}
/* Parses a 3-digit Julian day-of-year value from I into *YDAY.
Returns true if successful, false on failure. */
-static bool
+static char *
parse_yday (struct data_in *i, long *yday)
{
struct substring num_s;
ss_get_chars (&i->input, 3, &num_s);
if (ss_span (num_s, ss_cstr (CC_DIGITS)) != 3)
- {
- data_warning (i, _("Julian day must have exactly three digits."));
- return false;
- }
+ return xstrdup (_("Julian day must have exactly three digits."));
else if (!ss_get_long (&num_s, &num) || num < 1 || num > 366)
- {
- data_warning (i, _("Julian day (%ld) must be between 1 and 366."), num);
- return false;
- }
+ return xasprintf (_("Julian day (%ld) must be between 1 and 366."), num);
*yday = num;
- return true;
+ return NULL;
}
/* Parses a quarter-of-year integer between 1 and 4 from I.
Stores the corresponding month into *MONTH.
Returns true if successful, false if no quarter was present. */
-static bool
+static char *
parse_quarter (struct data_in *i, long int *month)
{
long quarter;
+ char *error;
- if (!parse_int (i, &quarter, SIZE_MAX))
- return false;
+ error = parse_int (i, &quarter, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (quarter >= 1 && quarter <= 4)
{
*month = (quarter - 1) * 3 + 1;
- return true;
+ return NULL;
}
- data_warning (i, _("Quarter (%ld) must be between 1 and 4."), quarter);
- return false;
+ return xasprintf (_("Quarter (%ld) must be between 1 and 4."), quarter);
}
/* Parses a week-of-year integer between 1 and 53 from I,
Stores the corresponding year-of-day into *YDAY.
Returns true if successful, false if no week was present. */
-static bool
+static char *
parse_week (struct data_in *i, long int *yday)
{
+ char *error;
long week;
- if (!parse_int (i, &week, SIZE_MAX))
- return false;
+ error = parse_int (i, &week, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (week >= 1 && week <= 53)
{
*yday = (week - 1) * 7 + 1;
- return true;
+ return NULL;
}
- data_warning (i, _("Week (%ld) must be between 1 and 53."), week);
- return false;
+ return xasprintf (_("Week (%ld) must be between 1 and 53."), week);
}
/* Parses a time delimiter from the beginning of I.
Returns true if successful, false if no delimiter was
present. */
-static bool
+static char *
parse_time_delimiter (struct data_in *i)
{
if (ss_ltrim (&i->input, ss_cstr (":" CC_SPACES)) > 0)
- return true;
+ return NULL;
- data_warning (i, _("Delimiter expected between fields in time."));
- return false;
+ return xstrdup (_("Delimiter expected between fields in time."));
}
/* Parses minutes and optional seconds from the beginning of I.
The time is converted into seconds, which are added to
*TIME.
Returns true if successful, false if an error was found. */
-static bool
+static char *
parse_minute_second (struct data_in *i, double *time)
{
long minute;
char buf[64];
+ char *error;
char *cp;
/* Parse minutes. */
- if (!parse_int (i, &minute, SIZE_MAX))
- return false;
+ error = parse_int (i, &minute, SIZE_MAX);
+ if (error != NULL)
+ return error;
if (minute < 0 || minute > 59)
- {
- data_warning (i, _("Minute (%ld) must be between 0 and 59."), minute);
- return false;
- }
+ return xasprintf (_("Minute (%ld) must be between 0 and 59."), minute);
*time += 60. * minute;
/* Check for seconds. */
if (ss_ltrim (&i->input, ss_cstr (":" CC_SPACES)) == 0
|| !c_isdigit (ss_first (i->input)))
- return true;
+ return NULL;
/* Parse seconds. */
cp = buf;
*time += strtod (buf, NULL);
- return true;
+ return NULL;
}
/* Parses a weekday name from the beginning of I,
storing a value of 1=Sunday...7=Saturday into *WEEKDAY.
Returns true if successful, false if an error was found. */
-static bool
+static char *
parse_weekday (struct data_in *i, long *weekday)
{
static const char *const weekday_names[] =
struct substring token = parse_name_token (i);
bool ok = match_name (ss_head (token, 2), weekday_names, weekday);
if (!ok)
- data_warning (i, _("Unrecognized weekday name. At least the first two "
- "letters of an English weekday name must be "
- "specified."));
- return ok;
+ return xstrdup (_("Unrecognized weekday name. At least the first two "
+ "letters of an English weekday name must be "
+ "specified."));
+ return NULL;
}
\f
/* Date & time formats. */
/* Parses WKDAY format. */
-static bool
+static char *
parse_WKDAY (struct data_in *i)
{
long weekday;
+ char *error;
if (trim_spaces_and_check_missing (i))
- return true;
+ return NULL;
- if (!parse_weekday (i, &weekday)
- || !parse_trailer (i))
- return false;
+ error = parse_weekday (i, &weekday);
+ if (error == NULL)
+ error = parse_trailer (i);
i->output->f = weekday;
- return true;
+ return error;
}
/* Parses MONTH format. */
-static bool
+static char *
parse_MONTH (struct data_in *i)
{
long month;
+ char *error;
if (trim_spaces_and_check_missing (i))
- return true;
+ return NULL;
- if (!parse_month (i, &month)
- || !parse_trailer (i))
- return false;
+ error = parse_month (i, &month);
+ if (error == NULL)
+ error = parse_trailer (i);
i->output->f = month;
- return true;
+ return error;
}
/* Parses DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, KWYR,
DATETIME, TIME and DTIME formats. */
-static bool
+static char *
parse_date (struct data_in *i)
{
long int year = INT_MIN;
const char *template = fmt_date_template (i->format);
size_t template_width = strlen (template);
+ char *error;
if (trim_spaces_and_check_missing (i))
- return true;
+ return NULL;
while (*template != '\0')
{
unsigned char ch = *template;
int count = 1;
- bool ok;
while (template[count] == ch)
count++;
template += count;
- ok = true;
switch (ch)
{
case 'd':
- ok = count < 3 ? parse_day (i, &day) : parse_yday (i, &yday);
+ error = count < 3 ? parse_day (i, &day) : parse_yday (i, &yday);
break;
case 'm':
- ok = parse_month (i, &month);
+ error = parse_month (i, &month);
break;
case 'y':
{
else
max_digits = 2;
}
- ok = parse_year (i, &year, max_digits);
+ error = parse_year (i, &year, max_digits);
}
break;
case 'q':
- ok = parse_quarter (i, &month);
+ error = parse_quarter (i, &month);
break;
case 'w':
- ok = parse_week (i, &yday);
+ error = parse_week (i, &yday);
break;
case 'D':
- ok = parse_time_units (i, 60. * 60. * 24., &time_sign, &time);
+ error = parse_time_units (i, 60. * 60. * 24., &time_sign, &time);
break;
case 'H':
- ok = parse_time_units (i, 60. * 60., &time_sign, &time);
+ error = parse_time_units (i, 60. * 60., &time_sign, &time);
break;
case 'M':
- ok = parse_minute_second (i, &time);
+ error = parse_minute_second (i, &time);
break;
case '-':
case '/':
case '.':
case 'X':
- ok = parse_date_delimiter (i);
+ error = parse_date_delimiter (i);
break;
case ':':
- ok = parse_time_delimiter (i);
+ error = parse_time_delimiter (i);
case ' ':
parse_spaces (i);
+ error = NULL;
break;
default:
assert (count == 1);
if (!ss_match_char (&i->input, c_toupper (ch))
&& !ss_match_char (&i->input, c_tolower (ch)))
- {
- data_warning (i, _("`%c' expected in date field."), ch);
- return false;
- }
+ error = xasprintf (_("`%c' expected in date field."), ch);
+ else
+ error = NULL;
break;
}
- if (!ok)
- return false;
+ if (error != NULL)
+ return error;
}
- if (!parse_trailer (i))
- return false;
+ error = parse_trailer (i);
+ if (error != NULL)
+ return error;
if (year != INT_MIN)
{
ofs = calendar_gregorian_to_offset (year, month, day, &error);
if (ofs == SYSMIS)
- {
- data_warning (i, "%s", error);
- free (error);
- return false;
- }
+ return error;
date = (yday - 1 + ofs) * 60. * 60. * 24.;
}
else
date = 0.;
i->output->f = date + (time_sign == SIGN_NEGATIVE ? -time : time);
- return true;
+ return NULL;
}
-
\f
/* Utility functions. */
-/* Outputs FORMAT with as a warning for input I. */
-static void
-data_warning (const struct data_in *i, const char *format, ...)
-{
- va_list args;
- struct msg m;
- struct string text;
-
- ds_init_empty (&text);
- ds_put_char (&text, '(');
- ds_put_format (&text, _("%s field) "), fmt_name (i->format));
-
- va_start (args, format);
- ds_put_vformat (&text, format, args);
- va_end (args);
-
- m.category = MSG_C_DATA;
- m.severity = MSG_S_WARNING;
- m.text = ds_cstr (&text);
- m.where.file_name = NULL;
- m.where.line_number = 0;
- m.where.first_column = i->first_column;
- m.where.last_column = i->last_column;
-
- msg_emit (&m);
-}
-
/* Sets the default result for I.
For a numeric format, this is the value set on SET BLANKS
(typically system-missing); for a string format, it is all