1 /* Copyright (C) 2002-2016 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 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 3, or (at your option)
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
35 typedef unsigned char uchar
;
37 /* read.c -- Deal with formatted reads */
40 /* set_integer()-- All of the integer assignments come here to
41 actually place the value into memory. */
44 set_integer (void *dest
, GFC_INTEGER_LARGEST value
, int length
)
48 #ifdef HAVE_GFC_INTEGER_16
49 /* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */
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 signed value of size give by length argument. */
93 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
94 GFC_UINTEGER_LARGEST value
;
99 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
103 for (int n
= 1; n
< 4 * length
; n
++)
104 value
= (value
<< 2) + 3;
108 return GFC_INTEGER_8_HUGE
;
110 return GFC_INTEGER_4_HUGE
;
112 return GFC_INTEGER_2_HUGE
;
114 return GFC_INTEGER_1_HUGE
;
116 internal_error (NULL
, "Bad integer kind");
121 /* convert_real()-- Convert a character representation of a floating
122 point number to the machine number. Returns nonzero if there is an
123 invalid input. Note: many architectures (e.g. IA-64, HP-PA)
124 require that the storage pointed to by the dest argument is
125 properly aligned for the type in question. */
128 convert_real (st_parameter_dt
*dtp
, void *dest
, const char *buffer
, int length
)
131 int round_mode
, old_round_mode
;
133 switch (dtp
->u
.p
.current_unit
->round_status
)
135 case ROUND_COMPATIBLE
:
136 /* FIXME: As NEAREST but round away from zero for a tie. */
137 case ROUND_UNSPECIFIED
:
138 /* Should not occur. */
139 case ROUND_PROCDEFINED
:
140 round_mode
= ROUND_NEAREST
;
143 round_mode
= dtp
->u
.p
.current_unit
->round_status
;
147 old_round_mode
= get_fpu_rounding_mode();
148 set_fpu_rounding_mode (round_mode
);
153 *((GFC_REAL_4
*) dest
) =
154 #if defined(HAVE_STRTOF)
155 gfc_strtof (buffer
, &endptr
);
157 (GFC_REAL_4
) gfc_strtod (buffer
, &endptr
);
162 *((GFC_REAL_8
*) dest
) = gfc_strtod (buffer
, &endptr
);
165 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
167 *((GFC_REAL_10
*) dest
) = gfc_strtold (buffer
, &endptr
);
171 #if defined(HAVE_GFC_REAL_16)
172 # if defined(GFC_REAL_16_IS_FLOAT128)
174 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, &endptr
);
176 # elif defined(HAVE_STRTOLD)
178 *((GFC_REAL_16
*) dest
) = gfc_strtold (buffer
, &endptr
);
184 internal_error (&dtp
->common
, "Unsupported real kind during IO");
187 set_fpu_rounding_mode (old_round_mode
);
189 if (buffer
== endptr
)
191 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
192 "Error during floating point read");
193 next_record (dtp
, 1);
200 /* convert_infnan()-- Convert character INF/NAN representation to the
201 machine number. Note: many architectures (e.g. IA-64, HP-PA) require
202 that the storage pointed to by the dest argument is properly aligned
203 for the type in question. */
206 convert_infnan (st_parameter_dt
*dtp
, void *dest
, const char *buffer
,
209 const char *s
= buffer
;
210 int is_inf
, plus
= 1;
226 *((GFC_REAL_4
*) dest
) = plus
? __builtin_inff () : -__builtin_inff ();
228 *((GFC_REAL_4
*) dest
) = plus
? __builtin_nanf ("") : -__builtin_nanf ("");
233 *((GFC_REAL_8
*) dest
) = plus
? __builtin_inf () : -__builtin_inf ();
235 *((GFC_REAL_8
*) dest
) = plus
? __builtin_nan ("") : -__builtin_nan ("");
238 #if defined(HAVE_GFC_REAL_10)
241 *((GFC_REAL_10
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
243 *((GFC_REAL_10
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
247 #if defined(HAVE_GFC_REAL_16)
248 # if defined(GFC_REAL_16_IS_FLOAT128)
250 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, NULL
);
255 *((GFC_REAL_16
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
257 *((GFC_REAL_16
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
263 internal_error (&dtp
->common
, "Unsupported real kind during IO");
270 /* read_l()-- Read a logical value */
273 read_l (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
280 p
= read_block_form (dtp
, &w
);
303 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
307 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
311 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
312 "Bad value on logical read");
313 next_record (dtp
, 1);
320 read_utf8 (st_parameter_dt
*dtp
, int *nbytes
)
322 static const uchar masks
[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
323 static const uchar patns
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
330 s
= read_block_form (dtp
, nbytes
);
334 /* If this is a short read, just return. */
342 /* The number of leading 1-bits in the first byte indicates how many
344 for (nb
= 2; nb
< 7; nb
++)
345 if ((c
& ~masks
[nb
-1]) == patns
[nb
-1])
350 c
= (c
& masks
[nb
-1]);
353 s
= read_block_form (dtp
, &nread
);
356 /* Decode the bytes read. */
357 for (i
= 1; i
< nb
; i
++)
359 gfc_char4_t n
= *s
++;
361 if ((n
& 0xC0) != 0x80)
364 c
= ((c
<< 6) + (n
& 0x3F));
367 /* Make sure the shortest possible encoding was used. */
368 if (c
<= 0x7F && nb
> 1) goto invalid
;
369 if (c
<= 0x7FF && nb
> 2) goto invalid
;
370 if (c
<= 0xFFFF && nb
> 3) goto invalid
;
371 if (c
<= 0x1FFFFF && nb
> 4) goto invalid
;
372 if (c
<= 0x3FFFFFF && nb
> 5) goto invalid
;
374 /* Make sure the character is valid. */
375 if (c
> 0x7FFFFFFF || (c
>= 0xD800 && c
<= 0xDFFF))
381 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, "Invalid UTF-8 encoding");
382 return (gfc_char4_t
) '?';
387 read_utf8_char1 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
394 len
= (width
< len
) ? len
: width
;
398 /* Proceed with decoding one character at a time. */
399 for (j
= 0; j
< len
; j
++, dest
++)
401 c
= read_utf8 (dtp
, &nbytes
);
403 /* Check for a short read and if so, break out. */
407 *dest
= c
> 255 ? '?' : (uchar
) c
;
410 /* If there was a short read, pad the remaining characters. */
411 for (i
= j
; i
< len
; i
++)
417 read_default_char1 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
422 s
= read_block_form (dtp
, &width
);
429 m
= (width
> len
) ? len
: width
;
434 memset (p
+ m
, ' ', n
);
439 read_utf8_char4 (st_parameter_dt
*dtp
, void *p
, int len
, int width
)
445 len
= (width
< len
) ? len
: width
;
447 dest
= (gfc_char4_t
*) p
;
449 /* Proceed with decoding one character at a time. */
450 for (j
= 0; j
< len
; j
++, dest
++)
452 *dest
= read_utf8 (dtp
, &nbytes
);
454 /* Check for a short read and if so, break out. */
459 /* If there was a short read, pad the remaining characters. */
460 for (i
= j
; i
< len
; i
++)
461 *dest
++ = (gfc_char4_t
) ' ';
467 read_default_char4 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
472 if (is_char4_unit(dtp
))
476 s4
= (gfc_char4_t
*) read_block_form4 (dtp
, &width
);
483 m
= ((int) width
> len
) ? len
: (int) width
;
485 dest
= (gfc_char4_t
*) p
;
487 for (n
= 0; n
< m
; n
++)
490 for (n
= 0; n
< len
- (int) width
; n
++)
491 *dest
++ = (gfc_char4_t
) ' ';
497 s
= read_block_form (dtp
, &width
);
504 m
= ((int) width
> len
) ? len
: (int) width
;
506 dest
= (gfc_char4_t
*) p
;
508 for (n
= 0; n
< m
; n
++, dest
++, s
++)
509 *dest
= (unsigned char ) *s
;
511 for (n
= 0; n
< len
- (int) width
; n
++, dest
++)
512 *dest
= (unsigned char) ' ';
517 /* read_a()-- Read a character record into a KIND=1 character destination,
518 processing UTF-8 encoding if necessary. */
521 read_a (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
527 if (wi
== -1) /* '(A)' edit descriptor */
531 /* Read in w characters, treating comma as not a separator. */
532 dtp
->u
.p
.sf_read_comma
= 0;
534 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
535 read_utf8_char1 (dtp
, p
, length
, w
);
537 read_default_char1 (dtp
, p
, length
, w
);
539 dtp
->u
.p
.sf_read_comma
=
540 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
544 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
545 processing UTF-8 encoding if necessary. */
548 read_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
553 if (w
== -1) /* '(A)' edit descriptor */
556 /* Read in w characters, treating comma as not a separator. */
557 dtp
->u
.p
.sf_read_comma
= 0;
559 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
560 read_utf8_char4 (dtp
, p
, length
, w
);
562 read_default_char4 (dtp
, p
, length
, w
);
564 dtp
->u
.p
.sf_read_comma
=
565 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
568 /* eat_leading_spaces()-- Given a character pointer and a width,
569 * ignore the leading spaces. */
572 eat_leading_spaces (int *width
, char *p
)
576 if (*width
== 0 || *p
!= ' ')
588 next_char (st_parameter_dt
*dtp
, char **p
, int *w
)
603 if (dtp
->u
.p
.blank_status
!= BLANK_UNSPECIFIED
)
604 return ' '; /* return a blank to signal a null */
606 /* At this point, the rest of the field has to be trailing blanks */
620 /* read_decimal()-- Read a decimal integer value. The values here are
624 read_decimal (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
626 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
627 GFC_INTEGER_LARGEST v
;
633 p
= read_block_form (dtp
, &w
);
638 p
= eat_leading_spaces (&w
, p
);
641 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
663 maxv
= si_max (length
);
668 /* At this point we have a digit-string */
673 c
= next_char (dtp
, &p
, &w
);
679 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
682 for ( ; w
> 0; p
++, w
--)
683 if (*p
!= ' ') break;
686 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
689 if (c
< '0' || c
> '9')
698 if (value
> maxv
- c
)
708 set_integer (dest
, v
, length
);
712 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
713 "Bad value during integer read");
714 next_record (dtp
, 1);
718 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
719 "Value overflowed during integer read");
720 next_record (dtp
, 1);
725 /* read_radix()-- This function reads values for non-decimal radixes.
726 * The difference here is that we treat the values here as unsigned
727 * values for the purposes of overflow. If minus sign is present and
728 * the top bit is set, the value will be incorrect. */
731 read_radix (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
,
734 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
735 GFC_INTEGER_LARGEST v
;
741 p
= read_block_form (dtp
, &w
);
746 p
= eat_leading_spaces (&w
, p
);
749 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
753 /* Maximum unsigned value, assuming two's complement. */
754 maxv
= 2 * si_max (length
) + 1;
755 maxv_r
= maxv
/ radix
;
776 /* At this point we have a digit-string */
781 c
= next_char (dtp
, &p
, &w
);
786 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
787 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
793 if (c
< '0' || c
> '1')
798 if (c
< '0' || c
> '7')
823 c
= c
- 'a' + '9' + 1;
832 c
= c
- 'A' + '9' + 1;
846 value
= radix
* value
;
848 if (maxv
- c
< value
)
857 set_integer (dest
, v
, length
);
861 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
862 "Bad value during integer read");
863 next_record (dtp
, 1);
867 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
868 "Value overflowed during integer read");
869 next_record (dtp
, 1);
874 /* read_f()-- Read a floating point number with F-style editing, which
875 is what all of the other floating point descriptors behave as. The
876 tricky part is that optional spaces are allowed after an E or D,
877 and the implicit decimal point if a decimal point is not present in
881 read_f (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
886 int w
, seen_dp
, exponent
;
891 int seen_int_digit
; /* Seen a digit before the decimal point? */
892 int seen_dec_digit
; /* Seen a digit after the decimal point? */
902 /* Read in the next block. */
903 p
= read_block_form (dtp
, &w
);
906 p
= eat_leading_spaces (&w
, (char*) p
);
910 /* In this buffer we're going to re-format the number cleanly to be parsed
911 by convert_real in the end; this assures we're using strtod from the
912 C library for parsing and thus probably get the best accuracy possible.
913 This process may add a '+0.0' in front of the number as well as change the
914 exponent because of an implicit decimal point or the like. Thus allocating
915 strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
916 original buffer had should be enough. */
918 if (buf_size
> READF_TMP
)
919 buffer
= xmalloc (buf_size
);
924 if (*p
== '-' || *p
== '+')
932 p
= eat_leading_spaces (&w
, (char*) p
);
936 /* Check for Infinity or NaN. */
937 if (unlikely ((w
>= 3 && (*p
== 'i' || *p
== 'I' || *p
== 'n' || *p
== 'N'))))
942 /* Scan through the buffer keeping track of spaces and parenthesis. We
943 null terminate the string as soon as we see a left paren or if we are
944 BLANK_NULL mode. Leading spaces have already been skipped above,
945 trailing spaces are ignored by converting to '\0'. A space
946 between "NaN" and the optional perenthesis is not permitted. */
953 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
967 if (seen_paren
++ != 1)
981 if (seen_paren
!= 0 && seen_paren
!= 2)
984 if ((strcmp (save
, "inf") == 0) || (strcmp (save
, "infinity") == 0))
989 else if (strcmp (save
, "nan") != 0)
992 convert_infnan (dtp
, dest
, buffer
, length
);
993 if (buf_size
> READF_TMP
)
998 /* Process the mantissa string. */
1004 if (dtp
->u
.p
.current_unit
->decimal_status
!= DECIMAL_COMMA
)
1010 if (!seen_int_digit
)
1017 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1022 else if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
1025 /* TODO: Should we check instead that there are only trailing
1026 blanks here, as is done below for exponents? */
1069 /* No exponent has been seen, so we use the current scale factor. */
1070 exponent
= - dtp
->u
.p
.scale_factor
;
1073 /* At this point the start of an exponent has been found. */
1075 p
= eat_leading_spaces (&w
, (char*) p
);
1076 if (*p
== '-' || *p
== '+')
1084 /* At this point a digit string is required. We calculate the value
1085 of the exponent in order to take account of the scale factor and
1086 the d parameter before explict conversion takes place. */
1090 /* Extension: allow default exponent of 0 when omitted. */
1091 if (dtp
->common
.flags
& IOPARM_DT_DEFAULT_EXP
)
1097 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
1099 while (w
> 0 && isdigit (*p
))
1102 exponent
+= *p
- '0';
1107 /* Only allow trailing blanks. */
1116 else /* BZ or BN status is enabled. */
1122 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1125 assert (dtp
->u
.p
.blank_status
== BLANK_NULL
);
1127 else if (!isdigit (*p
))
1132 exponent
+= *p
- '0';
1140 exponent
*= exponent_sign
;
1143 /* Use the precision specified in the format if no decimal point has been
1146 exponent
-= f
->u
.real
.d
;
1148 /* Output a trailing '0' after decimal point if not yet found. */
1149 if (seen_dp
&& !seen_dec_digit
)
1151 /* Handle input of style "E+NN" by inserting a 0 for the
1153 else if (!seen_int_digit
&& !seen_dec_digit
)
1155 notify_std (&dtp
->common
, GFC_STD_LEGACY
,
1156 "REAL input of style 'E+NN'");
1160 /* Print out the exponent to finish the reformatted number. Maximum 4
1161 digits for the exponent. */
1170 exponent
= - exponent
;
1173 if (exponent
>= 10000)
1176 for (dig
= 3; dig
>= 0; --dig
)
1178 out
[dig
] = (char) ('0' + exponent
% 10);
1185 /* Do the actual conversion. */
1186 convert_real (dtp
, dest
, buffer
, length
);
1187 if (buf_size
> READF_TMP
)
1191 /* The value read is zero. */
1196 *((GFC_REAL_4
*) dest
) = 0.0;
1200 *((GFC_REAL_8
*) dest
) = 0.0;
1203 #ifdef HAVE_GFC_REAL_10
1205 *((GFC_REAL_10
*) dest
) = 0.0;
1209 #ifdef HAVE_GFC_REAL_16
1211 *((GFC_REAL_16
*) dest
) = 0.0;
1216 internal_error (&dtp
->common
, "Unsupported real kind during IO");
1221 if (buf_size
> READF_TMP
)
1223 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
1224 "Bad value during floating point read");
1225 next_record (dtp
, 1);
1230 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1231 * and never look at it. */
1234 read_x (st_parameter_dt
*dtp
, int n
)
1238 if ((dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
|| is_internal_unit (dtp
))
1239 && dtp
->u
.p
.current_unit
->bytes_left
< n
)
1240 n
= dtp
->u
.p
.current_unit
->bytes_left
;
1247 if (is_internal_unit (dtp
))
1249 mem_alloc_r (dtp
->u
.p
.current_unit
->s
, &length
);
1250 if (unlikely (length
< n
))
1255 if (dtp
->u
.p
.sf_seen_eor
)
1261 q
= fbuf_getc (dtp
->u
.p
.current_unit
);
1264 else if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
1265 && (q
== '\n' || q
== '\r'))
1267 /* Unexpected end of line. Set the position. */
1268 dtp
->u
.p
.sf_seen_eor
= 1;
1270 /* If we see an EOR during non-advancing I/O, we need to skip
1271 the rest of the I/O statement. Set the corresponding flag. */
1272 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
1273 dtp
->u
.p
.eor_condition
= 1;
1275 /* If we encounter a CR, it might be a CRLF. */
1276 if (q
== '\r') /* Probably a CRLF */
1278 /* See if there is an LF. */
1279 q2
= fbuf_getc (dtp
->u
.p
.current_unit
);
1281 dtp
->u
.p
.sf_seen_eor
= 2;
1282 else if (q2
!= EOF
) /* Oops, seek back. */
1283 fbuf_seek (dtp
->u
.p
.current_unit
, -1, SEEK_CUR
);
1291 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
1292 dtp
->u
.p
.current_unit
->has_size
)
1293 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) n
;
1294 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
1295 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) n
;