2004-05-13 08:41:07 +02:00
|
|
|
/* Copyright (C) 2002-2003 Free Software Foundation, Inc.
|
|
|
|
Contributed by Andy Vaught
|
|
|
|
|
|
|
|
This file is part of the GNU Fortran 95 runtime library (libgfortran).
|
|
|
|
|
|
|
|
Libgfortran is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
2005-01-12 22:27:33 +01:00
|
|
|
In addition to the permissions in the GNU General Public License, the
|
|
|
|
Free Software Foundation gives you unlimited permission to link the
|
|
|
|
compiled version of this file into combinations with other programs,
|
|
|
|
and to distribute those combinations without any restriction coming
|
|
|
|
from the use of this file. (The General Public License restrictions
|
|
|
|
do apply in other respects; for example, they cover modification of
|
|
|
|
the file, and distribution when not linked into a combine
|
|
|
|
executable.)
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
Libgfortran is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Libgfortran; see the file COPYING. If not, write to
|
2005-08-17 04:49:08 +02:00
|
|
|
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA. */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2004-11-10 03:19:27 +01:00
|
|
|
#include <stdio.h>
|
2004-05-13 08:41:07 +02:00
|
|
|
#include "libgfortran.h"
|
|
|
|
#include "io.h"
|
|
|
|
|
|
|
|
/* read.c -- Deal with formatted reads */
|
|
|
|
|
|
|
|
/* set_integer()-- All of the integer assignments come here to
|
|
|
|
* actually place the value into memory. */
|
|
|
|
|
|
|
|
void
|
2005-06-23 20:50:25 +02:00
|
|
|
set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
|
2004-05-13 08:41:07 +02:00
|
|
|
{
|
|
|
|
switch (length)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
#ifdef HAVE_GFC_INTEGER_16
|
|
|
|
case 16:
|
2005-08-31 19:05:52 +02:00
|
|
|
{
|
|
|
|
GFC_INTEGER_16 tmp = value;
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2005-06-23 20:50:25 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2004-05-13 08:41:07 +02:00
|
|
|
case 8:
|
2005-08-31 19:05:52 +02:00
|
|
|
{
|
|
|
|
GFC_INTEGER_8 tmp = value;
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2005-08-31 19:05:52 +02:00
|
|
|
{
|
|
|
|
GFC_INTEGER_4 tmp = value;
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2005-08-31 19:05:52 +02:00
|
|
|
{
|
|
|
|
GFC_INTEGER_2 tmp = value;
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2005-08-31 19:05:52 +02:00
|
|
|
{
|
|
|
|
GFC_INTEGER_1 tmp = value;
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
internal_error ("Bad integer kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* max_value()-- Given a length (kind), return the maximum signed or
|
|
|
|
* unsigned value */
|
|
|
|
|
2005-06-23 20:50:25 +02:00
|
|
|
GFC_UINTEGER_LARGEST
|
2004-05-13 08:41:07 +02:00
|
|
|
max_value (int length, int signed_flag)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
GFC_UINTEGER_LARGEST value;
|
|
|
|
int n;
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
switch (length)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
#if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
|
|
|
|
case 16:
|
|
|
|
case 10:
|
|
|
|
value = 1;
|
|
|
|
for (n = 1; n < 4 * length; n++)
|
|
|
|
value = (value << 2) + 3;
|
|
|
|
if (! signed_flag)
|
|
|
|
value = 2*value+1;
|
|
|
|
break;
|
|
|
|
#endif
|
2004-05-13 08:41:07 +02:00
|
|
|
case 8:
|
|
|
|
value = signed_flag ? 0x7fffffffffffffff : 0xffffffffffffffff;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
value = signed_flag ? 0x7fffffff : 0xffffffff;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
value = signed_flag ? 0x7fff : 0xffff;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
value = signed_flag ? 0x7f : 0xff;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
internal_error ("Bad integer kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* convert_real()-- Convert a character representation of a floating
|
|
|
|
* point number to the machine number. Returns nonzero if there is a
|
|
|
|
* range problem during conversion. TODO: handle not-a-numbers and
|
2004-11-10 03:19:27 +01:00
|
|
|
* infinities. */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
convert_real (void *dest, const char *buffer, int length)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
switch (length)
|
|
|
|
{
|
|
|
|
case 4:
|
2005-08-31 18:58:28 +02:00
|
|
|
{
|
|
|
|
GFC_REAL_4 tmp =
|
2004-11-10 03:19:27 +01:00
|
|
|
#if defined(HAVE_STRTOF)
|
2005-08-31 18:58:28 +02:00
|
|
|
strtof (buffer, NULL);
|
2004-11-10 03:19:27 +01:00
|
|
|
#else
|
2005-08-31 18:58:28 +02:00
|
|
|
(GFC_REAL_4) strtod (buffer, NULL);
|
2004-11-10 03:19:27 +01:00
|
|
|
#endif
|
2005-08-31 18:58:28 +02:00
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
case 8:
|
2005-08-31 18:58:28 +02:00
|
|
|
{
|
|
|
|
GFC_REAL_8 tmp = strtod (buffer, NULL);
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2005-06-23 20:50:25 +02:00
|
|
|
break;
|
|
|
|
#if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
|
|
|
|
case 10:
|
2005-08-31 18:58:28 +02:00
|
|
|
{
|
|
|
|
GFC_REAL_10 tmp = strtold (buffer, NULL);
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2005-06-23 20:50:25 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
|
|
|
|
case 16:
|
2005-08-31 18:58:28 +02:00
|
|
|
{
|
|
|
|
GFC_REAL_16 tmp = strtold (buffer, NULL);
|
|
|
|
memcpy (dest, (void *) &tmp, length);
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
2005-06-23 20:50:25 +02:00
|
|
|
#endif
|
2004-05-13 08:41:07 +02:00
|
|
|
default:
|
2004-11-10 03:19:27 +01:00
|
|
|
internal_error ("Unsupported real kind during IO");
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
|
2005-06-17 18:20:29 +02:00
|
|
|
if (errno != 0 && errno != EINVAL)
|
2004-05-13 08:41:07 +02:00
|
|
|
{
|
|
|
|
generate_error (ERROR_READ_VALUE,
|
|
|
|
"Range error during floating point read");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_l()-- Read a logical value */
|
|
|
|
|
|
|
|
void
|
|
|
|
read_l (fnode * f, char *dest, int length)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int w;
|
|
|
|
|
|
|
|
w = f->u.w;
|
|
|
|
p = read_block (&w);
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (*p == ' ')
|
|
|
|
{
|
|
|
|
if (--w == 0)
|
|
|
|
goto bad;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == '.')
|
|
|
|
{
|
|
|
|
if (--w == 0)
|
|
|
|
goto bad;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
2005-06-23 20:50:25 +02:00
|
|
|
set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
2005-06-23 20:50:25 +02:00
|
|
|
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
|
2004-05-13 08:41:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bad:
|
|
|
|
generate_error (ERROR_READ_VALUE, "Bad value on logical read");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_a()-- Read a character record. This one is pretty easy. */
|
|
|
|
|
|
|
|
void
|
|
|
|
read_a (fnode * f, char *p, int length)
|
|
|
|
{
|
|
|
|
char *source;
|
|
|
|
int w, m, n;
|
|
|
|
|
|
|
|
w = f->u.w;
|
|
|
|
if (w == -1) /* '(A)' edit descriptor */
|
|
|
|
w = length;
|
|
|
|
|
|
|
|
source = read_block (&w);
|
|
|
|
if (source == NULL)
|
|
|
|
return;
|
|
|
|
if (w > length)
|
|
|
|
source += (w - length);
|
|
|
|
|
|
|
|
m = (w > length) ? length : w;
|
|
|
|
memcpy (p, source, m);
|
|
|
|
|
|
|
|
n = length - w;
|
|
|
|
if (n > 0)
|
|
|
|
memset (p + m, ' ', n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* eat_leading_spaces()-- Given a character pointer and a width,
|
|
|
|
* ignore the leading spaces. */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
eat_leading_spaces (int *width, char *p)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*width == 0 || *p != ' ')
|
|
|
|
break;
|
|
|
|
|
|
|
|
(*width)--;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char
|
|
|
|
next_char (char **p, int *w)
|
|
|
|
{
|
|
|
|
char c, *q;
|
|
|
|
|
|
|
|
if (*w == 0)
|
|
|
|
return '\0';
|
|
|
|
|
|
|
|
q = *p;
|
|
|
|
c = *q++;
|
|
|
|
*p = q;
|
|
|
|
|
|
|
|
(*w)--;
|
|
|
|
|
|
|
|
if (c != ' ')
|
|
|
|
return c;
|
2005-07-10 01:40:31 +02:00
|
|
|
if (g.blank_status != BLANK_UNSPECIFIED)
|
|
|
|
return ' '; /* return a blank to signal a null */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
/* At this point, the rest of the field has to be trailing blanks */
|
|
|
|
|
|
|
|
while (*w > 0)
|
|
|
|
{
|
|
|
|
if (*q++ != ' ')
|
|
|
|
return '?';
|
|
|
|
(*w)--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = q;
|
|
|
|
return '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_decimal()-- Read a decimal integer value. The values here are
|
|
|
|
* signed values. */
|
|
|
|
|
|
|
|
void
|
|
|
|
read_decimal (fnode * f, char *dest, int length)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
GFC_UINTEGER_LARGEST value, maxv, maxv_10;
|
|
|
|
GFC_INTEGER_LARGEST v;
|
|
|
|
int w, negative;
|
2004-05-13 08:41:07 +02:00
|
|
|
char c, *p;
|
|
|
|
|
|
|
|
w = f->u.w;
|
|
|
|
p = read_block (&w);
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = eat_leading_spaces (&w, p);
|
|
|
|
if (w == 0)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
|
2004-05-13 08:41:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxv = max_value (length, 1);
|
|
|
|
maxv_10 = maxv / 10;
|
|
|
|
|
|
|
|
negative = 0;
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
negative = 1;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
if (--w == 0)
|
|
|
|
goto bad;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point we have a digit-string */
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = next_char (&p, &w);
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
2005-07-10 01:40:31 +02:00
|
|
|
|
|
|
|
if (c == ' ')
|
|
|
|
{
|
|
|
|
if (g.blank_status == BLANK_NULL) continue;
|
|
|
|
if (g.blank_status == BLANK_ZERO) c = '0';
|
|
|
|
}
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
if (c < '0' || c > '9')
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
if (value > maxv_10)
|
|
|
|
goto overflow;
|
|
|
|
|
|
|
|
c -= '0';
|
|
|
|
value = 10 * value;
|
|
|
|
|
|
|
|
if (value > maxv - c)
|
|
|
|
goto overflow;
|
|
|
|
value += c;
|
|
|
|
}
|
|
|
|
|
2005-06-23 20:50:25 +02:00
|
|
|
v = value;
|
2004-05-13 08:41:07 +02:00
|
|
|
if (negative)
|
|
|
|
v = -v;
|
|
|
|
|
|
|
|
set_integer (dest, v, length);
|
|
|
|
return;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
bad:
|
2004-05-13 08:41:07 +02:00
|
|
|
generate_error (ERROR_READ_VALUE, "Bad value during integer read");
|
|
|
|
return;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
overflow:
|
2004-05-13 08:41:07 +02:00
|
|
|
generate_error (ERROR_READ_OVERFLOW,
|
|
|
|
"Value overflowed during integer read");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_radix()-- This function reads values for non-decimal radixes.
|
|
|
|
* The difference here is that we treat the values here as unsigned
|
|
|
|
* values for the purposes of overflow. If minus sign is present and
|
|
|
|
* the top bit is set, the value will be incorrect. */
|
|
|
|
|
|
|
|
void
|
|
|
|
read_radix (fnode * f, char *dest, int length, int radix)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
GFC_UINTEGER_LARGEST value, maxv, maxv_r;
|
|
|
|
GFC_INTEGER_LARGEST v;
|
|
|
|
int w, negative;
|
2004-05-13 08:41:07 +02:00
|
|
|
char c, *p;
|
|
|
|
|
|
|
|
w = f->u.w;
|
|
|
|
p = read_block (&w);
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = eat_leading_spaces (&w, p);
|
|
|
|
if (w == 0)
|
|
|
|
{
|
2005-06-23 20:50:25 +02:00
|
|
|
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
|
2004-05-13 08:41:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxv = max_value (length, 0);
|
|
|
|
maxv_r = maxv / radix;
|
|
|
|
|
|
|
|
negative = 0;
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
negative = 1;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
if (--w == 0)
|
|
|
|
goto bad;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point we have a digit-string */
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = next_char (&p, &w);
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
2005-07-10 01:40:31 +02:00
|
|
|
if (c == ' ')
|
|
|
|
{
|
|
|
|
if (g.blank_status == BLANK_NULL) continue;
|
|
|
|
if (g.blank_status == BLANK_ZERO) c = '0';
|
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
switch (radix)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
if (c < '0' || c > '1')
|
|
|
|
goto bad;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
if (c < '0' || c > '7')
|
|
|
|
goto bad;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
case 'e':
|
2004-08-05 10:37:42 +02:00
|
|
|
case 'f':
|
2004-05-13 08:41:07 +02:00
|
|
|
c = c - 'a' + '9' + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
case 'E':
|
2004-08-05 10:37:42 +02:00
|
|
|
case 'F':
|
2004-05-13 08:41:07 +02:00
|
|
|
c = c - 'A' + '9' + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value > maxv_r)
|
|
|
|
goto overflow;
|
|
|
|
|
|
|
|
c -= '0';
|
|
|
|
value = radix * value;
|
|
|
|
|
|
|
|
if (maxv - c < value)
|
|
|
|
goto overflow;
|
|
|
|
value += c;
|
|
|
|
}
|
|
|
|
|
2005-06-23 20:50:25 +02:00
|
|
|
v = value;
|
2004-05-13 08:41:07 +02:00
|
|
|
if (negative)
|
|
|
|
v = -v;
|
|
|
|
|
|
|
|
set_integer (dest, v, length);
|
|
|
|
return;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
bad:
|
2004-05-13 08:41:07 +02:00
|
|
|
generate_error (ERROR_READ_VALUE, "Bad value during integer read");
|
|
|
|
return;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
overflow:
|
2004-05-13 08:41:07 +02:00
|
|
|
generate_error (ERROR_READ_OVERFLOW,
|
|
|
|
"Value overflowed during integer read");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_f()-- Read a floating point number with F-style editing, which
|
2004-11-10 03:19:27 +01:00
|
|
|
is what all of the other floating point descriptors behave as. The
|
|
|
|
tricky part is that optional spaces are allowed after an E or D,
|
|
|
|
and the implicit decimal point if a decimal point is not present in
|
|
|
|
the input. */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
read_f (fnode * f, char *dest, int length)
|
|
|
|
{
|
|
|
|
int w, seen_dp, exponent;
|
|
|
|
int exponent_sign, val_sign;
|
2004-11-10 03:19:27 +01:00
|
|
|
int ndigits;
|
|
|
|
int edigits;
|
|
|
|
int i;
|
|
|
|
char *p, *buffer;
|
|
|
|
char *digits;
|
2004-05-13 08:41:07 +02:00
|
|
|
|
2004-11-10 03:19:27 +01:00
|
|
|
val_sign = 1;
|
2004-05-13 08:41:07 +02:00
|
|
|
seen_dp = 0;
|
|
|
|
w = f->u.w;
|
|
|
|
p = read_block (&w);
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = eat_leading_spaces (&w, p);
|
|
|
|
if (w == 0)
|
2005-06-12 21:59:17 +02:00
|
|
|
goto zero;
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
/* Optional sign */
|
|
|
|
|
|
|
|
if (*p == '-' || *p == '+')
|
|
|
|
{
|
|
|
|
if (*p == '-')
|
2004-11-10 03:19:27 +01:00
|
|
|
val_sign = -1;
|
2004-05-13 08:41:07 +02:00
|
|
|
p++;
|
2005-06-12 21:59:17 +02:00
|
|
|
w--;
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exponent_sign = 1;
|
2005-06-12 21:59:17 +02:00
|
|
|
p = eat_leading_spaces (&w, p);
|
|
|
|
if (w == 0)
|
|
|
|
goto zero;
|
2004-05-13 08:41:07 +02:00
|
|
|
|
2005-05-10 10:34:58 +02:00
|
|
|
/* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
|
|
|
|
is required at this point */
|
2004-05-13 08:41:07 +02:00
|
|
|
|
2005-05-10 10:34:58 +02:00
|
|
|
if (!isdigit (*p) && *p != '.' && *p != 'd' && *p != 'D'
|
|
|
|
&& *p != 'e' && *p != 'E')
|
2004-05-13 08:41:07 +02:00
|
|
|
goto bad_float;
|
|
|
|
|
2004-11-10 03:19:27 +01:00
|
|
|
/* Remember the position of the first digit. */
|
|
|
|
digits = p;
|
|
|
|
ndigits = 0;
|
|
|
|
|
|
|
|
/* Scan through the string to find the exponent. */
|
2004-05-13 08:41:07 +02:00
|
|
|
while (w > 0)
|
|
|
|
{
|
|
|
|
switch (*p)
|
|
|
|
{
|
2004-11-10 03:19:27 +01:00
|
|
|
case '.':
|
|
|
|
if (seen_dp)
|
|
|
|
goto bad_float;
|
|
|
|
seen_dp = 1;
|
|
|
|
/* Fall through */
|
|
|
|
|
2004-05-13 08:41:07 +02:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
case ' ':
|
2004-11-10 03:19:27 +01:00
|
|
|
ndigits++;
|
libgfortran.h (GFC_ITOA_BUF_SIZE, [...]): Define.
* libgfortran.h (GFC_ITOA_BUF_SIZE, GFC_XTOA_BUF_SIZE,
GFC_OTOA_BUF_SIZE, GFC_BTOA_BUF_SIZE): Define.
(gfc_itoa, xtoa): Add 2 extra arguments.
* runtime/environ.c: Include stdio.h.
(check_buffered): Use sprintf.
* runtime/error.c: Include assert.h.
(gfc_itoa, xtoa): Add 2 extra arguments, avoid using static
buffers.
(st_printf, st_sprintf): Adjust callers.
* io/write.c (otoa, btoa): Add 2 extra arguments, avoid using
static buffers.
(write_int, write_decimal): Add 2 extra arguments to conv
function pointer, adjust caller.
(write_integer): Adjust gfc_itoa caller.
* io/unit.c (get_array_unit_len): Return 0 rather than NULL.
* io/read.c (read_f): Remove spurious pointer dereference.
From-SVN: r104855
2005-10-01 13:50:10 +02:00
|
|
|
p++;
|
2004-05-13 08:41:07 +02:00
|
|
|
w--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
exponent_sign = -1;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
goto exp2;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'e':
|
|
|
|
case 'D':
|
|
|
|
case 'E':
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
goto exp1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto bad_float;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
/* No exponent has been seen, so we use the current scale factor */
|
2004-05-13 08:41:07 +02:00
|
|
|
exponent = -g.scale_factor;
|
|
|
|
goto done;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
bad_float:
|
2004-05-13 08:41:07 +02:00
|
|
|
generate_error (ERROR_READ_VALUE, "Bad value during floating point read");
|
|
|
|
return;
|
|
|
|
|
2005-06-12 21:59:17 +02:00
|
|
|
/* The value read is zero */
|
|
|
|
zero:
|
|
|
|
switch (length)
|
|
|
|
{
|
|
|
|
case 4:
|
2005-06-23 20:50:25 +02:00
|
|
|
*((GFC_REAL_4 *) dest) = 0;
|
2005-06-12 21:59:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
2005-06-23 20:50:25 +02:00
|
|
|
*((GFC_REAL_8 *) dest) = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef HAVE_GFC_REAL_10
|
|
|
|
case 10:
|
|
|
|
*((GFC_REAL_10 *) dest) = 0;
|
2005-06-12 21:59:17 +02:00
|
|
|
break;
|
2005-06-23 20:50:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GFC_REAL_16
|
|
|
|
case 16:
|
|
|
|
*((GFC_REAL_16 *) dest) = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
2005-06-12 21:59:17 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
internal_error ("Unsupported real kind during IO");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
/* At this point the start of an exponent has been found */
|
|
|
|
exp1:
|
2004-05-13 08:41:07 +02:00
|
|
|
while (w > 0 && *p == ' ')
|
|
|
|
{
|
|
|
|
w--;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*p)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
exponent_sign = -1;
|
|
|
|
/* Fall through */
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w == 0)
|
|
|
|
goto bad_float;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
/* At this point a digit string is required. We calculate the value
|
|
|
|
of the exponent in order to take account of the scale factor and
|
|
|
|
the d parameter before explict conversion takes place. */
|
|
|
|
exp2:
|
2004-05-13 08:41:07 +02:00
|
|
|
if (!isdigit (*p))
|
|
|
|
goto bad_float;
|
|
|
|
|
|
|
|
exponent = *p - '0';
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
|
2005-07-22 16:07:19 +02:00
|
|
|
if (g.blank_status == BLANK_UNSPECIFIED) /* Normal processing of exponent */
|
2004-05-13 08:41:07 +02:00
|
|
|
{
|
2005-07-22 16:07:19 +02:00
|
|
|
while (w > 0 && isdigit (*p))
|
2005-07-10 01:40:31 +02:00
|
|
|
{
|
2005-07-22 16:07:19 +02:00
|
|
|
exponent = 10 * exponent + *p - '0';
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only allow trailing blanks */
|
|
|
|
|
|
|
|
while (w > 0)
|
|
|
|
{
|
|
|
|
if (*p != ' ')
|
|
|
|
goto bad_float;
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* BZ or BN status is enabled */
|
|
|
|
{
|
|
|
|
while (w > 0)
|
|
|
|
{
|
|
|
|
if (*p == ' ')
|
2005-07-10 01:40:31 +02:00
|
|
|
{
|
2005-07-22 16:07:19 +02:00
|
|
|
if (g.blank_status == BLANK_ZERO) *p = '0';
|
|
|
|
if (g.blank_status == BLANK_NULL)
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
w--;
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-10 01:40:31 +02:00
|
|
|
}
|
2005-07-22 16:07:19 +02:00
|
|
|
else if (!isdigit (*p))
|
|
|
|
goto bad_float;
|
|
|
|
|
|
|
|
exponent = 10 * exponent + *p - '0';
|
|
|
|
p++;
|
|
|
|
w--;
|
2005-07-10 01:40:31 +02:00
|
|
|
}
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exponent = exponent * exponent_sign;
|
|
|
|
|
c99_functions.c, [...]: Whitespace fixes.
* intrinsics/c99_functions.c, intrinsics/eoshift0.c,
intrinsics/eoshift2.c, intrinsics/exit.c, intrinsics/flush.c,
intrinsics/ishftc.c, intrinsics/mvbits.c, intrinsics/pack_generic.c,
intrinsics/random.c, intrinsics/reshape_generic.c, intrinsics/size.c,
intrinsics/spread_generic.c, intrinsics/stat.c,
intrinsics/string_intrinsics.c, intrinsics/system_clock.c,
intrinsics/transpose_generic.c, intrinsics/unlink.c,
intrinsics/unpack_generic.c, io/backspace.c, io/format.c,
io/list_read.c, io/lock.c, io/open.c, io/transfer.c, io/unix.c,
io/write.c, runtime/environ.c, runtime/error.c,
runtime/in_pack_generic.c, runtime/in_unpack_generic.c, runtime/main.c,
runtime/memory.c, runtime/pause.c, runtime/stop.c,
runtime/string.c: Whitespace fixes.
From-SVN: r91794
2004-12-07 01:01:01 +01:00
|
|
|
done:
|
2004-11-10 03:19:27 +01:00
|
|
|
/* Use the precision specified in the format if no decimal point has been
|
|
|
|
seen. */
|
2004-05-13 08:41:07 +02:00
|
|
|
if (!seen_dp)
|
|
|
|
exponent -= f->u.real.d;
|
|
|
|
|
2004-11-10 03:19:27 +01:00
|
|
|
if (exponent > 0)
|
|
|
|
{
|
|
|
|
edigits = 2;
|
|
|
|
i = exponent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
edigits = 3;
|
|
|
|
i = -exponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i >= 10)
|
|
|
|
{
|
|
|
|
i /= 10;
|
|
|
|
edigits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = ndigits + edigits + 1;
|
|
|
|
if (val_sign < 0)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i < SCRATCH_SIZE)
|
|
|
|
buffer = scratch;
|
|
|
|
else
|
|
|
|
buffer = get_mem (i);
|
|
|
|
|
|
|
|
/* Reformat the string into a temporary buffer. As we're using atof it's
|
2005-07-10 01:40:31 +02:00
|
|
|
easiest to just leave the decimal point in place. */
|
2004-11-10 03:19:27 +01:00
|
|
|
p = buffer;
|
|
|
|
if (val_sign < 0)
|
|
|
|
*(p++) = '-';
|
|
|
|
for (; ndigits > 0; ndigits--)
|
|
|
|
{
|
2005-07-10 01:40:31 +02:00
|
|
|
if (*digits == ' ')
|
|
|
|
{
|
|
|
|
if (g.blank_status == BLANK_ZERO) *digits = '0';
|
|
|
|
if (g.blank_status == BLANK_NULL)
|
|
|
|
{
|
|
|
|
digits++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = *digits;
|
2004-11-10 03:19:27 +01:00
|
|
|
p++;
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
*(p++) = 'e';
|
|
|
|
sprintf (p, "%d", exponent);
|
2004-05-13 08:41:07 +02:00
|
|
|
|
2004-11-10 03:19:27 +01:00
|
|
|
/* Do the actual conversion. */
|
2004-11-10 03:44:39 +01:00
|
|
|
convert_real (dest, buffer, length);
|
2004-05-13 08:41:07 +02:00
|
|
|
|
|
|
|
if (buffer != scratch)
|
|
|
|
free_mem (buffer);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* read_x()-- Deal with the X/TR descriptor. We just read some data
|
|
|
|
* and never look at it. */
|
|
|
|
|
|
|
|
void
|
2005-09-03 20:54:55 +02:00
|
|
|
read_x (int n)
|
2004-05-13 08:41:07 +02:00
|
|
|
{
|
2005-07-22 16:07:19 +02:00
|
|
|
if ((current_unit->flags.pad == PAD_NO || is_internal_unit ())
|
|
|
|
&& current_unit->bytes_left < n)
|
|
|
|
n = current_unit->bytes_left;
|
|
|
|
|
|
|
|
if (n > 0)
|
2005-07-13 20:43:14 +02:00
|
|
|
read_block (&n);
|
2004-05-13 08:41:07 +02:00
|
|
|
}
|