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, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, 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_ZERO
)
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
);
340 if (c
< '0' || c
> '9')
349 if (value
> maxv
- c
)
358 set_integer (dest
, v
, length
);
362 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
366 generate_error (ERROR_READ_OVERFLOW
,
367 "Value overflowed during integer read");
372 /* read_radix()-- This function reads values for non-decimal radixes.
373 * The difference here is that we treat the values here as unsigned
374 * values for the purposes of overflow. If minus sign is present and
375 * the top bit is set, the value will be incorrect. */
378 read_radix (fnode
* f
, char *dest
, int length
, int radix
)
380 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
381 GFC_INTEGER_LARGEST v
;
390 p
= eat_leading_spaces (&w
, p
);
393 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
397 maxv
= max_value (length
, 0);
398 maxv_r
= maxv
/ radix
;
419 /* At this point we have a digit-string */
424 c
= next_char (&p
, &w
);
431 if (c
< '0' || c
> '1')
436 if (c
< '0' || c
> '7')
461 c
= c
- 'a' + '9' + 1;
470 c
= c
- 'A' + '9' + 1;
484 value
= radix
* value
;
486 if (maxv
- c
< value
)
495 set_integer (dest
, v
, length
);
499 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
503 generate_error (ERROR_READ_OVERFLOW
,
504 "Value overflowed during integer read");
509 /* read_f()-- Read a floating point number with F-style editing, which
510 is what all of the other floating point descriptors behave as. The
511 tricky part is that optional spaces are allowed after an E or D,
512 and the implicit decimal point if a decimal point is not present in
516 read_f (fnode
* f
, char *dest
, int length
)
518 int w
, seen_dp
, exponent
;
519 int exponent_sign
, val_sign
;
533 p
= eat_leading_spaces (&w
, p
);
539 if (*p
== '-' || *p
== '+')
548 p
= eat_leading_spaces (&w
, p
);
552 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
553 is required at this point */
555 if (!isdigit (*p
) && *p
!= '.' && *p
!= 'd' && *p
!= 'D'
556 && *p
!= 'e' && *p
!= 'E')
559 /* Remember the position of the first digit. */
563 /* Scan through the string to find the exponent. */
612 /* No exponent has been seen, so we use the current scale factor */
613 exponent
= -g
.scale_factor
;
617 generate_error (ERROR_READ_VALUE
, "Bad value during floating point read");
620 /* The value read is zero */
625 *((GFC_REAL_4
*) dest
) = 0;
629 *((GFC_REAL_8
*) dest
) = 0;
632 #ifdef HAVE_GFC_REAL_10
634 *((GFC_REAL_10
*) dest
) = 0;
638 #ifdef HAVE_GFC_REAL_16
640 *((GFC_REAL_16
*) dest
) = 0;
645 internal_error ("Unsupported real kind during IO");
649 /* At this point the start of an exponent has been found */
651 while (w
> 0 && *p
== ' ')
672 /* At this point a digit string is required. We calculate the value
673 of the exponent in order to take account of the scale factor and
674 the d parameter before explict conversion takes place. */
683 while (w
> 0 && isdigit (*p
))
685 exponent
= 10 * exponent
+ *p
- '0';
690 /* Only allow trailing blanks */
700 exponent
= exponent
* exponent_sign
;
703 /* Use the precision specified in the format if no decimal point has been
706 exponent
-= f
->u
.real
.d
;
725 i
= ndigits
+ edigits
+ 1;
729 if (i
< SCRATCH_SIZE
)
732 buffer
= get_mem (i
);
734 /* Reformat the string into a temporary buffer. As we're using atof it's
735 easiest to just leave the dcimal point in place. */
739 for (; ndigits
> 0; ndigits
--)
741 if (*digits
== ' ' && g
.blank_status
== BLANK_ZERO
)
749 sprintf (p
, "%d", exponent
);
751 /* Do the actual conversion. */
752 convert_real (dest
, buffer
, length
);
754 if (buffer
!= scratch
)
761 /* read_x()-- Deal with the X/TR descriptor. We just read some data
762 * and never look at it. */