⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read.c

📁 gcc-fortran,linux使用fortran的编译软件。很好用的。
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.   Contributed by Andy VaughtThis file is part of the GNU Fortran 95 runtime library (libgfortran).Libgfortran is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.In addition to the permissions in the GNU General Public License, theFree Software Foundation gives you unlimited permission to link thecompiled version of this file into combinations with other programs,and to distribute those combinations without any restriction comingfrom the use of this file.  (The General Public License restrictionsdo apply in other respects; for example, they cover modification ofthe file, and distribution when not linked into a combineexecutable.)Libgfortran is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Libgfortran; see the file COPYING.  If not, write tothe Free Software Foundation, 51 Franklin Street, Fifth Floor,Boston, MA 02110-1301, USA.  */#include "config.h"#include <string.h>#include <errno.h>#include <ctype.h>#include <stdlib.h>#include <stdio.h>#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.  */voidset_integer (void *dest, GFC_INTEGER_LARGEST value, int length){  switch (length)    {#ifdef HAVE_GFC_INTEGER_16    case 16:      {	GFC_INTEGER_16 tmp = value;	memcpy (dest, (void *) &tmp, length);      }      break;#endif    case 8:      {	GFC_INTEGER_8 tmp = value;	memcpy (dest, (void *) &tmp, length);      }      break;    case 4:      {	GFC_INTEGER_4 tmp = value;	memcpy (dest, (void *) &tmp, length);      }      break;    case 2:      {	GFC_INTEGER_2 tmp = value;	memcpy (dest, (void *) &tmp, length);      }      break;    case 1:      {	GFC_INTEGER_1 tmp = value;	memcpy (dest, (void *) &tmp, length);      }      break;    default:      internal_error (NULL, "Bad integer kind");    }}/* max_value()-- Given a length (kind), return the maximum signed or * unsigned value */GFC_UINTEGER_LARGESTmax_value (int length, int signed_flag){  GFC_UINTEGER_LARGEST value;  int n;  switch (length)    {#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    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 (NULL, "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 * infinities.  */intconvert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length){  errno = 0;  switch (length)    {    case 4:      {	GFC_REAL_4 tmp =#if defined(HAVE_STRTOF)	  strtof (buffer, NULL);#else	  (GFC_REAL_4) strtod (buffer, NULL);#endif	memcpy (dest, (void *) &tmp, length);      }      break;    case 8:      {	GFC_REAL_8 tmp = strtod (buffer, NULL);	memcpy (dest, (void *) &tmp, length);      }      break;#if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)    case 10:      {	GFC_REAL_10 tmp = strtold (buffer, NULL);	memcpy (dest, (void *) &tmp, length);      }      break;#endif#if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)    case 16:      {	GFC_REAL_16 tmp = strtold (buffer, NULL);	memcpy (dest, (void *) &tmp, length);      }      break;#endif    default:      internal_error (&dtp->common, "Unsupported real kind during IO");    }  if (errno != 0 && errno != EINVAL)    {      generate_error (&dtp->common, ERROR_READ_VALUE,		      "Range error during floating point read");      return 1;    }  return 0;}/* read_l()-- Read a logical value */voidread_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length){  char *p;  int w;  w = f->u.w;  p = read_block (dtp, &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':      set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);      break;    case 'f':    case 'F':      set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);      break;    default:    bad:      generate_error (&dtp->common, ERROR_READ_VALUE,		      "Bad value on logical read");      break;    }}/* read_a()-- Read a character record.  This one is pretty easy. */voidread_a (st_parameter_dt *dtp, const fnode *f, char *p, int length){  char *source;  int w, m, n;  w = f->u.w;  if (w == -1) /* '(A)' edit descriptor  */    w = length;  dtp->u.p.sf_read_comma = 0;  source = read_block (dtp, &w);  dtp->u.p.sf_read_comma = 1;  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 charnext_char (st_parameter_dt *dtp, char **p, int *w){  char c, *q;  if (*w == 0)    return '\0';  q = *p;  c = *q++;  *p = q;  (*w)--;  if (c != ' ')    return c;  if (dtp->u.p.blank_status != BLANK_UNSPECIFIED)    return ' ';  /* return a blank to signal a null */   /* 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. */voidread_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length){  GFC_UINTEGER_LARGEST value, maxv, maxv_10;  GFC_INTEGER_LARGEST v;  int w, negative;  char c, *p;  w = f->u.w;  p = read_block (dtp, &w);  if (p == NULL)    return;  p = eat_leading_spaces (&w, p);  if (w == 0)    {      set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);      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 (dtp, &p, &w);      if (c == '\0')	break;	      if (c == ' ')        {	  if (dtp->u.p.blank_status == BLANK_NULL) continue;	  if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';        }              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;    }  v = value;  if (negative)    v = -v;  set_integer (dest, v, length);  return; bad:  generate_error (&dtp->common, ERROR_READ_VALUE,		  "Bad value during integer read");  return; overflow:  generate_error (&dtp->common, 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. */voidread_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,	    int radix){  GFC_UINTEGER_LARGEST value, maxv, maxv_r;  GFC_INTEGER_LARGEST v;  int w, negative;  char c, *p;  w = f->u.w;  p = read_block (dtp, &w);  if (p == NULL)    return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -