* config/xtensa/xtensa.c (xtensa_expand_builtin): Use CALL_EXPR_FN.
[official-gcc.git] / libgfortran / io / read.c
blob57a58929d4a9d28283102770121e2a2c783cfd19
1 /* Copyright (C) 2002, 2003, 2005 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)
9 any later version.
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
18 executable.)
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. */
31 #include "config.h"
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include "libgfortran.h"
38 #include "io.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. */
45 void
46 set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
48 switch (length)
50 #ifdef HAVE_GFC_INTEGER_16
51 case 16:
53 GFC_INTEGER_16 tmp = value;
54 memcpy (dest, (void *) &tmp, length);
56 break;
57 #endif
58 case 8:
60 GFC_INTEGER_8 tmp = value;
61 memcpy (dest, (void *) &tmp, length);
63 break;
64 case 4:
66 GFC_INTEGER_4 tmp = value;
67 memcpy (dest, (void *) &tmp, length);
69 break;
70 case 2:
72 GFC_INTEGER_2 tmp = value;
73 memcpy (dest, (void *) &tmp, length);
75 break;
76 case 1:
78 GFC_INTEGER_1 tmp = value;
79 memcpy (dest, (void *) &tmp, length);
81 break;
82 default:
83 internal_error (NULL, "Bad integer kind");
88 /* max_value()-- Given a length (kind), return the maximum signed or
89 * unsigned value */
91 GFC_UINTEGER_LARGEST
92 max_value (int length, int signed_flag)
94 GFC_UINTEGER_LARGEST value;
95 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
96 int n;
97 #endif
99 switch (length)
101 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
102 case 16:
103 case 10:
104 value = 1;
105 for (n = 1; n < 4 * length; n++)
106 value = (value << 2) + 3;
107 if (! signed_flag)
108 value = 2*value+1;
109 break;
110 #endif
111 case 8:
112 value = signed_flag ? 0x7fffffffffffffff : 0xffffffffffffffff;
113 break;
114 case 4:
115 value = signed_flag ? 0x7fffffff : 0xffffffff;
116 break;
117 case 2:
118 value = signed_flag ? 0x7fff : 0xffff;
119 break;
120 case 1:
121 value = signed_flag ? 0x7f : 0xff;
122 break;
123 default:
124 internal_error (NULL, "Bad integer kind");
127 return value;
131 /* convert_real()-- Convert a character representation of a floating
132 * point number to the machine number. Returns nonzero if there is a
133 * range problem during conversion. TODO: handle not-a-numbers and
134 * infinities. */
137 convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length)
139 errno = 0;
141 switch (length)
143 case 4:
145 GFC_REAL_4 tmp =
146 #if defined(HAVE_STRTOF)
147 strtof (buffer, NULL);
148 #else
149 (GFC_REAL_4) strtod (buffer, NULL);
150 #endif
151 memcpy (dest, (void *) &tmp, length);
153 break;
154 case 8:
156 GFC_REAL_8 tmp = strtod (buffer, NULL);
157 memcpy (dest, (void *) &tmp, length);
159 break;
160 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
161 case 10:
163 GFC_REAL_10 tmp = strtold (buffer, NULL);
164 memcpy (dest, (void *) &tmp, length);
166 break;
167 #endif
168 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
169 case 16:
171 GFC_REAL_16 tmp = strtold (buffer, NULL);
172 memcpy (dest, (void *) &tmp, length);
174 break;
175 #endif
176 default:
177 internal_error (&dtp->common, "Unsupported real kind during IO");
180 if (errno != 0 && errno != EINVAL)
182 generate_error (&dtp->common, ERROR_READ_VALUE,
183 "Range error during floating point read");
184 return 1;
187 return 0;
191 /* read_l()-- Read a logical value */
193 void
194 read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
196 char *p;
197 int w;
199 w = f->u.w;
200 p = read_block (dtp, &w);
201 if (p == NULL)
202 return;
204 while (*p == ' ')
206 if (--w == 0)
207 goto bad;
208 p++;
211 if (*p == '.')
213 if (--w == 0)
214 goto bad;
215 p++;
218 switch (*p)
220 case 't':
221 case 'T':
222 set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
223 break;
224 case 'f':
225 case 'F':
226 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
227 break;
228 default:
229 bad:
230 generate_error (&dtp->common, ERROR_READ_VALUE,
231 "Bad value on logical read");
232 break;
237 /* read_a()-- Read a character record. This one is pretty easy. */
239 void
240 read_a (st_parameter_dt *dtp, const fnode *f, char *p, int length)
242 char *source;
243 int w, m, n;
245 w = f->u.w;
246 if (w == -1) /* '(A)' edit descriptor */
247 w = length;
249 dtp->u.p.sf_read_comma = 0;
250 source = read_block (dtp, &w);
251 dtp->u.p.sf_read_comma = 1;
252 if (source == NULL)
253 return;
254 if (w > length)
255 source += (w - length);
257 m = (w > length) ? length : w;
258 memcpy (p, source, m);
260 n = length - w;
261 if (n > 0)
262 memset (p + m, ' ', n);
266 /* eat_leading_spaces()-- Given a character pointer and a width,
267 * ignore the leading spaces. */
269 static char *
270 eat_leading_spaces (int *width, char *p)
272 for (;;)
274 if (*width == 0 || *p != ' ')
275 break;
277 (*width)--;
278 p++;
281 return p;
285 static char
286 next_char (st_parameter_dt *dtp, char **p, int *w)
288 char c, *q;
290 if (*w == 0)
291 return '\0';
293 q = *p;
294 c = *q++;
295 *p = q;
297 (*w)--;
299 if (c != ' ')
300 return c;
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 */
306 while (*w > 0)
308 if (*q++ != ' ')
309 return '?';
310 (*w)--;
313 *p = q;
314 return '\0';
318 /* read_decimal()-- Read a decimal integer value. The values here are
319 * signed values. */
321 void
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;
326 int w, negative;
327 char c, *p;
329 w = f->u.w;
330 p = read_block (dtp, &w);
331 if (p == NULL)
332 return;
334 p = eat_leading_spaces (&w, p);
335 if (w == 0)
337 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
338 return;
341 maxv = max_value (length, 1);
342 maxv_10 = maxv / 10;
344 negative = 0;
345 value = 0;
347 switch (*p)
349 case '-':
350 negative = 1;
351 /* Fall through */
353 case '+':
354 p++;
355 if (--w == 0)
356 goto bad;
357 /* Fall through */
359 default:
360 break;
363 /* At this point we have a digit-string */
364 value = 0;
366 for (;;)
368 c = next_char (dtp, &p, &w);
369 if (c == '\0')
370 break;
372 if (c == ' ')
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')
379 goto bad;
381 if (value > maxv_10)
382 goto overflow;
384 c -= '0';
385 value = 10 * value;
387 if (value > maxv - c)
388 goto overflow;
389 value += c;
392 v = value;
393 if (negative)
394 v = -v;
396 set_integer (dest, v, length);
397 return;
399 bad:
400 generate_error (&dtp->common, ERROR_READ_VALUE,
401 "Bad value during integer read");
402 return;
404 overflow:
405 generate_error (&dtp->common, ERROR_READ_OVERFLOW,
406 "Value overflowed during integer read");
407 return;
411 /* read_radix()-- This function reads values for non-decimal radixes.
412 * The difference here is that we treat the values here as unsigned
413 * values for the purposes of overflow. If minus sign is present and
414 * the top bit is set, the value will be incorrect. */
416 void
417 read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
418 int radix)
420 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
421 GFC_INTEGER_LARGEST v;
422 int w, negative;
423 char c, *p;
425 w = f->u.w;
426 p = read_block (dtp, &w);
427 if (p == NULL)
428 return;
430 p = eat_leading_spaces (&w, p);
431 if (w == 0)
433 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
434 return;
437 maxv = max_value (length, 0);
438 maxv_r = maxv / radix;
440 negative = 0;
441 value = 0;
443 switch (*p)
445 case '-':
446 negative = 1;
447 /* Fall through */
449 case '+':
450 p++;
451 if (--w == 0)
452 goto bad;
453 /* Fall through */
455 default:
456 break;
459 /* At this point we have a digit-string */
460 value = 0;
462 for (;;)
464 c = next_char (dtp, &p, &w);
465 if (c == '\0')
466 break;
467 if (c == ' ')
469 if (dtp->u.p.blank_status == BLANK_NULL) continue;
470 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
473 switch (radix)
475 case 2:
476 if (c < '0' || c > '1')
477 goto bad;
478 break;
480 case 8:
481 if (c < '0' || c > '7')
482 goto bad;
483 break;
485 case 16:
486 switch (c)
488 case '0':
489 case '1':
490 case '2':
491 case '3':
492 case '4':
493 case '5':
494 case '6':
495 case '7':
496 case '8':
497 case '9':
498 break;
500 case 'a':
501 case 'b':
502 case 'c':
503 case 'd':
504 case 'e':
505 case 'f':
506 c = c - 'a' + '9' + 1;
507 break;
509 case 'A':
510 case 'B':
511 case 'C':
512 case 'D':
513 case 'E':
514 case 'F':
515 c = c - 'A' + '9' + 1;
516 break;
518 default:
519 goto bad;
522 break;
525 if (value > maxv_r)
526 goto overflow;
528 c -= '0';
529 value = radix * value;
531 if (maxv - c < value)
532 goto overflow;
533 value += c;
536 v = value;
537 if (negative)
538 v = -v;
540 set_integer (dest, v, length);
541 return;
543 bad:
544 generate_error (&dtp->common, ERROR_READ_VALUE,
545 "Bad value during integer read");
546 return;
548 overflow:
549 generate_error (&dtp->common, ERROR_READ_OVERFLOW,
550 "Value overflowed during integer read");
551 return;
555 /* read_f()-- Read a floating point number with F-style editing, which
556 is what all of the other floating point descriptors behave as. The
557 tricky part is that optional spaces are allowed after an E or D,
558 and the implicit decimal point if a decimal point is not present in
559 the input. */
561 void
562 read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
564 int w, seen_dp, exponent;
565 int exponent_sign, val_sign;
566 int ndigits;
567 int edigits;
568 int i;
569 char *p, *buffer;
570 char *digits;
571 char scratch[SCRATCH_SIZE];
573 val_sign = 1;
574 seen_dp = 0;
575 w = f->u.w;
576 p = read_block (dtp, &w);
577 if (p == NULL)
578 return;
580 p = eat_leading_spaces (&w, p);
581 if (w == 0)
582 goto zero;
584 /* Optional sign */
586 if (*p == '-' || *p == '+')
588 if (*p == '-')
589 val_sign = -1;
590 p++;
591 w--;
594 exponent_sign = 1;
595 p = eat_leading_spaces (&w, p);
596 if (w == 0)
597 goto zero;
599 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
600 is required at this point */
602 if (!isdigit (*p) && *p != '.' && *p != 'd' && *p != 'D'
603 && *p != 'e' && *p != 'E')
604 goto bad_float;
606 /* Remember the position of the first digit. */
607 digits = p;
608 ndigits = 0;
610 /* Scan through the string to find the exponent. */
611 while (w > 0)
613 switch (*p)
615 case '.':
616 if (seen_dp)
617 goto bad_float;
618 seen_dp = 1;
619 /* Fall through */
621 case '0':
622 case '1':
623 case '2':
624 case '3':
625 case '4':
626 case '5':
627 case '6':
628 case '7':
629 case '8':
630 case '9':
631 case ' ':
632 ndigits++;
633 p++;
634 w--;
635 break;
637 case '-':
638 exponent_sign = -1;
639 /* Fall through */
641 case '+':
642 p++;
643 w--;
644 goto exp2;
646 case 'd':
647 case 'e':
648 case 'D':
649 case 'E':
650 p++;
651 w--;
652 goto exp1;
654 default:
655 goto bad_float;
659 /* No exponent has been seen, so we use the current scale factor */
660 exponent = -dtp->u.p.scale_factor;
661 goto done;
663 bad_float:
664 generate_error (&dtp->common, ERROR_READ_VALUE,
665 "Bad value during floating point read");
666 return;
668 /* The value read is zero */
669 zero:
670 switch (length)
672 case 4:
673 *((GFC_REAL_4 *) dest) = 0;
674 break;
676 case 8:
677 *((GFC_REAL_8 *) dest) = 0;
678 break;
680 #ifdef HAVE_GFC_REAL_10
681 case 10:
682 *((GFC_REAL_10 *) dest) = 0;
683 break;
684 #endif
686 #ifdef HAVE_GFC_REAL_16
687 case 16:
688 *((GFC_REAL_16 *) dest) = 0;
689 break;
690 #endif
692 default:
693 internal_error (&dtp->common, "Unsupported real kind during IO");
695 return;
697 /* At this point the start of an exponent has been found */
698 exp1:
699 while (w > 0 && *p == ' ')
701 w--;
702 p++;
705 switch (*p)
707 case '-':
708 exponent_sign = -1;
709 /* Fall through */
711 case '+':
712 p++;
713 w--;
714 break;
717 if (w == 0)
718 goto bad_float;
720 /* At this point a digit string is required. We calculate the value
721 of the exponent in order to take account of the scale factor and
722 the d parameter before explict conversion takes place. */
723 exp2:
724 if (!isdigit (*p))
725 goto bad_float;
727 exponent = *p - '0';
728 p++;
729 w--;
731 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED) /* Normal processing of exponent */
733 while (w > 0 && isdigit (*p))
735 exponent = 10 * exponent + *p - '0';
736 p++;
737 w--;
740 /* Only allow trailing blanks */
742 while (w > 0)
744 if (*p != ' ')
745 goto bad_float;
746 p++;
747 w--;
750 else /* BZ or BN status is enabled */
752 while (w > 0)
754 if (*p == ' ')
756 if (dtp->u.p.blank_status == BLANK_ZERO) *p = '0';
757 if (dtp->u.p.blank_status == BLANK_NULL)
759 p++;
760 w--;
761 continue;
764 else if (!isdigit (*p))
765 goto bad_float;
767 exponent = 10 * exponent + *p - '0';
768 p++;
769 w--;
773 exponent = exponent * exponent_sign;
775 done:
776 /* Use the precision specified in the format if no decimal point has been
777 seen. */
778 if (!seen_dp)
779 exponent -= f->u.real.d;
781 if (exponent > 0)
783 edigits = 2;
784 i = exponent;
786 else
788 edigits = 3;
789 i = -exponent;
792 while (i >= 10)
794 i /= 10;
795 edigits++;
798 i = ndigits + edigits + 1;
799 if (val_sign < 0)
800 i++;
802 if (i < SCRATCH_SIZE)
803 buffer = scratch;
804 else
805 buffer = get_mem (i);
807 /* Reformat the string into a temporary buffer. As we're using atof it's
808 easiest to just leave the decimal point in place. */
809 p = buffer;
810 if (val_sign < 0)
811 *(p++) = '-';
812 for (; ndigits > 0; ndigits--)
814 if (*digits == ' ')
816 if (dtp->u.p.blank_status == BLANK_ZERO) *digits = '0';
817 if (dtp->u.p.blank_status == BLANK_NULL)
819 digits++;
820 continue;
823 *p = *digits;
824 p++;
825 digits++;
827 *(p++) = 'e';
828 sprintf (p, "%d", exponent);
830 /* Do the actual conversion. */
831 convert_real (dtp, dest, buffer, length);
833 if (buffer != scratch)
834 free_mem (buffer);
836 return;
840 /* read_x()-- Deal with the X/TR descriptor. We just read some data
841 * and never look at it. */
843 void
844 read_x (st_parameter_dt *dtp, int n)
846 if (!is_stream_io (dtp))
848 if ((dtp->u.p.current_unit->flags.pad == PAD_NO || is_internal_unit (dtp))
849 && dtp->u.p.current_unit->bytes_left < n)
850 n = dtp->u.p.current_unit->bytes_left;
852 dtp->u.p.sf_read_comma = 0;
853 if (n > 0)
854 read_sf (dtp, &n, 1);
855 dtp->u.p.sf_read_comma = 1;
857 else
858 dtp->u.p.current_unit->strm_pos += (gfc_offset) n;