| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Andy Vaught |
| 3 |
Namelist output contributed by Paul Thomas |
| 4 |
F2003 I/O support contributed by Jerry DeLisle |
| 5 |
|
| 6 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 7 |
|
| 8 |
Libgfortran is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
Libgfortran is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. */ |
| 26 |
|
| 27 |
#include "io.h" |
| 28 |
#include "fbuf.h" |
| 29 |
#include "format.h" |
| 30 |
#include "unix.h" |
| 31 |
#include <assert.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#define star_fill(p, n) memset(p, '*', n) |
| 35 |
|
| 36 |
typedef unsigned char uchar; |
| 37 |
|
| 38 |
/* Helper functions for character(kind=4) internal units. These are needed |
| 39 |
by write_float.def. */ |
| 40 |
|
| 41 |
static void |
| 42 |
memcpy4 (gfc_char4_t *dest, const char *source, int k) |
| 43 |
{ |
| 44 |
int j; |
| 45 |
|
| 46 |
const char *p = source; |
| 47 |
for (j = 0; j < k; j++) |
| 48 |
*dest++ = (gfc_char4_t) *p++; |
| 49 |
} |
| 50 |
|
| 51 |
/* This include contains the heart and soul of formatted floating point. */ |
| 52 |
#include "write_float.def" |
| 53 |
|
| 54 |
/* Write out default char4. */ |
| 55 |
|
| 56 |
static void |
| 57 |
write_default_char4 (st_parameter_dt *dtp, const gfc_char4_t *source, |
| 58 |
int src_len, int w_len) |
| 59 |
{ |
| 60 |
char *p; |
| 61 |
int j, k = 0; |
| 62 |
gfc_char4_t c; |
| 63 |
uchar d; |
| 64 |
|
| 65 |
/* Take care of preceding blanks. */ |
| 66 |
if (w_len > src_len) |
| 67 |
{ |
| 68 |
k = w_len - src_len; |
| 69 |
p = write_block (dtp, k); |
| 70 |
if (p == NULL) |
| 71 |
return; |
| 72 |
if (is_char4_unit (dtp)) |
| 73 |
{ |
| 74 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 75 |
memset4 (p4, ' ', k); |
| 76 |
} |
| 77 |
else |
| 78 |
memset (p, ' ', k); |
| 79 |
} |
| 80 |
|
| 81 |
/* Get ready to handle delimiters if needed. */ |
| 82 |
switch (dtp->u.p.current_unit->delim_status) |
| 83 |
{ |
| 84 |
case DELIM_APOSTROPHE: |
| 85 |
d = '\''; |
| 86 |
break; |
| 87 |
case DELIM_QUOTE: |
| 88 |
d = '"'; |
| 89 |
break; |
| 90 |
default: |
| 91 |
d = ' '; |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
/* Now process the remaining characters, one at a time. */ |
| 96 |
for (j = 0; j < src_len; j++) |
| 97 |
{ |
| 98 |
c = source[j]; |
| 99 |
if (is_char4_unit (dtp)) |
| 100 |
{ |
| 101 |
gfc_char4_t *q; |
| 102 |
/* Handle delimiters if any. */ |
| 103 |
if (c == d && d != ' ') |
| 104 |
{ |
| 105 |
p = write_block (dtp, 2); |
| 106 |
if (p == NULL) |
| 107 |
return; |
| 108 |
q = (gfc_char4_t *) p; |
| 109 |
*q++ = c; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
p = write_block (dtp, 1); |
| 114 |
if (p == NULL) |
| 115 |
return; |
| 116 |
q = (gfc_char4_t *) p; |
| 117 |
} |
| 118 |
*q = c; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
/* Handle delimiters if any. */ |
| 123 |
if (c == d && d != ' ') |
| 124 |
{ |
| 125 |
p = write_block (dtp, 2); |
| 126 |
if (p == NULL) |
| 127 |
return; |
| 128 |
*p++ = (uchar) c; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
p = write_block (dtp, 1); |
| 133 |
if (p == NULL) |
| 134 |
return; |
| 135 |
} |
| 136 |
*p = c > 255 ? '?' : (uchar) c; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/* Write out UTF-8 converted from char4. */ |
| 143 |
|
| 144 |
static void |
| 145 |
write_utf8_char4 (st_parameter_dt *dtp, gfc_char4_t *source, |
| 146 |
int src_len, int w_len) |
| 147 |
{ |
| 148 |
char *p; |
| 149 |
int j, k = 0; |
| 150 |
gfc_char4_t c; |
| 151 |
static const uchar masks[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; |
| 152 |
static const uchar limits[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; |
| 153 |
int nbytes; |
| 154 |
uchar buf[6], d, *q; |
| 155 |
|
| 156 |
/* Take care of preceding blanks. */ |
| 157 |
if (w_len > src_len) |
| 158 |
{ |
| 159 |
k = w_len - src_len; |
| 160 |
p = write_block (dtp, k); |
| 161 |
if (p == NULL) |
| 162 |
return; |
| 163 |
memset (p, ' ', k); |
| 164 |
} |
| 165 |
|
| 166 |
/* Get ready to handle delimiters if needed. */ |
| 167 |
switch (dtp->u.p.current_unit->delim_status) |
| 168 |
{ |
| 169 |
case DELIM_APOSTROPHE: |
| 170 |
d = '\''; |
| 171 |
break; |
| 172 |
case DELIM_QUOTE: |
| 173 |
d = '"'; |
| 174 |
break; |
| 175 |
default: |
| 176 |
d = ' '; |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
/* Now process the remaining characters, one at a time. We need to |
| 181 |
adjust the src_len if the user has specified a field width. */ |
| 182 |
src_len = w_len > 0 ? w_len : src_len; |
| 183 |
for (j = k; j < src_len; j++) |
| 184 |
{ |
| 185 |
c = source[j]; |
| 186 |
if (c < 0x80) |
| 187 |
{ |
| 188 |
/* Handle the delimiters if any. */ |
| 189 |
if (c == d && d != ' ') |
| 190 |
{ |
| 191 |
p = write_block (dtp, 2); |
| 192 |
if (p == NULL) |
| 193 |
return; |
| 194 |
*p++ = (uchar) c; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
p = write_block (dtp, 1); |
| 199 |
if (p == NULL) |
| 200 |
return; |
| 201 |
} |
| 202 |
*p = (uchar) c; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
/* Convert to UTF-8 sequence. */ |
| 207 |
nbytes = 1; |
| 208 |
q = &buf[6]; |
| 209 |
|
| 210 |
do |
| 211 |
{ |
| 212 |
*--q = ((c & 0x3F) | 0x80); |
| 213 |
c >>= 6; |
| 214 |
nbytes++; |
| 215 |
} |
| 216 |
while (c >= 0x3F || (c & limits[nbytes-1])); |
| 217 |
|
| 218 |
*--q = (c | masks[nbytes-1]); |
| 219 |
|
| 220 |
p = write_block (dtp, nbytes); |
| 221 |
if (p == NULL) |
| 222 |
return; |
| 223 |
|
| 224 |
while (q < &buf[6]) |
| 225 |
*p++ = *q++; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
/* Check the first character in source if we are using CC_FORTRAN |
| 232 |
and set the cc.type appropriately. The cc.type is used later by write_cc |
| 233 |
to determine the output start-of-record, and next_record_cc to determine the |
| 234 |
output end-of-record. |
| 235 |
This function is called before the output buffer is allocated, so alloc_len |
| 236 |
is set to the appropriate size to allocate. */ |
| 237 |
|
| 238 |
static void |
| 239 |
write_check_cc (st_parameter_dt *dtp, const char **source, size_t *alloc_len) |
| 240 |
{ |
| 241 |
/* Only valid for CARRIAGECONTROL=FORTRAN. */ |
| 242 |
if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN |
| 243 |
|| alloc_len == NULL || source == NULL) |
| 244 |
return; |
| 245 |
|
| 246 |
/* Peek at the first character. */ |
| 247 |
int c = (*alloc_len > 0) ? (*source)[0] : EOF; |
| 248 |
if (c != EOF) |
| 249 |
{ |
| 250 |
/* The start-of-record character which will be printed. */ |
| 251 |
dtp->u.p.cc.u.start = '\n'; |
| 252 |
/* The number of characters to print at the start-of-record. |
| 253 |
len > 1 means copy the SOR character multiple times. |
| 254 |
len == 0 means no SOR will be output. */ |
| 255 |
dtp->u.p.cc.len = 1; |
| 256 |
|
| 257 |
switch (c) |
| 258 |
{ |
| 259 |
case '+': |
| 260 |
dtp->u.p.cc.type = CCF_OVERPRINT; |
| 261 |
dtp->u.p.cc.len = 0; |
| 262 |
break; |
| 263 |
case '-': |
| 264 |
dtp->u.p.cc.type = CCF_ONE_LF; |
| 265 |
dtp->u.p.cc.len = 1; |
| 266 |
break; |
| 267 |
case '0': |
| 268 |
dtp->u.p.cc.type = CCF_TWO_LF; |
| 269 |
dtp->u.p.cc.len = 2; |
| 270 |
break; |
| 271 |
case '1': |
| 272 |
dtp->u.p.cc.type = CCF_PAGE_FEED; |
| 273 |
dtp->u.p.cc.len = 1; |
| 274 |
dtp->u.p.cc.u.start = '\f'; |
| 275 |
break; |
| 276 |
case '$': |
| 277 |
dtp->u.p.cc.type = CCF_PROMPT; |
| 278 |
dtp->u.p.cc.len = 1; |
| 279 |
break; |
| 280 |
case '\0': |
| 281 |
dtp->u.p.cc.type = CCF_OVERPRINT_NOA; |
| 282 |
dtp->u.p.cc.len = 0; |
| 283 |
break; |
| 284 |
default: |
| 285 |
/* In the default case we copy ONE_LF. */ |
| 286 |
dtp->u.p.cc.type = CCF_DEFAULT; |
| 287 |
dtp->u.p.cc.len = 1; |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
/* We add n-1 to alloc_len so our write buffer is the right size. |
| 292 |
We are replacing the first character, and possibly prepending some |
| 293 |
additional characters. Note for n==0, we actually subtract one from |
| 294 |
alloc_len, which is correct, since that character is skipped. */ |
| 295 |
if (*alloc_len > 0) |
| 296 |
{ |
| 297 |
*source += 1; |
| 298 |
*alloc_len += dtp->u.p.cc.len - 1; |
| 299 |
} |
| 300 |
/* If we have no input, there is no first character to replace. Make |
| 301 |
sure we still allocate enough space for the start-of-record string. */ |
| 302 |
else |
| 303 |
*alloc_len = dtp->u.p.cc.len; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/* Write the start-of-record character(s) for CC_FORTRAN. |
| 309 |
Also adjusts the 'cc' struct to contain the end-of-record character |
| 310 |
for next_record_cc. |
| 311 |
The source_len is set to the remaining length to copy from the source, |
| 312 |
after the start-of-record string was inserted. */ |
| 313 |
|
| 314 |
static char * |
| 315 |
write_cc (st_parameter_dt *dtp, char *p, size_t *source_len) |
| 316 |
{ |
| 317 |
/* Only valid for CARRIAGECONTROL=FORTRAN. */ |
| 318 |
if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN || source_len == NULL) |
| 319 |
return p; |
| 320 |
|
| 321 |
/* Write the start-of-record string to the output buffer. Note that len is |
| 322 |
never more than 2. */ |
| 323 |
if (dtp->u.p.cc.len > 0) |
| 324 |
{ |
| 325 |
*(p++) = dtp->u.p.cc.u.start; |
| 326 |
if (dtp->u.p.cc.len > 1) |
| 327 |
*(p++) = dtp->u.p.cc.u.start; |
| 328 |
|
| 329 |
/* source_len comes from write_check_cc where it is set to the full |
| 330 |
allocated length of the output buffer. Therefore we subtract off the |
| 331 |
length of the SOR string to obtain the remaining source length. */ |
| 332 |
*source_len -= dtp->u.p.cc.len; |
| 333 |
} |
| 334 |
|
| 335 |
/* Common case. */ |
| 336 |
dtp->u.p.cc.len = 1; |
| 337 |
dtp->u.p.cc.u.end = '\r'; |
| 338 |
|
| 339 |
/* Update end-of-record character for next_record_w. */ |
| 340 |
switch (dtp->u.p.cc.type) |
| 341 |
{ |
| 342 |
case CCF_PROMPT: |
| 343 |
case CCF_OVERPRINT_NOA: |
| 344 |
/* No end-of-record. */ |
| 345 |
dtp->u.p.cc.len = 0; |
| 346 |
dtp->u.p.cc.u.end = '\0'; |
| 347 |
break; |
| 348 |
case CCF_OVERPRINT: |
| 349 |
case CCF_ONE_LF: |
| 350 |
case CCF_TWO_LF: |
| 351 |
case CCF_PAGE_FEED: |
| 352 |
case CCF_DEFAULT: |
| 353 |
default: |
| 354 |
/* Carriage return. */ |
| 355 |
dtp->u.p.cc.len = 1; |
| 356 |
dtp->u.p.cc.u.end = '\r'; |
| 357 |
break; |
| 358 |
} |
| 359 |
|
| 360 |
return p; |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
|
| 365 |
write_a (st_parameter_dt *dtp, const fnode *f, const char *source, size_t len) |
| 366 |
{ |
| 367 |
size_t wlen; |
| 368 |
char *p; |
| 369 |
|
| 370 |
wlen = f->u.string.length < 0 |
| 371 |
|| (f->format == FMT_G && f->u.string.length == 0) |
| 372 |
? len : (size_t) f->u.string.length; |
| 373 |
|
| 374 |
#ifdef HAVE_CRLF |
| 375 |
/* If this is formatted STREAM IO convert any embedded line feed characters |
| 376 |
to CR_LF on systems that use that sequence for newlines. See F2003 |
| 377 |
Standard sections 10.6.3 and 9.9 for further information. */ |
| 378 |
if (is_stream_io (dtp)) |
| 379 |
{ |
| 380 |
const char crlf[] = "\r\n"; |
| 381 |
size_t q, bytes; |
| 382 |
q = bytes = 0; |
| 383 |
|
| 384 |
/* Write out any padding if needed. */ |
| 385 |
if (len < wlen) |
| 386 |
{ |
| 387 |
p = write_block (dtp, wlen - len); |
| 388 |
if (p == NULL) |
| 389 |
return; |
| 390 |
memset (p, ' ', wlen - len); |
| 391 |
} |
| 392 |
|
| 393 |
/* Scan the source string looking for '\n' and convert it if found. */ |
| 394 |
for (size_t i = 0; i < wlen; i++) |
| 395 |
{ |
| 396 |
if (source[i] == '\n') |
| 397 |
{ |
| 398 |
/* Write out the previously scanned characters in the string. */ |
| 399 |
if (bytes > 0) |
| 400 |
{ |
| 401 |
p = write_block (dtp, bytes); |
| 402 |
if (p == NULL) |
| 403 |
return; |
| 404 |
memcpy (p, &source[q], bytes); |
| 405 |
q += bytes; |
| 406 |
bytes = 0; |
| 407 |
} |
| 408 |
|
| 409 |
/* Write out the CR_LF sequence. */ |
| 410 |
q++; |
| 411 |
p = write_block (dtp, 2); |
| 412 |
if (p == NULL) |
| 413 |
return; |
| 414 |
memcpy (p, crlf, 2); |
| 415 |
} |
| 416 |
else |
| 417 |
bytes++; |
| 418 |
} |
| 419 |
|
| 420 |
/* Write out any remaining bytes if no LF was found. */ |
| 421 |
if (bytes > 0) |
| 422 |
{ |
| 423 |
p = write_block (dtp, bytes); |
| 424 |
if (p == NULL) |
| 425 |
return; |
| 426 |
memcpy (p, &source[q], bytes); |
| 427 |
} |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
#endif |
| 432 |
if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN) |
| 433 |
write_check_cc (dtp, &source, &wlen); |
| 434 |
|
| 435 |
p = write_block (dtp, wlen); |
| 436 |
if (p == NULL) |
| 437 |
return; |
| 438 |
|
| 439 |
if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN) |
| 440 |
p = write_cc (dtp, p, &wlen); |
| 441 |
|
| 442 |
if (unlikely (is_char4_unit (dtp))) |
| 443 |
{ |
| 444 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 445 |
if (wlen < len) |
| 446 |
memcpy4 (p4, source, wlen); |
| 447 |
else |
| 448 |
{ |
| 449 |
memset4 (p4, ' ', wlen - len); |
| 450 |
memcpy4 (p4 + wlen - len, source, len); |
| 451 |
} |
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
if (wlen < len) |
| 456 |
memcpy (p, source, wlen); |
| 457 |
else |
| 458 |
{ |
| 459 |
memset (p, ' ', wlen - len); |
| 460 |
memcpy (p + wlen - len, source, len); |
| 461 |
} |
| 462 |
#ifdef HAVE_CRLF |
| 463 |
} |
| 464 |
#endif |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
/* The primary difference between write_a_char4 and write_a is that we have to |
| 469 |
deal with writing from the first byte of the 4-byte character and pay |
| 470 |
attention to the most significant bytes. For ENCODING="default" write the |
| 471 |
lowest significant byte. If the 3 most significant bytes contain |
| 472 |
non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value |
| 473 |
to the UTF-8 encoded string before writing out. */ |
| 474 |
|
| 475 |
void |
| 476 |
write_a_char4 (st_parameter_dt *dtp, const fnode *f, const char *source, size_t len) |
| 477 |
{ |
| 478 |
size_t wlen; |
| 479 |
gfc_char4_t *q; |
| 480 |
|
| 481 |
wlen = f->u.string.length < 0 |
| 482 |
|| (f->format == FMT_G && f->u.string.length == 0) |
| 483 |
? len : (size_t) f->u.string.length; |
| 484 |
|
| 485 |
q = (gfc_char4_t *) source; |
| 486 |
#ifdef HAVE_CRLF |
| 487 |
/* If this is formatted STREAM IO convert any embedded line feed characters |
| 488 |
to CR_LF on systems that use that sequence for newlines. See F2003 |
| 489 |
Standard sections 10.6.3 and 9.9 for further information. */ |
| 490 |
if (is_stream_io (dtp)) |
| 491 |
{ |
| 492 |
const gfc_char4_t crlf[] = {0x000d,0x000a}; |
| 493 |
size_t bytes; |
| 494 |
gfc_char4_t *qq; |
| 495 |
bytes = 0; |
| 496 |
|
| 497 |
/* Write out any padding if needed. */ |
| 498 |
if (len < wlen) |
| 499 |
{ |
| 500 |
char *p; |
| 501 |
p = write_block (dtp, wlen - len); |
| 502 |
if (p == NULL) |
| 503 |
return; |
| 504 |
memset (p, ' ', wlen - len); |
| 505 |
} |
| 506 |
|
| 507 |
/* Scan the source string looking for '\n' and convert it if found. */ |
| 508 |
qq = (gfc_char4_t *) source; |
| 509 |
for (size_t i = 0; i < wlen; i++) |
| 510 |
{ |
| 511 |
if (qq[i] == '\n') |
| 512 |
{ |
| 513 |
/* Write out the previously scanned characters in the string. */ |
| 514 |
if (bytes > 0) |
| 515 |
{ |
| 516 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 517 |
write_utf8_char4 (dtp, q, bytes, 0); |
| 518 |
else |
| 519 |
write_default_char4 (dtp, q, bytes, 0); |
| 520 |
bytes = 0; |
| 521 |
} |
| 522 |
|
| 523 |
/* Write out the CR_LF sequence. */ |
| 524 |
write_default_char4 (dtp, crlf, 2, 0); |
| 525 |
} |
| 526 |
else |
| 527 |
bytes++; |
| 528 |
} |
| 529 |
|
| 530 |
/* Write out any remaining bytes if no LF was found. */ |
| 531 |
if (bytes > 0) |
| 532 |
{ |
| 533 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 534 |
write_utf8_char4 (dtp, q, bytes, 0); |
| 535 |
else |
| 536 |
write_default_char4 (dtp, q, bytes, 0); |
| 537 |
} |
| 538 |
} |
| 539 |
else |
| 540 |
{ |
| 541 |
#endif |
| 542 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 543 |
write_utf8_char4 (dtp, q, len, wlen); |
| 544 |
else |
| 545 |
write_default_char4 (dtp, q, len, wlen); |
| 546 |
#ifdef HAVE_CRLF |
| 547 |
} |
| 548 |
#endif |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
static GFC_INTEGER_LARGEST |
| 553 |
extract_int (const void *p, int len) |
| 554 |
{ |
| 555 |
GFC_INTEGER_LARGEST i = 0; |
| 556 |
|
| 557 |
if (p == NULL) |
| 558 |
return i; |
| 559 |
|
| 560 |
switch (len) |
| 561 |
{ |
| 562 |
case 1: |
| 563 |
{ |
| 564 |
GFC_INTEGER_1 tmp; |
| 565 |
memcpy ((void *) &tmp, p, len); |
| 566 |
i = tmp; |
| 567 |
} |
| 568 |
break; |
| 569 |
case 2: |
| 570 |
{ |
| 571 |
GFC_INTEGER_2 tmp; |
| 572 |
memcpy ((void *) &tmp, p, len); |
| 573 |
i = tmp; |
| 574 |
} |
| 575 |
break; |
| 576 |
case 4: |
| 577 |
{ |
| 578 |
GFC_INTEGER_4 tmp; |
| 579 |
memcpy ((void *) &tmp, p, len); |
| 580 |
i = tmp; |
| 581 |
} |
| 582 |
break; |
| 583 |
case 8: |
| 584 |
{ |
| 585 |
GFC_INTEGER_8 tmp; |
| 586 |
memcpy ((void *) &tmp, p, len); |
| 587 |
i = tmp; |
| 588 |
} |
| 589 |
break; |
| 590 |
#ifdef HAVE_GFC_INTEGER_16 |
| 591 |
case 16: |
| 592 |
{ |
| 593 |
GFC_INTEGER_16 tmp; |
| 594 |
memcpy ((void *) &tmp, p, len); |
| 595 |
i = tmp; |
| 596 |
} |
| 597 |
break; |
| 598 |
#endif |
| 599 |
default: |
| 600 |
internal_error (NULL, "bad integer kind"); |
| 601 |
} |
| 602 |
|
| 603 |
return i; |
| 604 |
} |
| 605 |
|
| 606 |
static GFC_UINTEGER_LARGEST |
| 607 |
extract_uint (const void *p, int len) |
| 608 |
{ |
| 609 |
GFC_UINTEGER_LARGEST i = 0; |
| 610 |
|
| 611 |
if (p == NULL) |
| 612 |
return i; |
| 613 |
|
| 614 |
switch (len) |
| 615 |
{ |
| 616 |
case 1: |
| 617 |
{ |
| 618 |
GFC_INTEGER_1 tmp; |
| 619 |
memcpy ((void *) &tmp, p, len); |
| 620 |
i = (GFC_UINTEGER_1) tmp; |
| 621 |
} |
| 622 |
break; |
| 623 |
case 2: |
| 624 |
{ |
| 625 |
GFC_INTEGER_2 tmp; |
| 626 |
memcpy ((void *) &tmp, p, len); |
| 627 |
i = (GFC_UINTEGER_2) tmp; |
| 628 |
} |
| 629 |
break; |
| 630 |
case 4: |
| 631 |
{ |
| 632 |
GFC_INTEGER_4 tmp; |
| 633 |
memcpy ((void *) &tmp, p, len); |
| 634 |
i = (GFC_UINTEGER_4) tmp; |
| 635 |
} |
| 636 |
break; |
| 637 |
case 8: |
| 638 |
{ |
| 639 |
GFC_INTEGER_8 tmp; |
| 640 |
memcpy ((void *) &tmp, p, len); |
| 641 |
i = (GFC_UINTEGER_8) tmp; |
| 642 |
} |
| 643 |
break; |
| 644 |
#ifdef HAVE_GFC_INTEGER_16 |
| 645 |
case 10: |
| 646 |
case 16: |
| 647 |
{ |
| 648 |
GFC_INTEGER_16 tmp = 0; |
| 649 |
memcpy ((void *) &tmp, p, len); |
| 650 |
i = (GFC_UINTEGER_16) tmp; |
| 651 |
} |
| 652 |
break; |
| 653 |
# ifdef HAVE_GFC_REAL_17 |
| 654 |
case 17: |
| 655 |
{ |
| 656 |
GFC_INTEGER_16 tmp = 0; |
| 657 |
memcpy ((void *) &tmp, p, 16); |
| 658 |
i = (GFC_UINTEGER_16) tmp; |
| 659 |
} |
| 660 |
break; |
| 661 |
# endif |
| 662 |
#endif |
| 663 |
default: |
| 664 |
internal_error (NULL, "bad integer kind"); |
| 665 |
} |
| 666 |
|
| 667 |
return i; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
void |
| 672 |
write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len) |
| 673 |
{ |
| 674 |
char *p; |
| 675 |
int wlen; |
| 676 |
GFC_INTEGER_LARGEST n; |
| 677 |
|
| 678 |
wlen = (f->format == FMT_G && f->u.w == 0) ? 1 : f->u.w; |
| 679 |
|
| 680 |
p = write_block (dtp, wlen); |
| 681 |
if (p == NULL) |
| 682 |
return; |
| 683 |
|
| 684 |
n = extract_int (source, len); |
| 685 |
|
| 686 |
if (unlikely (is_char4_unit (dtp))) |
| 687 |
{ |
| 688 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 689 |
memset4 (p4, ' ', wlen -1); |
| 690 |
p4[wlen - 1] = (n) ? 'T' : 'F'; |
| 691 |
return; |
| 692 |
} |
| 693 |
|
| 694 |
memset (p, ' ', wlen -1); |
| 695 |
p[wlen - 1] = (n) ? 'T' : 'F'; |
| 696 |
} |
| 697 |
|
| 698 |
static void |
| 699 |
write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n, int len) |
| 700 |
{ |
| 701 |
int w, m, digits, nzero, nblank; |
| 702 |
char *p; |
| 703 |
|
| 704 |
w = f->u.integer.w; |
| 705 |
m = f->u.integer.m; |
| 706 |
|
| 707 |
/* Special case: */ |
| 708 |
|
| 709 |
if (m == 0 && n == 0) |
| 710 |
{ |
| 711 |
if (w == 0) |
| 712 |
w = 1; |
| 713 |
|
| 714 |
p = write_block (dtp, w); |
| 715 |
if (p == NULL) |
| 716 |
return; |
| 717 |
if (unlikely (is_char4_unit (dtp))) |
| 718 |
{ |
| 719 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 720 |
memset4 (p4, ' ', w); |
| 721 |
} |
| 722 |
else |
| 723 |
memset (p, ' ', w); |
| 724 |
goto done; |
| 725 |
} |
| 726 |
|
| 727 |
digits = strlen (q); |
| 728 |
|
| 729 |
/* Select a width if none was specified. The idea here is to always |
| 730 |
print something. */ |
| 731 |
|
| 732 |
if (w == DEFAULT_WIDTH) |
| 733 |
w = default_width_for_integer (len); |
| 734 |
|
| 735 |
if (w == 0) |
| 736 |
w = ((digits < m) ? m : digits); |
| 737 |
|
| 738 |
p = write_block (dtp, w); |
| 739 |
if (p == NULL) |
| 740 |
return; |
| 741 |
|
| 742 |
nzero = 0; |
| 743 |
if (digits < m) |
| 744 |
nzero = m - digits; |
| 745 |
|
| 746 |
/* See if things will work. */ |
| 747 |
|
| 748 |
nblank = w - (nzero + digits); |
| 749 |
|
| 750 |
if (unlikely (is_char4_unit (dtp))) |
| 751 |
{ |
| 752 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 753 |
if (nblank < 0) |
| 754 |
{ |
| 755 |
memset4 (p4, '*', w); |
| 756 |
return; |
| 757 |
} |
| 758 |
|
| 759 |
if (!dtp->u.p.no_leading_blank) |
| 760 |
{ |
| 761 |
memset4 (p4, ' ', nblank); |
| 762 |
q += nblank; |
| 763 |
memset4 (p4, '0', nzero); |
| 764 |
q += nzero; |
| 765 |
memcpy4 (p4, q, digits); |
| 766 |
} |
| 767 |
else |
| 768 |
{ |
| 769 |
memset4 (p4, '0', nzero); |
| 770 |
q += nzero; |
| 771 |
memcpy4 (p4, q, digits); |
| 772 |
q += digits; |
| 773 |
memset4 (p4, ' ', nblank); |
| 774 |
dtp->u.p.no_leading_blank = 0; |
| 775 |
} |
| 776 |
return; |
| 777 |
} |
| 778 |
|
| 779 |
if (nblank < 0) |
| 780 |
{ |
| 781 |
star_fill (p, w); |
| 782 |
goto done; |
| 783 |
} |
| 784 |
|
| 785 |
if (!dtp->u.p.no_leading_blank) |
| 786 |
{ |
| 787 |
memset (p, ' ', nblank); |
| 788 |
p += nblank; |
| 789 |
memset (p, '0', nzero); |
| 790 |
p += nzero; |
| 791 |
memcpy (p, q, digits); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
memset (p, '0', nzero); |
| 796 |
p += nzero; |
| 797 |
memcpy (p, q, digits); |
| 798 |
p += digits; |
| 799 |
memset (p, ' ', nblank); |
| 800 |
dtp->u.p.no_leading_blank = 0; |
| 801 |
} |
| 802 |
|
| 803 |
done: |
| 804 |
return; |
| 805 |
} |
| 806 |
|
| 807 |
static void |
| 808 |
write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source, |
| 809 |
int len) |
| 810 |
{ |
| 811 |
GFC_INTEGER_LARGEST n = 0; |
| 812 |
GFC_UINTEGER_LARGEST absn; |
| 813 |
int w, m, digits, nsign, nzero, nblank; |
| 814 |
char *p; |
| 815 |
const char *q; |
| 816 |
sign_t sign; |
| 817 |
char itoa_buf[GFC_BTOA_BUF_SIZE]; |
| 818 |
|
| 819 |
w = f->u.integer.w; |
| 820 |
m = f->format == FMT_G ? -1 : f->u.integer.m; |
| 821 |
|
| 822 |
n = extract_int (source, len); |
| 823 |
|
| 824 |
/* Special case: */ |
| 825 |
if (m == 0 && n == 0) |
| 826 |
{ |
| 827 |
if (w == 0) |
| 828 |
w = 1; |
| 829 |
|
| 830 |
p = write_block (dtp, w); |
| 831 |
if (p == NULL) |
| 832 |
return; |
| 833 |
if (unlikely (is_char4_unit (dtp))) |
| 834 |
{ |
| 835 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 836 |
memset4 (p4, ' ', w); |
| 837 |
} |
| 838 |
else |
| 839 |
memset (p, ' ', w); |
| 840 |
goto done; |
| 841 |
} |
| 842 |
|
| 843 |
sign = calculate_sign (dtp, n < 0); |
| 844 |
if (n < 0) |
| 845 |
/* Use unsigned to protect from overflow. */ |
| 846 |
absn = -(GFC_UINTEGER_LARGEST) n; |
| 847 |
else |
| 848 |
absn = n; |
| 849 |
nsign = sign == S_NONE ? 0 : 1; |
| 850 |
|
| 851 |
/* gfc_itoa() converts the nonnegative value to decimal representation. */ |
| 852 |
q = gfc_itoa (absn, itoa_buf, sizeof (itoa_buf)); |
| 853 |
digits = strlen (q); |
| 854 |
|
| 855 |
/* Select a width if none was specified. The idea here is to always |
| 856 |
print something. */ |
| 857 |
if (w == DEFAULT_WIDTH) |
| 858 |
w = default_width_for_integer (len); |
| 859 |
|
| 860 |
if (w == 0) |
| 861 |
w = ((digits < m) ? m : digits) + nsign; |
| 862 |
|
| 863 |
p = write_block (dtp, w); |
| 864 |
if (p == NULL) |
| 865 |
return; |
| 866 |
|
| 867 |
nzero = 0; |
| 868 |
if (digits < m) |
| 869 |
nzero = m - digits; |
| 870 |
|
| 871 |
/* See if things will work. */ |
| 872 |
|
| 873 |
nblank = w - (nsign + nzero + digits); |
| 874 |
|
| 875 |
if (unlikely (is_char4_unit (dtp))) |
| 876 |
{ |
| 877 |
gfc_char4_t *p4 = (gfc_char4_t *)p; |
| 878 |
if (nblank < 0) |
| 879 |
{ |
| 880 |
memset4 (p4, '*', w); |
| 881 |
goto done; |
| 882 |
} |
| 883 |
|
| 884 |
if (!dtp->u.p.namelist_mode) |
| 885 |
{ |
| 886 |
memset4 (p4, ' ', nblank); |
| 887 |
p4 += nblank; |
| 888 |
} |
| 889 |
|
| 890 |
switch (sign) |
| 891 |
{ |
| 892 |
case S_PLUS: |
| 893 |
*p4++ = '+'; |
| 894 |
break; |
| 895 |
case S_MINUS: |
| 896 |
*p4++ = '-'; |
| 897 |
break; |
| 898 |
case S_NONE: |
| 899 |
break; |
| 900 |
} |
| 901 |
|
| 902 |
memset4 (p4, '0', nzero); |
| 903 |
p4 += nzero; |
| 904 |
|
| 905 |
memcpy4 (p4, q, digits); |
| 906 |
return; |
| 907 |
|
| 908 |
if (dtp->u.p.namelist_mode) |
| 909 |
{ |
| 910 |
p4 += digits; |
| 911 |
memset4 (p4, ' ', nblank); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
if (nblank < 0) |
| 916 |
{ |
| 917 |
star_fill (p, w); |
| 918 |
goto done; |
| 919 |
} |
| 920 |
|
| 921 |
if (!dtp->u.p.namelist_mode) |
| 922 |
{ |
| 923 |
memset (p, ' ', nblank); |
| 924 |
p += nblank; |
| 925 |
} |
| 926 |
|
| 927 |
switch (sign) |
| 928 |
{ |
| 929 |
case S_PLUS: |
| 930 |
*p++ = '+'; |
| 931 |
break; |
| 932 |
case S_MINUS: |
| 933 |
*p++ = '-'; |
| 934 |
break; |
| 935 |
case S_NONE: |
| 936 |
break; |
| 937 |
} |
| 938 |
|
| 939 |
memset (p, '0', nzero); |
| 940 |
p += nzero; |
| 941 |
|
| 942 |
memcpy (p, q, digits); |
| 943 |
|
| 944 |
if (dtp->u.p.namelist_mode) |
| 945 |
{ |
| 946 |
p += digits; |
| 947 |
memset (p, ' ', nblank); |
| 948 |
} |
| 949 |
|
| 950 |
done: |
| 951 |
return; |
| 952 |
} |
| 953 |
|
| 954 |
/* Same as above, but somewhat simpler because we only treat unsigned |
| 955 |
numbers. */ |
| 956 |
|
| 957 |
static void |
| 958 |
write_decimal_unsigned (st_parameter_dt *dtp, const fnode *f, |
| 959 |
const char *source, int len) |
| 960 |
{ |
| 961 |
GFC_UINTEGER_LARGEST n = 0; |
| 962 |
int w, m, digits, nsign, nzero, nblank; |
| 963 |
char *p; |
| 964 |
const char *q; |
| 965 |
sign_t sign; |
| 966 |
char itoa_buf[GFC_BTOA_BUF_SIZE]; |
| 967 |
|
| 968 |
w = f->u.integer.w; |
| 969 |
m = f->format == FMT_G ? -1 : f->u.integer.m; |
| 970 |
|
| 971 |
n = extract_uint (source, len); |
| 972 |
|
| 973 |
/* Special case: */ |
| 974 |
if (m == 0 && n == 0) |
| 975 |
{ |
| 976 |
if (w == 0) |
| 977 |
w = 1; |
| 978 |
|
| 979 |
p = write_block (dtp, w); |
| 980 |
if (p == NULL) |
| 981 |
return; |
| 982 |
|
| 983 |
if (unlikely (is_char4_unit (dtp))) |
| 984 |
{ |
| 985 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 986 |
memset4 (p4, ' ', w); |
| 987 |
} |
| 988 |
else |
| 989 |
memset (p, ' ', w); |
| 990 |
goto done; |
| 991 |
} |
| 992 |
|
| 993 |
/* Just in case somebody wants a + sign. */ |
| 994 |
sign = calculate_sign (dtp, false); |
| 995 |
nsign = sign == S_NONE ? 0 : 1; |
| 996 |
|
| 997 |
q = gfc_itoa (n, itoa_buf, sizeof (itoa_buf)); |
| 998 |
digits = strlen (q); |
| 999 |
|
| 1000 |
/* Select a width if none was specified. The idea here is to always |
| 1001 |
print something. */ |
| 1002 |
if (w == DEFAULT_WIDTH) |
| 1003 |
w = default_width_for_integer (len); |
| 1004 |
|
| 1005 |
if (w == 0) |
| 1006 |
w = ((digits < m) ? m : digits) + nsign; |
| 1007 |
|
| 1008 |
p = write_block (dtp, w); |
| 1009 |
if (p == NULL) |
| 1010 |
return; |
| 1011 |
|
| 1012 |
nzero = 0; |
| 1013 |
if (digits < m) |
| 1014 |
nzero = m - digits; |
| 1015 |
|
| 1016 |
/* See if things will work. */ |
| 1017 |
|
| 1018 |
nblank = w - (nsign + nzero + digits); |
| 1019 |
|
| 1020 |
if (unlikely (is_char4_unit (dtp))) |
| 1021 |
{ |
| 1022 |
gfc_char4_t *p4 = (gfc_char4_t *)p; |
| 1023 |
if (nblank < 0) |
| 1024 |
{ |
| 1025 |
memset4 (p4, '*', w); |
| 1026 |
goto done; |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (!dtp->u.p.namelist_mode) |
| 1030 |
{ |
| 1031 |
memset4 (p4, ' ', nblank); |
| 1032 |
p4 += nblank; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (sign == S_PLUS) |
| 1036 |
*p4++ = '+'; |
| 1037 |
|
| 1038 |
memset4 (p4, '0', nzero); |
| 1039 |
p4 += nzero; |
| 1040 |
|
| 1041 |
memcpy4 (p4, q, digits); |
| 1042 |
|
| 1043 |
if (dtp->u.p.namelist_mode) |
| 1044 |
{ |
| 1045 |
p4 += digits; |
| 1046 |
memset4 (p4, ' ', nblank); |
| 1047 |
} |
| 1048 |
|
| 1049 |
return; |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (nblank < 0) |
| 1053 |
{ |
| 1054 |
star_fill (p, w); |
| 1055 |
goto done; |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (!dtp->u.p.namelist_mode) |
| 1059 |
{ |
| 1060 |
memset (p, ' ', nblank); |
| 1061 |
p += nblank; |
| 1062 |
} |
| 1063 |
|
| 1064 |
if (sign == S_PLUS) |
| 1065 |
*p++ = '+'; |
| 1066 |
|
| 1067 |
memset (p, '0', nzero); |
| 1068 |
p += nzero; |
| 1069 |
|
| 1070 |
memcpy (p, q, digits); |
| 1071 |
|
| 1072 |
if (dtp->u.p.namelist_mode) |
| 1073 |
{ |
| 1074 |
p += digits; |
| 1075 |
memset (p, ' ', nblank); |
| 1076 |
} |
| 1077 |
|
| 1078 |
done: |
| 1079 |
return; |
| 1080 |
|
| 1081 |
} |
| 1082 |
/* Convert hexadecimal to ASCII. */ |
| 1083 |
|
| 1084 |
static const char * |
| 1085 |
xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len) |
| 1086 |
{ |
| 1087 |
int digit; |
| 1088 |
char *p; |
| 1089 |
|
| 1090 |
assert (len >= GFC_XTOA_BUF_SIZE); |
| 1091 |
|
| 1092 |
if (n == 0) |
| 1093 |
return "0"; |
| 1094 |
|
| 1095 |
p = buffer + GFC_XTOA_BUF_SIZE - 1; |
| 1096 |
*p = '\0'; |
| 1097 |
|
| 1098 |
while (n != 0) |
| 1099 |
{ |
| 1100 |
digit = n & 0xF; |
| 1101 |
if (digit > 9) |
| 1102 |
digit += 'A' - '0' - 10; |
| 1103 |
|
| 1104 |
*--p = '0' + digit; |
| 1105 |
n >>= 4; |
| 1106 |
} |
| 1107 |
|
| 1108 |
return p; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
/* Convert unsigned octal to ASCII. */ |
| 1113 |
|
| 1114 |
static const char * |
| 1115 |
otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len) |
| 1116 |
{ |
| 1117 |
char *p; |
| 1118 |
|
| 1119 |
assert (len >= GFC_OTOA_BUF_SIZE); |
| 1120 |
|
| 1121 |
if (n == 0) |
| 1122 |
return "0"; |
| 1123 |
|
| 1124 |
p = buffer + GFC_OTOA_BUF_SIZE - 1; |
| 1125 |
*p = '\0'; |
| 1126 |
|
| 1127 |
while (n != 0) |
| 1128 |
{ |
| 1129 |
*--p = '0' + (n & 7); |
| 1130 |
n >>= 3; |
| 1131 |
} |
| 1132 |
|
| 1133 |
return p; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
/* Convert unsigned binary to ASCII. */ |
| 1138 |
|
| 1139 |
static const char * |
| 1140 |
btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len) |
| 1141 |
{ |
| 1142 |
char *p; |
| 1143 |
|
| 1144 |
assert (len >= GFC_BTOA_BUF_SIZE); |
| 1145 |
|
| 1146 |
if (n == 0) |
| 1147 |
return "0"; |
| 1148 |
|
| 1149 |
p = buffer + GFC_BTOA_BUF_SIZE - 1; |
| 1150 |
*p = '\0'; |
| 1151 |
|
| 1152 |
while (n != 0) |
| 1153 |
{ |
| 1154 |
*--p = '0' + (n & 1); |
| 1155 |
n >>= 1; |
| 1156 |
} |
| 1157 |
|
| 1158 |
return p; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* The following three functions, btoa_big, otoa_big, and xtoa_big, are needed |
| 1162 |
to convert large reals with kind sizes that exceed the largest integer type |
| 1163 |
available on certain platforms. In these cases, byte by byte conversion is |
| 1164 |
performed. Endianess is taken into account. */ |
| 1165 |
|
| 1166 |
/* Conversion to binary. */ |
| 1167 |
|
| 1168 |
static const char * |
| 1169 |
btoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n) |
| 1170 |
{ |
| 1171 |
char *q; |
| 1172 |
int i, j; |
| 1173 |
|
| 1174 |
q = buffer; |
| 1175 |
if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 1176 |
{ |
| 1177 |
const char *p = s; |
| 1178 |
for (i = 0; i < len; i++) |
| 1179 |
{ |
| 1180 |
char c = *p; |
| 1181 |
|
| 1182 |
/* Test for zero. Needed by write_boz later. */ |
| 1183 |
if (*p != 0) |
| 1184 |
*n = 1; |
| 1185 |
|
| 1186 |
for (j = 0; j < 8; j++) |
| 1187 |
{ |
| 1188 |
*q++ = (c & 128) ? '1' : '0'; |
| 1189 |
c <<= 1; |
| 1190 |
} |
| 1191 |
p++; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
else |
| 1195 |
{ |
| 1196 |
const char *p = s + len - 1; |
| 1197 |
for (i = 0; i < len; i++) |
| 1198 |
{ |
| 1199 |
char c = *p; |
| 1200 |
|
| 1201 |
/* Test for zero. Needed by write_boz later. */ |
| 1202 |
if (*p != 0) |
| 1203 |
*n = 1; |
| 1204 |
|
| 1205 |
for (j = 0; j < 8; j++) |
| 1206 |
{ |
| 1207 |
*q++ = (c & 128) ? '1' : '0'; |
| 1208 |
c <<= 1; |
| 1209 |
} |
| 1210 |
p--; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (*n == 0) |
| 1215 |
return "0"; |
| 1216 |
|
| 1217 |
/* Move past any leading zeros. */ |
| 1218 |
while (*buffer == '0') |
| 1219 |
buffer++; |
| 1220 |
|
| 1221 |
return buffer; |
| 1222 |
|
| 1223 |
} |
| 1224 |
|
| 1225 |
/* Conversion to octal. */ |
| 1226 |
|
| 1227 |
static const char * |
| 1228 |
otoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n) |
| 1229 |
{ |
| 1230 |
char *q; |
| 1231 |
int i, j, k; |
| 1232 |
uint8_t octet; |
| 1233 |
|
| 1234 |
q = buffer + GFC_OTOA_BUF_SIZE - 1; |
| 1235 |
*q = '\0'; |
| 1236 |
i = k = octet = 0; |
| 1237 |
|
| 1238 |
if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 1239 |
{ |
| 1240 |
const char *p = s + len - 1; |
| 1241 |
char c = *p; |
| 1242 |
while (i < len) |
| 1243 |
{ |
| 1244 |
/* Test for zero. Needed by write_boz later. */ |
| 1245 |
if (*p != 0) |
| 1246 |
*n = 1; |
| 1247 |
|
| 1248 |
for (j = 0; j < 3 && i < len; j++) |
| 1249 |
{ |
| 1250 |
octet |= (c & 1) << j; |
| 1251 |
c >>= 1; |
| 1252 |
if (++k > 7) |
| 1253 |
{ |
| 1254 |
i++; |
| 1255 |
k = 0; |
| 1256 |
c = *--p; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
*--q = '0' + octet; |
| 1260 |
octet = 0; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
else |
| 1264 |
{ |
| 1265 |
const char *p = s; |
| 1266 |
char c = *p; |
| 1267 |
while (i < len) |
| 1268 |
{ |
| 1269 |
/* Test for zero. Needed by write_boz later. */ |
| 1270 |
if (*p != 0) |
| 1271 |
*n = 1; |
| 1272 |
|
| 1273 |
for (j = 0; j < 3 && i < len; j++) |
| 1274 |
{ |
| 1275 |
octet |= (c & 1) << j; |
| 1276 |
c >>= 1; |
| 1277 |
if (++k > 7) |
| 1278 |
{ |
| 1279 |
i++; |
| 1280 |
k = 0; |
| 1281 |
c = *++p; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
*--q = '0' + octet; |
| 1285 |
octet = 0; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (*n == 0) |
| 1290 |
return "0"; |
| 1291 |
|
| 1292 |
/* Move past any leading zeros. */ |
| 1293 |
while (*q == '0') |
| 1294 |
q++; |
| 1295 |
|
| 1296 |
return q; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/* Conversion to hexadecimal. */ |
| 1300 |
|
| 1301 |
static const char * |
| 1302 |
xtoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n) |
| 1303 |
{ |
| 1304 |
static char a[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
| 1305 |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
| 1306 |
|
| 1307 |
char *q; |
| 1308 |
uint8_t h, l; |
| 1309 |
int i; |
| 1310 |
|
| 1311 |
/* write_z, which calls xtoa_big, is called from transfer.c, |
| 1312 |
formatted_transfer_scalar_write. There it is passed the kind as |
| 1313 |
'len' argument, which means a maximum of 16. The buffer is large |
| 1314 |
enough, but the compiler does not know that, so shut up the |
| 1315 |
warning here. */ |
| 1316 |
|
| 1317 |
if (len > 16) |
| 1318 |
__builtin_unreachable (); |
| 1319 |
|
| 1320 |
q = buffer; |
| 1321 |
|
| 1322 |
if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 1323 |
{ |
| 1324 |
const char *p = s; |
| 1325 |
for (i = 0; i < len; i++) |
| 1326 |
{ |
| 1327 |
/* Test for zero. Needed by write_boz later. */ |
| 1328 |
if (*p != 0) |
| 1329 |
*n = 1; |
| 1330 |
|
| 1331 |
h = (*p >> 4) & 0x0F; |
| 1332 |
l = *p++ & 0x0F; |
| 1333 |
*q++ = a[h]; |
| 1334 |
*q++ = a[l]; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
else |
| 1338 |
{ |
| 1339 |
const char *p = s + len - 1; |
| 1340 |
for (i = 0; i < len; i++) |
| 1341 |
{ |
| 1342 |
/* Test for zero. Needed by write_boz later. */ |
| 1343 |
if (*p != 0) |
| 1344 |
*n = 1; |
| 1345 |
|
| 1346 |
h = (*p >> 4) & 0x0F; |
| 1347 |
l = *p-- & 0x0F; |
| 1348 |
*q++ = a[h]; |
| 1349 |
*q++ = a[l]; |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
*q = '\0'; |
| 1354 |
|
| 1355 |
if (*n == 0) |
| 1356 |
return "0"; |
| 1357 |
|
| 1358 |
/* Move past any leading zeros. */ |
| 1359 |
while (*buffer == '0') |
| 1360 |
buffer++; |
| 1361 |
|
| 1362 |
return buffer; |
| 1363 |
} |
| 1364 |
|
| 1365 |
|
| 1366 |
void |
| 1367 |
write_i (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1368 |
{ |
| 1369 |
write_decimal (dtp, f, p, len); |
| 1370 |
} |
| 1371 |
|
| 1372 |
void |
| 1373 |
write_iu (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1374 |
{ |
| 1375 |
write_decimal_unsigned (dtp, f, p, len); |
| 1376 |
} |
| 1377 |
|
| 1378 |
void |
| 1379 |
write_b (st_parameter_dt *dtp, const fnode *f, const char *source, int len) |
| 1380 |
{ |
| 1381 |
const char *p; |
| 1382 |
char itoa_buf[GFC_BTOA_BUF_SIZE]; |
| 1383 |
GFC_UINTEGER_LARGEST n = 0; |
| 1384 |
|
| 1385 |
/* Ensure we end up with a null terminated string. */ |
| 1386 |
memset(itoa_buf, '\0', GFC_BTOA_BUF_SIZE); |
| 1387 |
|
| 1388 |
if (len > (int) sizeof (GFC_UINTEGER_LARGEST)) |
| 1389 |
{ |
| 1390 |
p = btoa_big (source, itoa_buf, len, &n); |
| 1391 |
write_boz (dtp, f, p, n, len); |
| 1392 |
} |
| 1393 |
else |
| 1394 |
{ |
| 1395 |
n = extract_uint (source, len); |
| 1396 |
p = btoa (n, itoa_buf, sizeof (itoa_buf)); |
| 1397 |
|
| 1398 |
/* Test for zero. Needed by write_boz. */ |
| 1399 |
if (n != 0) |
| 1400 |
n = 1; |
| 1401 |
write_boz (dtp, f, p, n, len); |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
void |
| 1407 |
write_o (st_parameter_dt *dtp, const fnode *f, const char *source, int len) |
| 1408 |
{ |
| 1409 |
const char *p; |
| 1410 |
char itoa_buf[GFC_OTOA_BUF_SIZE]; |
| 1411 |
GFC_UINTEGER_LARGEST n = 0; |
| 1412 |
|
| 1413 |
if (len > (int) sizeof (GFC_UINTEGER_LARGEST)) |
| 1414 |
{ |
| 1415 |
p = otoa_big (source, itoa_buf, len, &n); |
| 1416 |
write_boz (dtp, f, p, n, len); |
| 1417 |
} |
| 1418 |
else |
| 1419 |
{ |
| 1420 |
n = extract_uint (source, len); |
| 1421 |
p = otoa (n, itoa_buf, sizeof (itoa_buf)); |
| 1422 |
write_boz (dtp, f, p, n, len); |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
void |
| 1427 |
write_z (st_parameter_dt *dtp, const fnode *f, const char *source, int len) |
| 1428 |
{ |
| 1429 |
const char *p; |
| 1430 |
char itoa_buf[GFC_XTOA_BUF_SIZE]; |
| 1431 |
GFC_UINTEGER_LARGEST n = 0; |
| 1432 |
|
| 1433 |
if (len > (int) sizeof (GFC_UINTEGER_LARGEST)) |
| 1434 |
{ |
| 1435 |
p = xtoa_big (source, itoa_buf, len, &n); |
| 1436 |
write_boz (dtp, f, p, n, len); |
| 1437 |
} |
| 1438 |
else |
| 1439 |
{ |
| 1440 |
n = extract_uint (source, len); |
| 1441 |
p = xtoa (n, itoa_buf, sizeof (itoa_buf)); |
| 1442 |
write_boz (dtp, f, p, n, len); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* Take care of the X/TR descriptor. */ |
| 1447 |
|
| 1448 |
void |
| 1449 |
write_x (st_parameter_dt *dtp, int len, int nspaces) |
| 1450 |
{ |
| 1451 |
char *p; |
| 1452 |
|
| 1453 |
p = write_block (dtp, len); |
| 1454 |
if (p == NULL) |
| 1455 |
return; |
| 1456 |
if (nspaces > 0 && len - nspaces >= 0) |
| 1457 |
{ |
| 1458 |
if (unlikely (is_char4_unit (dtp))) |
| 1459 |
{ |
| 1460 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 1461 |
memset4 (&p4[len - nspaces], ' ', nspaces); |
| 1462 |
} |
| 1463 |
else |
| 1464 |
memset (&p[len - nspaces], ' ', nspaces); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
|
| 1469 |
/* List-directed writing. */ |
| 1470 |
|
| 1471 |
|
| 1472 |
/* Write a single character to the output. Returns nonzero if |
| 1473 |
something goes wrong. */ |
| 1474 |
|
| 1475 |
static int |
| 1476 |
write_char (st_parameter_dt *dtp, int c) |
| 1477 |
{ |
| 1478 |
char *p; |
| 1479 |
|
| 1480 |
p = write_block (dtp, 1); |
| 1481 |
if (p == NULL) |
| 1482 |
return 1; |
| 1483 |
if (unlikely (is_char4_unit (dtp))) |
| 1484 |
{ |
| 1485 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 1486 |
*p4 = c; |
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
*p = (uchar) c; |
| 1491 |
|
| 1492 |
return 0; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
/* Write a list-directed logical value. */ |
| 1497 |
|
| 1498 |
static void |
| 1499 |
write_logical (st_parameter_dt *dtp, const char *source, int length) |
| 1500 |
{ |
| 1501 |
write_char (dtp, extract_int (source, length) ? 'T' : 'F'); |
| 1502 |
} |
| 1503 |
|
| 1504 |
|
| 1505 |
/* Write a list-directed integer value. */ |
| 1506 |
|
| 1507 |
static void |
| 1508 |
write_integer (st_parameter_dt *dtp, const char *source, int kind) |
| 1509 |
{ |
| 1510 |
int width; |
| 1511 |
fnode f; |
| 1512 |
|
| 1513 |
switch (kind) |
| 1514 |
{ |
| 1515 |
case 1: |
| 1516 |
width = 4; |
| 1517 |
break; |
| 1518 |
|
| 1519 |
case 2: |
| 1520 |
width = 6; |
| 1521 |
break; |
| 1522 |
|
| 1523 |
case 4: |
| 1524 |
width = 11; |
| 1525 |
break; |
| 1526 |
|
| 1527 |
case 8: |
| 1528 |
width = 20; |
| 1529 |
break; |
| 1530 |
|
| 1531 |
case 16: |
| 1532 |
width = 40; |
| 1533 |
break; |
| 1534 |
|
| 1535 |
default: |
| 1536 |
width = 0; |
| 1537 |
break; |
| 1538 |
} |
| 1539 |
f.u.integer.w = width; |
| 1540 |
f.u.integer.m = -1; |
| 1541 |
f.format = FMT_NONE; |
| 1542 |
write_decimal (dtp, &f, source, kind); |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* Write a list-directed unsigned value. We use the same formatting |
| 1546 |
as for integer. */ |
| 1547 |
|
| 1548 |
static void |
| 1549 |
write_unsigned (st_parameter_dt *dtp, const char *source, int kind) |
| 1550 |
{ |
| 1551 |
int width; |
| 1552 |
fnode f; |
| 1553 |
|
| 1554 |
switch (kind) |
| 1555 |
{ |
| 1556 |
case 1: |
| 1557 |
width = 4; |
| 1558 |
break; |
| 1559 |
|
| 1560 |
case 2: |
| 1561 |
width = 6; |
| 1562 |
break; |
| 1563 |
|
| 1564 |
case 4: |
| 1565 |
width = 11; |
| 1566 |
break; |
| 1567 |
|
| 1568 |
case 8: |
| 1569 |
width = 20; |
| 1570 |
break; |
| 1571 |
|
| 1572 |
case 16: |
| 1573 |
width = 40; |
| 1574 |
break; |
| 1575 |
|
| 1576 |
default: |
| 1577 |
width = 0; |
| 1578 |
break; |
| 1579 |
} |
| 1580 |
f.u.integer.w = width; |
| 1581 |
f.u.integer.m = -1; |
| 1582 |
f.format = FMT_NONE; |
| 1583 |
write_decimal_unsigned (dtp, &f, source, kind); |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
/* Write a list-directed string. We have to worry about delimiting |
| 1588 |
the strings if the file has been opened in that mode. */ |
| 1589 |
|
| 1590 |
#define DELIM 1 |
| 1591 |
#define NODELIM 0 |
| 1592 |
|
| 1593 |
static void |
| 1594 |
write_character (st_parameter_dt *dtp, const char *source, int kind, size_t length, int mode) |
| 1595 |
{ |
| 1596 |
size_t extra; |
| 1597 |
char *p, d; |
| 1598 |
|
| 1599 |
if (mode == DELIM) |
| 1600 |
{ |
| 1601 |
switch (dtp->u.p.current_unit->delim_status) |
| 1602 |
{ |
| 1603 |
case DELIM_APOSTROPHE: |
| 1604 |
d = '\''; |
| 1605 |
break; |
| 1606 |
case DELIM_QUOTE: |
| 1607 |
d = '"'; |
| 1608 |
break; |
| 1609 |
default: |
| 1610 |
d = ' '; |
| 1611 |
break; |
| 1612 |
} |
| 1613 |
} |
| 1614 |
else |
| 1615 |
d = ' '; |
| 1616 |
|
| 1617 |
if (kind == 1) |
| 1618 |
{ |
| 1619 |
if (d == ' ') |
| 1620 |
extra = 0; |
| 1621 |
else |
| 1622 |
{ |
| 1623 |
extra = 2; |
| 1624 |
|
| 1625 |
for (size_t i = 0; i < length; i++) |
| 1626 |
if (source[i] == d) |
| 1627 |
extra++; |
| 1628 |
} |
| 1629 |
|
| 1630 |
p = write_block (dtp, length + extra); |
| 1631 |
if (p == NULL) |
| 1632 |
return; |
| 1633 |
|
| 1634 |
if (unlikely (is_char4_unit (dtp))) |
| 1635 |
{ |
| 1636 |
gfc_char4_t d4 = (gfc_char4_t) d; |
| 1637 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 1638 |
|
| 1639 |
if (d4 == ' ') |
| 1640 |
memcpy4 (p4, source, length); |
| 1641 |
else |
| 1642 |
{ |
| 1643 |
*p4++ = d4; |
| 1644 |
|
| 1645 |
for (size_t i = 0; i < length; i++) |
| 1646 |
{ |
| 1647 |
*p4++ = (gfc_char4_t) source[i]; |
| 1648 |
if (source[i] == d) |
| 1649 |
*p4++ = d4; |
| 1650 |
} |
| 1651 |
|
| 1652 |
*p4 = d4; |
| 1653 |
} |
| 1654 |
return; |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (d == ' ') |
| 1658 |
memcpy (p, source, length); |
| 1659 |
else |
| 1660 |
{ |
| 1661 |
*p++ = d; |
| 1662 |
|
| 1663 |
for (size_t i = 0; i < length; i++) |
| 1664 |
{ |
| 1665 |
*p++ = source[i]; |
| 1666 |
if (source[i] == d) |
| 1667 |
*p++ = d; |
| 1668 |
} |
| 1669 |
|
| 1670 |
*p = d; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
else |
| 1674 |
{ |
| 1675 |
if (d == ' ') |
| 1676 |
{ |
| 1677 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 1678 |
write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0); |
| 1679 |
else |
| 1680 |
write_default_char4 (dtp, (gfc_char4_t *) source, length, 0); |
| 1681 |
} |
| 1682 |
else |
| 1683 |
{ |
| 1684 |
p = write_block (dtp, 1); |
| 1685 |
*p = d; |
| 1686 |
|
| 1687 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 1688 |
write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0); |
| 1689 |
else |
| 1690 |
write_default_char4 (dtp, (gfc_char4_t *) source, length, 0); |
| 1691 |
|
| 1692 |
p = write_block (dtp, 1); |
| 1693 |
*p = d; |
| 1694 |
} |
| 1695 |
} |
| 1696 |
} |
| 1697 |
|
| 1698 |
/* Floating point helper functions. */ |
| 1699 |
|
| 1700 |
#define BUF_STACK_SZ 384 |
| 1701 |
|
| 1702 |
static int |
| 1703 |
get_precision (st_parameter_dt *dtp, const fnode *f, const char *source, int kind) |
| 1704 |
{ |
| 1705 |
if (f->format != FMT_EN) |
| 1706 |
return determine_precision (dtp, f, kind); |
| 1707 |
else |
| 1708 |
return determine_en_precision (dtp, f, source, kind); |
| 1709 |
} |
| 1710 |
|
| 1711 |
/* 4932 is the maximum exponent of long double and quad precision, 3 |
| 1712 |
extra characters for the sign, the decimal point, and the |
| 1713 |
trailing null. Extra digits are added by the calling functions for |
| 1714 |
requested precision. Likewise for float and double. F0 editing produces |
| 1715 |
full precision output. */ |
| 1716 |
static int |
| 1717 |
size_from_kind (st_parameter_dt *dtp, const fnode *f, int kind) |
| 1718 |
{ |
| 1719 |
int size; |
| 1720 |
|
| 1721 |
if ((f->format == FMT_F && f->u.real.w == 0) || f->u.real.w == DEFAULT_WIDTH) |
| 1722 |
{ |
| 1723 |
switch (kind) |
| 1724 |
{ |
| 1725 |
case 4: |
| 1726 |
size = 38 + 3; /* These constants shown for clarity. */ |
| 1727 |
break; |
| 1728 |
case 8: |
| 1729 |
size = 308 + 3; |
| 1730 |
break; |
| 1731 |
case 10: |
| 1732 |
size = 4932 + 3; |
| 1733 |
break; |
| 1734 |
case 16: |
| 1735 |
#ifdef HAVE_GFC_REAL_17 |
| 1736 |
case 17: |
| 1737 |
#endif |
| 1738 |
size = 4932 + 3; |
| 1739 |
break; |
| 1740 |
default: |
| 1741 |
internal_error (&dtp->common, "bad real kind"); |
| 1742 |
break; |
| 1743 |
} |
| 1744 |
} |
| 1745 |
else |
| 1746 |
size = f->u.real.w + 1; /* One byte for a NULL character. */ |
| 1747 |
|
| 1748 |
return size; |
| 1749 |
} |
| 1750 |
|
| 1751 |
static char * |
| 1752 |
select_buffer (st_parameter_dt *dtp, const fnode *f, int precision, |
| 1753 |
char *buf, size_t *size, int kind) |
| 1754 |
{ |
| 1755 |
char *result; |
| 1756 |
|
| 1757 |
/* The buffer needs at least one more byte to allow room for |
| 1758 |
normalizing and 1 to hold null terminator. */ |
| 1759 |
*size = size_from_kind (dtp, f, kind) + precision + 1 + 1; |
| 1760 |
|
| 1761 |
if (*size > BUF_STACK_SZ) |
| 1762 |
result = xmalloc (*size); |
| 1763 |
else |
| 1764 |
result = buf; |
| 1765 |
return result; |
| 1766 |
} |
| 1767 |
|
| 1768 |
static char * |
| 1769 |
select_string (st_parameter_dt *dtp, const fnode *f, char *buf, size_t *size, |
| 1770 |
int kind) |
| 1771 |
{ |
| 1772 |
char *result; |
| 1773 |
*size = size_from_kind (dtp, f, kind) + f->u.real.d + 1; |
| 1774 |
if (*size > BUF_STACK_SZ) |
| 1775 |
result = xmalloc (*size); |
| 1776 |
else |
| 1777 |
result = buf; |
| 1778 |
return result; |
| 1779 |
} |
| 1780 |
|
| 1781 |
static void |
| 1782 |
write_float_string (st_parameter_dt *dtp, char *fstr, size_t len) |
| 1783 |
{ |
| 1784 |
char *p = write_block (dtp, len); |
| 1785 |
if (p == NULL) |
| 1786 |
return; |
| 1787 |
|
| 1788 |
if (unlikely (is_char4_unit (dtp))) |
| 1789 |
{ |
| 1790 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 1791 |
memcpy4 (p4, fstr, len); |
| 1792 |
return; |
| 1793 |
} |
| 1794 |
memcpy (p, fstr, len); |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
static void |
| 1799 |
write_float_0 (st_parameter_dt *dtp, const fnode *f, const char *source, int kind) |
| 1800 |
{ |
| 1801 |
char buf_stack[BUF_STACK_SZ]; |
| 1802 |
char str_buf[BUF_STACK_SZ]; |
| 1803 |
char *buffer, *result; |
| 1804 |
size_t buf_size, res_len, flt_str_len; |
| 1805 |
|
| 1806 |
/* Precision for snprintf call. */ |
| 1807 |
int precision = get_precision (dtp, f, source, kind); |
| 1808 |
|
| 1809 |
/* String buffer to hold final result. */ |
| 1810 |
result = select_string (dtp, f, str_buf, &res_len, kind); |
| 1811 |
|
| 1812 |
buffer = select_buffer (dtp, f, precision, buf_stack, &buf_size, kind); |
| 1813 |
|
| 1814 |
get_float_string (dtp, f, source , kind, 0, buffer, |
| 1815 |
precision, buf_size, result, &flt_str_len); |
| 1816 |
write_float_string (dtp, result, flt_str_len); |
| 1817 |
|
| 1818 |
if (buf_size > BUF_STACK_SZ) |
| 1819 |
free (buffer); |
| 1820 |
if (res_len > BUF_STACK_SZ) |
| 1821 |
free (result); |
| 1822 |
} |
| 1823 |
|
| 1824 |
void |
| 1825 |
write_d (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1826 |
{ |
| 1827 |
write_float_0 (dtp, f, p, len); |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
void |
| 1832 |
write_e (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1833 |
{ |
| 1834 |
write_float_0 (dtp, f, p, len); |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
void |
| 1839 |
write_f (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1840 |
{ |
| 1841 |
write_float_0 (dtp, f, p, len); |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
void |
| 1846 |
write_en (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1847 |
{ |
| 1848 |
write_float_0 (dtp, f, p, len); |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
void |
| 1853 |
write_es (st_parameter_dt *dtp, const fnode *f, const char *p, int len) |
| 1854 |
{ |
| 1855 |
write_float_0 (dtp, f, p, len); |
| 1856 |
} |
| 1857 |
|
| 1858 |
|
| 1859 |
/* Set an fnode to default format. */ |
| 1860 |
|
| 1861 |
static void |
| 1862 |
set_fnode_default (st_parameter_dt *dtp, fnode *f, int length) |
| 1863 |
{ |
| 1864 |
f->format = FMT_G; |
| 1865 |
switch (length) |
| 1866 |
{ |
| 1867 |
case 4: |
| 1868 |
f->u.real.w = 16; |
| 1869 |
f->u.real.d = 9; |
| 1870 |
f->u.real.e = 2; |
| 1871 |
break; |
| 1872 |
case 8: |
| 1873 |
f->u.real.w = 25; |
| 1874 |
f->u.real.d = 17; |
| 1875 |
f->u.real.e = 3; |
| 1876 |
break; |
| 1877 |
case 10: |
| 1878 |
f->u.real.w = 30; |
| 1879 |
f->u.real.d = 21; |
| 1880 |
f->u.real.e = 4; |
| 1881 |
break; |
| 1882 |
case 16: |
| 1883 |
/* Adjust decimal precision depending on binary precision, 106 or 113. */ |
| 1884 |
#if GFC_REAL_16_DIGITS == 113 |
| 1885 |
f->u.real.w = 45; |
| 1886 |
f->u.real.d = 36; |
| 1887 |
f->u.real.e = 4; |
| 1888 |
#else |
| 1889 |
f->u.real.w = 41; |
| 1890 |
f->u.real.d = 32; |
| 1891 |
f->u.real.e = 4; |
| 1892 |
#endif |
| 1893 |
break; |
| 1894 |
#ifdef HAVE_GFC_REAL_17 |
| 1895 |
case 17: |
| 1896 |
f->u.real.w = 45; |
| 1897 |
f->u.real.d = 36; |
| 1898 |
f->u.real.e = 4; |
| 1899 |
break; |
| 1900 |
#endif |
| 1901 |
default: |
| 1902 |
internal_error (&dtp->common, "bad real kind"); |
| 1903 |
break; |
| 1904 |
} |
| 1905 |
} |
| 1906 |
|
| 1907 |
/* Output a real number with default format. |
| 1908 |
To guarantee that a binary -> decimal -> binary roundtrip conversion |
| 1909 |
recovers the original value, IEEE 754-2008 requires 9, 17, 21 and 36 |
| 1910 |
significant digits for REAL kinds 4, 8, 10, and 16, respectively. |
| 1911 |
Thus, we use 1PG16.9E2 for REAL(4), 1PG25.17E3 for REAL(8), 1PG30.21E4 |
| 1912 |
for REAL(10) and 1PG45.36E4 for REAL(16). The exception is that the |
| 1913 |
Fortran standard requires outputting an extra digit when the scale |
| 1914 |
factor is 1 and when the magnitude of the value is such that E |
| 1915 |
editing is used. However, gfortran compensates for this, and thus |
| 1916 |
for list formatted the same number of significant digits is |
| 1917 |
generated both when using F and E editing. */ |
| 1918 |
|
| 1919 |
void |
| 1920 |
write_real (st_parameter_dt *dtp, const char *source, int kind) |
| 1921 |
{ |
| 1922 |
fnode f ; |
| 1923 |
char buf_stack[BUF_STACK_SZ]; |
| 1924 |
char str_buf[BUF_STACK_SZ]; |
| 1925 |
char *buffer, *result; |
| 1926 |
size_t buf_size, res_len, flt_str_len; |
| 1927 |
int orig_scale = dtp->u.p.scale_factor; |
| 1928 |
dtp->u.p.scale_factor = 1; |
| 1929 |
set_fnode_default (dtp, &f, kind); |
| 1930 |
|
| 1931 |
/* Precision for snprintf call. */ |
| 1932 |
int precision = get_precision (dtp, &f, source, kind); |
| 1933 |
|
| 1934 |
/* String buffer to hold final result. */ |
| 1935 |
result = select_string (dtp, &f, str_buf, &res_len, kind); |
| 1936 |
|
| 1937 |
/* Scratch buffer to hold final result. */ |
| 1938 |
buffer = select_buffer (dtp, &f, precision, buf_stack, &buf_size, kind); |
| 1939 |
|
| 1940 |
get_float_string (dtp, &f, source , kind, 1, buffer, |
| 1941 |
precision, buf_size, result, &flt_str_len); |
| 1942 |
write_float_string (dtp, result, flt_str_len); |
| 1943 |
|
| 1944 |
dtp->u.p.scale_factor = orig_scale; |
| 1945 |
if (buf_size > BUF_STACK_SZ) |
| 1946 |
free (buffer); |
| 1947 |
if (res_len > BUF_STACK_SZ) |
| 1948 |
free (result); |
| 1949 |
} |
| 1950 |
|
| 1951 |
/* Similar to list formatted REAL output, for kPG0 where k > 0 we |
| 1952 |
compensate for the extra digit. */ |
| 1953 |
|
| 1954 |
void |
| 1955 |
write_real_w0 (st_parameter_dt *dtp, const char *source, int kind, |
| 1956 |
const fnode* f) |
| 1957 |
{ |
| 1958 |
fnode ff; |
| 1959 |
char buf_stack[BUF_STACK_SZ]; |
| 1960 |
char str_buf[BUF_STACK_SZ]; |
| 1961 |
char *buffer, *result; |
| 1962 |
size_t buf_size, res_len, flt_str_len; |
| 1963 |
int comp_d = 0; |
| 1964 |
|
| 1965 |
set_fnode_default (dtp, &ff, kind); |
| 1966 |
|
| 1967 |
ff.format = f->format; |
| 1968 |
|
| 1969 |
/* For FMT_G, Compensate for extra digits when using scale factor, d |
| 1970 |
is not specified, and the magnitude is such that E editing |
| 1971 |
is used. */ |
| 1972 |
if (f->format == FMT_G) |
| 1973 |
{ |
| 1974 |
if (f->u.real.d > 0) |
| 1975 |
ff.u.real.d = f->u.real.d; |
| 1976 |
if (dtp->u.p.scale_factor > 0 && f->u.real.d == 0) |
| 1977 |
comp_d = 1; |
| 1978 |
else |
| 1979 |
comp_d = 0; |
| 1980 |
} |
| 1981 |
else |
| 1982 |
if (f->u.real.d >= 0) |
| 1983 |
ff.u.real.d = f->u.real.d; |
| 1984 |
|
| 1985 |
|
| 1986 |
if (f->u.real.e >= 0) |
| 1987 |
ff.u.real.e = f->u.real.e; |
| 1988 |
|
| 1989 |
dtp->u.p.g0_no_blanks = 1; |
| 1990 |
|
| 1991 |
/* Precision for snprintf call. */ |
| 1992 |
int precision = get_precision (dtp, &ff, source, kind); |
| 1993 |
|
| 1994 |
/* String buffer to hold final result. */ |
| 1995 |
result = select_string (dtp, &ff, str_buf, &res_len, kind); |
| 1996 |
|
| 1997 |
buffer = select_buffer (dtp, &ff, precision, buf_stack, &buf_size, kind); |
| 1998 |
|
| 1999 |
get_float_string (dtp, &ff, source , kind, comp_d, buffer, |
| 2000 |
precision, buf_size, result, &flt_str_len); |
| 2001 |
write_float_string (dtp, result, flt_str_len); |
| 2002 |
|
| 2003 |
dtp->u.p.g0_no_blanks = 0; |
| 2004 |
if (buf_size > BUF_STACK_SZ) |
| 2005 |
free (buffer); |
| 2006 |
if (res_len > BUF_STACK_SZ) |
| 2007 |
free (result); |
| 2008 |
} |
| 2009 |
|
| 2010 |
|
| 2011 |
static void |
| 2012 |
write_complex (st_parameter_dt *dtp, const char *source, int kind, size_t size) |
| 2013 |
{ |
| 2014 |
char semi_comma = |
| 2015 |
dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'; |
| 2016 |
|
| 2017 |
/* Set for no blanks so we get a string result with no leading |
| 2018 |
blanks. We will pad left later. */ |
| 2019 |
dtp->u.p.g0_no_blanks = 1; |
| 2020 |
|
| 2021 |
fnode f ; |
| 2022 |
char buf_stack[BUF_STACK_SZ]; |
| 2023 |
char str1_buf[BUF_STACK_SZ]; |
| 2024 |
char str2_buf[BUF_STACK_SZ]; |
| 2025 |
char *buffer, *result1, *result2; |
| 2026 |
size_t buf_size, res_len1, res_len2, flt_str_len1, flt_str_len2; |
| 2027 |
int width, lblanks, orig_scale = dtp->u.p.scale_factor; |
| 2028 |
|
| 2029 |
dtp->u.p.scale_factor = 1; |
| 2030 |
set_fnode_default (dtp, &f, kind); |
| 2031 |
|
| 2032 |
/* Set width for two values, parenthesis, and comma. */ |
| 2033 |
width = 2 * f.u.real.w + 3; |
| 2034 |
|
| 2035 |
/* Set for no blanks so we get a string result with no leading |
| 2036 |
blanks. We will pad left later. */ |
| 2037 |
dtp->u.p.g0_no_blanks = 1; |
| 2038 |
|
| 2039 |
/* Precision for snprintf call. */ |
| 2040 |
int precision = get_precision (dtp, &f, source, kind); |
| 2041 |
|
| 2042 |
/* String buffers to hold final result. */ |
| 2043 |
result1 = select_string (dtp, &f, str1_buf, &res_len1, kind); |
| 2044 |
result2 = select_string (dtp, &f, str2_buf, &res_len2, kind); |
| 2045 |
|
| 2046 |
buffer = select_buffer (dtp, &f, precision, buf_stack, &buf_size, kind); |
| 2047 |
|
| 2048 |
get_float_string (dtp, &f, source , kind, 0, buffer, |
| 2049 |
precision, buf_size, result1, &flt_str_len1); |
| 2050 |
get_float_string (dtp, &f, source + size / 2 , kind, 0, buffer, |
| 2051 |
precision, buf_size, result2, &flt_str_len2); |
| 2052 |
if (!dtp->u.p.namelist_mode) |
| 2053 |
{ |
| 2054 |
lblanks = width - flt_str_len1 - flt_str_len2 - 3; |
| 2055 |
write_x (dtp, lblanks, lblanks); |
| 2056 |
} |
| 2057 |
write_char (dtp, '('); |
| 2058 |
write_float_string (dtp, result1, flt_str_len1); |
| 2059 |
write_char (dtp, semi_comma); |
| 2060 |
write_float_string (dtp, result2, flt_str_len2); |
| 2061 |
write_char (dtp, ')'); |
| 2062 |
|
| 2063 |
dtp->u.p.scale_factor = orig_scale; |
| 2064 |
dtp->u.p.g0_no_blanks = 0; |
| 2065 |
if (buf_size > BUF_STACK_SZ) |
| 2066 |
free (buffer); |
| 2067 |
if (res_len1 > BUF_STACK_SZ) |
| 2068 |
free (result1); |
| 2069 |
if (res_len2 > BUF_STACK_SZ) |
| 2070 |
free (result2); |
| 2071 |
} |
| 2072 |
|
| 2073 |
|
| 2074 |
/* Write the separator between items. */ |
| 2075 |
|
| 2076 |
static void |
| 2077 |
write_separator (st_parameter_dt *dtp) |
| 2078 |
{ |
| 2079 |
char *p; |
| 2080 |
|
| 2081 |
p = write_block (dtp, options.separator_len); |
| 2082 |
if (p == NULL) |
| 2083 |
return; |
| 2084 |
if (unlikely (is_char4_unit (dtp))) |
| 2085 |
{ |
| 2086 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 2087 |
memcpy4 (p4, options.separator, options.separator_len); |
| 2088 |
} |
| 2089 |
else |
| 2090 |
memcpy (p, options.separator, options.separator_len); |
| 2091 |
} |
| 2092 |
|
| 2093 |
|
| 2094 |
/* Write an item with list formatting. |
| 2095 |
TODO: handle skipping to the next record correctly, particularly |
| 2096 |
with strings. */ |
| 2097 |
|
| 2098 |
static void |
| 2099 |
list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 2100 |
size_t size) |
| 2101 |
{ |
| 2102 |
if (dtp->u.p.current_unit == NULL) |
| 2103 |
return; |
| 2104 |
|
| 2105 |
if (dtp->u.p.first_item) |
| 2106 |
{ |
| 2107 |
dtp->u.p.first_item = 0; |
| 2108 |
if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN) |
| 2109 |
write_char (dtp, ' '); |
| 2110 |
} |
| 2111 |
else |
| 2112 |
{ |
| 2113 |
if (type != BT_CHARACTER || !dtp->u.p.char_flag || |
| 2114 |
(dtp->u.p.current_unit->delim_status != DELIM_NONE |
| 2115 |
&& dtp->u.p.current_unit->delim_status != DELIM_UNSPECIFIED)) |
| 2116 |
write_separator (dtp); |
| 2117 |
} |
| 2118 |
|
| 2119 |
switch (type) |
| 2120 |
{ |
| 2121 |
case BT_INTEGER: |
| 2122 |
write_integer (dtp, p, kind); |
| 2123 |
break; |
| 2124 |
case BT_UNSIGNED: |
| 2125 |
write_unsigned (dtp, p, kind); |
| 2126 |
break; |
| 2127 |
case BT_LOGICAL: |
| 2128 |
write_logical (dtp, p, kind); |
| 2129 |
break; |
| 2130 |
case BT_CHARACTER: |
| 2131 |
write_character (dtp, p, kind, size, DELIM); |
| 2132 |
break; |
| 2133 |
case BT_REAL: |
| 2134 |
write_real (dtp, p, kind); |
| 2135 |
break; |
| 2136 |
case BT_COMPLEX: |
| 2137 |
write_complex (dtp, p, kind, size); |
| 2138 |
break; |
| 2139 |
case BT_CLASS: |
| 2140 |
{ |
| 2141 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 2142 |
char iotype[] = "LISTDIRECTED"; |
| 2143 |
gfc_charlen_type iotype_len = 12; |
| 2144 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 2145 |
char *child_iomsg; |
| 2146 |
gfc_charlen_type child_iomsg_len; |
| 2147 |
GFC_INTEGER_4 noiostat; |
| 2148 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 2149 |
gfc_full_array_i4 vlist; |
| 2150 |
|
| 2151 |
GFC_DESCRIPTOR_DATA(&vlist) = NULL; |
| 2152 |
GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0); |
| 2153 |
|
| 2154 |
/* Set iostat, intent(out). */ |
| 2155 |
noiostat = 0; |
| 2156 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 2157 |
? dtp->common.iostat : &noiostat); |
| 2158 |
|
| 2159 |
/* Set iomsge, intent(inout). */ |
| 2160 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 2161 |
{ |
| 2162 |
child_iomsg = dtp->common.iomsg; |
| 2163 |
child_iomsg_len = dtp->common.iomsg_len; |
| 2164 |
} |
| 2165 |
else |
| 2166 |
{ |
| 2167 |
child_iomsg = tmp_iomsg; |
| 2168 |
child_iomsg_len = IOMSG_LEN; |
| 2169 |
} |
| 2170 |
|
| 2171 |
/* Call the user defined formatted WRITE procedure. */ |
| 2172 |
dtp->u.p.current_unit->child_dtio++; |
| 2173 |
dtp->u.p.fdtio_ptr (p, &unit, iotype, &vlist, |
| 2174 |
child_iostat, child_iomsg, |
| 2175 |
iotype_len, child_iomsg_len); |
| 2176 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 2177 |
dtp->u.p.current_unit->child_dtio--; |
| 2178 |
|
| 2179 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 2180 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 2181 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 2182 |
{ |
| 2183 |
char message[IOMSG_LEN + 1]; |
| 2184 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 2185 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 2186 |
message[child_iomsg_len] = '\0'; |
| 2187 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 2188 |
message); |
| 2189 |
} |
| 2190 |
} |
| 2191 |
break; |
| 2192 |
default: |
| 2193 |
internal_error (&dtp->common, "list_formatted_write(): Bad type"); |
| 2194 |
} |
| 2195 |
|
| 2196 |
fbuf_flush_list (dtp->u.p.current_unit, LIST_WRITING); |
| 2197 |
dtp->u.p.char_flag = (type == BT_CHARACTER); |
| 2198 |
} |
| 2199 |
|
| 2200 |
|
| 2201 |
void |
| 2202 |
list_formatted_write (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 2203 |
size_t size, size_t nelems) |
| 2204 |
{ |
| 2205 |
size_t elem; |
| 2206 |
char *tmp; |
| 2207 |
size_t stride = type == BT_CHARACTER ? |
| 2208 |
size * GFC_SIZE_OF_CHAR_KIND(kind) : size; |
| 2209 |
|
| 2210 |
tmp = (char *) p; |
| 2211 |
|
| 2212 |
/* Big loop over all the elements. */ |
| 2213 |
for (elem = 0; elem < nelems; elem++) |
| 2214 |
{ |
| 2215 |
dtp->u.p.item_count++; |
| 2216 |
list_formatted_write_scalar (dtp, type, tmp + elem * stride, kind, size); |
| 2217 |
} |
| 2218 |
} |
| 2219 |
|
| 2220 |
/* NAMELIST OUTPUT |
| 2221 |
|
| 2222 |
nml_write_obj writes a namelist object to the output stream. It is called |
| 2223 |
recursively for derived type components: |
| 2224 |
obj = is the namelist_info for the current object. |
| 2225 |
offset = the offset relative to the address held by the object for |
| 2226 |
derived type arrays. |
| 2227 |
base = is the namelist_info of the derived type, when obj is a |
| 2228 |
component. |
| 2229 |
base_name = the full name for a derived type, including qualifiers |
| 2230 |
if any. |
| 2231 |
The returned value is a pointer to the object beyond the last one |
| 2232 |
accessed, including nested derived types. Notice that the namelist is |
| 2233 |
a linear linked list of objects, including derived types and their |
| 2234 |
components. A tree, of sorts, is implied by the compound names of |
| 2235 |
the derived type components and this is how this function recurses through |
| 2236 |
the list. */ |
| 2237 |
|
| 2238 |
/* A generous estimate of the number of characters needed to print |
| 2239 |
repeat counts and indices, including commas, asterices and brackets. */ |
| 2240 |
|
| 2241 |
#define NML_DIGITS 20 |
| 2242 |
|
| 2243 |
static void |
| 2244 |
namelist_write_newline (st_parameter_dt *dtp) |
| 2245 |
{ |
| 2246 |
if (!is_internal_unit (dtp)) |
| 2247 |
{ |
| 2248 |
#ifdef HAVE_CRLF |
| 2249 |
write_character (dtp, "\r\n", 1, 2, NODELIM); |
| 2250 |
#else |
| 2251 |
write_character (dtp, "\n", 1, 1, NODELIM); |
| 2252 |
#endif |
| 2253 |
return; |
| 2254 |
} |
| 2255 |
|
| 2256 |
if (is_array_io (dtp)) |
| 2257 |
{ |
| 2258 |
gfc_offset record; |
| 2259 |
int finished; |
| 2260 |
char *p; |
| 2261 |
int length = dtp->u.p.current_unit->bytes_left; |
| 2262 |
|
| 2263 |
p = write_block (dtp, length); |
| 2264 |
if (p == NULL) |
| 2265 |
return; |
| 2266 |
|
| 2267 |
if (unlikely (is_char4_unit (dtp))) |
| 2268 |
{ |
| 2269 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 2270 |
memset4 (p4, ' ', length); |
| 2271 |
} |
| 2272 |
else |
| 2273 |
memset (p, ' ', length); |
| 2274 |
|
| 2275 |
/* Now that the current record has been padded out, |
| 2276 |
determine where the next record in the array is. */ |
| 2277 |
record = next_array_record (dtp, dtp->u.p.current_unit->ls, |
| 2278 |
&finished); |
| 2279 |
if (finished) |
| 2280 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; |
| 2281 |
else |
| 2282 |
{ |
| 2283 |
/* Now seek to this record */ |
| 2284 |
record = record * dtp->u.p.current_unit->recl; |
| 2285 |
|
| 2286 |
if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0) |
| 2287 |
{ |
| 2288 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 2289 |
return; |
| 2290 |
} |
| 2291 |
|
| 2292 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 2293 |
} |
| 2294 |
} |
| 2295 |
else |
| 2296 |
write_character (dtp, " ", 1, 1, NODELIM); |
| 2297 |
} |
| 2298 |
|
| 2299 |
|
| 2300 |
static namelist_info * |
| 2301 |
nml_write_obj (st_parameter_dt *dtp, namelist_info *obj, index_type offset, |
| 2302 |
namelist_info *base, char *base_name) |
| 2303 |
{ |
| 2304 |
int rep_ctr; |
| 2305 |
int num; |
| 2306 |
int nml_carry; |
| 2307 |
int len; |
| 2308 |
index_type obj_size; |
| 2309 |
index_type nelem; |
| 2310 |
size_t dim_i; |
| 2311 |
size_t clen; |
| 2312 |
index_type elem_ctr; |
| 2313 |
size_t obj_name_len; |
| 2314 |
void *p; |
| 2315 |
char cup; |
| 2316 |
char *obj_name; |
| 2317 |
char *ext_name; |
| 2318 |
char *q; |
| 2319 |
size_t ext_name_len; |
| 2320 |
char rep_buff[NML_DIGITS]; |
| 2321 |
namelist_info *cmp; |
| 2322 |
namelist_info *retval = obj->next; |
| 2323 |
size_t base_name_len; |
| 2324 |
size_t base_var_name_len; |
| 2325 |
size_t tot_len; |
| 2326 |
|
| 2327 |
/* Set the character to be used to separate values |
| 2328 |
to a comma or semi-colon. */ |
| 2329 |
|
| 2330 |
char semi_comma = |
| 2331 |
dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'; |
| 2332 |
|
| 2333 |
/* Write namelist variable names in upper case. If a derived type, |
| 2334 |
nothing is output. If a component, base and base_name are set. */ |
| 2335 |
|
| 2336 |
if (obj->type != BT_DERIVED || obj->dtio_sub != NULL) |
| 2337 |
{ |
| 2338 |
namelist_write_newline (dtp); |
| 2339 |
write_character (dtp, " ", 1, 1, NODELIM); |
| 2340 |
|
| 2341 |
len = 0; |
| 2342 |
if (base) |
| 2343 |
{ |
| 2344 |
len = strlen (base->var_name); |
| 2345 |
base_name_len = strlen (base_name); |
| 2346 |
for (dim_i = 0; dim_i < base_name_len; dim_i++) |
| 2347 |
{ |
| 2348 |
cup = safe_toupper (base_name[dim_i]); |
| 2349 |
write_character (dtp, &cup, 1, 1, NODELIM); |
| 2350 |
} |
| 2351 |
} |
| 2352 |
clen = strlen (obj->var_name); |
| 2353 |
for (dim_i = len; dim_i < clen; dim_i++) |
| 2354 |
{ |
| 2355 |
cup = safe_toupper (obj->var_name[dim_i]); |
| 2356 |
if (cup == '+') |
| 2357 |
cup = '%'; |
| 2358 |
write_character (dtp, &cup, 1, 1, NODELIM); |
| 2359 |
} |
| 2360 |
write_character (dtp, "=", 1, 1, NODELIM); |
| 2361 |
} |
| 2362 |
|
| 2363 |
/* Counts the number of data output on a line, including names. */ |
| 2364 |
|
| 2365 |
num = 1; |
| 2366 |
|
| 2367 |
len = obj->len; |
| 2368 |
|
| 2369 |
switch (obj->type) |
| 2370 |
{ |
| 2371 |
|
| 2372 |
case BT_REAL: |
| 2373 |
obj_size = size_from_real_kind (len); |
| 2374 |
break; |
| 2375 |
|
| 2376 |
case BT_COMPLEX: |
| 2377 |
obj_size = size_from_complex_kind (len); |
| 2378 |
break; |
| 2379 |
|
| 2380 |
case BT_CHARACTER: |
| 2381 |
obj_size = obj->string_length; |
| 2382 |
break; |
| 2383 |
|
| 2384 |
default: |
| 2385 |
obj_size = len; |
| 2386 |
} |
| 2387 |
|
| 2388 |
if (obj->var_rank) |
| 2389 |
obj_size = obj->size; |
| 2390 |
|
| 2391 |
/* Set the index vector and count the number of elements. */ |
| 2392 |
|
| 2393 |
nelem = 1; |
| 2394 |
for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++) |
| 2395 |
{ |
| 2396 |
obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj, dim_i); |
| 2397 |
nelem = nelem * GFC_DESCRIPTOR_EXTENT (obj, dim_i); |
| 2398 |
} |
| 2399 |
|
| 2400 |
/* Main loop to output the data held in the object. */ |
| 2401 |
|
| 2402 |
rep_ctr = 1; |
| 2403 |
for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++) |
| 2404 |
{ |
| 2405 |
|
| 2406 |
/* Build the pointer to the data value. The offset is passed by |
| 2407 |
recursive calls to this function for arrays of derived types. |
| 2408 |
Is NULL otherwise. */ |
| 2409 |
|
| 2410 |
p = (void *)(obj->mem_pos + elem_ctr * obj_size); |
| 2411 |
p += offset; |
| 2412 |
|
| 2413 |
/* Check for repeat counts of intrinsic types. */ |
| 2414 |
|
| 2415 |
if ((elem_ctr < (nelem - 1)) && |
| 2416 |
(obj->type != BT_DERIVED) && |
| 2417 |
!memcmp (p, (void *)(p + obj_size ), obj_size )) |
| 2418 |
{ |
| 2419 |
rep_ctr++; |
| 2420 |
} |
| 2421 |
|
| 2422 |
/* Execute a repeated output. Note the flag no_leading_blank that |
| 2423 |
is used in the functions used to output the intrinsic types. */ |
| 2424 |
|
| 2425 |
else |
| 2426 |
{ |
| 2427 |
if (rep_ctr > 1) |
| 2428 |
{ |
| 2429 |
snprintf(rep_buff, NML_DIGITS, " %d*", rep_ctr); |
| 2430 |
write_character (dtp, rep_buff, 1, strlen (rep_buff), NODELIM); |
| 2431 |
dtp->u.p.no_leading_blank = 1; |
| 2432 |
} |
| 2433 |
num++; |
| 2434 |
|
| 2435 |
/* Output the data, if an intrinsic type, or recurse into this |
| 2436 |
routine to treat derived types. */ |
| 2437 |
|
| 2438 |
switch (obj->type) |
| 2439 |
{ |
| 2440 |
|
| 2441 |
case BT_INTEGER: |
| 2442 |
write_integer (dtp, p, len); |
| 2443 |
break; |
| 2444 |
|
| 2445 |
case BT_LOGICAL: |
| 2446 |
write_logical (dtp, p, len); |
| 2447 |
break; |
| 2448 |
|
| 2449 |
case BT_CHARACTER: |
| 2450 |
if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8) |
| 2451 |
write_character (dtp, p, 4, obj->string_length, DELIM); |
| 2452 |
else |
| 2453 |
write_character (dtp, p, 1, obj->string_length, DELIM); |
| 2454 |
break; |
| 2455 |
|
| 2456 |
case BT_REAL: |
| 2457 |
write_real (dtp, p, len); |
| 2458 |
break; |
| 2459 |
|
| 2460 |
case BT_COMPLEX: |
| 2461 |
dtp->u.p.no_leading_blank = 0; |
| 2462 |
num++; |
| 2463 |
write_complex (dtp, p, len, obj_size); |
| 2464 |
break; |
| 2465 |
|
| 2466 |
case BT_DERIVED: |
| 2467 |
case BT_CLASS: |
| 2468 |
/* To treat a derived type, we need to build two strings: |
| 2469 |
ext_name = the name, including qualifiers that prepends |
| 2470 |
component names in the output - passed to |
| 2471 |
nml_write_obj. |
| 2472 |
obj_name = the derived type name with no qualifiers but % |
| 2473 |
appended. This is used to identify the |
| 2474 |
components. */ |
| 2475 |
|
| 2476 |
/* First ext_name => get length of all possible components */ |
| 2477 |
if (obj->dtio_sub != NULL) |
| 2478 |
{ |
| 2479 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 2480 |
char iotype[] = "NAMELIST"; |
| 2481 |
gfc_charlen_type iotype_len = 8; |
| 2482 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 2483 |
char *child_iomsg; |
| 2484 |
gfc_charlen_type child_iomsg_len; |
| 2485 |
GFC_INTEGER_4 noiostat; |
| 2486 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 2487 |
gfc_full_array_i4 vlist; |
| 2488 |
formatted_dtio dtio_ptr = (formatted_dtio)obj->dtio_sub; |
| 2489 |
|
| 2490 |
GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0); |
| 2491 |
|
| 2492 |
/* Set iostat, intent(out). */ |
| 2493 |
noiostat = 0; |
| 2494 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 2495 |
? dtp->common.iostat : &noiostat); |
| 2496 |
|
| 2497 |
/* Set iomsg, intent(inout). */ |
| 2498 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 2499 |
{ |
| 2500 |
child_iomsg = dtp->common.iomsg; |
| 2501 |
child_iomsg_len = dtp->common.iomsg_len; |
| 2502 |
} |
| 2503 |
else |
| 2504 |
{ |
| 2505 |
child_iomsg = tmp_iomsg; |
| 2506 |
child_iomsg_len = IOMSG_LEN; |
| 2507 |
} |
| 2508 |
|
| 2509 |
/* Call the user defined formatted WRITE procedure. */ |
| 2510 |
dtp->u.p.current_unit->child_dtio++; |
| 2511 |
if (obj->type == BT_DERIVED) |
| 2512 |
{ |
| 2513 |
/* Build a class container. */ |
| 2514 |
gfc_class list_obj; |
| 2515 |
list_obj.data = p; |
| 2516 |
list_obj.vptr = obj->vtable; |
| 2517 |
list_obj.len = 0; |
| 2518 |
dtio_ptr ((void *)&list_obj, &unit, iotype, &vlist, |
| 2519 |
child_iostat, child_iomsg, |
| 2520 |
iotype_len, child_iomsg_len); |
| 2521 |
} |
| 2522 |
else |
| 2523 |
{ |
| 2524 |
dtio_ptr (p, &unit, iotype, &vlist, |
| 2525 |
child_iostat, child_iomsg, |
| 2526 |
iotype_len, child_iomsg_len); |
| 2527 |
} |
| 2528 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 2529 |
dtp->u.p.current_unit->child_dtio--; |
| 2530 |
|
| 2531 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 2532 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 2533 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 2534 |
{ |
| 2535 |
char message[IOMSG_LEN + 1]; |
| 2536 |
|
| 2537 |
/* Trim trailing spaces from the message. */ |
| 2538 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 2539 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 2540 |
message[child_iomsg_len] = '\0'; |
| 2541 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 2542 |
message); |
| 2543 |
} |
| 2544 |
|
| 2545 |
goto obj_loop; |
| 2546 |
} |
| 2547 |
|
| 2548 |
base_name_len = base_name ? strlen (base_name) : 0; |
| 2549 |
base_var_name_len = base ? strlen (base->var_name) : 0; |
| 2550 |
ext_name_len = base_name_len + base_var_name_len |
| 2551 |
+ strlen (obj->var_name) + obj->var_rank * NML_DIGITS + 1; |
| 2552 |
ext_name = xmalloc (ext_name_len); |
| 2553 |
|
| 2554 |
if (base_name) |
| 2555 |
memcpy (ext_name, base_name, base_name_len); |
| 2556 |
clen = strlen (obj->var_name + base_var_name_len); |
| 2557 |
memcpy (ext_name + base_name_len, |
| 2558 |
obj->var_name + base_var_name_len, clen); |
| 2559 |
|
| 2560 |
/* Append the qualifier. */ |
| 2561 |
|
| 2562 |
tot_len = base_name_len + clen; |
| 2563 |
for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++) |
| 2564 |
{ |
| 2565 |
if (!dim_i) |
| 2566 |
{ |
| 2567 |
ext_name[tot_len] = '('; |
| 2568 |
tot_len++; |
| 2569 |
} |
| 2570 |
snprintf (ext_name + tot_len, ext_name_len - tot_len, "%d", |
| 2571 |
(int) obj->ls[dim_i].idx); |
| 2572 |
tot_len += strlen (ext_name + tot_len); |
| 2573 |
ext_name[tot_len] = ((int) dim_i == obj->var_rank - 1) ? ')' : ','; |
| 2574 |
tot_len++; |
| 2575 |
} |
| 2576 |
|
| 2577 |
ext_name[tot_len] = '\0'; |
| 2578 |
for (q = ext_name; *q; q++) |
| 2579 |
if (*q == '+') |
| 2580 |
*q = '%'; |
| 2581 |
|
| 2582 |
/* Now obj_name. */ |
| 2583 |
|
| 2584 |
obj_name_len = strlen (obj->var_name) + 1; |
| 2585 |
obj_name = xmalloc (obj_name_len + 1); |
| 2586 |
memcpy (obj_name, obj->var_name, obj_name_len-1); |
| 2587 |
memcpy (obj_name + obj_name_len-1, "%", 2); |
| 2588 |
|
| 2589 |
/* Now loop over the components. Update the component pointer |
| 2590 |
with the return value from nml_write_obj => this loop jumps |
| 2591 |
past nested derived types. */ |
| 2592 |
|
| 2593 |
for (cmp = obj->next; |
| 2594 |
cmp && !strncmp (cmp->var_name, obj_name, obj_name_len); |
| 2595 |
cmp = retval) |
| 2596 |
{ |
| 2597 |
retval = nml_write_obj (dtp, cmp, |
| 2598 |
(index_type)(p - obj->mem_pos), |
| 2599 |
obj, ext_name); |
| 2600 |
} |
| 2601 |
|
| 2602 |
free (obj_name); |
| 2603 |
free (ext_name); |
| 2604 |
goto obj_loop; |
| 2605 |
|
| 2606 |
default: |
| 2607 |
internal_error (&dtp->common, "Bad type for namelist write"); |
| 2608 |
} |
| 2609 |
|
| 2610 |
/* Reset the leading blank suppression, write a comma (or semi-colon) |
| 2611 |
and, if 5 values have been output, write a newline and advance |
| 2612 |
to column 2. Reset the repeat counter. */ |
| 2613 |
|
| 2614 |
dtp->u.p.no_leading_blank = 0; |
| 2615 |
if (obj->type == BT_CHARACTER) |
| 2616 |
{ |
| 2617 |
if (dtp->u.p.nml_delim != '\0') |
| 2618 |
write_character (dtp, &semi_comma, 1, 1, NODELIM); |
| 2619 |
} |
| 2620 |
else |
| 2621 |
write_character (dtp, &semi_comma, 1, 1, NODELIM); |
| 2622 |
if (num > 5) |
| 2623 |
{ |
| 2624 |
num = 0; |
| 2625 |
if (dtp->u.p.nml_delim == '\0') |
| 2626 |
write_character (dtp, &semi_comma, 1, 1, NODELIM); |
| 2627 |
namelist_write_newline (dtp); |
| 2628 |
write_character (dtp, " ", 1, 1, NODELIM); |
| 2629 |
} |
| 2630 |
rep_ctr = 1; |
| 2631 |
} |
| 2632 |
|
| 2633 |
/* Cycle through and increment the index vector. */ |
| 2634 |
|
| 2635 |
obj_loop: |
| 2636 |
|
| 2637 |
nml_carry = 1; |
| 2638 |
for (dim_i = 0; nml_carry && (dim_i < (size_t) obj->var_rank); dim_i++) |
| 2639 |
{ |
| 2640 |
obj->ls[dim_i].idx += nml_carry ; |
| 2641 |
nml_carry = 0; |
| 2642 |
if (obj->ls[dim_i].idx > GFC_DESCRIPTOR_UBOUND(obj,dim_i)) |
| 2643 |
{ |
| 2644 |
obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj,dim_i); |
| 2645 |
nml_carry = 1; |
| 2646 |
} |
| 2647 |
} |
| 2648 |
} |
| 2649 |
|
| 2650 |
/* Return a pointer beyond the furthest object accessed. */ |
| 2651 |
|
| 2652 |
return retval; |
| 2653 |
} |
| 2654 |
|
| 2655 |
|
| 2656 |
/* This is the entry function for namelist writes. It outputs the name |
| 2657 |
of the namelist and iterates through the namelist by calls to |
| 2658 |
nml_write_obj. The call below has dummys in the arguments used in |
| 2659 |
the treatment of derived types. */ |
| 2660 |
|
| 2661 |
void |
| 2662 |
namelist_write (st_parameter_dt *dtp) |
| 2663 |
{ |
| 2664 |
namelist_info *t1, *t2, *dummy = NULL; |
| 2665 |
index_type dummy_offset = 0; |
| 2666 |
char c; |
| 2667 |
char *dummy_name = NULL; |
| 2668 |
|
| 2669 |
/* Set the delimiter for namelist output. */ |
| 2670 |
switch (dtp->u.p.current_unit->delim_status) |
| 2671 |
{ |
| 2672 |
case DELIM_APOSTROPHE: |
| 2673 |
dtp->u.p.nml_delim = '\''; |
| 2674 |
break; |
| 2675 |
case DELIM_QUOTE: |
| 2676 |
case DELIM_UNSPECIFIED: |
| 2677 |
dtp->u.p.nml_delim = '"'; |
| 2678 |
break; |
| 2679 |
default: |
| 2680 |
dtp->u.p.nml_delim = '\0'; |
| 2681 |
} |
| 2682 |
|
| 2683 |
if (is_internal_unit (dtp)) |
| 2684 |
write_character (dtp, " ", 1, 1, NODELIM); |
| 2685 |
write_character (dtp, "&", 1, 1, NODELIM); |
| 2686 |
|
| 2687 |
/* Write namelist name in upper case - f95 std. */ |
| 2688 |
for (gfc_charlen_type i = 0; i < dtp->namelist_name_len; i++ ) |
| 2689 |
{ |
| 2690 |
c = safe_toupper (dtp->namelist_name[i]); |
| 2691 |
write_character (dtp, &c, 1 ,1, NODELIM); |
| 2692 |
} |
| 2693 |
|
| 2694 |
if (dtp->u.p.ionml != NULL) |
| 2695 |
{ |
| 2696 |
t1 = dtp->u.p.ionml; |
| 2697 |
while (t1 != NULL) |
| 2698 |
{ |
| 2699 |
t2 = t1; |
| 2700 |
t1 = nml_write_obj (dtp, t2, dummy_offset, dummy, dummy_name); |
| 2701 |
} |
| 2702 |
} |
| 2703 |
|
| 2704 |
namelist_write_newline (dtp); |
| 2705 |
write_character (dtp, " /", 1, 2, NODELIM); |
| 2706 |
} |
| 2707 |
|
| 2708 |
#undef NML_DIGITS |