1 /* Copyright (C) 2002-2024 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/>. */
34 typedef unsigned char uchar
;
36 /* read.c -- Deal with formatted reads */
39 /* set_integer()-- All of the integer assignments come here to
40 actually place the value into memory. */
43 set_integer (void *dest
, GFC_INTEGER_LARGEST value
, int length
)
45 NOTE ("set_integer: %lld %p", (long long int) value
, dest
);
48 #ifdef HAVE_GFC_INTEGER_16
49 #ifdef HAVE_GFC_REAL_17
52 GFC_INTEGER_16 tmp
= value
;
53 memcpy (dest
, (void *) &tmp
, 16);
57 /* length=10 comes about for kind=10 real/complex BOZ, see PR41711. */
61 GFC_INTEGER_16 tmp
= value
;
62 memcpy (dest
, (void *) &tmp
, length
);
68 GFC_INTEGER_8 tmp
= value
;
69 memcpy (dest
, (void *) &tmp
, length
);
74 GFC_INTEGER_4 tmp
= value
;
75 memcpy (dest
, (void *) &tmp
, length
);
80 GFC_INTEGER_2 tmp
= value
;
81 memcpy (dest
, (void *) &tmp
, length
);
86 GFC_INTEGER_1 tmp
= value
;
87 memcpy (dest
, (void *) &tmp
, length
);
91 internal_error (NULL
, "Bad integer kind");
95 /* set_integer()-- All of the integer assignments come here to
96 actually place the value into memory. */
99 set_unsigned (void *dest
, GFC_UINTEGER_LARGEST value
, int length
)
101 NOTE ("set_integer: %lld %p", (long long int) value
, dest
);
104 #ifdef HAVE_GFC_UINTEGER_16
105 #ifdef HAVE_GFC_REAL_17
108 GFC_UINTEGER_16 tmp
= value
;
109 memcpy (dest
, (void *) &tmp
, 16);
113 /* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */
117 GFC_UINTEGER_16 tmp
= value
;
118 memcpy (dest
, (void *) &tmp
, length
);
124 GFC_UINTEGER_8 tmp
= value
;
125 memcpy (dest
, (void *) &tmp
, length
);
130 GFC_UINTEGER_4 tmp
= value
;
131 memcpy (dest
, (void *) &tmp
, length
);
136 GFC_UINTEGER_2 tmp
= value
;
137 memcpy (dest
, (void *) &tmp
, length
);
142 GFC_UINTEGER_1 tmp
= value
;
143 memcpy (dest
, (void *) &tmp
, length
);
147 internal_error (NULL
, "Bad integer kind");
152 /* Max signed value of size give by length argument. */
157 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
158 GFC_UINTEGER_LARGEST value
;
163 #if defined HAVE_GFC_REAL_17
166 for (int n
= 1; n
< 4 * 16; n
++)
167 value
= (value
<< 2) + 3;
170 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
174 for (int n
= 1; n
< 4 * length
; n
++)
175 value
= (value
<< 2) + 3;
179 return GFC_INTEGER_8_HUGE
;
181 return GFC_INTEGER_4_HUGE
;
183 return GFC_INTEGER_2_HUGE
;
185 return GFC_INTEGER_1_HUGE
;
187 internal_error (NULL
, "Bad integer kind");
196 #ifdef HAVE_GFC_UINTEGER_16
199 return GFC_UINTEGER_16_HUGE
;
202 return GFC_UINTEGER_8_HUGE
;
204 return GFC_UINTEGER_4_HUGE
;
206 return GFC_UINTEGER_2_HUGE
;
208 return GFC_UINTEGER_1_HUGE
;
210 internal_error (NULL
, "Bad unsigned kind");
214 /* convert_real()-- Convert a character representation of a floating
215 point number to the machine number. Returns nonzero if there is an
216 invalid input. Note: many architectures (e.g. IA-64, HP-PA)
217 require that the storage pointed to by the dest argument is
218 properly aligned for the type in question. */
221 convert_real (st_parameter_dt
*dtp
, void *dest
, const char *buffer
, int length
)
224 int round_mode
, old_round_mode
;
226 switch (dtp
->u
.p
.current_unit
->round_status
)
228 case ROUND_COMPATIBLE
:
229 /* FIXME: As NEAREST but round away from zero for a tie. */
230 case ROUND_UNSPECIFIED
:
231 /* Should not occur. */
232 case ROUND_PROCDEFINED
:
233 round_mode
= ROUND_NEAREST
;
236 round_mode
= dtp
->u
.p
.current_unit
->round_status
;
240 old_round_mode
= get_fpu_rounding_mode();
241 set_fpu_rounding_mode (round_mode
);
246 *((GFC_REAL_4
*) dest
) =
247 #if defined(HAVE_STRTOF)
248 gfc_strtof (buffer
, &endptr
);
250 (GFC_REAL_4
) gfc_strtod (buffer
, &endptr
);
255 *((GFC_REAL_8
*) dest
) = gfc_strtod (buffer
, &endptr
);
258 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
260 *((GFC_REAL_10
*) dest
) = gfc_strtold (buffer
, &endptr
);
264 #if defined(HAVE_GFC_REAL_16)
265 # if defined(GFC_REAL_16_IS_FLOAT128)
267 # if defined(GFC_REAL_16_USE_IEC_60559)
268 *((GFC_REAL_16
*) dest
) = strtof128 (buffer
, &endptr
);
270 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, &endptr
);
273 # elif defined(HAVE_STRTOLD)
275 *((GFC_REAL_16
*) dest
) = gfc_strtold (buffer
, &endptr
);
280 #if defined(HAVE_GFC_REAL_17)
282 # if defined(POWER_IEEE128)
283 *((GFC_REAL_17
*) dest
) = __strtoieee128 (buffer
, &endptr
);
284 # elif defined(GFC_REAL_17_USE_IEC_60559)
285 *((GFC_REAL_17
*) dest
) = strtof128 (buffer
, &endptr
);
287 *((GFC_REAL_17
*) dest
) = __qmath_(strtoflt128
) (buffer
, &endptr
);
293 internal_error (&dtp
->common
, "Unsupported real kind during IO");
296 set_fpu_rounding_mode (old_round_mode
);
298 if (buffer
== endptr
)
300 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
301 "Error during floating point read");
302 next_record (dtp
, 1);
309 /* convert_infnan()-- Convert character INF/NAN representation to the
310 machine number. Note: many architectures (e.g. IA-64, HP-PA) require
311 that the storage pointed to by the dest argument is properly aligned
312 for the type in question. */
315 convert_infnan (st_parameter_dt
*dtp
, void *dest
, const char *buffer
,
318 const char *s
= buffer
;
319 int is_inf
, plus
= 1;
335 *((GFC_REAL_4
*) dest
) = plus
? __builtin_inff () : -__builtin_inff ();
337 *((GFC_REAL_4
*) dest
) = plus
? __builtin_nanf ("") : -__builtin_nanf ("");
342 *((GFC_REAL_8
*) dest
) = plus
? __builtin_inf () : -__builtin_inf ();
344 *((GFC_REAL_8
*) dest
) = plus
? __builtin_nan ("") : -__builtin_nan ("");
347 #if defined(HAVE_GFC_REAL_10)
350 *((GFC_REAL_10
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
352 *((GFC_REAL_10
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
356 #if defined(HAVE_GFC_REAL_16)
357 # if defined(GFC_REAL_16_IS_FLOAT128)
359 # if defined(GFC_REAL_16_USE_IEC_60559)
361 *((GFC_REAL_16
*) dest
) = plus
? __builtin_inff128 () : -__builtin_inff128 ();
363 *((GFC_REAL_16
*) dest
) = plus
? __builtin_nanf128 ("") : -__builtin_nanf128 ("");
365 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, NULL
);
371 *((GFC_REAL_16
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
373 *((GFC_REAL_16
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
378 #if defined(HAVE_GFC_REAL_17)
381 *((GFC_REAL_17
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
383 *((GFC_REAL_17
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
388 internal_error (&dtp
->common
, "Unsupported real kind during IO");
395 /* read_l()-- Read a logical value */
398 read_l (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
405 p
= read_block_form (dtp
, &w
);
428 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
432 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
436 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
437 "Bad value on logical read");
438 next_record (dtp
, 1);
445 read_utf8 (st_parameter_dt
*dtp
, size_t *nbytes
)
447 static const uchar masks
[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
448 static const uchar patns
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
455 s
= read_block_form (dtp
, nbytes
);
459 /* If this is a short read, just return. */
467 /* The number of leading 1-bits in the first byte indicates how many
469 for (nb
= 2; nb
< 7; nb
++)
470 if ((c
& ~masks
[nb
-1]) == patns
[nb
-1])
475 c
= (c
& masks
[nb
-1]);
478 s
= read_block_form (dtp
, &nread
);
481 /* Decode the bytes read. */
482 for (size_t i
= 1; i
< nb
; i
++)
484 gfc_char4_t n
= *s
++;
486 if ((n
& 0xC0) != 0x80)
489 c
= ((c
<< 6) + (n
& 0x3F));
492 /* Make sure the shortest possible encoding was used. */
493 if (c
<= 0x7F && nb
> 1) goto invalid
;
494 if (c
<= 0x7FF && nb
> 2) goto invalid
;
495 if (c
<= 0xFFFF && nb
> 3) goto invalid
;
496 if (c
<= 0x1FFFFF && nb
> 4) goto invalid
;
497 if (c
<= 0x3FFFFFF && nb
> 5) goto invalid
;
499 /* Make sure the character is valid. */
500 if (c
> 0x7FFFFFFF || (c
>= 0xD800 && c
<= 0xDFFF))
506 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, "Invalid UTF-8 encoding");
507 return (gfc_char4_t
) '?';
512 read_utf8_char1 (st_parameter_dt
*dtp
, char *p
, size_t len
, size_t width
)
518 len
= (width
< len
) ? len
: width
;
522 /* Proceed with decoding one character at a time. */
523 for (j
= 0; j
< len
; j
++, dest
++)
525 c
= read_utf8 (dtp
, &nbytes
);
527 /* Check for a short read and if so, break out. */
531 *dest
= c
> 255 ? '?' : (uchar
) c
;
534 /* If there was a short read, pad the remaining characters. */
535 for (size_t i
= j
; i
< len
; i
++)
541 read_default_char1 (st_parameter_dt
*dtp
, char *p
, size_t len
, size_t width
)
546 s
= read_block_form (dtp
, &width
);
553 m
= (width
> len
) ? len
: width
;
557 memset (p
+ m
, ' ', len
- width
);
562 read_utf8_char4 (st_parameter_dt
*dtp
, void *p
, size_t len
, size_t width
)
567 len
= (width
< len
) ? len
: width
;
569 dest
= (gfc_char4_t
*) p
;
571 /* Proceed with decoding one character at a time. */
572 for (j
= 0; j
< len
; j
++, dest
++)
574 *dest
= read_utf8 (dtp
, &nbytes
);
576 /* Check for a short read and if so, break out. */
581 /* If there was a short read, pad the remaining characters. */
582 for (size_t i
= j
; i
< len
; i
++)
583 *dest
++ = (gfc_char4_t
) ' ';
589 read_default_char4 (st_parameter_dt
*dtp
, char *p
, size_t len
, size_t width
)
594 if (is_char4_unit(dtp
))
598 s4
= (gfc_char4_t
*) read_block_form4 (dtp
, &width
);
605 m
= (width
> len
) ? len
: width
;
607 dest
= (gfc_char4_t
*) p
;
609 for (n
= 0; n
< m
; n
++)
614 for (n
= 0; n
< len
- width
; n
++)
615 *dest
++ = (gfc_char4_t
) ' ';
622 s
= read_block_form (dtp
, &width
);
629 m
= (width
> len
) ? len
: width
;
631 dest
= (gfc_char4_t
*) p
;
633 for (n
= 0; n
< m
; n
++, dest
++, s
++)
634 *dest
= (unsigned char ) *s
;
638 for (n
= 0; n
< len
- width
; n
++, dest
++)
639 *dest
= (unsigned char) ' ';
645 /* read_a()-- Read a character record into a KIND=1 character destination,
646 processing UTF-8 encoding if necessary. */
649 read_a (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, size_t length
)
653 if (f
->u
.w
== -1) /* '(A)' edit descriptor */
658 /* Read in w characters, treating comma as not a separator. */
659 dtp
->u
.p
.sf_read_comma
= 0;
661 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
662 read_utf8_char1 (dtp
, p
, length
, w
);
664 read_default_char1 (dtp
, p
, length
, w
);
666 dtp
->u
.p
.sf_read_comma
=
667 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
671 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
672 processing UTF-8 encoding if necessary. */
675 read_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, size_t length
)
679 if (f
->u
.w
== -1) /* '(A)' edit descriptor */
684 /* Read in w characters, treating comma as not a separator. */
685 dtp
->u
.p
.sf_read_comma
= 0;
687 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
688 read_utf8_char4 (dtp
, p
, length
, w
);
690 read_default_char4 (dtp
, p
, length
, w
);
692 dtp
->u
.p
.sf_read_comma
=
693 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
696 /* eat_leading_spaces()-- Given a character pointer and a width,
697 ignore the leading spaces. */
700 eat_leading_spaces (size_t *width
, char *p
)
704 if (*width
== 0 || *p
!= ' ')
716 next_char (st_parameter_dt
*dtp
, char **p
, size_t *w
)
731 if (dtp
->u
.p
.blank_status
!= BLANK_UNSPECIFIED
)
732 return ' '; /* return a blank to signal a null */
734 /* At this point, the rest of the field has to be trailing blanks */
748 /* read_decimal()-- Read a decimal integer value. The values here are
752 read_decimal (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
754 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
755 GFC_INTEGER_LARGEST v
;
762 /* This is a legacy extension, and the frontend will only allow such cases
763 * through when -fdec-format-defaults is passed.
765 if (w
== (size_t) DEFAULT_WIDTH
)
766 w
= default_width_for_integer (length
);
768 p
= read_block_form (dtp
, &w
);
773 p
= eat_leading_spaces (&w
, p
);
776 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
798 maxv
= si_max (length
);
803 /* At this point we have a digit-string */
808 c
= next_char (dtp
, &p
, &w
);
814 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
817 for ( ; w
> 0; p
++, w
--)
818 if (*p
!= ' ') break;
821 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
824 if (c
< '0' || c
> '9')
833 if (value
> maxv
- c
)
843 set_integer (dest
, v
, length
);
847 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
848 "Bad value during integer read");
849 next_record (dtp
, 1);
853 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
854 "Value overflowed during integer read");
855 next_record (dtp
, 1);
859 /* read_decimal_unsigned() - almost the same as above. Checks for sign
860 and overflow are performed with -pedantic. */
863 read_decimal_unsigned (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
,
866 GFC_UINTEGER_LARGEST value
, old_value
;
873 /* This is a legacy extension, and the frontend will only allow such cases
874 * through when -fdec-format-defaults is passed.
876 if (w
== (size_t) DEFAULT_WIDTH
)
877 w
= default_width_for_integer (length
);
879 p
= read_block_form (dtp
, &w
);
884 p
= eat_leading_spaces (&w
, p
);
887 set_unsigned (dest
, (GFC_UINTEGER_LARGEST
) 0, length
);
896 if (compile_options
.pedantic
)
913 /* At this point we have a digit-string. */
918 c
= next_char (dtp
, &p
, &w
);
924 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
927 for ( ; w
> 0; p
++, w
--)
928 if (*p
!= ' ') break;
931 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
934 if (c
< '0' || c
> '9')
939 value
= 10 * value
+ c
;
940 if (compile_options
.pedantic
&& value
< old_value
)
947 if (compile_options
.pedantic
&& value
> us_max (length
))
950 set_unsigned (dest
, value
, length
);
954 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
955 "Bad value during unsigned integer read");
956 next_record (dtp
, 1);
960 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
961 "Negative sign for unsigned integer read");
962 next_record (dtp
, 1);
966 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
967 "Value overflowed during unsigned integer read");
968 next_record (dtp
, 1);
973 /* read_radix()-- This function reads values for non-decimal radixes.
974 The difference here is that we treat the values here as unsigned
975 values for the purposes of overflow. If minus sign is present and
976 the top bit is set, the value will be incorrect. */
979 read_radix (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
,
982 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
983 GFC_INTEGER_LARGEST v
;
990 p
= read_block_form (dtp
, &w
);
995 p
= eat_leading_spaces (&w
, p
);
998 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
1002 /* Maximum unsigned value, assuming two's complement. */
1003 maxv
= 2 * si_max (length
) + 1;
1004 maxv_r
= maxv
/ radix
;
1025 /* At this point we have a digit-string */
1030 c
= next_char (dtp
, &p
, &w
);
1035 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
1036 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
1042 if (c
< '0' || c
> '1')
1047 if (c
< '0' || c
> '7')
1072 c
= c
- 'a' + '9' + 1;
1081 c
= c
- 'A' + '9' + 1;
1095 value
= radix
* value
;
1097 if (maxv
- c
< value
)
1106 set_integer (dest
, v
, length
);
1110 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
1111 "Bad value during integer read");
1112 next_record (dtp
, 1);
1116 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
1117 "Value overflowed during integer read");
1118 next_record (dtp
, 1);
1123 /* read_f()-- Read a floating point number with F-style editing, which
1124 is what all of the other floating point descriptors behave as. The
1125 tricky part is that optional spaces are allowed after an E or D,
1126 and the implicit decimal point if a decimal point is not present in
1130 read_f (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
1132 #define READF_TMP 50
1133 char tmp
[READF_TMP
];
1134 size_t buf_size
= 0;
1136 int seen_dp
, exponent
;
1141 int seen_int_digit
; /* Seen a digit before the decimal point? */
1142 int seen_dec_digit
; /* Seen a digit after the decimal point? */
1152 /* Read in the next block. */
1153 p
= read_block_form (dtp
, &w
);
1156 p
= eat_leading_spaces (&w
, (char*) p
);
1160 /* In this buffer we're going to re-format the number cleanly to be parsed
1161 by convert_real in the end; this assures we're using strtod from the
1162 C library for parsing and thus probably get the best accuracy possible.
1163 This process may add a '+0.0' in front of the number as well as change the
1164 exponent because of an implicit decimal point or the like. Thus allocating
1165 strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
1166 original buffer had should be enough. */
1168 if (buf_size
> READF_TMP
)
1169 buffer
= xmalloc (buf_size
);
1174 if (*p
== '-' || *p
== '+')
1182 p
= eat_leading_spaces (&w
, (char*) p
);
1186 /* Check for Infinity or NaN. */
1187 if (unlikely ((w
>= 3 && (*p
== 'i' || *p
== 'I' || *p
== 'n' || *p
== 'N'))))
1192 /* Scan through the buffer keeping track of spaces and parenthesis. We
1193 null terminate the string as soon as we see a left paren or if we are
1194 BLANK_NULL mode. Leading spaces have already been skipped above,
1195 trailing spaces are ignored by converting to '\0'. A space
1196 between "NaN" and the optional perenthesis is not permitted. */
1199 *out
= safe_tolower (*p
);
1203 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1209 if (seen_paren
== 1)
1217 if (seen_paren
++ != 1)
1221 if (!safe_isalnum (*out
))
1231 if (seen_paren
!= 0 && seen_paren
!= 2)
1234 if ((strcmp (save
, "inf") == 0) || (strcmp (save
, "infinity") == 0))
1239 else if (strcmp (save
, "nan") != 0)
1242 convert_infnan (dtp
, dest
, buffer
, length
);
1243 if (buf_size
> READF_TMP
)
1248 /* Process the mantissa string. */
1254 if (dtp
->u
.p
.current_unit
->decimal_status
!= DECIMAL_COMMA
)
1258 if (!seen_int_digit
)
1265 if (dtp
->u
.p
.current_unit
->decimal_status
!= DECIMAL_POINT
)
1269 if (!seen_int_digit
)
1276 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1281 else if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
1284 /* TODO: Should we check instead that there are only trailing
1285 blanks here, as is done below for exponents? */
1328 /* No exponent has been seen, so we use the current scale factor. */
1329 exponent
= - dtp
->u
.p
.scale_factor
;
1332 /* At this point the start of an exponent has been found. */
1334 p
= eat_leading_spaces (&w
, (char*) p
);
1335 if (*p
== '-' || *p
== '+')
1343 /* At this point a digit string is required. We calculate the value
1344 of the exponent in order to take account of the scale factor and
1345 the d parameter before explict conversion takes place. */
1349 /* Extension: allow default exponent of 0 when omitted. */
1350 if (dtp
->common
.flags
& IOPARM_DT_DEC_EXT
)
1356 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
1358 while (w
> 0 && safe_isdigit (*p
))
1361 exponent
+= *p
- '0';
1366 /* Only allow trailing blanks. */
1375 else /* BZ or BN status is enabled. */
1381 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1384 assert (dtp
->u
.p
.blank_status
== BLANK_NULL
);
1386 else if (!safe_isdigit (*p
))
1391 exponent
+= *p
- '0';
1399 exponent
*= exponent_sign
;
1402 /* Use the precision specified in the format if no decimal point has been
1405 exponent
-= f
->u
.real
.d
;
1407 /* Output a trailing '0' after decimal point if not yet found. */
1408 if (seen_dp
&& !seen_dec_digit
)
1410 /* Handle input of style "E+NN" by inserting a 0 for the
1412 else if (!seen_int_digit
&& !seen_dec_digit
)
1414 notify_std (&dtp
->common
, GFC_STD_LEGACY
,
1415 "REAL input of style 'E+NN'");
1419 /* Print out the exponent to finish the reformatted number. Maximum 4
1420 digits for the exponent. */
1429 exponent
= - exponent
;
1432 if (exponent
>= 10000)
1435 for (dig
= 3; dig
>= 0; --dig
)
1437 out
[dig
] = (char) ('0' + exponent
% 10);
1444 /* Do the actual conversion. */
1445 convert_real (dtp
, dest
, buffer
, length
);
1446 if (buf_size
> READF_TMP
)
1450 /* The value read is zero. */
1455 *((GFC_REAL_4
*) dest
) = 0.0;
1459 *((GFC_REAL_8
*) dest
) = 0.0;
1462 #ifdef HAVE_GFC_REAL_10
1464 *((GFC_REAL_10
*) dest
) = 0.0;
1468 #ifdef HAVE_GFC_REAL_16
1470 *((GFC_REAL_16
*) dest
) = 0.0;
1474 #ifdef HAVE_GFC_REAL_17
1476 *((GFC_REAL_17
*) dest
) = 0.0;
1481 internal_error (&dtp
->common
, "Unsupported real kind during IO");
1486 if (buf_size
> READF_TMP
)
1488 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
1489 "Bad value during floating point read");
1490 next_record (dtp
, 1);
1495 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1496 and never look at it. */
1499 read_x (st_parameter_dt
*dtp
, size_t n
)
1504 if ((dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
|| is_internal_unit (dtp
))
1505 && dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) n
)
1506 n
= dtp
->u
.p
.current_unit
->bytes_left
;
1511 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
1516 /* Proceed with decoding one character at a time. */
1517 for (j
= 0; j
< n
; j
++)
1519 c
= read_utf8 (dtp
, &nbytes
);
1521 /* Check for a short read and if so, break out. */
1522 if (nbytes
== 0 || c
== (gfc_char4_t
)0)
1530 if (is_internal_unit (dtp
))
1532 mem_alloc_r (dtp
->u
.p
.current_unit
->s
, &length
);
1533 if (unlikely (length
< n
))
1538 if (dtp
->u
.p
.sf_seen_eor
)
1544 q
= fbuf_getc (dtp
->u
.p
.current_unit
);
1547 else if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
1548 && (q
== '\n' || q
== '\r'))
1550 /* Unexpected end of line. Set the position. */
1551 dtp
->u
.p
.sf_seen_eor
= 1;
1553 /* If we see an EOR during non-advancing I/O, we need to skip
1554 the rest of the I/O statement. Set the corresponding flag. */
1555 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
1556 dtp
->u
.p
.eor_condition
= 1;
1558 /* If we encounter a CR, it might be a CRLF. */
1559 if (q
== '\r') /* Probably a CRLF */
1561 /* See if there is an LF. */
1562 q2
= fbuf_getc (dtp
->u
.p
.current_unit
);
1564 dtp
->u
.p
.sf_seen_eor
= 2;
1565 else if (q2
!= EOF
) /* Oops, seek back. */
1566 fbuf_seek (dtp
->u
.p
.current_unit
, -1, SEEK_CUR
);
1574 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
1575 dtp
->u
.p
.current_unit
->has_size
)
1576 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) n
;
1577 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
1578 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) n
;