1 /* Copyright (C) 2002, 2003, 2005, 2007, 2008 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
37 /* read.c -- Deal with formatted reads */
39 /* set_integer()-- All of the integer assignments come here to
40 * actually place the value into memory. */
43 set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
47 #ifdef HAVE_GFC_INTEGER_16
50 GFC_INTEGER_16 tmp = value;
51 memcpy (dest, (void *) &tmp, length);
57 GFC_INTEGER_8 tmp = value;
58 memcpy (dest, (void *) &tmp, length);
63 GFC_INTEGER_4 tmp = value;
64 memcpy (dest, (void *) &tmp, length);
69 GFC_INTEGER_2 tmp = value;
70 memcpy (dest, (void *) &tmp, length);
75 GFC_INTEGER_1 tmp = value;
76 memcpy (dest, (void *) &tmp, length);
80 internal_error (NULL, "Bad integer kind");
85 /* max_value()-- Given a length (kind), return the maximum signed or
89 max_value (int length, int signed_flag)
91 GFC_UINTEGER_LARGEST value;
92 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
98 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
102 for (n = 1; n < 4 * length; n++)
103 value = (value << 2) + 3;
109 value = signed_flag ? 0x7fffffffffffffff : 0xffffffffffffffff;
112 value = signed_flag ? 0x7fffffff : 0xffffffff;
115 value = signed_flag ? 0x7fff : 0xffff;
118 value = signed_flag ? 0x7f : 0xff;
121 internal_error (NULL, "Bad integer kind");
128 /* convert_real()-- Convert a character representation of a floating
129 * point number to the machine number. Returns nonzero if there is a
130 * range problem during conversion. TODO: handle not-a-numbers and
134 convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length)
143 #if defined(HAVE_STRTOF)
144 strtof (buffer, NULL);
146 (GFC_REAL_4) strtod (buffer, NULL);
148 memcpy (dest, (void *) &tmp, length);
153 GFC_REAL_8 tmp = strtod (buffer, NULL);
154 memcpy (dest, (void *) &tmp, length);
157 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
160 GFC_REAL_10 tmp = strtold (buffer, NULL);
161 memcpy (dest, (void *) &tmp, length);
165 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
168 GFC_REAL_16 tmp = strtold (buffer, NULL);
169 memcpy (dest, (void *) &tmp, length);
174 internal_error (&dtp->common, "Unsupported real kind during IO");
179 generate_error (&dtp->common, LIBERROR_READ_VALUE,
180 "Error during floating point read");
181 next_record (dtp, 1);
189 /* read_l()-- Read a logical value */
192 read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
198 p = read_block (dtp, &w);
220 set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
224 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
228 generate_error (&dtp->common, LIBERROR_READ_VALUE,
229 "Bad value on logical read");
230 next_record (dtp, 1);
236 /* read_a()-- Read a character record. This one is pretty easy. */
239 read_a (st_parameter_dt *dtp, const fnode *f, char *p, int length)
245 if (w == -1) /* '(A)' edit descriptor */
248 dtp->u.p.sf_read_comma = 0;
249 source = read_block (dtp, &w);
250 dtp->u.p.sf_read_comma =
251 dtp->u.p.decimal_status == DECIMAL_COMMA ? 0 : 1;
255 source += (w - length);
257 m = (w > length) ? length : w;
258 memcpy (p, source, m);
262 memset (p + m, ' ', n);
266 /* eat_leading_spaces()-- Given a character pointer and a width,
267 * ignore the leading spaces. */
270 eat_leading_spaces (int *width, char *p)
274 if (*width == 0 || *p != ' ')
286 next_char (st_parameter_dt *dtp, char **p, int *w)
301 if (dtp->u.p.blank_status != BLANK_UNSPECIFIED)
302 return ' '; /* return a blank to signal a null */
304 /* At this point, the rest of the field has to be trailing blanks */
318 /* read_decimal()-- Read a decimal integer value. The values here are
322 read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
324 GFC_UINTEGER_LARGEST value, maxv, maxv_10;
325 GFC_INTEGER_LARGEST v;
330 p = read_block (dtp, &w);
334 p = eat_leading_spaces (&w, p);
337 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
341 maxv = max_value (length, 1);
363 /* At this point we have a digit-string */
368 c = next_char (dtp, &p, &w);
374 if (dtp->u.p.blank_status == BLANK_NULL) continue;
375 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
378 if (c < '0' || c > '9')
387 if (value > maxv - c)
396 set_integer (dest, v, length);
400 generate_error (&dtp->common, LIBERROR_READ_VALUE,
401 "Bad value during integer read");
402 next_record (dtp, 1);
406 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
407 "Value overflowed during integer read");
408 next_record (dtp, 1);
413 /* read_radix()-- This function reads values for non-decimal radixes.
414 * The difference here is that we treat the values here as unsigned
415 * values for the purposes of overflow. If minus sign is present and
416 * the top bit is set, the value will be incorrect. */
419 read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
422 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
423 GFC_INTEGER_LARGEST v;
428 p = read_block (dtp, &w);
432 p = eat_leading_spaces (&w, p);
435 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
439 maxv = max_value (length, 0);
440 maxv_r = maxv / radix;
461 /* At this point we have a digit-string */
466 c = next_char (dtp, &p, &w);
471 if (dtp->u.p.blank_status == BLANK_NULL) continue;
472 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
478 if (c < '0' || c > '1')
483 if (c < '0' || c > '7')
508 c = c - 'a' + '9' + 1;
517 c = c - 'A' + '9' + 1;
531 value = radix * value;
533 if (maxv - c < value)
542 set_integer (dest, v, length);
546 generate_error (&dtp->common, LIBERROR_READ_VALUE,
547 "Bad value during integer read");
548 next_record (dtp, 1);
552 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
553 "Value overflowed during integer read");
554 next_record (dtp, 1);
559 /* read_f()-- Read a floating point number with F-style editing, which
560 is what all of the other floating point descriptors behave as. The
561 tricky part is that optional spaces are allowed after an E or D,
562 and the implicit decimal point if a decimal point is not present in
566 read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
568 int w, seen_dp, exponent;
569 int exponent_sign, val_sign;
575 char scratch[SCRATCH_SIZE];
580 p = read_block (dtp, &w);
584 p = eat_leading_spaces (&w, p);
590 if (*p == '-' || *p == '+')
599 p = eat_leading_spaces (&w, p);
603 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
604 is required at this point */
606 if (!isdigit (*p) && *p != '.' && *p != ',' && *p != 'd' && *p != 'D'
607 && *p != 'e' && *p != 'E')
610 /* Remember the position of the first digit. */
614 /* Scan through the string to find the exponent. */
620 if (dtp->u.p.decimal_status == DECIMAL_COMMA && *p == ',')
667 /* No exponent has been seen, so we use the current scale factor */
668 exponent = -dtp->u.p.scale_factor;
672 generate_error (&dtp->common, LIBERROR_READ_VALUE,
673 "Bad value during floating point read");
674 next_record (dtp, 1);
677 /* The value read is zero */
682 *((GFC_REAL_4 *) dest) = 0;
686 *((GFC_REAL_8 *) dest) = 0;
689 #ifdef HAVE_GFC_REAL_10
691 *((GFC_REAL_10 *) dest) = 0;
695 #ifdef HAVE_GFC_REAL_16
697 *((GFC_REAL_16 *) dest) = 0;
702 internal_error (&dtp->common, "Unsupported real kind during IO");
706 /* At this point the start of an exponent has been found */
708 while (w > 0 && *p == ' ')
729 /* At this point a digit string is required. We calculate the value
730 of the exponent in order to take account of the scale factor and
731 the d parameter before explict conversion takes place. */
740 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED) /* Normal processing of exponent */
742 while (w > 0 && isdigit (*p))
744 exponent = 10 * exponent + *p - '0';
749 /* Only allow trailing blanks */
759 else /* BZ or BN status is enabled */
765 if (dtp->u.p.blank_status == BLANK_ZERO) *p = '0';
766 if (dtp->u.p.blank_status == BLANK_NULL)
773 else if (!isdigit (*p))
776 exponent = 10 * exponent + *p - '0';
782 exponent = exponent * exponent_sign;
785 /* Use the precision specified in the format if no decimal point has been
788 exponent -= f->u.real.d;
807 i = ndigits + edigits + 1;
811 if (i < SCRATCH_SIZE)
814 buffer = get_mem (i);
816 /* Reformat the string into a temporary buffer. As we're using atof it's
817 easiest to just leave the decimal point in place. */
821 for (; ndigits > 0; ndigits--)
825 if (dtp->u.p.blank_status == BLANK_ZERO) *digits = '0';
826 if (dtp->u.p.blank_status == BLANK_NULL)
837 sprintf (p, "%d", exponent);
839 /* Do the actual conversion. */
840 convert_real (dtp, dest, buffer, length);
842 if (buffer != scratch)
849 /* read_x()-- Deal with the X/TR descriptor. We just read some data
850 * and never look at it. */
853 read_x (st_parameter_dt *dtp, int n)
855 if (!is_stream_io (dtp))
857 if ((dtp->u.p.pad_status == PAD_NO || is_internal_unit (dtp))
858 && dtp->u.p.current_unit->bytes_left < n)
859 n = dtp->u.p.current_unit->bytes_left;
861 dtp->u.p.sf_read_comma = 0;
863 read_sf (dtp, &n, 1);
864 dtp->u.p.sf_read_comma = 1;
867 dtp->u.p.current_unit->strm_pos += (gfc_offset) n;