1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
37 #include "libgfortran.h"
40 /* 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
;
99 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
103 for (n
= 1; n
< 4 * length
; n
++)
104 value
= (value
<< 2) + 3;
110 value
= signed_flag
? 0x7fffffffffffffff : 0xffffffffffffffff;
113 value
= signed_flag
? 0x7fffffff : 0xffffffff;
116 value
= signed_flag
? 0x7fff : 0xffff;
119 value
= signed_flag
? 0x7f : 0xff;
122 internal_error (NULL
, "Bad integer kind");
129 /* convert_real()-- Convert a character representation of a floating
130 * point number to the machine number. Returns nonzero if there is a
131 * range problem during conversion. TODO: handle not-a-numbers and
135 convert_real (st_parameter_dt
*dtp
, void *dest
, const char *buffer
, int length
)
144 #if defined(HAVE_STRTOF)
145 strtof (buffer
, NULL
);
147 (GFC_REAL_4
) strtod (buffer
, NULL
);
149 memcpy (dest
, (void *) &tmp
, length
);
154 GFC_REAL_8 tmp
= strtod (buffer
, NULL
);
155 memcpy (dest
, (void *) &tmp
, length
);
158 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
161 GFC_REAL_10 tmp
= strtold (buffer
, NULL
);
162 memcpy (dest
, (void *) &tmp
, length
);
166 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
169 GFC_REAL_16 tmp
= strtold (buffer
, NULL
);
170 memcpy (dest
, (void *) &tmp
, length
);
175 internal_error (&dtp
->common
, "Unsupported real kind during IO");
178 if (errno
!= 0 && errno
!= EINVAL
)
180 generate_error (&dtp
->common
, ERROR_READ_VALUE
,
181 "Range error during floating point read");
189 /* read_l()-- Read a logical value */
192 read_l (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
198 p
= read_block (dtp
, &w
);
220 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
224 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
228 generate_error (&dtp
->common
, ERROR_READ_VALUE
,
229 "Bad value on logical read");
235 /* read_a()-- Read a character record. This one is pretty easy. */
238 read_a (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
244 if (w
== -1) /* '(A)' edit descriptor */
247 dtp
->u
.p
.sf_read_comma
= 0;
248 source
= read_block (dtp
, &w
);
249 dtp
->u
.p
.sf_read_comma
= 1;
253 source
+= (w
- length
);
255 m
= (w
> length
) ? length
: w
;
256 memcpy (p
, source
, m
);
260 memset (p
+ m
, ' ', n
);
264 /* eat_leading_spaces()-- Given a character pointer and a width,
265 * ignore the leading spaces. */
268 eat_leading_spaces (int *width
, char *p
)
272 if (*width
== 0 || *p
!= ' ')
284 next_char (st_parameter_dt
*dtp
, char **p
, int *w
)
299 if (dtp
->u
.p
.blank_status
!= BLANK_UNSPECIFIED
)
300 return ' '; /* return a blank to signal a null */
302 /* At this point, the rest of the field has to be trailing blanks */
316 /* read_decimal()-- Read a decimal integer value. The values here are
320 read_decimal (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
322 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
323 GFC_INTEGER_LARGEST v
;
328 p
= read_block (dtp
, &w
);
332 p
= eat_leading_spaces (&w
, p
);
335 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
339 maxv
= max_value (length
, 1);
361 /* At this point we have a digit-string */
366 c
= next_char (dtp
, &p
, &w
);
372 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
373 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
376 if (c
< '0' || c
> '9')
385 if (value
> maxv
- c
)
394 set_integer (dest
, v
, length
);
398 generate_error (&dtp
->common
, ERROR_READ_VALUE
,
399 "Bad value during integer read");
403 generate_error (&dtp
->common
, ERROR_READ_OVERFLOW
,
404 "Value overflowed during integer read");
409 /* read_radix()-- This function reads values for non-decimal radixes.
410 * The difference here is that we treat the values here as unsigned
411 * values for the purposes of overflow. If minus sign is present and
412 * the top bit is set, the value will be incorrect. */
415 read_radix (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
,
418 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
419 GFC_INTEGER_LARGEST v
;
424 p
= read_block (dtp
, &w
);
428 p
= eat_leading_spaces (&w
, p
);
431 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
435 maxv
= max_value (length
, 0);
436 maxv_r
= maxv
/ radix
;
457 /* At this point we have a digit-string */
462 c
= next_char (dtp
, &p
, &w
);
467 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
468 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
474 if (c
< '0' || c
> '1')
479 if (c
< '0' || c
> '7')
504 c
= c
- 'a' + '9' + 1;
513 c
= c
- 'A' + '9' + 1;
527 value
= radix
* value
;
529 if (maxv
- c
< value
)
538 set_integer (dest
, v
, length
);
542 generate_error (&dtp
->common
, ERROR_READ_VALUE
,
543 "Bad value during integer read");
547 generate_error (&dtp
->common
, ERROR_READ_OVERFLOW
,
548 "Value overflowed during integer read");
553 /* read_f()-- Read a floating point number with F-style editing, which
554 is what all of the other floating point descriptors behave as. The
555 tricky part is that optional spaces are allowed after an E or D,
556 and the implicit decimal point if a decimal point is not present in
560 read_f (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
562 int w
, seen_dp
, exponent
;
563 int exponent_sign
, val_sign
;
569 char scratch
[SCRATCH_SIZE
];
574 p
= read_block (dtp
, &w
);
578 p
= eat_leading_spaces (&w
, p
);
584 if (*p
== '-' || *p
== '+')
593 p
= eat_leading_spaces (&w
, p
);
597 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
598 is required at this point */
600 if (!isdigit (*p
) && *p
!= '.' && *p
!= 'd' && *p
!= 'D'
601 && *p
!= 'e' && *p
!= 'E')
604 /* Remember the position of the first digit. */
608 /* Scan through the string to find the exponent. */
657 /* No exponent has been seen, so we use the current scale factor */
658 exponent
= -dtp
->u
.p
.scale_factor
;
662 generate_error (&dtp
->common
, ERROR_READ_VALUE
,
663 "Bad value during floating point read");
666 /* The value read is zero */
671 *((GFC_REAL_4
*) dest
) = 0;
675 *((GFC_REAL_8
*) dest
) = 0;
678 #ifdef HAVE_GFC_REAL_10
680 *((GFC_REAL_10
*) dest
) = 0;
684 #ifdef HAVE_GFC_REAL_16
686 *((GFC_REAL_16
*) dest
) = 0;
691 internal_error (&dtp
->common
, "Unsupported real kind during IO");
695 /* At this point the start of an exponent has been found */
697 while (w
> 0 && *p
== ' ')
718 /* At this point a digit string is required. We calculate the value
719 of the exponent in order to take account of the scale factor and
720 the d parameter before explict conversion takes place. */
729 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
) /* Normal processing of exponent */
731 while (w
> 0 && isdigit (*p
))
733 exponent
= 10 * exponent
+ *p
- '0';
738 /* Only allow trailing blanks */
748 else /* BZ or BN status is enabled */
754 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) *p
= '0';
755 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
762 else if (!isdigit (*p
))
765 exponent
= 10 * exponent
+ *p
- '0';
771 exponent
= exponent
* exponent_sign
;
774 /* Use the precision specified in the format if no decimal point has been
777 exponent
-= f
->u
.real
.d
;
796 i
= ndigits
+ edigits
+ 1;
800 if (i
< SCRATCH_SIZE
)
803 buffer
= get_mem (i
);
805 /* Reformat the string into a temporary buffer. As we're using atof it's
806 easiest to just leave the decimal point in place. */
810 for (; ndigits
> 0; ndigits
--)
814 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) *digits
= '0';
815 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
826 sprintf (p
, "%d", exponent
);
828 /* Do the actual conversion. */
829 convert_real (dtp
, dest
, buffer
, length
);
831 if (buffer
!= scratch
)
838 /* read_x()-- Deal with the X/TR descriptor. We just read some data
839 * and never look at it. */
842 read_x (st_parameter_dt
*dtp
, int n
)
844 if ((dtp
->u
.p
.current_unit
->flags
.pad
== PAD_NO
|| is_internal_unit (dtp
))
845 && dtp
->u
.p
.current_unit
->bytes_left
< n
)
846 n
= dtp
->u
.p
.current_unit
->bytes_left
;
848 dtp
->u
.p
.sf_read_comma
= 0;
850 read_block (dtp
, &n
);
851 dtp
->u
.p
.sf_read_comma
= 1;