1 /* Copyright (C) 2002-2003 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 ("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 ("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 (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 ("Unsupported real kind during IO");
178 if (errno
!= 0 && errno
!= EINVAL
)
180 generate_error (ERROR_READ_VALUE
,
181 "Range error during floating point read");
189 /* read_l()-- Read a logical value */
192 read_l (fnode
* f
, char *dest
, int length
)
220 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
224 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
228 generate_error (ERROR_READ_VALUE
, "Bad value on logical read");
234 /* read_a()-- Read a character record. This one is pretty easy. */
237 read_a (fnode
* f
, char *p
, int length
)
243 if (w
== -1) /* '(A)' edit descriptor */
246 source
= read_block (&w
);
250 source
+= (w
- length
);
252 m
= (w
> length
) ? length
: w
;
253 memcpy (p
, source
, m
);
257 memset (p
+ m
, ' ', n
);
261 /* eat_leading_spaces()-- Given a character pointer and a width,
262 * ignore the leading spaces. */
265 eat_leading_spaces (int *width
, char *p
)
269 if (*width
== 0 || *p
!= ' ')
281 next_char (char **p
, int *w
)
296 if (g
.blank_status
!= BLANK_UNSPECIFIED
)
297 return ' '; /* return a blank to signal a null */
299 /* At this point, the rest of the field has to be trailing blanks */
313 /* read_decimal()-- Read a decimal integer value. The values here are
317 read_decimal (fnode
* f
, char *dest
, int length
)
319 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
320 GFC_INTEGER_LARGEST v
;
329 p
= eat_leading_spaces (&w
, p
);
332 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
336 maxv
= max_value (length
, 1);
358 /* At this point we have a digit-string */
363 c
= next_char (&p
, &w
);
369 if (g
.blank_status
== BLANK_NULL
) continue;
370 if (g
.blank_status
== BLANK_ZERO
) c
= '0';
373 if (c
< '0' || c
> '9')
382 if (value
> maxv
- c
)
391 set_integer (dest
, v
, length
);
395 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
399 generate_error (ERROR_READ_OVERFLOW
,
400 "Value overflowed during integer read");
405 /* read_radix()-- This function reads values for non-decimal radixes.
406 * The difference here is that we treat the values here as unsigned
407 * values for the purposes of overflow. If minus sign is present and
408 * the top bit is set, the value will be incorrect. */
411 read_radix (fnode
* f
, char *dest
, int length
, int radix
)
413 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
414 GFC_INTEGER_LARGEST v
;
423 p
= eat_leading_spaces (&w
, p
);
426 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
430 maxv
= max_value (length
, 0);
431 maxv_r
= maxv
/ radix
;
452 /* At this point we have a digit-string */
457 c
= next_char (&p
, &w
);
462 if (g
.blank_status
== BLANK_NULL
) continue;
463 if (g
.blank_status
== BLANK_ZERO
) c
= '0';
469 if (c
< '0' || c
> '1')
474 if (c
< '0' || c
> '7')
499 c
= c
- 'a' + '9' + 1;
508 c
= c
- 'A' + '9' + 1;
522 value
= radix
* value
;
524 if (maxv
- c
< value
)
533 set_integer (dest
, v
, length
);
537 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
541 generate_error (ERROR_READ_OVERFLOW
,
542 "Value overflowed during integer read");
547 /* read_f()-- Read a floating point number with F-style editing, which
548 is what all of the other floating point descriptors behave as. The
549 tricky part is that optional spaces are allowed after an E or D,
550 and the implicit decimal point if a decimal point is not present in
554 read_f (fnode
* f
, char *dest
, int length
)
556 int w
, seen_dp
, exponent
;
557 int exponent_sign
, val_sign
;
571 p
= eat_leading_spaces (&w
, p
);
577 if (*p
== '-' || *p
== '+')
586 p
= eat_leading_spaces (&w
, p
);
590 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
591 is required at this point */
593 if (!isdigit (*p
) && *p
!= '.' && *p
!= 'd' && *p
!= 'D'
594 && *p
!= 'e' && *p
!= 'E')
597 /* Remember the position of the first digit. */
601 /* Scan through the string to find the exponent. */
650 /* No exponent has been seen, so we use the current scale factor */
651 exponent
= -g
.scale_factor
;
655 generate_error (ERROR_READ_VALUE
, "Bad value during floating point read");
658 /* The value read is zero */
663 *((GFC_REAL_4
*) dest
) = 0;
667 *((GFC_REAL_8
*) dest
) = 0;
670 #ifdef HAVE_GFC_REAL_10
672 *((GFC_REAL_10
*) dest
) = 0;
676 #ifdef HAVE_GFC_REAL_16
678 *((GFC_REAL_16
*) dest
) = 0;
683 internal_error ("Unsupported real kind during IO");
687 /* At this point the start of an exponent has been found */
689 while (w
> 0 && *p
== ' ')
710 /* At this point a digit string is required. We calculate the value
711 of the exponent in order to take account of the scale factor and
712 the d parameter before explict conversion takes place. */
721 if (g
.blank_status
== BLANK_UNSPECIFIED
) /* Normal processing of exponent */
723 while (w
> 0 && isdigit (*p
))
725 exponent
= 10 * exponent
+ *p
- '0';
730 /* Only allow trailing blanks */
740 else /* BZ or BN status is enabled */
746 if (g
.blank_status
== BLANK_ZERO
) *p
= '0';
747 if (g
.blank_status
== BLANK_NULL
)
754 else if (!isdigit (*p
))
757 exponent
= 10 * exponent
+ *p
- '0';
763 exponent
= exponent
* exponent_sign
;
766 /* Use the precision specified in the format if no decimal point has been
769 exponent
-= f
->u
.real
.d
;
788 i
= ndigits
+ edigits
+ 1;
792 if (i
< SCRATCH_SIZE
)
795 buffer
= get_mem (i
);
797 /* Reformat the string into a temporary buffer. As we're using atof it's
798 easiest to just leave the decimal point in place. */
802 for (; ndigits
> 0; ndigits
--)
806 if (g
.blank_status
== BLANK_ZERO
) *digits
= '0';
807 if (g
.blank_status
== BLANK_NULL
)
818 sprintf (p
, "%d", exponent
);
820 /* Do the actual conversion. */
821 convert_real (dest
, buffer
, length
);
823 if (buffer
!= scratch
)
830 /* read_x()-- Deal with the X/TR descriptor. We just read some data
831 * and never look at it. */
836 if ((current_unit
->flags
.pad
== PAD_NO
|| is_internal_unit ())
837 && current_unit
->bytes_left
< n
)
838 n
= current_unit
->bytes_left
;