1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
31 #include "julcal/julcal.h"
40 /*#define DEBUGGING 1 */
41 #include "debug-print.h"
44 /* Specialized error routine. */
46 static void dls_error (const struct data_in *, const char *format, ...)
47 __attribute__ ((format (printf, 2, 3)));
50 dls_error (const struct data_in *i, const char *format, ...)
54 if (i->flags & DI_IGNORE_ERROR)
60 va_start (args, format);
61 snprintf (buf, 1024, format, args);
69 ds_init (NULL, &title, 64);
70 if (!getl_reading_script)
71 ds_concat (&title, _("data-file error: "));
73 ds_printf (&title, _("(column %d"), i->f1);
75 ds_printf (&title, _("(columns %d-%d"), i->f1, i->f2);
76 ds_printf (&title, _(", field type %s) "), fmt_to_string (&i->format));
79 err_location (&e.where);
80 e.title = ds_value (&title);
89 /* Excludes leading and trailing whitespace from I by adjusting
92 trim_whitespace (struct data_in *i)
94 while (i->s < i->e && isspace (i->s[0]))
97 while (i->s < i->e && isspace (i->e[-1]))
101 /* Returns nonzero if we're not at the end of the string being
104 have_char (struct data_in *i)
109 /* Format parsers. */
111 static int parse_int (struct data_in *i, long *result);
113 /* This function is based on strtod() from the GNU C library. */
115 parse_numeric (struct data_in *i)
117 short int sign; /* +1 or -1. */
118 double num; /* The number so far. */
120 int got_dot; /* Found a decimal point. */
121 int got_digit; /* Count of digits. */
123 int decimal; /* Decimal point character. */
124 int grouping; /* Grouping character. */
126 long int exponent; /* Number's exponent. */
127 int type; /* Usually same as i->format.type. */
131 type = i->format.type;
132 if (type == FMT_DOLLAR && have_char (i) && *i->s == '$')
141 sign = *i->s == '-' ? -1 : 1;
142 if (*i->s == '-' || *i->s == '+')
148 decimal = set_decimal;
149 grouping = set_grouping;
153 decimal = set_grouping;
154 grouping = set_decimal;
162 for (; have_char (i); i->s++)
168 /* Make sure that multiplication by 10 will not overflow. */
169 if (num > DBL_MAX * 0.1)
170 /* The value of the digit doesn't matter, since we have already
171 gotten as many digits as can be represented in a `double'.
172 This doesn't necessarily mean the result will overflow.
173 The exponent may reduce it to within range.
175 We just need to record that there was another
176 digit so that we can multiply by 10 later. */
179 num = (num * 10.0) + (*i->s - '0');
181 /* Keep track of the number of digits after the decimal point.
182 If we just divided by 10 here, we would lose precision. */
186 else if (!got_dot && *i->s == decimal)
187 /* Record that we have found the decimal point. */
189 else if ((type != FMT_COMMA && type != FMT_DOT) || *i->s != grouping)
190 /* Any other character terminates the number. */
205 && (tolower (*i->s) == 'e' || tolower (*i->s) == 'd'
206 || (type == FMT_E && (*i->s == '+' || *i->s == '-'))))
208 /* Get the exponent specified after the `e' or `E'. */
213 if (!parse_int (i, &exp))
219 exponent -= i->format.d;
221 if (type == FMT_PCT && have_char (i) && *i->s == '%')
225 dls_error (i, _("Field contents followed by garbage."));
236 /* Multiply NUM by 10 to the EXPONENT power, checking for overflow
241 if (-exponent + got_digit > -(DBL_MIN_10_EXP) + 5
242 || num < DBL_MIN * pow (10.0, (double) -exponent))
244 num *= pow (10.0, (double) exponent);
246 else if (exponent > 0)
248 if (num > DBL_MAX * pow (10.0, (double) -exponent))
250 num *= pow (10.0, (double) exponent);
253 i->v->f = sign * num;
257 /* Return an overflow error. */
258 dls_error (i, _("Overflow in floating-point constant."));
263 /* Return an underflow error. */
264 dls_error (i, _("Underflow in floating-point constant."));
269 /* There was no number. */
270 dls_error (i, _("Field does not form a valid floating-point constant."));
275 /* Returns the integer value of hex digit C. */
279 const char s[] = "0123456789abcdef";
280 const char *cp = strchr (s, tolower ((unsigned char) c));
287 parse_N (struct data_in *i)
289 const unsigned char *cp;
291 for (cp = i->s; cp < i->e; cp++)
295 dls_error (i, _("All characters in field must be digits."));
299 i->v->f = i->v->f * 10.0 + *cp - '0';
303 i->v->f /= pow (10.0, i->format.d);
308 parse_PIBHEX (struct data_in *i)
311 const unsigned char *cp;
316 for (cp = i->s; cp < i->e; cp++)
320 dls_error (i, _("Unrecognized character in field."));
324 n = n * 16.0 + hexit_value (*cp);
332 parse_RBHEX (struct data_in *i)
334 /* Validate input. */
336 if ((i->e - i->s) % 2)
338 dls_error (i, _("Field must have even length."));
343 const unsigned char *cp;
345 for (cp = i->s; cp < i->e; cp++)
348 dls_error (i, _("Field must contain only hex digits."));
358 unsigned char c[sizeof (double)];
364 memset (u.c, 0, sizeof u.c);
365 for (j = 0; j < min ((i->e - i->s) / 2, sizeof u.d); j++)
366 u.c[j] = 16 * hexit_value (i->s[j * 2]) + hexit_value (i->s[j * 2 + 1]);
375 parse_Z (struct data_in *i)
379 /* Warn user that we suck. */
385 msg (MW, _("Quality of zoned decimal (Z) input format code is "
386 "suspect. Check your results three times, report bugs "
392 /* Validate input. */
397 dls_error (i, _("Zoned decimal field contains fewer than 2 "
402 /* Copy sign into buf[0]. */
403 if ((i->e[-1] & 0xc0) != 0xc0)
405 dls_error (i, _("Bad sign byte in zoned decimal number."));
408 buf[0] = (i->e[-1] ^ (i->e[-1] >> 1)) & 0x10 ? '-' : '+';
410 /* Copy digits into buf[1 ... len - 1] and terminate string. */
412 const unsigned char *sp;
415 for (sp = i->s, dp = buf + 1; sp < i->e - 1; sp++, dp++)
418 else if ((*sp & 0xf0) == 0xf0 && (*sp & 0xf) < 10)
419 *dp = (*sp & 0xf) + '0';
422 dls_error (i, _("Format error in zoned decimal number."));
429 /* Parse as number. */
433 i->v->f = strtod ((char *) buf, (char **) &tail);
434 if ((unsigned char *) tail != i->e)
436 dls_error (i, _("Error in syntax of zoned decimal number."));
445 parse_IB (struct data_in *i)
452 /* We want the data to be in big-endian format. If this is a
453 little-endian machine, reverse the byte order. */
454 if (endian == LITTLE)
456 memcpy (buf, i->s, i->e - i->s);
457 mm_reverse (buf, i->e - i->s);
463 /* If the value is negative, we need to logical-NOT each value
474 for (j = 0; j < i->e - i->s; j++)
475 i->v->f = i->v->f * 256.0 + (p[j] ^ xor);
478 /* If the value is negative, add 1 and set the sign, to complete a
479 two's-complement negation. */
481 i->v->f = -(i->v->f + 1.0);
484 i->v->f /= pow (10.0, i->format.d);
490 parse_PIB (struct data_in *i)
496 for (j = 0; j < i->e - i->s; j++)
497 i->v->f = i->v->f * 256.0 + i->s[j];
499 for (j = i->e - i->s - 1; j >= 0; j--)
500 i->v->f = i->v->f * 256.0 + i->s[j];
503 i->v->f /= pow (10.0, i->format.d);
509 parse_P (struct data_in *i)
511 const unsigned char *cp;
514 for (cp = i->s; cp < i->e - 1; cp++)
516 i->v->f = i->v->f * 10 + (*cp >> 4);
517 i->v->f = i->v->f * 10 + (*cp & 15);
519 i->v->f = i->v->f * 10 + (*cp >> 4);
520 if ((*cp ^ (*cp >> 1)) & 0x10)
524 i->v->f /= pow (10.0, i->format.d);
530 parse_PK (struct data_in *i)
532 const unsigned char *cp;
535 for (cp = i->s; cp < i->e; cp++)
537 i->v->f = i->v->f * 10 + (*cp >> 4);
538 i->v->f = i->v->f * 10 + (*cp & 15);
542 i->v->f /= pow (10.0, i->format.d);
548 parse_RB (struct data_in *i)
553 unsigned char c[sizeof (double)];
557 memset (u.c, 0, sizeof u.c);
558 memcpy (u.c, i->s, min ((int) sizeof (u.c), i->e - i->s));
565 parse_A (struct data_in *i)
567 ptrdiff_t len = i->e - i->s;
569 if (len >= i->format.w)
570 memcpy (i->v->s, i->s, i->format.w);
573 memcpy (i->v->s, i->s, len);
574 memset (i->v->s + len, ' ', i->format.w - len);
578 memset (i->v->s + i->format.w, '%',
579 REM_RND_UP (i->format.w, MAX_SHORT_STRING));
586 parse_AHEX (struct data_in *i)
588 /* Validate input. */
590 if ((i->e - i->s) % 2)
592 dls_error (i, _("Field must have even length."));
597 const unsigned char *cp;
599 for (cp = i->s; cp < i->e; cp++)
602 dls_error (i, _("Field must contain only hex digits."));
611 for (j = 0; j < min (i->e - i->s, i->format.w); j += 2)
612 i->v->s[j / 2] = hexit_value (i->s[j]) * 16 + hexit_value (i->s[j + 1]);
613 memset (i->v->s + (i->e - i->s) / 2, ' ', (i->format.w - (i->e - i->s)) / 2);
617 memset (i->v->s + i->format.w / 2, '%',
618 REM_RND_UP (i->format.w / 2, MAX_SHORT_STRING));
624 /* Date & time format components. */
626 /* Advances *CP past any whitespace characters. */
628 skip_whitespace (struct data_in *i)
630 while (isspace ((unsigned char) *i->s))
635 parse_leader (struct data_in *i)
642 force_have_char (struct data_in *i)
647 dls_error (i, _("Unexpected end of field."));
652 parse_int (struct data_in *i, long *result)
656 if (!force_have_char (i))
664 else if (*i->s == '-')
671 if (!isdigit (*i->s))
673 dls_error (i, _("Digit expected in field."));
680 *result = *result * 10 + *i->s++ - '0';
681 if (!have_char (i) || !isdigit (*i->s))
691 parse_day (struct data_in *i, long *day)
693 if (!parse_int (i, day))
695 if (*day >= 1 && *day <= 31)
698 dls_error (i, _("Day (%ld) must be between 1 and 31."), *day);
703 parse_day_count (struct data_in *i, long *day_count)
705 return parse_int (i, day_count);
709 parse_date_delimiter (struct data_in *i)
714 && (*i->s == '-' || *i->s == '/' || isspace (*i->s)
715 || *i->s == '.' || *i->s == ','))
723 dls_error (i, _("Delimiter expected between fields in date."));
727 /* Formats NUMBER as Roman numerals in ROMAN, or as Arabic numerals if
728 the Roman expansion would be too long. */
730 to_roman (int number, char roman[32])
732 int save_number = number;
736 int value; /* Value corresponding to this digit. */
737 char name; /* Digit name. */
740 static const struct roman_digit roman_tab[7] =
755 assert (32 >= INT_DIGITS + 1);
765 for (i = 0; i < 7; i++)
767 int digit = roman_tab[i].value;
768 while (number >= digit)
773 *cp++ = roman_tab[i].name;
776 for (j = i + 1; j < 7; j++)
778 if (i == 4 && j == 5) /* VX is not a shortened form of V. */
781 digit = roman_tab[i].value - roman_tab[j].value;
782 while (number >= digit)
787 *cp++ = roman_tab[j].name;
788 *cp++ = roman_tab[i].name;
796 sprintf (roman, "%d", save_number);
799 /* Returns true if C is a (lowercase) roman numeral. */
800 #define CHAR_IS_ROMAN(C) \
801 ((C) == 'x' || (C) == 'v' || (C) == 'i')
803 /* Returns the value of a single (lowercase) roman numeral. */
804 #define ROMAN_VALUE(C) \
805 ((C) == 'x' ? 10 : ((C) == 'v' ? 5 : 1))
808 parse_month (struct data_in *i, long *month)
810 if (!force_have_char (i))
815 if (!parse_int (i, month))
817 if (*month >= 1 && *month <= 12)
820 dls_error (i, _("Month (%ld) must be between 1 and 12."), *month);
824 if (CHAR_IS_ROMAN (tolower (*i->s)))
826 int last = ROMAN_VALUE (tolower (*i->s));
834 if (!have_char || !CHAR_IS_ROMAN (tolower (*i->s)))
841 value = ROMAN_VALUE (tolower (*i->s));
844 else if (value > last)
846 *month += value - last;
856 if (*month < 1 || *month > 12)
860 to_roman (*month, buf);
861 dls_error (i, _("Month (%s) must be between I and XII."), buf);
869 static const char *months[12] =
871 "january", "february", "march", "april", "may", "june",
872 "july", "august", "september", "october", "november", "december",
881 have_char (i) && isalpha (*i->s) && mp < &month_buf[31];
883 *mp++ = tolower (*i->s);
886 if (have_char (i) && isalpha (*i->s))
888 dls_error (i, _("Month name (%s...) is too long."), month_buf);
892 for (j = 0; j < 12; j++)
893 if (lex_id_match (months[j], month_buf))
899 dls_error (i, _("Bad month name (%s)."), month_buf);
905 parse_year (struct data_in *i, long *year)
907 if (!parse_int (i, year))
910 if (*year >= 0 && *year <= 199)
912 if (*year >= 1582 || *year <= 19999)
915 dls_error (i, _("Year (%ld) must be between 1582 and 19999."), *year);
920 parse_trailer (struct data_in *i)
926 dls_error (i, _("Trailing garbage \"%s\" following date."), i->s);
931 parse_julian (struct data_in *i, long *julian)
933 if (!parse_int (i, julian))
937 int day = *julian % 1000;
939 if (day < 1 || day > 366)
941 dls_error (i, _("Julian day (%d) must be between 1 and 366."), day);
947 int year = *julian / 1000;
949 if (year >= 0 && year <= 199)
951 else if (year < 1582 || year > 19999)
953 dls_error (i, _("Year (%d) must be between 1582 and 19999."), year);
962 parse_quarter (struct data_in *i, long *quarter)
964 if (!parse_int (i, quarter))
966 if (*quarter >= 1 && *quarter <= 4)
969 dls_error (i, _("Quarter (%ld) must be between 1 and 4."), *quarter);
974 parse_q_delimiter (struct data_in *i)
977 if (!have_char (i) || tolower (*i->s) != 'q')
979 dls_error (i, _("`Q' expected between quarter and year."));
988 parse_week (struct data_in *i, long *week)
990 if (!parse_int (i, week))
992 if (*week >= 1 && *week <= 53)
995 dls_error (i, _("Week (%ld) must be between 1 and 53."), *week);
1000 parse_wk_delimiter (struct data_in *i)
1002 skip_whitespace (i);
1003 if (i->s + 1 >= i->e
1004 || tolower (i->s[0]) != 'w' || tolower (i->s[1]) != 'k')
1006 dls_error (i, _("`WK' expected between week and year."));
1010 skip_whitespace (i);
1015 parse_time_delimiter (struct data_in *i)
1019 while (have_char (i)
1020 && (*i->s == ':' || *i->s == '.' || isspace (*i->s)))
1029 dls_error (i, _("Delimiter expected between fields in time."));
1034 parse_hour (struct data_in *i, long *hour)
1036 if (!parse_int (i, hour))
1041 dls_error (i, _("Hour (%ld) must be positive."), *hour);
1046 parse_minute (struct data_in *i, long *minute)
1048 if (!parse_int (i, minute))
1050 if (*minute >= 0 && *minute <= 59)
1053 dls_error (i, _("Minute (%ld) must be between 0 and 59."), *minute);
1058 parse_opt_second (struct data_in *i, double *second)
1065 while (have_char (i)
1066 && (*i->s == ':' || *i->s == '.' || isspace (*i->s)))
1072 if (!delim || !isdigit (*i->s))
1079 while (have_char (i) && isdigit (*i->s))
1081 if (have_char (i) && *i->s == '.')
1083 while (have_char (i) && isdigit (*i->s))
1087 *second = strtod (buf, NULL);
1093 parse_hour24 (struct data_in *i, long *hour24)
1095 if (!parse_int (i, hour24))
1097 if (*hour24 >= 0 && *hour24 <= 23)
1100 dls_error (i, _("Hour (%ld) must be between 0 and 23."), *hour24);
1106 parse_weekday (struct data_in *i, int *weekday)
1109 #define TUPLE(A,B) \
1112 if (i->s + 1 >= i->e)
1114 dls_error (i, _("Day of the week expected in date value."));
1118 switch (TUPLE (tolower (i->s[0]), tolower (i->s[1])))
1120 case TUPLE ('s', 'u'):
1124 case TUPLE ('m', 'o'):
1128 case TUPLE ('t', 'u'):
1132 case TUPLE ('w', 'e'):
1136 case TUPLE ('t', 'h'):
1140 case TUPLE ('f', 'r'):
1144 case TUPLE ('s', 'a'):
1149 dls_error (i, _("Day of the week expected in date value."));
1153 while (have_char (i) && isalpha (*i->s))
1162 parse_spaces (struct data_in *i)
1164 skip_whitespace (i);
1169 parse_sign (struct data_in *i, int *sign)
1171 if (!force_have_char (i))
1193 /* Date & time formats. */
1196 valid_date (struct data_in *i)
1198 if (i->v->f == SYSMIS)
1200 dls_error (i, _("Date is not in valid range between "
1201 "15 Oct 1582 and 31 Dec 19999."));
1209 parse_DATE (struct data_in *i)
1211 long day, month, year;
1213 if (!parse_leader (i)
1214 || !parse_day (i, &day)
1215 || !parse_date_delimiter (i)
1216 || !parse_month (i, &month)
1217 || !parse_date_delimiter (i)
1218 || !parse_year (i, &year)
1219 || !parse_trailer (i))
1222 i->v->f = calendar_to_julian (year, month, day);
1223 if (!valid_date (i))
1225 i->v->f *= 60. * 60. * 24.;
1231 parse_ADATE (struct data_in *i)
1233 long month, day, year;
1235 if (!parse_leader (i)
1236 || !parse_month (i, &month)
1237 || !parse_date_delimiter (i)
1238 || !parse_day (i, &day)
1239 || !parse_date_delimiter (i)
1240 || !parse_year (i, &year)
1241 || !parse_trailer (i))
1244 i->v->f = calendar_to_julian (year, month, day);
1245 if (!valid_date (i))
1247 i->v->f *= 60. * 60. * 24.;
1253 parse_EDATE (struct data_in *i)
1255 long month, day, year;
1257 if (!parse_leader (i)
1258 || !parse_day (i, &day)
1259 || !parse_date_delimiter (i)
1260 || !parse_month (i, &month)
1261 || !parse_date_delimiter (i)
1262 || !parse_year (i, &year)
1263 || !parse_trailer (i))
1266 i->v->f = calendar_to_julian (year, month, day);
1267 if (!valid_date (i))
1269 i->v->f *= 60. * 60. * 24.;
1275 parse_SDATE (struct data_in *i)
1277 long month, day, year;
1279 if (!parse_leader (i)
1280 || !parse_year (i, &year)
1281 || !parse_date_delimiter (i)
1282 || !parse_month (i, &month)
1283 || !parse_date_delimiter (i)
1284 || !parse_day (i, &day)
1285 || !parse_trailer (i))
1288 i->v->f = calendar_to_julian (year, month, day);
1289 if (!valid_date (i))
1291 i->v->f *= 60. * 60. * 24.;
1297 parse_JDATE (struct data_in *i)
1301 if (!parse_leader (i)
1302 || !parse_julian (i, &julian)
1303 || !parse_trailer (i))
1306 if (julian / 1000 == 1582)
1307 i->v->f = calendar_to_julian (1583, 1, 1) - 365;
1309 i->v->f = calendar_to_julian (julian / 1000, 1, 1);
1313 i->v->f = (i->v->f + julian % 1000 - 1) * 60. * 60. * 24.;
1318 return valid_date (i);
1322 parse_QYR (struct data_in *i)
1326 if (!parse_leader (i)
1327 || !parse_quarter (i, &quarter)
1328 || !parse_q_delimiter (i)
1329 || !parse_year (i, &year)
1330 || !parse_trailer (i))
1333 i->v->f = calendar_to_julian (year, (quarter - 1) * 3 + 1, 1);
1334 if (!valid_date (i))
1336 i->v->f *= 60. * 60. * 24.;
1342 parse_MOYR (struct data_in *i)
1346 if (!parse_leader (i)
1347 || !parse_month (i, &month)
1348 || !parse_date_delimiter (i)
1349 || !parse_year (i, &year)
1350 || !parse_trailer (i))
1353 i->v->f = calendar_to_julian (year, month, 1);
1354 if (!valid_date (i))
1356 i->v->f *= 60. * 60. * 24.;
1362 parse_WKYR (struct data_in *i)
1366 if (!parse_leader (i)
1367 || !parse_week (i, &week)
1368 || !parse_wk_delimiter (i)
1369 || !parse_year (i, &year)
1370 || !parse_trailer (i))
1373 i->v->f = calendar_to_julian (year, 1, 1);
1374 if (!valid_date (i))
1376 i->v->f = (i->v->f + (week - 1) * 7) * 60. * 60. * 24.;
1382 parse_TIME (struct data_in *i)
1388 if (!parse_leader (i)
1389 || !parse_sign (i, &sign)
1390 || !parse_spaces (i)
1391 || !parse_hour (i, &hour)
1392 || !parse_time_delimiter (i)
1393 || !parse_minute (i, &minute)
1394 || !parse_opt_second (i, &second))
1397 i->v->f = hour * 60. * 60. + minute * 60. + second;
1404 parse_DTIME (struct data_in *i)
1407 long day_count, hour;
1411 if (!parse_leader (i)
1412 || !parse_sign (i, &sign)
1413 || !parse_spaces (i)
1414 || !parse_day_count (i, &day_count)
1415 || !parse_time_delimiter (i)
1416 || !parse_hour (i, &hour)
1417 || !parse_time_delimiter (i)
1418 || !parse_minute (i, &minute)
1419 || !parse_opt_second (i, &second))
1422 i->v->f = (day_count * 60. * 60. * 24.
1432 parse_DATETIME (struct data_in *i)
1434 long day, month, year;
1439 if (!parse_leader (i)
1440 || !parse_day (i, &day)
1441 || !parse_date_delimiter (i)
1442 || !parse_month (i, &month)
1443 || !parse_date_delimiter (i)
1444 || !parse_year (i, &year)
1445 || !parse_time_delimiter (i)
1446 || !parse_hour24 (i, &hour24)
1447 || !parse_time_delimiter (i)
1448 || !parse_minute (i, &minute)
1449 || !parse_opt_second (i, &second))
1452 i->v->f = calendar_to_julian (year, month, day);
1453 if (!valid_date (i))
1455 i->v->f = (i->v->f * 60. * 60. * 24.
1456 + hour24 * 60. * 60.
1464 parse_WKDAY (struct data_in *i)
1468 if (!parse_leader (i)
1469 || !parse_weekday (i, &weekday)
1470 || !parse_trailer (i))
1478 parse_MONTH (struct data_in *i)
1482 if (!parse_leader (i)
1483 || !parse_month (i, &month)
1484 || !parse_trailer (i))
1491 /* Main dispatcher. */
1494 default_result (struct data_in *i)
1496 const struct fmt_desc *const fmt = &formats[i->format.type];
1498 /* Default to SYSMIS or blanks. */
1499 if (fmt->cat & FCAT_STRING)
1502 memset (i->v->s, ' ', ROUND_UP (i->format.w, MAX_SHORT_STRING));
1504 memset (i->v->s, ' ', i->format.w);
1508 i->v->f = set_blanks;
1512 data_in (struct data_in *i)
1514 const struct fmt_desc *const fmt = &formats[i->format.type];
1516 /* Check that we've got a string to work with. */
1517 if (i->e == i->s || i->format.w <= 0)
1523 i->f2 = i->f1 + (i->e - i->s) - 1;
1525 /* Make sure that the string isn't too long. */
1526 if (i->format.w > fmt->Imax_w)
1528 dls_error (i, _("Field too long (%d characters). Truncated after "
1530 i->format.w, fmt->Imax_w);
1531 i->format.w = fmt->Imax_w;
1534 if (fmt->cat & FCAT_BLANKS_SYSMIS)
1536 const unsigned char *cp;
1546 i->v->f = set_blanks;
1553 static int (*const handlers[FMT_NUMBER_OF_FORMATS])(struct data_in *) =
1555 parse_numeric, parse_N, parse_numeric, parse_numeric,
1556 parse_numeric, parse_numeric, parse_numeric,
1557 parse_Z, parse_A, parse_AHEX, parse_IB, parse_P, parse_PIB,
1558 parse_PIBHEX, parse_PK, parse_RB, parse_RBHEX,
1559 NULL, NULL, NULL, NULL, NULL,
1560 parse_DATE, parse_EDATE, parse_SDATE, parse_ADATE, parse_JDATE,
1561 parse_QYR, parse_MOYR, parse_WKYR,
1562 parse_DATETIME, parse_TIME, parse_DTIME,
1563 parse_WKDAY, parse_MONTH,
1566 int (*handler)(struct data_in *);
1569 handler = handlers[i->format.type];
1570 assert (handler != NULL);
1572 success = handler (i);
1580 /* Utility function. */
1582 /* Sets DI->{s,e} appropriately given that LINE has length LEN and the
1583 field starts at one-based column FC and ends at one-based column
1586 data_in_finite_line (struct data_in *di, const char *line, size_t len,
1589 di->s = line + ((size_t) fc <= len ? fc - 1 : len);
1590 di->e = line + ((size_t) lc <= len ? lc : len);