1 /* Copyright (C) 2002, 2003, 2005, 2007, 2008 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
37 typedef unsigned char uchar;
39 /* read.c -- Deal with formatted reads */
42 /* set_integer()-- All of the integer assignments come here to
43 * actually place the value into memory. */
46 set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
50 #ifdef HAVE_GFC_INTEGER_16
53 GFC_INTEGER_16 tmp = value;
54 memcpy (dest, (void *) &tmp, length);
60 GFC_INTEGER_8 tmp = value;
61 memcpy (dest, (void *) &tmp, length);
66 GFC_INTEGER_4 tmp = value;
67 memcpy (dest, (void *) &tmp, length);
72 GFC_INTEGER_2 tmp = value;
73 memcpy (dest, (void *) &tmp, length);
78 GFC_INTEGER_1 tmp = value;
79 memcpy (dest, (void *) &tmp, length);
83 internal_error (NULL, "Bad integer kind");
88 /* max_value()-- Given a length (kind), return the maximum signed or
92 max_value (int length, int signed_flag)
94 GFC_UINTEGER_LARGEST value;
95 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
101 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
105 for (n = 1; n < 4 * length; n++)
106 value = (value << 2) + 3;
112 value = signed_flag ? 0x7fffffffffffffff : 0xffffffffffffffff;
115 value = signed_flag ? 0x7fffffff : 0xffffffff;
118 value = signed_flag ? 0x7fff : 0xffff;
121 value = signed_flag ? 0x7f : 0xff;
124 internal_error (NULL, "Bad integer kind");
131 /* convert_real()-- Convert a character representation of a floating
132 * point number to the machine number. Returns nonzero if there is a
133 * range problem during conversion. TODO: handle not-a-numbers and
137 convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length)
146 #if defined(HAVE_STRTOF)
147 strtof (buffer, NULL);
149 (GFC_REAL_4) strtod (buffer, NULL);
151 memcpy (dest, (void *) &tmp, length);
156 GFC_REAL_8 tmp = strtod (buffer, NULL);
157 memcpy (dest, (void *) &tmp, length);
160 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
163 GFC_REAL_10 tmp = strtold (buffer, NULL);
164 memcpy (dest, (void *) &tmp, length);
168 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
171 GFC_REAL_16 tmp = strtold (buffer, NULL);
172 memcpy (dest, (void *) &tmp, length);
177 internal_error (&dtp->common, "Unsupported real kind during IO");
182 generate_error (&dtp->common, LIBERROR_READ_VALUE,
183 "Error during floating point read");
184 next_record (dtp, 1);
192 /* read_l()-- Read a logical value */
195 read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
204 if (read_block_form (dtp, p, &w) == FAILURE)
225 set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
229 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
233 generate_error (&dtp->common, LIBERROR_READ_VALUE,
234 "Bad value on logical read");
235 next_record (dtp, 1);
241 static inline gfc_char4_t
242 read_utf8 (st_parameter_dt *dtp, size_t *nbytes)
244 static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
245 static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
246 static uchar buffer[6];
253 s = (char *) &buffer[0];
254 status = read_block_form (dtp, s, nbytes);
255 if (status == FAILURE)
258 /* If this is a short read, just return. */
266 /* The number of leading 1-bits in the first byte indicates how many
268 for (nb = 2; nb < 7; nb++)
269 if ((c & ~masks[nb-1]) == patns[nb-1])
274 c = (c & masks[nb-1]);
277 s = (char *) &buffer[1];
278 status = read_block_form (dtp, s, &nread);
279 if (status == FAILURE)
281 /* Decode the bytes read. */
282 for (i = 1; i < nb; i++)
284 gfc_char4_t n = *s++;
286 if ((n & 0xC0) != 0x80)
289 c = ((c << 6) + (n & 0x3F));
292 /* Make sure the shortest possible encoding was used. */
293 if (c <= 0x7F && nb > 1) goto invalid;
294 if (c <= 0x7FF && nb > 2) goto invalid;
295 if (c <= 0xFFFF && nb > 3) goto invalid;
296 if (c <= 0x1FFFFF && nb > 4) goto invalid;
297 if (c <= 0x3FFFFFF && nb > 5) goto invalid;
299 /* Make sure the character is valid. */
300 if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
306 generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
307 return (gfc_char4_t) '?';
312 read_utf8_char1 (st_parameter_dt *dtp, char *p, int len, size_t width)
319 len = ((int) width < len) ? len : (int) width;
323 /* Proceed with decoding one character at a time. */
324 for (j = 0; j < len; j++, dest++)
326 c = read_utf8 (dtp, &nbytes);
328 /* Check for a short read and if so, break out. */
332 *dest = c > 255 ? '?' : (uchar) c;
335 /* If there was a short read, pad the remaining characters. */
336 for (i = j; i < len; i++)
342 read_default_char1 (st_parameter_dt *dtp, char *p, int len, size_t width)
347 s = gfc_alloca (width);
349 status = read_block_form (dtp, s, &width);
351 if (status == FAILURE)
353 if (width > (size_t) len)
356 m = ((int) width > len) ? len : (int) width;
361 memset (p + m, ' ', n);
366 read_utf8_char4 (st_parameter_dt *dtp, void *p, int len, size_t width)
372 len = ((int) width < len) ? len : (int) width;
374 dest = (gfc_char4_t *) p;
376 /* Proceed with decoding one character at a time. */
377 for (j = 0; j < len; j++, dest++)
379 *dest = read_utf8 (dtp, &nbytes);
381 /* Check for a short read and if so, break out. */
386 /* If there was a short read, pad the remaining characters. */
387 for (i = j; i < len; i++)
388 *dest++ = (gfc_char4_t) ' ';
394 read_default_char4 (st_parameter_dt *dtp, char *p, int len, size_t width)
400 s = gfc_alloca (width);
402 status = read_block_form (dtp, s, &width);
404 if (status == FAILURE)
406 if (width > (size_t) len)
409 m = ((int) width > len) ? len : (int) width;
411 dest = (gfc_char4_t *) p;
413 for (n = 0; n < m; n++, dest++, s++)
414 *dest = (unsigned char ) *s;
416 for (n = 0; n < len - (int) width; n++, dest++)
417 *dest = (unsigned char) ' ';
421 /* read_a()-- Read a character record into a KIND=1 character destination,
422 processing UTF-8 encoding if necessary. */
425 read_a (st_parameter_dt *dtp, const fnode *f, char *p, int length)
431 if (wi == -1) /* '(A)' edit descriptor */
435 /* Read in w characters, treating comma as not a separator. */
436 dtp->u.p.sf_read_comma = 0;
438 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
439 read_utf8_char1 (dtp, p, length, w);
441 read_default_char1 (dtp, p, length, w);
443 dtp->u.p.sf_read_comma =
444 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
448 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
449 processing UTF-8 encoding if necessary. */
452 read_a_char4 (st_parameter_dt *dtp, const fnode *f, char *p, int length)
458 if (wi == -1) /* '(A)' edit descriptor */
462 /* Read in w characters, treating comma as not a separator. */
463 dtp->u.p.sf_read_comma = 0;
465 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
466 read_utf8_char4 (dtp, p, length, w);
468 read_default_char4 (dtp, p, length, w);
470 dtp->u.p.sf_read_comma =
471 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
474 /* eat_leading_spaces()-- Given a character pointer and a width,
475 * ignore the leading spaces. */
478 eat_leading_spaces (int *width, char *p)
482 if (*width == 0 || *p != ' ')
494 next_char (st_parameter_dt *dtp, char **p, int *w)
509 if (dtp->u.p.blank_status != BLANK_UNSPECIFIED)
510 return ' '; /* return a blank to signal a null */
512 /* At this point, the rest of the field has to be trailing blanks */
526 /* read_decimal()-- Read a decimal integer value. The values here are
530 read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
532 GFC_UINTEGER_LARGEST value, maxv, maxv_10;
533 GFC_INTEGER_LARGEST v;
542 if (read_block_form (dtp, p, &wu) == FAILURE)
547 p = eat_leading_spaces (&w, p);
550 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
554 maxv = max_value (length, 1);
576 /* At this point we have a digit-string */
581 c = next_char (dtp, &p, &w);
587 if (dtp->u.p.blank_status == BLANK_NULL) continue;
588 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
591 if (c < '0' || c > '9')
594 if (value > maxv_10 && compile_options.range_check == 1)
600 if (value > maxv - c && compile_options.range_check == 1)
609 set_integer (dest, v, length);
613 generate_error (&dtp->common, LIBERROR_READ_VALUE,
614 "Bad value during integer read");
615 next_record (dtp, 1);
619 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
620 "Value overflowed during integer read");
621 next_record (dtp, 1);
626 /* read_radix()-- This function reads values for non-decimal radixes.
627 * The difference here is that we treat the values here as unsigned
628 * values for the purposes of overflow. If minus sign is present and
629 * the top bit is set, the value will be incorrect. */
632 read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
635 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
636 GFC_INTEGER_LARGEST v;
645 if (read_block_form (dtp, p, &wu) == FAILURE)
650 p = eat_leading_spaces (&w, p);
653 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
657 maxv = max_value (length, 0);
658 maxv_r = maxv / radix;
679 /* At this point we have a digit-string */
684 c = next_char (dtp, &p, &w);
689 if (dtp->u.p.blank_status == BLANK_NULL) continue;
690 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
696 if (c < '0' || c > '1')
701 if (c < '0' || c > '7')
726 c = c - 'a' + '9' + 1;
735 c = c - 'A' + '9' + 1;
749 value = radix * value;
751 if (maxv - c < value)
760 set_integer (dest, v, length);
764 generate_error (&dtp->common, LIBERROR_READ_VALUE,
765 "Bad value during integer read");
766 next_record (dtp, 1);
770 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
771 "Value overflowed during integer read");
772 next_record (dtp, 1);
777 /* read_f()-- Read a floating point number with F-style editing, which
778 is what all of the other floating point descriptors behave as. The
779 tricky part is that optional spaces are allowed after an E or D,
780 and the implicit decimal point if a decimal point is not present in
784 read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
787 int w, seen_dp, exponent;
788 int exponent_sign, val_sign;
794 char scratch[SCRATCH_SIZE];
802 if (read_block_form (dtp, p, &wu) == FAILURE)
807 p = eat_leading_spaces (&w, p);
813 if (*p == '-' || *p == '+')
822 p = eat_leading_spaces (&w, p);
826 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
827 is required at this point */
829 if (!isdigit (*p) && *p != '.' && *p != ',' && *p != 'd' && *p != 'D'
830 && *p != 'e' && *p != 'E')
833 /* Remember the position of the first digit. */
837 /* Scan through the string to find the exponent. */
843 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA
893 /* No exponent has been seen, so we use the current scale factor */
894 exponent = -dtp->u.p.scale_factor;
898 generate_error (&dtp->common, LIBERROR_READ_VALUE,
899 "Bad value during floating point read");
900 next_record (dtp, 1);
903 /* The value read is zero */
908 *((GFC_REAL_4 *) dest) = 0;
912 *((GFC_REAL_8 *) dest) = 0;
915 #ifdef HAVE_GFC_REAL_10
917 *((GFC_REAL_10 *) dest) = 0;
921 #ifdef HAVE_GFC_REAL_16
923 *((GFC_REAL_16 *) dest) = 0;
928 internal_error (&dtp->common, "Unsupported real kind during IO");
932 /* At this point the start of an exponent has been found */
934 while (w > 0 && *p == ' ')
955 /* At this point a digit string is required. We calculate the value
956 of the exponent in order to take account of the scale factor and
957 the d parameter before explict conversion takes place. */
959 /* Normal processing of exponent */
961 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
963 while (w > 0 && isdigit (*p))
965 exponent = 10 * exponent + *p - '0';
970 /* Only allow trailing blanks */
980 else /* BZ or BN status is enabled */
986 if (dtp->u.p.blank_status == BLANK_ZERO) *p = '0';
987 if (dtp->u.p.blank_status == BLANK_NULL)
994 else if (!isdigit (*p))
997 exponent = 10 * exponent + *p - '0';
1003 exponent = exponent * exponent_sign;
1006 /* Use the precision specified in the format if no decimal point has been
1009 exponent -= f->u.real.d;
1028 i = ndigits + edigits + 1;
1032 if (i < SCRATCH_SIZE)
1035 buffer = get_mem (i);
1037 /* Reformat the string into a temporary buffer. As we're using atof it's
1038 easiest to just leave the decimal point in place. */
1042 for (; ndigits > 0; ndigits--)
1046 if (dtp->u.p.blank_status == BLANK_ZERO) *digits = '0';
1047 if (dtp->u.p.blank_status == BLANK_NULL)
1058 sprintf (p, "%d", exponent);
1060 /* Do the actual conversion. */
1061 convert_real (dtp, dest, buffer, length);
1063 if (buffer != scratch)
1069 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1070 * and never look at it. */
1073 read_x (st_parameter_dt * dtp, int n)
1075 if ((dtp->u.p.current_unit->pad_status == PAD_NO || is_internal_unit (dtp))
1076 && dtp->u.p.current_unit->bytes_left < n)
1077 n = dtp->u.p.current_unit->bytes_left;
1079 dtp->u.p.sf_read_comma = 0;
1081 read_sf (dtp, &n, 1);
1082 dtp->u.p.sf_read_comma = 1;
1083 dtp->u.p.current_unit->strm_pos += (gfc_offset) n;