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
= 1;
444 if (dtp
->common
.flags
& IOPARM_DT_HAS_F2003
)
445 dtp
->u
.p
.sf_read_comma
= dtp
->u
.p
.decimal_status
== DECIMAL_COMMA
? 0 : 1;
449 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
450 processing UTF-8 encoding if necessary. */
453 read_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
459 if (wi
== -1) /* '(A)' edit descriptor */
463 /* Read in w characters, treating comma as not a separator. */
464 dtp
->u
.p
.sf_read_comma
= 0;
466 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
467 read_utf8_char4 (dtp
, p
, length
, w
);
469 read_default_char4 (dtp
, p
, length
, w
);
471 dtp
->u
.p
.sf_read_comma
= 1;
472 if (dtp
->common
.flags
& IOPARM_DT_HAS_F2003
)
473 dtp
->u
.p
.sf_read_comma
= dtp
->u
.p
.decimal_status
== DECIMAL_COMMA
? 0 : 1;
476 /* eat_leading_spaces()-- Given a character pointer and a width,
477 * ignore the leading spaces. */
480 eat_leading_spaces (int *width
, char *p
)
484 if (*width
== 0 || *p
!= ' ')
496 next_char (st_parameter_dt
*dtp
, char **p
, int *w
)
511 if (dtp
->u
.p
.blank_status
!= BLANK_UNSPECIFIED
)
512 return ' '; /* return a blank to signal a null */
514 /* At this point, the rest of the field has to be trailing blanks */
528 /* read_decimal()-- Read a decimal integer value. The values here are
532 read_decimal (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
534 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
535 GFC_INTEGER_LARGEST v
;
544 if (read_block_form (dtp
, p
, &wu
) == FAILURE
)
549 p
= eat_leading_spaces (&w
, p
);
552 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
556 maxv
= max_value (length
, 1);
578 /* At this point we have a digit-string */
583 c
= next_char (dtp
, &p
, &w
);
589 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
590 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
593 if (c
< '0' || c
> '9')
596 if (value
> maxv_10
&& compile_options
.range_check
== 1)
602 if (value
> maxv
- c
&& compile_options
.range_check
== 1)
611 set_integer (dest
, v
, length
);
615 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
616 "Bad value during integer read");
617 next_record (dtp
, 1);
621 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
622 "Value overflowed during integer read");
623 next_record (dtp
, 1);
628 /* read_radix()-- This function reads values for non-decimal radixes.
629 * The difference here is that we treat the values here as unsigned
630 * values for the purposes of overflow. If minus sign is present and
631 * the top bit is set, the value will be incorrect. */
634 read_radix (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
,
637 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
638 GFC_INTEGER_LARGEST v
;
647 if (read_block_form (dtp
, p
, &wu
) == FAILURE
)
652 p
= eat_leading_spaces (&w
, p
);
655 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
659 maxv
= max_value (length
, 0);
660 maxv_r
= maxv
/ radix
;
681 /* At this point we have a digit-string */
686 c
= next_char (dtp
, &p
, &w
);
691 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
692 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
698 if (c
< '0' || c
> '1')
703 if (c
< '0' || c
> '7')
728 c
= c
- 'a' + '9' + 1;
737 c
= c
- 'A' + '9' + 1;
751 value
= radix
* value
;
753 if (maxv
- c
< value
)
762 set_integer (dest
, v
, length
);
766 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
767 "Bad value during integer read");
768 next_record (dtp
, 1);
772 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
773 "Value overflowed during integer read");
774 next_record (dtp
, 1);
779 /* read_f()-- Read a floating point number with F-style editing, which
780 is what all of the other floating point descriptors behave as. The
781 tricky part is that optional spaces are allowed after an E or D,
782 and the implicit decimal point if a decimal point is not present in
786 read_f (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
789 int w
, seen_dp
, exponent
;
790 int exponent_sign
, val_sign
;
796 char scratch
[SCRATCH_SIZE
];
804 if (read_block_form (dtp
, p
, &wu
) == FAILURE
)
809 p
= eat_leading_spaces (&w
, p
);
815 if (*p
== '-' || *p
== '+')
824 p
= eat_leading_spaces (&w
, p
);
828 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
829 is required at this point */
831 if (!isdigit (*p
) && *p
!= '.' && *p
!= ',' && *p
!= 'd' && *p
!= 'D'
832 && *p
!= 'e' && *p
!= 'E')
835 /* Remember the position of the first digit. */
839 /* Scan through the string to find the exponent. */
845 if ((dtp
->common
.flags
& IOPARM_DT_HAS_F2003
)
846 && (dtp
->u
.p
.decimal_status
== DECIMAL_COMMA
&& *p
== ','))
895 /* No exponent has been seen, so we use the current scale factor */
896 exponent
= -dtp
->u
.p
.scale_factor
;
900 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
901 "Bad value during floating point read");
902 next_record (dtp
, 1);
905 /* The value read is zero */
910 *((GFC_REAL_4
*) dest
) = 0;
914 *((GFC_REAL_8
*) dest
) = 0;
917 #ifdef HAVE_GFC_REAL_10
919 *((GFC_REAL_10
*) dest
) = 0;
923 #ifdef HAVE_GFC_REAL_16
925 *((GFC_REAL_16
*) dest
) = 0;
930 internal_error (&dtp
->common
, "Unsupported real kind during IO");
934 /* At this point the start of an exponent has been found */
936 while (w
> 0 && *p
== ' ')
957 /* At this point a digit string is required. We calculate the value
958 of the exponent in order to take account of the scale factor and
959 the d parameter before explict conversion takes place. */
968 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
) /* Normal processing of exponent */
970 while (w
> 0 && isdigit (*p
))
972 exponent
= 10 * exponent
+ *p
- '0';
977 /* Only allow trailing blanks */
987 else /* BZ or BN status is enabled */
993 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) *p
= '0';
994 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
1001 else if (!isdigit (*p
))
1004 exponent
= 10 * exponent
+ *p
- '0';
1010 exponent
= exponent
* exponent_sign
;
1013 /* Use the precision specified in the format if no decimal point has been
1016 exponent
-= f
->u
.real
.d
;
1035 i
= ndigits
+ edigits
+ 1;
1039 if (i
< SCRATCH_SIZE
)
1042 buffer
= get_mem (i
);
1044 /* Reformat the string into a temporary buffer. As we're using atof it's
1045 easiest to just leave the decimal point in place. */
1049 for (; ndigits
> 0; ndigits
--)
1053 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) *digits
= '0';
1054 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
1065 sprintf (p
, "%d", exponent
);
1067 /* Do the actual conversion. */
1068 convert_real (dtp
, dest
, buffer
, length
);
1070 if (buffer
!= scratch
)
1076 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1077 * and never look at it. */
1080 read_x (st_parameter_dt
* dtp
, int n
)
1082 if (dtp
->common
.flags
& IOPARM_DT_HAS_F2003
)
1084 if ((dtp
->u
.p
.pad_status
== PAD_NO
|| is_internal_unit (dtp
))
1085 && dtp
->u
.p
.current_unit
->bytes_left
< n
)
1086 n
= dtp
->u
.p
.current_unit
->bytes_left
;
1090 if (is_internal_unit (dtp
) && dtp
->u
.p
.current_unit
->bytes_left
< n
)
1091 n
= dtp
->u
.p
.current_unit
->bytes_left
;
1094 dtp
->u
.p
.sf_read_comma
= 0;
1096 read_sf (dtp
, &n
, 1);
1097 dtp
->u
.p
.sf_read_comma
= 1;
1098 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) n
;