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 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 #include "libgfortran.h"
31 /* read.c -- Deal with formatted reads */
33 /* set_integer()-- All of the integer assignments come here to
34 * actually place the value into memory. */
37 set_integer (void *dest
, int64_t value
, int length
)
42 *((int64_t *) dest
) = value
;
45 *((int32_t *) dest
) = value
;
48 *((int16_t *) dest
) = value
;
51 *((int8_t *) dest
) = value
;
54 internal_error ("Bad integer kind");
59 /* max_value()-- Given a length (kind), return the maximum signed or
63 max_value (int length
, int signed_flag
)
70 value
= signed_flag
? 0x7fffffffffffffff : 0xffffffffffffffff;
73 value
= signed_flag
? 0x7fffffff : 0xffffffff;
76 value
= signed_flag
? 0x7fff : 0xffff;
79 value
= signed_flag
? 0x7f : 0xff;
82 internal_error ("Bad integer kind");
89 /* convert_real()-- Convert a character representation of a floating
90 * point number to the machine number. Returns nonzero if there is a
91 * range problem during conversion. TODO: handle not-a-numbers and
95 convert_real (void *dest
, const char *buffer
, int length
)
103 #if defined(HAVE_STRTOF)
104 strtof (buffer
, NULL
);
106 (float) strtod (buffer
, NULL
);
110 *((double *) dest
) = strtod (buffer
, NULL
);
113 internal_error ("Unsupported real kind during IO");
118 generate_error (ERROR_READ_VALUE
,
119 "Range error during floating point read");
127 /* read_l()-- Read a logical value */
130 read_l (fnode
* f
, char *dest
, int length
)
158 set_integer (dest
, 1, length
);
162 set_integer (dest
, 0, length
);
166 generate_error (ERROR_READ_VALUE
, "Bad value on logical read");
172 /* read_a()-- Read a character record. This one is pretty easy. */
175 read_a (fnode
* f
, char *p
, int length
)
181 if (w
== -1) /* '(A)' edit descriptor */
184 source
= read_block (&w
);
188 source
+= (w
- length
);
190 m
= (w
> length
) ? length
: w
;
191 memcpy (p
, source
, m
);
195 memset (p
+ m
, ' ', n
);
199 /* eat_leading_spaces()-- Given a character pointer and a width,
200 * ignore the leading spaces. */
203 eat_leading_spaces (int *width
, char *p
)
207 if (*width
== 0 || *p
!= ' ')
219 next_char (char **p
, int *w
)
234 if (g
.blank_status
== BLANK_ZERO
)
237 /* At this point, the rest of the field has to be trailing blanks */
251 /* read_decimal()-- Read a decimal integer value. The values here are
255 read_decimal (fnode
* f
, char *dest
, int length
)
257 unsigned value
, maxv
, maxv_10
;
266 p
= eat_leading_spaces (&w
, p
);
269 set_integer (dest
, 0, length
);
273 maxv
= max_value (length
, 1);
295 /* At this point we have a digit-string */
300 c
= next_char (&p
, &w
);
304 if (c
< '0' || c
> '9')
313 if (value
> maxv
- c
)
318 v
= (signed int) value
;
322 set_integer (dest
, v
, length
);
326 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
330 generate_error (ERROR_READ_OVERFLOW
,
331 "Value overflowed during integer read");
336 /* read_radix()-- This function reads values for non-decimal radixes.
337 * The difference here is that we treat the values here as unsigned
338 * values for the purposes of overflow. If minus sign is present and
339 * the top bit is set, the value will be incorrect. */
342 read_radix (fnode
* f
, char *dest
, int length
, int radix
)
344 unsigned value
, maxv
, maxv_r
;
353 p
= eat_leading_spaces (&w
, p
);
356 set_integer (dest
, 0, length
);
360 maxv
= max_value (length
, 0);
361 maxv_r
= maxv
/ radix
;
382 /* At this point we have a digit-string */
387 c
= next_char (&p
, &w
);
394 if (c
< '0' || c
> '1')
399 if (c
< '0' || c
> '7')
424 c
= c
- 'a' + '9' + 1;
433 c
= c
- 'A' + '9' + 1;
447 value
= radix
* value
;
449 if (maxv
- c
< value
)
454 v
= (signed int) value
;
458 set_integer (dest
, v
, length
);
462 generate_error (ERROR_READ_VALUE
, "Bad value during integer read");
466 generate_error (ERROR_READ_OVERFLOW
,
467 "Value overflowed during integer read");
472 /* read_f()-- Read a floating point number with F-style editing, which
473 is what all of the other floating point descriptors behave as. The
474 tricky part is that optional spaces are allowed after an E or D,
475 and the implicit decimal point if a decimal point is not present in
479 read_f (fnode
* f
, char *dest
, int length
)
481 int w
, seen_dp
, exponent
;
482 int exponent_sign
, val_sign
;
496 p
= eat_leading_spaces (&w
, p
);
502 *((float *) dest
) = 0.0f
;
506 *((double *) dest
) = 0.0;
510 internal_error ("Unsupported real kind during IO");
518 if (*p
== '-' || *p
== '+')
530 /* A digit (or a '.') is required at this point */
532 if (!isdigit (*p
) && *p
!= '.')
535 /* Remember the position of the first digit. */
539 /* Scan through the string to find the exponent. */
588 /* No exponent has been seen, so we use the current scale factor */
589 exponent
= -g
.scale_factor
;
593 generate_error (ERROR_READ_VALUE
, "Bad value during floating point read");
594 if (buffer
!= scratch
)
598 /* At this point the start of an exponent has been found */
600 while (w
> 0 && *p
== ' ')
621 /* At this point a digit string is required. We calculate the value
622 of the exponent in order to take account of the scale factor and
623 the d parameter before explict conversion takes place. */
632 while (w
> 0 && isdigit (*p
))
634 exponent
= 10 * exponent
+ *p
- '0';
639 /* Only allow trailing blanks */
649 exponent
= exponent
* exponent_sign
;
652 /* Use the precision specified in the format if no decimal point has been
655 exponent
-= f
->u
.real
.d
;
674 i
= ndigits
+ edigits
+ 1;
678 if (i
< SCRATCH_SIZE
)
681 buffer
= get_mem (i
);
683 /* Reformat the string into a temporary buffer. As we're using atof it's
684 easiest to just leave the dcimal point in place. */
688 for (; ndigits
> 0; ndigits
--)
690 if (*digits
== ' ' && g
.blank_status
== BLANK_ZERO
)
698 sprintf (p
, "%d", exponent
);
700 /* Do the actual conversion. */
701 convert_real (dest
, buffer
, length
);
703 if (buffer
!= scratch
)
710 /* read_x()-- Deal with the X/TR descriptor. We just read some data
711 * and never look at it. */