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
52 *((GFC_INTEGER_16
*) dest
) = value
;
56 *((GFC_INTEGER_8
*) dest
) = value
;
59 *((GFC_INTEGER_4
*) dest
) = value
;
62 *((GFC_INTEGER_2
*) dest
) = value
;
65 *((GFC_INTEGER_1
*) dest
) = value
;
68 internal_error ("Bad integer kind");
73 /* max_value()-- Given a length (kind), return the maximum signed or
77 max_value (int length
, int signed_flag
)
79 GFC_UINTEGER_LARGEST value
;
84 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
88 for (n
= 1; n
< 4 * length
; n
++)
89 value
= (value
<< 2) + 3;
95 value
= signed_flag
? 0x7fffffffffffffff : 0xffffffffffffffff;
98 value
= signed_flag
? 0x7fffffff : 0xffffffff;
101 value
= signed_flag
? 0x7fff : 0xffff;
104 value
= signed_flag
? 0x7f : 0xff;
107 internal_error ("Bad integer kind");
114 /* convert_real()-- Convert a character representation of a floating
115 * point number to the machine number. Returns nonzero if there is a
116 * range problem during conversion. TODO: handle not-a-numbers and
120 convert_real (void *dest
, const char *buffer
, int length
)
127 *((GFC_REAL_4
*) dest
) =
128 #if defined(HAVE_STRTOF)
129 strtof (buffer
, NULL
);
131 (GFC_REAL_4
) strtod (buffer
, NULL
);
135 *((GFC_REAL_8
*) dest
) = strtod (buffer
, NULL
);
137 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
139 *((GFC_REAL_10
*) dest
) = strtold (buffer
, NULL
);
142 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
144 *((GFC_REAL_16
*) dest
) = strtold (buffer
, NULL
);
148 internal_error ("Unsupported real kind during IO");
151 if (errno
!= 0 && errno
!= EINVAL
)
153 generate_error (ERROR_READ_VALUE
,
154 "Range error during floating point read");
162 /* read_l()-- Read a logical value */
165 read_l (fnode
* f
, char *dest
, int length
)
193 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
197 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
201 generate_error (ERROR_READ_VALUE
, "Bad value on logical read");
207 /* read_a()-- Read a character record. This one is pretty easy. */
210 read_a (fnode
* f
, char *p
, int length
)
216 if (w
== -1) /* '(A)' edit descriptor */
219 source
= read_block (&w
);
223 source
+= (w
- length
);
225 m
= (w
> length
) ? length
: w
;
226 memcpy (p
, source
, m
);
230 memset (p
+ m
, ' ', n
);
234 /* eat_leading_spaces()-- Given a character pointer and a width,
235 * ignore the leading spaces. */
238 eat_leading_spaces (int *width
, char *p
)
242 if (*width
== 0 || *p
!= ' ')
254 next_char (char **p
, int *w
)
269 if (g
.blank_status
!= BLANK_UNSPECIFIED
)
270 return ' '; /* return a blank to signal a null */
272 /* At this point, the rest of the field has to be trailing blanks */
286 /* read_decimal()-- Read a decimal integer value. The values here are
290 read_decimal (fnode
* f
, char *dest
, int length
)
292 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
293 GFC_INTEGER_LARGEST v
;
302 p
= eat_leading_spaces (&w
, p
);
305 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
309 maxv
= max_value (length
, 1);
331 /* At this point we have a digit-string */
336 c
= next_char (&p
, &w
);
342 if (g
.blank_status
== BLANK_NULL
) continue;
343 if (g
.blank_status
== BLANK_ZERO
) c
= '0';
346 if (c
< '0' || c
> '9')
355 if (value
> maxv
- c
)
364 set_integer (dest
, v
, length
);
368 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
372 generate_error (ERROR_READ_OVERFLOW
,
373 "Value overflowed during integer read");
378 /* read_radix()-- This function reads values for non-decimal radixes.
379 * The difference here is that we treat the values here as unsigned
380 * values for the purposes of overflow. If minus sign is present and
381 * the top bit is set, the value will be incorrect. */
384 read_radix (fnode
* f
, char *dest
, int length
, int radix
)
386 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
387 GFC_INTEGER_LARGEST v
;
396 p
= eat_leading_spaces (&w
, p
);
399 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
403 maxv
= max_value (length
, 0);
404 maxv_r
= maxv
/ radix
;
425 /* At this point we have a digit-string */
430 c
= next_char (&p
, &w
);
435 if (g
.blank_status
== BLANK_NULL
) continue;
436 if (g
.blank_status
== BLANK_ZERO
) c
= '0';
442 if (c
< '0' || c
> '1')
447 if (c
< '0' || c
> '7')
472 c
= c
- 'a' + '9' + 1;
481 c
= c
- 'A' + '9' + 1;
495 value
= radix
* value
;
497 if (maxv
- c
< value
)
506 set_integer (dest
, v
, length
);
510 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
514 generate_error (ERROR_READ_OVERFLOW
,
515 "Value overflowed during integer read");
520 /* read_f()-- Read a floating point number with F-style editing, which
521 is what all of the other floating point descriptors behave as. The
522 tricky part is that optional spaces are allowed after an E or D,
523 and the implicit decimal point if a decimal point is not present in
527 read_f (fnode
* f
, char *dest
, int length
)
529 int w
, seen_dp
, exponent
;
530 int exponent_sign
, val_sign
;
544 p
= eat_leading_spaces (&w
, p
);
550 if (*p
== '-' || *p
== '+')
559 p
= eat_leading_spaces (&w
, p
);
563 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
564 is required at this point */
566 if (!isdigit (*p
) && *p
!= '.' && *p
!= 'd' && *p
!= 'D'
567 && *p
!= 'e' && *p
!= 'E')
570 /* Remember the position of the first digit. */
574 /* Scan through the string to find the exponent. */
623 /* No exponent has been seen, so we use the current scale factor */
624 exponent
= -g
.scale_factor
;
628 generate_error (ERROR_READ_VALUE
, "Bad value during floating point read");
631 /* The value read is zero */
636 *((GFC_REAL_4
*) dest
) = 0;
640 *((GFC_REAL_8
*) dest
) = 0;
643 #ifdef HAVE_GFC_REAL_10
645 *((GFC_REAL_10
*) dest
) = 0;
649 #ifdef HAVE_GFC_REAL_16
651 *((GFC_REAL_16
*) dest
) = 0;
656 internal_error ("Unsupported real kind during IO");
660 /* At this point the start of an exponent has been found */
662 while (w
> 0 && *p
== ' ')
683 /* At this point a digit string is required. We calculate the value
684 of the exponent in order to take account of the scale factor and
685 the d parameter before explict conversion takes place. */
694 if (g
.blank_status
== BLANK_UNSPECIFIED
) /* Normal processing of exponent */
696 while (w
> 0 && isdigit (*p
))
698 exponent
= 10 * exponent
+ *p
- '0';
703 /* Only allow trailing blanks */
713 else /* BZ or BN status is enabled */
719 if (g
.blank_status
== BLANK_ZERO
) *p
= '0';
720 if (g
.blank_status
== BLANK_NULL
)
727 else if (!isdigit (*p
))
730 exponent
= 10 * exponent
+ *p
- '0';
736 exponent
= exponent
* exponent_sign
;
739 /* Use the precision specified in the format if no decimal point has been
742 exponent
-= f
->u
.real
.d
;
761 i
= ndigits
+ edigits
+ 1;
765 if (i
< SCRATCH_SIZE
)
768 buffer
= get_mem (i
);
770 /* Reformat the string into a temporary buffer. As we're using atof it's
771 easiest to just leave the decimal point in place. */
775 for (; ndigits
> 0; ndigits
--)
779 if (g
.blank_status
== BLANK_ZERO
) *digits
= '0';
780 if (g
.blank_status
== BLANK_NULL
)
791 sprintf (p
, "%d", exponent
);
793 /* Do the actual conversion. */
794 convert_real (dest
, buffer
, length
);
796 if (buffer
!= scratch
)
803 /* read_x()-- Deal with the X/TR descriptor. We just read some data
804 * and never look at it. */
813 if ((current_unit
->flags
.pad
== PAD_NO
|| is_internal_unit ())
814 && current_unit
->bytes_left
< n
)
815 n
= current_unit
->bytes_left
;