| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Andy Vaught |
| 3 |
F2003 I/O support contributed by Jerry DeLisle |
| 4 |
|
| 5 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 6 |
|
| 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 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
Libgfortran is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
Under Section 7 of GPL version 3, you are granted additional |
| 18 |
permissions described in the GCC Runtime Library Exception, version |
| 19 |
3.1, as published by the Free Software Foundation. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License and |
| 22 |
a copy of the GCC Runtime Library Exception along with this program; |
| 23 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 |
<http://www.gnu.org/licenses/>. */ |
| 25 |
|
| 26 |
#include "io.h" |
| 27 |
#include "fbuf.h" |
| 28 |
#include "format.h" |
| 29 |
#include "unix.h" |
| 30 |
#include <string.h> |
| 31 |
#include <assert.h> |
| 32 |
#include "async.h" |
| 33 |
|
| 34 |
typedef unsigned char uchar; |
| 35 |
|
| 36 |
/* read.c -- Deal with formatted reads */ |
| 37 |
|
| 38 |
|
| 39 |
/* set_integer()-- All of the integer assignments come here to |
| 40 |
actually place the value into memory. */ |
| 41 |
|
| 42 |
void |
| 43 |
set_integer (void *dest, GFC_INTEGER_LARGEST value, int length) |
| 44 |
{ |
| 45 |
NOTE ("set_integer: %lld %p", (long long int) value, dest); |
| 46 |
switch (length) |
| 47 |
{ |
| 48 |
#ifdef HAVE_GFC_INTEGER_16 |
| 49 |
#ifdef HAVE_GFC_REAL_17 |
| 50 |
case 17: |
| 51 |
{ |
| 52 |
GFC_INTEGER_16 tmp = value; |
| 53 |
memcpy (dest, (void *) &tmp, 16); |
| 54 |
} |
| 55 |
break; |
| 56 |
#endif |
| 57 |
/* length=10 comes about for kind=10 real/complex BOZ, see PR41711. */ |
| 58 |
case 10: |
| 59 |
case 16: |
| 60 |
{ |
| 61 |
GFC_INTEGER_16 tmp = value; |
| 62 |
memcpy (dest, (void *) &tmp, length); |
| 63 |
} |
| 64 |
break; |
| 65 |
#endif |
| 66 |
case 8: |
| 67 |
{ |
| 68 |
GFC_INTEGER_8 tmp = value; |
| 69 |
memcpy (dest, (void *) &tmp, length); |
| 70 |
} |
| 71 |
break; |
| 72 |
case 4: |
| 73 |
{ |
| 74 |
GFC_INTEGER_4 tmp = value; |
| 75 |
memcpy (dest, (void *) &tmp, length); |
| 76 |
} |
| 77 |
break; |
| 78 |
case 2: |
| 79 |
{ |
| 80 |
GFC_INTEGER_2 tmp = value; |
| 81 |
memcpy (dest, (void *) &tmp, length); |
| 82 |
} |
| 83 |
break; |
| 84 |
case 1: |
| 85 |
{ |
| 86 |
GFC_INTEGER_1 tmp = value; |
| 87 |
memcpy (dest, (void *) &tmp, length); |
| 88 |
} |
| 89 |
break; |
| 90 |
default: |
| 91 |
internal_error (NULL, "Bad integer kind"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* set_integer()-- All of the integer assignments come here to |
| 96 |
actually place the value into memory. */ |
| 97 |
|
| 98 |
void |
| 99 |
set_unsigned (void *dest, GFC_UINTEGER_LARGEST value, int length) |
| 100 |
{ |
| 101 |
NOTE ("set_integer: %lld %p", (long long int) value, dest); |
| 102 |
switch (length) |
| 103 |
{ |
| 104 |
#ifdef HAVE_GFC_UINTEGER_16 |
| 105 |
#ifdef HAVE_GFC_REAL_17 |
| 106 |
case 17: |
| 107 |
{ |
| 108 |
GFC_UINTEGER_16 tmp = value; |
| 109 |
memcpy (dest, (void *) &tmp, 16); |
| 110 |
} |
| 111 |
break; |
| 112 |
#endif |
| 113 |
/* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */ |
| 114 |
case 10: |
| 115 |
case 16: |
| 116 |
{ |
| 117 |
GFC_UINTEGER_16 tmp = value; |
| 118 |
memcpy (dest, (void *) &tmp, length); |
| 119 |
} |
| 120 |
break; |
| 121 |
#endif |
| 122 |
case 8: |
| 123 |
{ |
| 124 |
GFC_UINTEGER_8 tmp = value; |
| 125 |
memcpy (dest, (void *) &tmp, length); |
| 126 |
} |
| 127 |
break; |
| 128 |
case 4: |
| 129 |
{ |
| 130 |
GFC_UINTEGER_4 tmp = value; |
| 131 |
memcpy (dest, (void *) &tmp, length); |
| 132 |
} |
| 133 |
break; |
| 134 |
case 2: |
| 135 |
{ |
| 136 |
GFC_UINTEGER_2 tmp = value; |
| 137 |
memcpy (dest, (void *) &tmp, length); |
| 138 |
} |
| 139 |
break; |
| 140 |
case 1: |
| 141 |
{ |
| 142 |
GFC_UINTEGER_1 tmp = value; |
| 143 |
memcpy (dest, (void *) &tmp, length); |
| 144 |
} |
| 145 |
break; |
| 146 |
default: |
| 147 |
internal_error (NULL, "Bad integer kind"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/* Max signed value of size give by length argument. */ |
| 153 |
|
| 154 |
GFC_UINTEGER_LARGEST |
| 155 |
si_max (int length) |
| 156 |
{ |
| 157 |
#if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10 |
| 158 |
GFC_UINTEGER_LARGEST value; |
| 159 |
#endif |
| 160 |
|
| 161 |
switch (length) |
| 162 |
{ |
| 163 |
#if defined HAVE_GFC_REAL_17 |
| 164 |
case 17: |
| 165 |
value = 1; |
| 166 |
for (int n = 1; n < 4 * 16; n++) |
| 167 |
value = (value << 2) + 3; |
| 168 |
return value; |
| 169 |
#endif |
| 170 |
#if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10 |
| 171 |
case 16: |
| 172 |
case 10: |
| 173 |
value = 1; |
| 174 |
for (int n = 1; n < 4 * length; n++) |
| 175 |
value = (value << 2) + 3; |
| 176 |
return value; |
| 177 |
#endif |
| 178 |
case 8: |
| 179 |
return GFC_INTEGER_8_HUGE; |
| 180 |
case 4: |
| 181 |
return GFC_INTEGER_4_HUGE; |
| 182 |
case 2: |
| 183 |
return GFC_INTEGER_2_HUGE; |
| 184 |
case 1: |
| 185 |
return GFC_INTEGER_1_HUGE; |
| 186 |
default: |
| 187 |
internal_error (NULL, "Bad integer kind"); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
GFC_UINTEGER_LARGEST |
| 192 |
us_max (int length) |
| 193 |
{ |
| 194 |
switch (length) |
| 195 |
{ |
| 196 |
#ifdef HAVE_GFC_UINTEGER_16 |
| 197 |
case 17: |
| 198 |
case 16: |
| 199 |
return GFC_UINTEGER_16_HUGE; |
| 200 |
#endif |
| 201 |
case 8: |
| 202 |
return GFC_UINTEGER_8_HUGE; |
| 203 |
case 4: |
| 204 |
return GFC_UINTEGER_4_HUGE; |
| 205 |
case 2: |
| 206 |
return GFC_UINTEGER_2_HUGE; |
| 207 |
case 1: |
| 208 |
return GFC_UINTEGER_1_HUGE; |
| 209 |
default: |
| 210 |
internal_error (NULL, "Bad unsigned kind"); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* convert_real()-- Convert a character representation of a floating |
| 215 |
point number to the machine number. Returns nonzero if there is an |
| 216 |
invalid input. Note: many architectures (e.g. IA-64, HP-PA) |
| 217 |
require that the storage pointed to by the dest argument is |
| 218 |
properly aligned for the type in question. */ |
| 219 |
|
| 220 |
int |
| 221 |
convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length) |
| 222 |
{ |
| 223 |
char *endptr = NULL; |
| 224 |
int round_mode, old_round_mode; |
| 225 |
|
| 226 |
switch (dtp->u.p.current_unit->round_status) |
| 227 |
{ |
| 228 |
case ROUND_COMPATIBLE: |
| 229 |
/* FIXME: As NEAREST but round away from zero for a tie. */ |
| 230 |
case ROUND_UNSPECIFIED: |
| 231 |
/* Should not occur. */ |
| 232 |
case ROUND_PROCDEFINED: |
| 233 |
round_mode = ROUND_NEAREST; |
| 234 |
break; |
| 235 |
default: |
| 236 |
round_mode = dtp->u.p.current_unit->round_status; |
| 237 |
break; |
| 238 |
} |
| 239 |
|
| 240 |
old_round_mode = get_fpu_rounding_mode(); |
| 241 |
set_fpu_rounding_mode (round_mode); |
| 242 |
|
| 243 |
switch (length) |
| 244 |
{ |
| 245 |
case 4: |
| 246 |
*((GFC_REAL_4*) dest) = |
| 247 |
#if defined(HAVE_STRTOF) |
| 248 |
gfc_strtof (buffer, &endptr); |
| 249 |
#else |
| 250 |
(GFC_REAL_4) gfc_strtod (buffer, &endptr); |
| 251 |
#endif |
| 252 |
break; |
| 253 |
|
| 254 |
case 8: |
| 255 |
*((GFC_REAL_8*) dest) = gfc_strtod (buffer, &endptr); |
| 256 |
break; |
| 257 |
|
| 258 |
#if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD) |
| 259 |
case 10: |
| 260 |
*((GFC_REAL_10*) dest) = gfc_strtold (buffer, &endptr); |
| 261 |
break; |
| 262 |
#endif |
| 263 |
|
| 264 |
#if defined(HAVE_GFC_REAL_16) |
| 265 |
# if defined(GFC_REAL_16_IS_FLOAT128) |
| 266 |
case 16: |
| 267 |
# if defined(GFC_REAL_16_USE_IEC_60559) |
| 268 |
*((GFC_REAL_16*) dest) = strtof128 (buffer, &endptr); |
| 269 |
# else |
| 270 |
*((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, &endptr); |
| 271 |
# endif |
| 272 |
break; |
| 273 |
# elif defined(HAVE_STRTOLD) |
| 274 |
case 16: |
| 275 |
*((GFC_REAL_16*) dest) = gfc_strtold (buffer, &endptr); |
| 276 |
break; |
| 277 |
# endif |
| 278 |
#endif |
| 279 |
|
| 280 |
#if defined(HAVE_GFC_REAL_17) |
| 281 |
case 17: |
| 282 |
# if defined(POWER_IEEE128) |
| 283 |
*((GFC_REAL_17*) dest) = __strtoieee128 (buffer, &endptr); |
| 284 |
# elif defined(GFC_REAL_17_USE_IEC_60559) |
| 285 |
*((GFC_REAL_17*) dest) = strtof128 (buffer, &endptr); |
| 286 |
# else |
| 287 |
*((GFC_REAL_17*) dest) = __qmath_(strtoflt128) (buffer, &endptr); |
| 288 |
# endif |
| 289 |
break; |
| 290 |
#endif |
| 291 |
|
| 292 |
default: |
| 293 |
internal_error (&dtp->common, "Unsupported real kind during IO"); |
| 294 |
} |
| 295 |
|
| 296 |
set_fpu_rounding_mode (old_round_mode); |
| 297 |
|
| 298 |
if (buffer == endptr) |
| 299 |
{ |
| 300 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 301 |
"Error during floating point read"); |
| 302 |
next_record (dtp, 1); |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
/* convert_infnan()-- Convert character INF/NAN representation to the |
| 310 |
machine number. Note: many architectures (e.g. IA-64, HP-PA) require |
| 311 |
that the storage pointed to by the dest argument is properly aligned |
| 312 |
for the type in question. */ |
| 313 |
|
| 314 |
int |
| 315 |
convert_infnan (st_parameter_dt *dtp, void *dest, const char *buffer, |
| 316 |
int length) |
| 317 |
{ |
| 318 |
const char *s = buffer; |
| 319 |
int is_inf, plus = 1; |
| 320 |
|
| 321 |
if (*s == '+') |
| 322 |
s++; |
| 323 |
else if (*s == '-') |
| 324 |
{ |
| 325 |
s++; |
| 326 |
plus = 0; |
| 327 |
} |
| 328 |
|
| 329 |
is_inf = *s == 'i'; |
| 330 |
|
| 331 |
switch (length) |
| 332 |
{ |
| 333 |
case 4: |
| 334 |
if (is_inf) |
| 335 |
*((GFC_REAL_4*) dest) = plus ? __builtin_inff () : -__builtin_inff (); |
| 336 |
else |
| 337 |
*((GFC_REAL_4*) dest) = plus ? __builtin_nanf ("") : -__builtin_nanf (""); |
| 338 |
break; |
| 339 |
|
| 340 |
case 8: |
| 341 |
if (is_inf) |
| 342 |
*((GFC_REAL_8*) dest) = plus ? __builtin_inf () : -__builtin_inf (); |
| 343 |
else |
| 344 |
*((GFC_REAL_8*) dest) = plus ? __builtin_nan ("") : -__builtin_nan (""); |
| 345 |
break; |
| 346 |
|
| 347 |
#if defined(HAVE_GFC_REAL_10) |
| 348 |
case 10: |
| 349 |
if (is_inf) |
| 350 |
*((GFC_REAL_10*) dest) = plus ? __builtin_infl () : -__builtin_infl (); |
| 351 |
else |
| 352 |
*((GFC_REAL_10*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl (""); |
| 353 |
break; |
| 354 |
#endif |
| 355 |
|
| 356 |
#if defined(HAVE_GFC_REAL_16) |
| 357 |
# if defined(GFC_REAL_16_IS_FLOAT128) |
| 358 |
case 16: |
| 359 |
# if defined(GFC_REAL_16_USE_IEC_60559) |
| 360 |
if (is_inf) |
| 361 |
*((GFC_REAL_16*) dest) = plus ? __builtin_inff128 () : -__builtin_inff128 (); |
| 362 |
else |
| 363 |
*((GFC_REAL_16*) dest) = plus ? __builtin_nanf128 ("") : -__builtin_nanf128 (""); |
| 364 |
# else |
| 365 |
*((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, NULL); |
| 366 |
# endif |
| 367 |
break; |
| 368 |
# else |
| 369 |
case 16: |
| 370 |
if (is_inf) |
| 371 |
*((GFC_REAL_16*) dest) = plus ? __builtin_infl () : -__builtin_infl (); |
| 372 |
else |
| 373 |
*((GFC_REAL_16*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl (""); |
| 374 |
break; |
| 375 |
# endif |
| 376 |
#endif |
| 377 |
|
| 378 |
#if defined(HAVE_GFC_REAL_17) |
| 379 |
case 17: |
| 380 |
if (is_inf) |
| 381 |
*((GFC_REAL_17*) dest) = plus ? __builtin_infl () : -__builtin_infl (); |
| 382 |
else |
| 383 |
*((GFC_REAL_17*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl (""); |
| 384 |
break; |
| 385 |
#endif |
| 386 |
|
| 387 |
default: |
| 388 |
internal_error (&dtp->common, "Unsupported real kind during IO"); |
| 389 |
} |
| 390 |
|
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
/* read_l()-- Read a logical value */ |
| 396 |
|
| 397 |
void |
| 398 |
read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length) |
| 399 |
{ |
| 400 |
char *p; |
| 401 |
size_t w; |
| 402 |
|
| 403 |
w = f->u.w; |
| 404 |
|
| 405 |
p = read_block_form (dtp, &w); |
| 406 |
|
| 407 |
if (p == NULL) |
| 408 |
return; |
| 409 |
|
| 410 |
while (*p == ' ') |
| 411 |
{ |
| 412 |
if (--w == 0) |
| 413 |
goto bad; |
| 414 |
p++; |
| 415 |
} |
| 416 |
|
| 417 |
if (*p == '.') |
| 418 |
{ |
| 419 |
if (--w == 0) |
| 420 |
goto bad; |
| 421 |
p++; |
| 422 |
} |
| 423 |
|
| 424 |
switch (*p) |
| 425 |
{ |
| 426 |
case 't': |
| 427 |
case 'T': |
| 428 |
set_integer (dest, (GFC_INTEGER_LARGEST) 1, length); |
| 429 |
break; |
| 430 |
case 'f': |
| 431 |
case 'F': |
| 432 |
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length); |
| 433 |
break; |
| 434 |
default: |
| 435 |
bad: |
| 436 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 437 |
"Bad value on logical read"); |
| 438 |
next_record (dtp, 1); |
| 439 |
break; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
static gfc_char4_t |
| 445 |
read_utf8 (st_parameter_dt *dtp, size_t *nbytes) |
| 446 |
{ |
| 447 |
static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 }; |
| 448 |
static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; |
| 449 |
size_t nb, nread; |
| 450 |
gfc_char4_t c; |
| 451 |
char *s; |
| 452 |
|
| 453 |
*nbytes = 1; |
| 454 |
|
| 455 |
s = read_block_form (dtp, nbytes); |
| 456 |
if (s == NULL) |
| 457 |
return 0; |
| 458 |
|
| 459 |
/* If this is a short read, just return. */ |
| 460 |
if (*nbytes == 0) |
| 461 |
return 0; |
| 462 |
|
| 463 |
c = (uchar) s[0]; |
| 464 |
if (c < 0x80) |
| 465 |
return c; |
| 466 |
|
| 467 |
/* The number of leading 1-bits in the first byte indicates how many |
| 468 |
bytes follow. */ |
| 469 |
for (nb = 2; nb < 7; nb++) |
| 470 |
if ((c & ~masks[nb-1]) == patns[nb-1]) |
| 471 |
goto found; |
| 472 |
goto invalid; |
| 473 |
|
| 474 |
found: |
| 475 |
c = (c & masks[nb-1]); |
| 476 |
nread = nb - 1; |
| 477 |
|
| 478 |
s = read_block_form (dtp, &nread); |
| 479 |
if (s == NULL) |
| 480 |
return 0; |
| 481 |
/* Decode the bytes read. */ |
| 482 |
for (size_t i = 1; i < nb; i++) |
| 483 |
{ |
| 484 |
gfc_char4_t n = *s++; |
| 485 |
|
| 486 |
if ((n & 0xC0) != 0x80) |
| 487 |
goto invalid; |
| 488 |
|
| 489 |
c = ((c << 6) + (n & 0x3F)); |
| 490 |
} |
| 491 |
|
| 492 |
/* Make sure the shortest possible encoding was used. */ |
| 493 |
if (c <= 0x7F && nb > 1) goto invalid; |
| 494 |
if (c <= 0x7FF && nb > 2) goto invalid; |
| 495 |
if (c <= 0xFFFF && nb > 3) goto invalid; |
| 496 |
if (c <= 0x1FFFFF && nb > 4) goto invalid; |
| 497 |
if (c <= 0x3FFFFFF && nb > 5) goto invalid; |
| 498 |
|
| 499 |
/* Make sure the character is valid. */ |
| 500 |
if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF)) |
| 501 |
goto invalid; |
| 502 |
|
| 503 |
return c; |
| 504 |
|
| 505 |
invalid: |
| 506 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding"); |
| 507 |
return (gfc_char4_t) '?'; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
static void |
| 512 |
read_utf8_char1 (st_parameter_dt *dtp, char *p, size_t len, size_t width) |
| 513 |
{ |
| 514 |
gfc_char4_t c; |
| 515 |
char *dest; |
| 516 |
size_t nbytes, j; |
| 517 |
|
| 518 |
len = (width < len) ? len : width; |
| 519 |
|
| 520 |
dest = (char *) p; |
| 521 |
|
| 522 |
/* Proceed with decoding one character at a time. */ |
| 523 |
for (j = 0; j < len; j++, dest++) |
| 524 |
{ |
| 525 |
c = read_utf8 (dtp, &nbytes); |
| 526 |
|
| 527 |
/* Check for a short read and if so, break out. */ |
| 528 |
if (nbytes == 0) |
| 529 |
break; |
| 530 |
|
| 531 |
*dest = c > 255 ? '?' : (uchar) c; |
| 532 |
} |
| 533 |
|
| 534 |
/* If there was a short read, pad the remaining characters. */ |
| 535 |
for (size_t i = j; i < len; i++) |
| 536 |
*dest++ = ' '; |
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
read_default_char1 (st_parameter_dt *dtp, char *p, size_t len, size_t width) |
| 542 |
{ |
| 543 |
char *s; |
| 544 |
size_t m; |
| 545 |
|
| 546 |
s = read_block_form (dtp, &width); |
| 547 |
|
| 548 |
if (s == NULL) |
| 549 |
return; |
| 550 |
if (width > len) |
| 551 |
s += (width - len); |
| 552 |
|
| 553 |
m = (width > len) ? len : width; |
| 554 |
memcpy (p, s, m); |
| 555 |
|
| 556 |
if (len > width) |
| 557 |
memset (p + m, ' ', len - width); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
static void |
| 562 |
read_utf8_char4 (st_parameter_dt *dtp, void *p, size_t len, size_t width) |
| 563 |
{ |
| 564 |
gfc_char4_t *dest; |
| 565 |
size_t nbytes, j; |
| 566 |
|
| 567 |
len = (width < len) ? len : width; |
| 568 |
|
| 569 |
dest = (gfc_char4_t *) p; |
| 570 |
|
| 571 |
/* Proceed with decoding one character at a time. */ |
| 572 |
for (j = 0; j < len; j++, dest++) |
| 573 |
{ |
| 574 |
*dest = read_utf8 (dtp, &nbytes); |
| 575 |
|
| 576 |
/* Check for a short read and if so, break out. */ |
| 577 |
if (nbytes == 0) |
| 578 |
break; |
| 579 |
} |
| 580 |
|
| 581 |
/* If there was a short read, pad the remaining characters. */ |
| 582 |
for (size_t i = j; i < len; i++) |
| 583 |
*dest++ = (gfc_char4_t) ' '; |
| 584 |
return; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
static void |
| 589 |
read_default_char4 (st_parameter_dt *dtp, char *p, size_t len, size_t width) |
| 590 |
{ |
| 591 |
size_t m, n; |
| 592 |
gfc_char4_t *dest; |
| 593 |
|
| 594 |
if (is_char4_unit(dtp)) |
| 595 |
{ |
| 596 |
gfc_char4_t *s4; |
| 597 |
|
| 598 |
s4 = (gfc_char4_t *) read_block_form4 (dtp, &width); |
| 599 |
|
| 600 |
if (s4 == NULL) |
| 601 |
return; |
| 602 |
if (width > len) |
| 603 |
s4 += (width - len); |
| 604 |
|
| 605 |
m = (width > len) ? len : width; |
| 606 |
|
| 607 |
dest = (gfc_char4_t *) p; |
| 608 |
|
| 609 |
for (n = 0; n < m; n++) |
| 610 |
*dest++ = *s4++; |
| 611 |
|
| 612 |
if (len > width) |
| 613 |
{ |
| 614 |
for (n = 0; n < len - width; n++) |
| 615 |
*dest++ = (gfc_char4_t) ' '; |
| 616 |
} |
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
char *s; |
| 621 |
|
| 622 |
s = read_block_form (dtp, &width); |
| 623 |
|
| 624 |
if (s == NULL) |
| 625 |
return; |
| 626 |
if (width > len) |
| 627 |
s += (width - len); |
| 628 |
|
| 629 |
m = (width > len) ? len : width; |
| 630 |
|
| 631 |
dest = (gfc_char4_t *) p; |
| 632 |
|
| 633 |
for (n = 0; n < m; n++, dest++, s++) |
| 634 |
*dest = (unsigned char ) *s; |
| 635 |
|
| 636 |
if (len > width) |
| 637 |
{ |
| 638 |
for (n = 0; n < len - width; n++, dest++) |
| 639 |
*dest = (unsigned char) ' '; |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
/* read_a()-- Read a character record into a KIND=1 character destination, |
| 646 |
processing UTF-8 encoding if necessary. */ |
| 647 |
|
| 648 |
void |
| 649 |
read_a (st_parameter_dt *dtp, const fnode *f, char *p, size_t length) |
| 650 |
{ |
| 651 |
size_t w; |
| 652 |
|
| 653 |
if (f->u.w == -1) /* '(A)' edit descriptor */ |
| 654 |
w = length; |
| 655 |
else |
| 656 |
w = f->u.w; |
| 657 |
|
| 658 |
/* Read in w characters, treating comma as not a separator. */ |
| 659 |
dtp->u.p.sf_read_comma = 0; |
| 660 |
|
| 661 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 662 |
read_utf8_char1 (dtp, p, length, w); |
| 663 |
else |
| 664 |
read_default_char1 (dtp, p, length, w); |
| 665 |
|
| 666 |
dtp->u.p.sf_read_comma = |
| 667 |
dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
/* read_a_char4()-- Read a character record into a KIND=4 character destination, |
| 672 |
processing UTF-8 encoding if necessary. */ |
| 673 |
|
| 674 |
void |
| 675 |
read_a_char4 (st_parameter_dt *dtp, const fnode *f, char *p, size_t length) |
| 676 |
{ |
| 677 |
size_t w; |
| 678 |
|
| 679 |
if (f->u.w == -1) /* '(A)' edit descriptor */ |
| 680 |
w = length; |
| 681 |
else |
| 682 |
w = f->u.w; |
| 683 |
|
| 684 |
/* Read in w characters, treating comma as not a separator. */ |
| 685 |
dtp->u.p.sf_read_comma = 0; |
| 686 |
|
| 687 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 688 |
read_utf8_char4 (dtp, p, length, w); |
| 689 |
else |
| 690 |
read_default_char4 (dtp, p, length, w); |
| 691 |
|
| 692 |
dtp->u.p.sf_read_comma = |
| 693 |
dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1; |
| 694 |
} |
| 695 |
|
| 696 |
/* eat_leading_spaces()-- Given a character pointer and a width, |
| 697 |
ignore the leading spaces. */ |
| 698 |
|
| 699 |
static char * |
| 700 |
eat_leading_spaces (size_t *width, char *p) |
| 701 |
{ |
| 702 |
for (;;) |
| 703 |
{ |
| 704 |
if (*width == 0 || *p != ' ') |
| 705 |
break; |
| 706 |
|
| 707 |
(*width)--; |
| 708 |
p++; |
| 709 |
} |
| 710 |
|
| 711 |
return p; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
static char |
| 716 |
next_char (st_parameter_dt *dtp, char **p, size_t *w) |
| 717 |
{ |
| 718 |
char c, *q; |
| 719 |
|
| 720 |
if (*w == 0) |
| 721 |
return '\0'; |
| 722 |
|
| 723 |
q = *p; |
| 724 |
c = *q++; |
| 725 |
*p = q; |
| 726 |
|
| 727 |
(*w)--; |
| 728 |
|
| 729 |
if (c != ' ') |
| 730 |
return c; |
| 731 |
if (dtp->u.p.blank_status != BLANK_UNSPECIFIED) |
| 732 |
return ' '; /* return a blank to signal a null */ |
| 733 |
|
| 734 |
/* At this point, the rest of the field has to be trailing blanks */ |
| 735 |
|
| 736 |
while (*w > 0) |
| 737 |
{ |
| 738 |
if (*q++ != ' ') |
| 739 |
return '?'; |
| 740 |
(*w)--; |
| 741 |
} |
| 742 |
|
| 743 |
*p = q; |
| 744 |
return '\0'; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
/* read_decimal()-- Read a decimal integer value. The values here are |
| 749 |
signed values. */ |
| 750 |
|
| 751 |
void |
| 752 |
read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length) |
| 753 |
{ |
| 754 |
GFC_UINTEGER_LARGEST value, maxv, maxv_10; |
| 755 |
GFC_INTEGER_LARGEST v; |
| 756 |
size_t w, padding; |
| 757 |
int negative; |
| 758 |
char c, *p; |
| 759 |
|
| 760 |
w = padding = f->u.w; |
| 761 |
|
| 762 |
/* This is a legacy extension, and the frontend will only allow such cases |
| 763 |
* through when -fdec-format-defaults is passed. |
| 764 |
*/ |
| 765 |
if (w == (size_t) DEFAULT_WIDTH) |
| 766 |
w = default_width_for_integer (length); |
| 767 |
|
| 768 |
p = read_block_form (dtp, &w); |
| 769 |
|
| 770 |
if (p == NULL) |
| 771 |
return; |
| 772 |
|
| 773 |
/* If the read was not the full width we may need to pad with blanks or zeros |
| 774 |
* depending on the PAD mode. Save the number of pad characters needed. */ |
| 775 |
padding -= w; |
| 776 |
|
| 777 |
p = eat_leading_spaces (&w, p); |
| 778 |
if (w == 0) |
| 779 |
{ |
| 780 |
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length); |
| 781 |
return; |
| 782 |
} |
| 783 |
|
| 784 |
negative = 0; |
| 785 |
|
| 786 |
switch (*p) |
| 787 |
{ |
| 788 |
case '-': |
| 789 |
negative = 1; |
| 790 |
/* Fall through */ |
| 791 |
|
| 792 |
case '+': |
| 793 |
p++; |
| 794 |
if (--w == 0) |
| 795 |
goto bad; |
| 796 |
/* Fall through */ |
| 797 |
|
| 798 |
default: |
| 799 |
break; |
| 800 |
} |
| 801 |
|
| 802 |
maxv = si_max (length); |
| 803 |
if (negative) |
| 804 |
maxv++; |
| 805 |
maxv_10 = maxv / 10; |
| 806 |
|
| 807 |
/* At this point we have a digit-string */ |
| 808 |
value = 0; |
| 809 |
|
| 810 |
for (;;) |
| 811 |
{ |
| 812 |
c = next_char (dtp, &p, &w); |
| 813 |
if (c == '\0') |
| 814 |
{ |
| 815 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 816 |
{ |
| 817 |
for (size_t n = 0; n < padding; n++) |
| 818 |
value = 10 * value; |
| 819 |
} |
| 820 |
break; |
| 821 |
} |
| 822 |
|
| 823 |
if (c == ' ') |
| 824 |
{ |
| 825 |
if (dtp->u.p.blank_status == BLANK_NULL) |
| 826 |
{ |
| 827 |
/* Skip spaces. */ |
| 828 |
for ( ; w > 0; p++, w--) |
| 829 |
if (*p != ' ') break; |
| 830 |
continue; |
| 831 |
} |
| 832 |
if (dtp->u.p.blank_status == BLANK_ZERO) c = '0'; |
| 833 |
} |
| 834 |
|
| 835 |
if (c < '0' || c > '9') |
| 836 |
goto bad; |
| 837 |
|
| 838 |
if (value > maxv_10) |
| 839 |
goto overflow; |
| 840 |
|
| 841 |
c -= '0'; |
| 842 |
value = 10 * value; |
| 843 |
|
| 844 |
if (value > maxv - c) |
| 845 |
goto overflow; |
| 846 |
value += c; |
| 847 |
} |
| 848 |
|
| 849 |
if (negative) |
| 850 |
v = -value; |
| 851 |
else |
| 852 |
v = value; |
| 853 |
|
| 854 |
set_integer (dest, v, length); |
| 855 |
return; |
| 856 |
|
| 857 |
bad: |
| 858 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 859 |
"Bad value during integer read"); |
| 860 |
next_record (dtp, 1); |
| 861 |
return; |
| 862 |
|
| 863 |
overflow: |
| 864 |
generate_error (&dtp->common, LIBERROR_READ_OVERFLOW, |
| 865 |
"Value overflowed during integer read"); |
| 866 |
next_record (dtp, 1); |
| 867 |
|
| 868 |
} |
| 869 |
|
| 870 |
/* read_decimal_unsigned() - almost the same as above. Checks for sign |
| 871 |
and overflow are performed with -pedantic. */ |
| 872 |
|
| 873 |
void |
| 874 |
read_decimal_unsigned (st_parameter_dt *dtp, const fnode *f, char *dest, |
| 875 |
int length) |
| 876 |
{ |
| 877 |
GFC_UINTEGER_LARGEST value, old_value; |
| 878 |
size_t w, padding; |
| 879 |
int negative; |
| 880 |
char c, *p; |
| 881 |
|
| 882 |
w = padding = f->u.w; |
| 883 |
|
| 884 |
/* This is a legacy extension, and the frontend will only allow such cases |
| 885 |
* through when -fdec-format-defaults is passed. |
| 886 |
*/ |
| 887 |
if (w == (size_t) DEFAULT_WIDTH) |
| 888 |
w = default_width_for_integer (length); |
| 889 |
|
| 890 |
p = read_block_form (dtp, &w); |
| 891 |
|
| 892 |
if (p == NULL) |
| 893 |
return; |
| 894 |
|
| 895 |
/* If the read was not the full width we may need to pad with blanks or zeros |
| 896 |
* depending on the PAD mode. Save the number of pad characters needed. */ |
| 897 |
padding -= w; |
| 898 |
|
| 899 |
p = eat_leading_spaces (&w, p); |
| 900 |
if (w == 0) |
| 901 |
{ |
| 902 |
set_unsigned (dest, (GFC_UINTEGER_LARGEST) 0, length); |
| 903 |
return; |
| 904 |
} |
| 905 |
|
| 906 |
negative = 0; |
| 907 |
|
| 908 |
switch (*p) |
| 909 |
{ |
| 910 |
case '-': |
| 911 |
if (compile_options.pedantic) |
| 912 |
goto no_sign; |
| 913 |
|
| 914 |
negative = 1; |
| 915 |
|
| 916 |
/* Fall through. */ |
| 917 |
|
| 918 |
case '+': |
| 919 |
p++; |
| 920 |
if (--w == 0) |
| 921 |
goto bad; |
| 922 |
/* Fall through. */ |
| 923 |
|
| 924 |
default: |
| 925 |
break; |
| 926 |
} |
| 927 |
|
| 928 |
/* At this point we have a digit-string. */ |
| 929 |
value = 0; |
| 930 |
|
| 931 |
for (;;) |
| 932 |
{ |
| 933 |
c = next_char (dtp, &p, &w); |
| 934 |
if (c == '\0') |
| 935 |
{ |
| 936 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 937 |
{ |
| 938 |
for (size_t n = 0; n < padding; n++) |
| 939 |
value = 10 * value; |
| 940 |
} |
| 941 |
break; |
| 942 |
} |
| 943 |
|
| 944 |
if (c == ' ') |
| 945 |
{ |
| 946 |
if (dtp->u.p.blank_status == BLANK_NULL) |
| 947 |
{ |
| 948 |
/* Skip spaces. */ |
| 949 |
for ( ; w > 0; p++, w--) |
| 950 |
if (*p != ' ') break; |
| 951 |
continue; |
| 952 |
} |
| 953 |
if (dtp->u.p.blank_status == BLANK_ZERO) c = '0'; |
| 954 |
} |
| 955 |
|
| 956 |
if (c < '0' || c > '9') |
| 957 |
goto bad; |
| 958 |
|
| 959 |
c -= '0'; |
| 960 |
old_value = value; |
| 961 |
value = 10 * value + c; |
| 962 |
if (compile_options.pedantic && value < old_value) |
| 963 |
goto overflow; |
| 964 |
} |
| 965 |
|
| 966 |
if (negative) |
| 967 |
value = -value; |
| 968 |
|
| 969 |
if (compile_options.pedantic && value > us_max (length)) |
| 970 |
goto overflow; |
| 971 |
|
| 972 |
set_unsigned (dest, value, length); |
| 973 |
return; |
| 974 |
|
| 975 |
bad: |
| 976 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 977 |
"Bad value during unsigned integer read"); |
| 978 |
next_record (dtp, 1); |
| 979 |
return; |
| 980 |
|
| 981 |
no_sign: |
| 982 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 983 |
"Negative sign for unsigned integer read"); |
| 984 |
next_record (dtp, 1); |
| 985 |
return; |
| 986 |
|
| 987 |
overflow: |
| 988 |
generate_error (&dtp->common, LIBERROR_READ_OVERFLOW, |
| 989 |
"Value overflowed during unsigned integer read"); |
| 990 |
next_record (dtp, 1); |
| 991 |
|
| 992 |
} |
| 993 |
|
| 994 |
|
| 995 |
/* read_radix()-- This function reads values for non-decimal radixes. |
| 996 |
The difference here is that we treat the values here as unsigned |
| 997 |
values for the purposes of overflow. If minus sign is present and |
| 998 |
the top bit is set, the value will be incorrect. */ |
| 999 |
|
| 1000 |
void |
| 1001 |
read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length, |
| 1002 |
int radix) |
| 1003 |
{ |
| 1004 |
GFC_UINTEGER_LARGEST value, maxv, maxv_r; |
| 1005 |
GFC_INTEGER_LARGEST v; |
| 1006 |
size_t w, padding; |
| 1007 |
int negative; |
| 1008 |
char c, *p; |
| 1009 |
|
| 1010 |
w = padding = f->u.w; |
| 1011 |
|
| 1012 |
p = read_block_form (dtp, &w); |
| 1013 |
|
| 1014 |
if (p == NULL) |
| 1015 |
return; |
| 1016 |
|
| 1017 |
/* If the read was not the full width we may need to pad with blanks or zeros |
| 1018 |
* depending on the PAD mode. Save the number of pad characters needed. */ |
| 1019 |
padding -= w; |
| 1020 |
|
| 1021 |
p = eat_leading_spaces (&w, p); |
| 1022 |
if (w == 0) |
| 1023 |
{ |
| 1024 |
set_integer (dest, (GFC_INTEGER_LARGEST) 0, length); |
| 1025 |
return; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/* Maximum unsigned value, assuming two's complement. */ |
| 1029 |
maxv = 2 * si_max (length) + 1; |
| 1030 |
maxv_r = maxv / radix; |
| 1031 |
|
| 1032 |
negative = 0; |
| 1033 |
value = 0; |
| 1034 |
|
| 1035 |
switch (*p) |
| 1036 |
{ |
| 1037 |
case '-': |
| 1038 |
negative = 1; |
| 1039 |
/* Fall through */ |
| 1040 |
|
| 1041 |
case '+': |
| 1042 |
p++; |
| 1043 |
if (--w == 0) |
| 1044 |
goto bad; |
| 1045 |
/* Fall through */ |
| 1046 |
|
| 1047 |
default: |
| 1048 |
break; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/* At this point we have a digit-string */ |
| 1052 |
value = 0; |
| 1053 |
|
| 1054 |
for (;;) |
| 1055 |
{ |
| 1056 |
c = next_char (dtp, &p, &w); |
| 1057 |
if (c == '\0') |
| 1058 |
{ |
| 1059 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 1060 |
{ |
| 1061 |
for (size_t n = 0; n < padding; n++) |
| 1062 |
value = radix * value; |
| 1063 |
} |
| 1064 |
break; |
| 1065 |
} |
| 1066 |
if (c == ' ') |
| 1067 |
{ |
| 1068 |
if (dtp->u.p.blank_status == BLANK_NULL) continue; |
| 1069 |
if (dtp->u.p.blank_status == BLANK_ZERO) c = '0'; |
| 1070 |
} |
| 1071 |
|
| 1072 |
switch (radix) |
| 1073 |
{ |
| 1074 |
case 2: |
| 1075 |
if (c < '0' || c > '1') |
| 1076 |
goto bad; |
| 1077 |
break; |
| 1078 |
|
| 1079 |
case 8: |
| 1080 |
if (c < '0' || c > '7') |
| 1081 |
goto bad; |
| 1082 |
break; |
| 1083 |
|
| 1084 |
case 16: |
| 1085 |
switch (c) |
| 1086 |
{ |
| 1087 |
case '0': |
| 1088 |
case '1': |
| 1089 |
case '2': |
| 1090 |
case '3': |
| 1091 |
case '4': |
| 1092 |
case '5': |
| 1093 |
case '6': |
| 1094 |
case '7': |
| 1095 |
case '8': |
| 1096 |
case '9': |
| 1097 |
break; |
| 1098 |
|
| 1099 |
case 'a': |
| 1100 |
case 'b': |
| 1101 |
case 'c': |
| 1102 |
case 'd': |
| 1103 |
case 'e': |
| 1104 |
case 'f': |
| 1105 |
c = c - 'a' + '9' + 1; |
| 1106 |
break; |
| 1107 |
|
| 1108 |
case 'A': |
| 1109 |
case 'B': |
| 1110 |
case 'C': |
| 1111 |
case 'D': |
| 1112 |
case 'E': |
| 1113 |
case 'F': |
| 1114 |
c = c - 'A' + '9' + 1; |
| 1115 |
break; |
| 1116 |
|
| 1117 |
default: |
| 1118 |
goto bad; |
| 1119 |
} |
| 1120 |
|
| 1121 |
break; |
| 1122 |
} |
| 1123 |
|
| 1124 |
if (value > maxv_r) |
| 1125 |
goto overflow; |
| 1126 |
|
| 1127 |
c -= '0'; |
| 1128 |
value = radix * value; |
| 1129 |
|
| 1130 |
if (maxv - c < value) |
| 1131 |
goto overflow; |
| 1132 |
value += c; |
| 1133 |
} |
| 1134 |
|
| 1135 |
v = value; |
| 1136 |
if (negative) |
| 1137 |
v = -v; |
| 1138 |
|
| 1139 |
set_integer (dest, v, length); |
| 1140 |
return; |
| 1141 |
|
| 1142 |
bad: |
| 1143 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 1144 |
"Bad value during integer read"); |
| 1145 |
next_record (dtp, 1); |
| 1146 |
return; |
| 1147 |
|
| 1148 |
overflow: |
| 1149 |
generate_error (&dtp->common, LIBERROR_READ_OVERFLOW, |
| 1150 |
"Value overflowed during integer read"); |
| 1151 |
next_record (dtp, 1); |
| 1152 |
|
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
/* read_f()-- Read a floating point number with F-style editing, which |
| 1157 |
is what all of the other floating point descriptors behave as. The |
| 1158 |
tricky part is that optional spaces are allowed after an E or D, |
| 1159 |
and the implicit decimal point if a decimal point is not present in |
| 1160 |
the input. */ |
| 1161 |
|
| 1162 |
void |
| 1163 |
read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length) |
| 1164 |
{ |
| 1165 |
#define READF_TMP 50 |
| 1166 |
char tmp[READF_TMP]; |
| 1167 |
size_t buf_size = 0; |
| 1168 |
size_t w; |
| 1169 |
int seen_dp, exponent; |
| 1170 |
int exponent_sign; |
| 1171 |
const char *p; |
| 1172 |
char *buffer; |
| 1173 |
char *out; |
| 1174 |
int seen_int_digit; /* Seen a digit before the decimal point? */ |
| 1175 |
int seen_dec_digit; /* Seen a digit after the decimal point? */ |
| 1176 |
|
| 1177 |
seen_dp = 0; |
| 1178 |
seen_int_digit = 0; |
| 1179 |
seen_dec_digit = 0; |
| 1180 |
exponent_sign = 1; |
| 1181 |
exponent = 0; |
| 1182 |
w = f->u.w; |
| 1183 |
buffer = tmp; |
| 1184 |
|
| 1185 |
/* Read in the next block. */ |
| 1186 |
p = read_block_form (dtp, &w); |
| 1187 |
if (p == NULL) |
| 1188 |
return; |
| 1189 |
p = eat_leading_spaces (&w, (char*) p); |
| 1190 |
if (w == 0) |
| 1191 |
goto zero; |
| 1192 |
|
| 1193 |
/* In this buffer we're going to re-format the number cleanly to be parsed |
| 1194 |
by convert_real in the end; this assures we're using strtod from the |
| 1195 |
C library for parsing and thus probably get the best accuracy possible. |
| 1196 |
This process may add a '+0.0' in front of the number as well as change the |
| 1197 |
exponent because of an implicit decimal point or the like. Thus allocating |
| 1198 |
strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the |
| 1199 |
original buffer had should be enough. */ |
| 1200 |
buf_size = w + 11; |
| 1201 |
if (buf_size > READF_TMP) |
| 1202 |
buffer = xmalloc (buf_size); |
| 1203 |
|
| 1204 |
out = buffer; |
| 1205 |
|
| 1206 |
/* Optional sign */ |
| 1207 |
if (*p == '-' || *p == '+') |
| 1208 |
{ |
| 1209 |
if (*p == '-') |
| 1210 |
*(out++) = '-'; |
| 1211 |
++p; |
| 1212 |
--w; |
| 1213 |
} |
| 1214 |
|
| 1215 |
p = eat_leading_spaces (&w, (char*) p); |
| 1216 |
if (w == 0) |
| 1217 |
goto zero; |
| 1218 |
|
| 1219 |
/* Check for Infinity or NaN. */ |
| 1220 |
if (unlikely ((w >= 3 && (*p == 'i' || *p == 'I' || *p == 'n' || *p == 'N')))) |
| 1221 |
{ |
| 1222 |
int seen_paren = 0; |
| 1223 |
char *save = out; |
| 1224 |
|
| 1225 |
/* Scan through the buffer keeping track of spaces and parenthesis. We |
| 1226 |
null terminate the string as soon as we see a left paren or if we are |
| 1227 |
BLANK_NULL mode. Leading spaces have already been skipped above, |
| 1228 |
trailing spaces are ignored by converting to '\0'. A space |
| 1229 |
between "NaN" and the optional perenthesis is not permitted. */ |
| 1230 |
while (w > 0) |
| 1231 |
{ |
| 1232 |
*out = safe_tolower (*p); |
| 1233 |
switch (*p) |
| 1234 |
{ |
| 1235 |
case ' ': |
| 1236 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 1237 |
{ |
| 1238 |
*out = '0'; |
| 1239 |
break; |
| 1240 |
} |
| 1241 |
*out = '\0'; |
| 1242 |
if (seen_paren == 1) |
| 1243 |
goto bad_float; |
| 1244 |
break; |
| 1245 |
case '(': |
| 1246 |
seen_paren++; |
| 1247 |
*out = '\0'; |
| 1248 |
break; |
| 1249 |
case ')': |
| 1250 |
if (seen_paren++ != 1) |
| 1251 |
goto bad_float; |
| 1252 |
break; |
| 1253 |
default: |
| 1254 |
if (!safe_isalnum (*out)) |
| 1255 |
goto bad_float; |
| 1256 |
} |
| 1257 |
--w; |
| 1258 |
++p; |
| 1259 |
++out; |
| 1260 |
} |
| 1261 |
|
| 1262 |
*out = '\0'; |
| 1263 |
|
| 1264 |
if (seen_paren != 0 && seen_paren != 2) |
| 1265 |
goto bad_float; |
| 1266 |
|
| 1267 |
if ((strcmp (save, "inf") == 0) || (strcmp (save, "infinity") == 0)) |
| 1268 |
{ |
| 1269 |
if (seen_paren) |
| 1270 |
goto bad_float; |
| 1271 |
} |
| 1272 |
else if (strcmp (save, "nan") != 0) |
| 1273 |
goto bad_float; |
| 1274 |
|
| 1275 |
convert_infnan (dtp, dest, buffer, length); |
| 1276 |
if (buf_size > READF_TMP) |
| 1277 |
free (buffer); |
| 1278 |
return; |
| 1279 |
} |
| 1280 |
|
| 1281 |
/* Process the mantissa string. */ |
| 1282 |
while (w > 0) |
| 1283 |
{ |
| 1284 |
switch (*p) |
| 1285 |
{ |
| 1286 |
case ',': |
| 1287 |
if (dtp->u.p.current_unit->decimal_status != DECIMAL_COMMA) |
| 1288 |
goto bad_float; |
| 1289 |
if (seen_dp) |
| 1290 |
goto bad_float; |
| 1291 |
if (!seen_int_digit) |
| 1292 |
*(out++) = '0'; |
| 1293 |
*(out++) = '.'; |
| 1294 |
seen_dp = 1; |
| 1295 |
break; |
| 1296 |
|
| 1297 |
case '.': |
| 1298 |
if (dtp->u.p.current_unit->decimal_status != DECIMAL_POINT) |
| 1299 |
goto bad_float; |
| 1300 |
if (seen_dp) |
| 1301 |
goto bad_float; |
| 1302 |
if (!seen_int_digit) |
| 1303 |
*(out++) = '0'; |
| 1304 |
*(out++) = '.'; |
| 1305 |
seen_dp = 1; |
| 1306 |
break; |
| 1307 |
|
| 1308 |
case ' ': |
| 1309 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 1310 |
{ |
| 1311 |
*(out++) = '0'; |
| 1312 |
goto found_digit; |
| 1313 |
} |
| 1314 |
else if (dtp->u.p.blank_status == BLANK_NULL) |
| 1315 |
break; |
| 1316 |
else |
| 1317 |
/* TODO: Should we check instead that there are only trailing |
| 1318 |
blanks here, as is done below for exponents? */ |
| 1319 |
goto done; |
| 1320 |
/* Fall through. */ |
| 1321 |
case '0': |
| 1322 |
case '1': |
| 1323 |
case '2': |
| 1324 |
case '3': |
| 1325 |
case '4': |
| 1326 |
case '5': |
| 1327 |
case '6': |
| 1328 |
case '7': |
| 1329 |
case '8': |
| 1330 |
case '9': |
| 1331 |
*(out++) = *p; |
| 1332 |
found_digit: |
| 1333 |
if (!seen_dp) |
| 1334 |
seen_int_digit = 1; |
| 1335 |
else |
| 1336 |
seen_dec_digit = 1; |
| 1337 |
break; |
| 1338 |
|
| 1339 |
case '-': |
| 1340 |
case '+': |
| 1341 |
goto exponent; |
| 1342 |
|
| 1343 |
case 'e': |
| 1344 |
case 'E': |
| 1345 |
case 'd': |
| 1346 |
case 'D': |
| 1347 |
case 'q': |
| 1348 |
case 'Q': |
| 1349 |
++p; |
| 1350 |
--w; |
| 1351 |
goto exponent; |
| 1352 |
|
| 1353 |
default: |
| 1354 |
goto bad_float; |
| 1355 |
} |
| 1356 |
|
| 1357 |
++p; |
| 1358 |
--w; |
| 1359 |
} |
| 1360 |
|
| 1361 |
/* No exponent has been seen, so we use the current scale factor. */ |
| 1362 |
exponent = - dtp->u.p.scale_factor; |
| 1363 |
goto done; |
| 1364 |
|
| 1365 |
/* At this point the start of an exponent has been found. */ |
| 1366 |
exponent: |
| 1367 |
p = eat_leading_spaces (&w, (char*) p); |
| 1368 |
if (*p == '-' || *p == '+') |
| 1369 |
{ |
| 1370 |
if (*p == '-') |
| 1371 |
exponent_sign = -1; |
| 1372 |
++p; |
| 1373 |
--w; |
| 1374 |
} |
| 1375 |
|
| 1376 |
/* At this point a digit string is required. We calculate the value |
| 1377 |
of the exponent in order to take account of the scale factor and |
| 1378 |
the d parameter before explict conversion takes place. */ |
| 1379 |
|
| 1380 |
if (w == 0) |
| 1381 |
{ |
| 1382 |
/* Extension: allow default exponent of 0 when omitted. */ |
| 1383 |
if (dtp->common.flags & IOPARM_DT_DEC_EXT) |
| 1384 |
goto done; |
| 1385 |
else |
| 1386 |
goto bad_float; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (dtp->u.p.blank_status == BLANK_UNSPECIFIED) |
| 1390 |
{ |
| 1391 |
while (w > 0 && safe_isdigit (*p)) |
| 1392 |
{ |
| 1393 |
exponent *= 10; |
| 1394 |
exponent += *p - '0'; |
| 1395 |
++p; |
| 1396 |
--w; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* Only allow trailing blanks. */ |
| 1400 |
while (w > 0) |
| 1401 |
{ |
| 1402 |
if (*p != ' ') |
| 1403 |
goto bad_float; |
| 1404 |
++p; |
| 1405 |
--w; |
| 1406 |
} |
| 1407 |
} |
| 1408 |
else /* BZ or BN status is enabled. */ |
| 1409 |
{ |
| 1410 |
while (w > 0) |
| 1411 |
{ |
| 1412 |
if (*p == ' ') |
| 1413 |
{ |
| 1414 |
if (dtp->u.p.blank_status == BLANK_ZERO) |
| 1415 |
exponent *= 10; |
| 1416 |
else |
| 1417 |
assert (dtp->u.p.blank_status == BLANK_NULL); |
| 1418 |
} |
| 1419 |
else if (!safe_isdigit (*p)) |
| 1420 |
goto bad_float; |
| 1421 |
else |
| 1422 |
{ |
| 1423 |
exponent *= 10; |
| 1424 |
exponent += *p - '0'; |
| 1425 |
} |
| 1426 |
|
| 1427 |
++p; |
| 1428 |
--w; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
exponent *= exponent_sign; |
| 1433 |
|
| 1434 |
done: |
| 1435 |
/* Use the precision specified in the format if no decimal point has been |
| 1436 |
seen. */ |
| 1437 |
if (!seen_dp) |
| 1438 |
exponent -= f->u.real.d; |
| 1439 |
|
| 1440 |
/* Output a trailing '0' after decimal point if not yet found. */ |
| 1441 |
if (seen_dp && !seen_dec_digit) |
| 1442 |
*(out++) = '0'; |
| 1443 |
/* Handle input of style "E+NN" by inserting a 0 for the |
| 1444 |
significand. */ |
| 1445 |
else if (!seen_int_digit && !seen_dec_digit) |
| 1446 |
{ |
| 1447 |
notify_std (&dtp->common, GFC_STD_LEGACY, |
| 1448 |
"REAL input of style 'E+NN'"); |
| 1449 |
*(out++) = '0'; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* Print out the exponent to finish the reformatted number. Maximum 4 |
| 1453 |
digits for the exponent. */ |
| 1454 |
if (exponent != 0) |
| 1455 |
{ |
| 1456 |
int dig; |
| 1457 |
|
| 1458 |
*(out++) = 'e'; |
| 1459 |
if (exponent < 0) |
| 1460 |
{ |
| 1461 |
*(out++) = '-'; |
| 1462 |
exponent = - exponent; |
| 1463 |
} |
| 1464 |
|
| 1465 |
if (exponent >= 10000) |
| 1466 |
goto bad_float; |
| 1467 |
|
| 1468 |
for (dig = 3; dig >= 0; --dig) |
| 1469 |
{ |
| 1470 |
out[dig] = (char) ('0' + exponent % 10); |
| 1471 |
exponent /= 10; |
| 1472 |
} |
| 1473 |
out += 4; |
| 1474 |
} |
| 1475 |
*(out++) = '\0'; |
| 1476 |
|
| 1477 |
/* Do the actual conversion. */ |
| 1478 |
convert_real (dtp, dest, buffer, length); |
| 1479 |
if (buf_size > READF_TMP) |
| 1480 |
free (buffer); |
| 1481 |
return; |
| 1482 |
|
| 1483 |
/* The value read is zero. */ |
| 1484 |
zero: |
| 1485 |
switch (length) |
| 1486 |
{ |
| 1487 |
case 4: |
| 1488 |
*((GFC_REAL_4 *) dest) = 0.0; |
| 1489 |
break; |
| 1490 |
|
| 1491 |
case 8: |
| 1492 |
*((GFC_REAL_8 *) dest) = 0.0; |
| 1493 |
break; |
| 1494 |
|
| 1495 |
#ifdef HAVE_GFC_REAL_10 |
| 1496 |
case 10: |
| 1497 |
*((GFC_REAL_10 *) dest) = 0.0; |
| 1498 |
break; |
| 1499 |
#endif |
| 1500 |
|
| 1501 |
#ifdef HAVE_GFC_REAL_16 |
| 1502 |
case 16: |
| 1503 |
*((GFC_REAL_16 *) dest) = 0.0; |
| 1504 |
break; |
| 1505 |
#endif |
| 1506 |
|
| 1507 |
#ifdef HAVE_GFC_REAL_17 |
| 1508 |
case 17: |
| 1509 |
*((GFC_REAL_17 *) dest) = 0.0; |
| 1510 |
break; |
| 1511 |
#endif |
| 1512 |
|
| 1513 |
default: |
| 1514 |
internal_error (&dtp->common, "Unsupported real kind during IO"); |
| 1515 |
} |
| 1516 |
return; |
| 1517 |
|
| 1518 |
bad_float: |
| 1519 |
if (buf_size > READF_TMP) |
| 1520 |
free (buffer); |
| 1521 |
generate_error (&dtp->common, LIBERROR_READ_VALUE, |
| 1522 |
"Bad value during floating point read"); |
| 1523 |
next_record (dtp, 1); |
| 1524 |
return; |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
/* read_x()-- Deal with the X/TR descriptor. We just read some data |
| 1529 |
and never look at it. */ |
| 1530 |
|
| 1531 |
void |
| 1532 |
read_x (st_parameter_dt *dtp, size_t n) |
| 1533 |
{ |
| 1534 |
size_t length; |
| 1535 |
int q, q2; |
| 1536 |
|
| 1537 |
if ((dtp->u.p.current_unit->pad_status == PAD_NO || is_internal_unit (dtp)) |
| 1538 |
&& dtp->u.p.current_unit->bytes_left < (gfc_offset) n) |
| 1539 |
n = dtp->u.p.current_unit->bytes_left; |
| 1540 |
|
| 1541 |
if (n == 0) |
| 1542 |
return; |
| 1543 |
|
| 1544 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 1545 |
{ |
| 1546 |
gfc_char4_t c; |
| 1547 |
size_t nbytes, j; |
| 1548 |
|
| 1549 |
/* Proceed with decoding one character at a time. */ |
| 1550 |
for (j = 0; j < n; j++) |
| 1551 |
{ |
| 1552 |
c = read_utf8 (dtp, &nbytes); |
| 1553 |
|
| 1554 |
/* Check for a short read and if so, break out. */ |
| 1555 |
if (nbytes == 0 || c == (gfc_char4_t)0) |
| 1556 |
break; |
| 1557 |
} |
| 1558 |
return; |
| 1559 |
} |
| 1560 |
|
| 1561 |
length = n; |
| 1562 |
|
| 1563 |
if (is_internal_unit (dtp)) |
| 1564 |
{ |
| 1565 |
mem_alloc_r (dtp->u.p.current_unit->s, &length); |
| 1566 |
if (unlikely (length < n)) |
| 1567 |
n = length; |
| 1568 |
goto done; |
| 1569 |
} |
| 1570 |
|
| 1571 |
if (dtp->u.p.sf_seen_eor) |
| 1572 |
return; |
| 1573 |
|
| 1574 |
n = 0; |
| 1575 |
while (n < length) |
| 1576 |
{ |
| 1577 |
q = fbuf_getc (dtp->u.p.current_unit); |
| 1578 |
if (q == EOF) |
| 1579 |
break; |
| 1580 |
else if (dtp->u.p.current_unit->flags.cc != CC_NONE |
| 1581 |
&& (q == '\n' || q == '\r')) |
| 1582 |
{ |
| 1583 |
/* Unexpected end of line. Set the position. */ |
| 1584 |
dtp->u.p.sf_seen_eor = 1; |
| 1585 |
|
| 1586 |
/* If we see an EOR during non-advancing I/O, we need to skip |
| 1587 |
the rest of the I/O statement. Set the corresponding flag. */ |
| 1588 |
if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar) |
| 1589 |
dtp->u.p.eor_condition = 1; |
| 1590 |
|
| 1591 |
/* If we encounter a CR, it might be a CRLF. */ |
| 1592 |
if (q == '\r') /* Probably a CRLF */ |
| 1593 |
{ |
| 1594 |
/* See if there is an LF. */ |
| 1595 |
q2 = fbuf_getc (dtp->u.p.current_unit); |
| 1596 |
if (q2 == '\n') |
| 1597 |
dtp->u.p.sf_seen_eor = 2; |
| 1598 |
else if (q2 != EOF) /* Oops, seek back. */ |
| 1599 |
fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR); |
| 1600 |
} |
| 1601 |
goto done; |
| 1602 |
} |
| 1603 |
n++; |
| 1604 |
} |
| 1605 |
|
| 1606 |
done: |
| 1607 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 1608 |
dtp->u.p.current_unit->has_size) |
| 1609 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) n; |
| 1610 |
dtp->u.p.current_unit->bytes_left -= n; |
| 1611 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) n; |
| 1612 |
} |