xfail gnat.dg/trampoline3.adb scan-assembler-not check on hppa*-*-*
[official-gcc.git] / libgfortran / io / write.c
blob49beaee4724e6978894b6554ccfad0fb41dca820
1 /* Copyright (C) 2002-2024 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
6 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
27 #include "io.h"
28 #include "fbuf.h"
29 #include "format.h"
30 #include "unix.h"
31 #include <assert.h>
32 #include <string.h>
34 #define star_fill(p, n) memset(p, '*', n)
36 typedef unsigned char uchar;
38 /* Helper functions for character(kind=4) internal units. These are needed
39 by write_float.def. */
41 static void
42 memcpy4 (gfc_char4_t *dest, const char *source, int k)
44 int j;
46 const char *p = source;
47 for (j = 0; j < k; j++)
48 *dest++ = (gfc_char4_t) *p++;
51 /* This include contains the heart and soul of formatted floating point. */
52 #include "write_float.def"
54 /* Write out default char4. */
56 static void
57 write_default_char4 (st_parameter_dt *dtp, const gfc_char4_t *source,
58 int src_len, int w_len)
60 char *p;
61 int j, k = 0;
62 gfc_char4_t c;
63 uchar d;
65 /* Take care of preceding blanks. */
66 if (w_len > src_len)
68 k = w_len - src_len;
69 p = write_block (dtp, k);
70 if (p == NULL)
71 return;
72 if (is_char4_unit (dtp))
74 gfc_char4_t *p4 = (gfc_char4_t *) p;
75 memset4 (p4, ' ', k);
77 else
78 memset (p, ' ', k);
81 /* Get ready to handle delimiters if needed. */
82 switch (dtp->u.p.current_unit->delim_status)
84 case DELIM_APOSTROPHE:
85 d = '\'';
86 break;
87 case DELIM_QUOTE:
88 d = '"';
89 break;
90 default:
91 d = ' ';
92 break;
95 /* Now process the remaining characters, one at a time. */
96 for (j = 0; j < src_len; j++)
98 c = source[j];
99 if (is_char4_unit (dtp))
101 gfc_char4_t *q;
102 /* Handle delimiters if any. */
103 if (c == d && d != ' ')
105 p = write_block (dtp, 2);
106 if (p == NULL)
107 return;
108 q = (gfc_char4_t *) p;
109 *q++ = c;
111 else
113 p = write_block (dtp, 1);
114 if (p == NULL)
115 return;
116 q = (gfc_char4_t *) p;
118 *q = c;
120 else
122 /* Handle delimiters if any. */
123 if (c == d && d != ' ')
125 p = write_block (dtp, 2);
126 if (p == NULL)
127 return;
128 *p++ = (uchar) c;
130 else
132 p = write_block (dtp, 1);
133 if (p == NULL)
134 return;
136 *p = c > 255 ? '?' : (uchar) c;
142 /* Write out UTF-8 converted from char4. */
144 static void
145 write_utf8_char4 (st_parameter_dt *dtp, gfc_char4_t *source,
146 int src_len, int w_len)
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;
156 /* Take care of preceding blanks. */
157 if (w_len > src_len)
159 k = w_len - src_len;
160 p = write_block (dtp, k);
161 if (p == NULL)
162 return;
163 memset (p, ' ', k);
166 /* Get ready to handle delimiters if needed. */
167 switch (dtp->u.p.current_unit->delim_status)
169 case DELIM_APOSTROPHE:
170 d = '\'';
171 break;
172 case DELIM_QUOTE:
173 d = '"';
174 break;
175 default:
176 d = ' ';
177 break;
180 /* Now process the remaining characters, one at a time. */
181 for (j = k; j < src_len; j++)
183 c = source[j];
184 if (c < 0x80)
186 /* Handle the delimiters if any. */
187 if (c == d && d != ' ')
189 p = write_block (dtp, 2);
190 if (p == NULL)
191 return;
192 *p++ = (uchar) c;
194 else
196 p = write_block (dtp, 1);
197 if (p == NULL)
198 return;
200 *p = (uchar) c;
202 else
204 /* Convert to UTF-8 sequence. */
205 nbytes = 1;
206 q = &buf[6];
210 *--q = ((c & 0x3F) | 0x80);
211 c >>= 6;
212 nbytes++;
214 while (c >= 0x3F || (c & limits[nbytes-1]));
216 *--q = (c | masks[nbytes-1]);
218 p = write_block (dtp, nbytes);
219 if (p == NULL)
220 return;
222 while (q < &buf[6])
223 *p++ = *q++;
229 /* Check the first character in source if we are using CC_FORTRAN
230 and set the cc.type appropriately. The cc.type is used later by write_cc
231 to determine the output start-of-record, and next_record_cc to determine the
232 output end-of-record.
233 This function is called before the output buffer is allocated, so alloc_len
234 is set to the appropriate size to allocate. */
236 static void
237 write_check_cc (st_parameter_dt *dtp, const char **source, size_t *alloc_len)
239 /* Only valid for CARRIAGECONTROL=FORTRAN. */
240 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN
241 || alloc_len == NULL || source == NULL)
242 return;
244 /* Peek at the first character. */
245 int c = (*alloc_len > 0) ? (*source)[0] : EOF;
246 if (c != EOF)
248 /* The start-of-record character which will be printed. */
249 dtp->u.p.cc.u.start = '\n';
250 /* The number of characters to print at the start-of-record.
251 len > 1 means copy the SOR character multiple times.
252 len == 0 means no SOR will be output. */
253 dtp->u.p.cc.len = 1;
255 switch (c)
257 case '+':
258 dtp->u.p.cc.type = CCF_OVERPRINT;
259 dtp->u.p.cc.len = 0;
260 break;
261 case '-':
262 dtp->u.p.cc.type = CCF_ONE_LF;
263 dtp->u.p.cc.len = 1;
264 break;
265 case '0':
266 dtp->u.p.cc.type = CCF_TWO_LF;
267 dtp->u.p.cc.len = 2;
268 break;
269 case '1':
270 dtp->u.p.cc.type = CCF_PAGE_FEED;
271 dtp->u.p.cc.len = 1;
272 dtp->u.p.cc.u.start = '\f';
273 break;
274 case '$':
275 dtp->u.p.cc.type = CCF_PROMPT;
276 dtp->u.p.cc.len = 1;
277 break;
278 case '\0':
279 dtp->u.p.cc.type = CCF_OVERPRINT_NOA;
280 dtp->u.p.cc.len = 0;
281 break;
282 default:
283 /* In the default case we copy ONE_LF. */
284 dtp->u.p.cc.type = CCF_DEFAULT;
285 dtp->u.p.cc.len = 1;
286 break;
289 /* We add n-1 to alloc_len so our write buffer is the right size.
290 We are replacing the first character, and possibly prepending some
291 additional characters. Note for n==0, we actually subtract one from
292 alloc_len, which is correct, since that character is skipped. */
293 if (*alloc_len > 0)
295 *source += 1;
296 *alloc_len += dtp->u.p.cc.len - 1;
298 /* If we have no input, there is no first character to replace. Make
299 sure we still allocate enough space for the start-of-record string. */
300 else
301 *alloc_len = dtp->u.p.cc.len;
306 /* Write the start-of-record character(s) for CC_FORTRAN.
307 Also adjusts the 'cc' struct to contain the end-of-record character
308 for next_record_cc.
309 The source_len is set to the remaining length to copy from the source,
310 after the start-of-record string was inserted. */
312 static char *
313 write_cc (st_parameter_dt *dtp, char *p, size_t *source_len)
315 /* Only valid for CARRIAGECONTROL=FORTRAN. */
316 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN || source_len == NULL)
317 return p;
319 /* Write the start-of-record string to the output buffer. Note that len is
320 never more than 2. */
321 if (dtp->u.p.cc.len > 0)
323 *(p++) = dtp->u.p.cc.u.start;
324 if (dtp->u.p.cc.len > 1)
325 *(p++) = dtp->u.p.cc.u.start;
327 /* source_len comes from write_check_cc where it is set to the full
328 allocated length of the output buffer. Therefore we subtract off the
329 length of the SOR string to obtain the remaining source length. */
330 *source_len -= dtp->u.p.cc.len;
333 /* Common case. */
334 dtp->u.p.cc.len = 1;
335 dtp->u.p.cc.u.end = '\r';
337 /* Update end-of-record character for next_record_w. */
338 switch (dtp->u.p.cc.type)
340 case CCF_PROMPT:
341 case CCF_OVERPRINT_NOA:
342 /* No end-of-record. */
343 dtp->u.p.cc.len = 0;
344 dtp->u.p.cc.u.end = '\0';
345 break;
346 case CCF_OVERPRINT:
347 case CCF_ONE_LF:
348 case CCF_TWO_LF:
349 case CCF_PAGE_FEED:
350 case CCF_DEFAULT:
351 default:
352 /* Carriage return. */
353 dtp->u.p.cc.len = 1;
354 dtp->u.p.cc.u.end = '\r';
355 break;
358 return p;
361 void
363 write_a (st_parameter_dt *dtp, const fnode *f, const char *source, size_t len)
365 size_t wlen;
366 char *p;
368 wlen = f->u.string.length < 0
369 || (f->format == FMT_G && f->u.string.length == 0)
370 ? len : (size_t) f->u.string.length;
372 #ifdef HAVE_CRLF
373 /* If this is formatted STREAM IO convert any embedded line feed characters
374 to CR_LF on systems that use that sequence for newlines. See F2003
375 Standard sections 10.6.3 and 9.9 for further information. */
376 if (is_stream_io (dtp))
378 const char crlf[] = "\r\n";
379 size_t q, bytes;
380 q = bytes = 0;
382 /* Write out any padding if needed. */
383 if (len < wlen)
385 p = write_block (dtp, wlen - len);
386 if (p == NULL)
387 return;
388 memset (p, ' ', wlen - len);
391 /* Scan the source string looking for '\n' and convert it if found. */
392 for (size_t i = 0; i < wlen; i++)
394 if (source[i] == '\n')
396 /* Write out the previously scanned characters in the string. */
397 if (bytes > 0)
399 p = write_block (dtp, bytes);
400 if (p == NULL)
401 return;
402 memcpy (p, &source[q], bytes);
403 q += bytes;
404 bytes = 0;
407 /* Write out the CR_LF sequence. */
408 q++;
409 p = write_block (dtp, 2);
410 if (p == NULL)
411 return;
412 memcpy (p, crlf, 2);
414 else
415 bytes++;
418 /* Write out any remaining bytes if no LF was found. */
419 if (bytes > 0)
421 p = write_block (dtp, bytes);
422 if (p == NULL)
423 return;
424 memcpy (p, &source[q], bytes);
427 else
429 #endif
430 if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN)
431 write_check_cc (dtp, &source, &wlen);
433 p = write_block (dtp, wlen);
434 if (p == NULL)
435 return;
437 if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN)
438 p = write_cc (dtp, p, &wlen);
440 if (unlikely (is_char4_unit (dtp)))
442 gfc_char4_t *p4 = (gfc_char4_t *) p;
443 if (wlen < len)
444 memcpy4 (p4, source, wlen);
445 else
447 memset4 (p4, ' ', wlen - len);
448 memcpy4 (p4 + wlen - len, source, len);
450 return;
453 if (wlen < len)
454 memcpy (p, source, wlen);
455 else
457 memset (p, ' ', wlen - len);
458 memcpy (p + wlen - len, source, len);
460 #ifdef HAVE_CRLF
462 #endif
466 /* The primary difference between write_a_char4 and write_a is that we have to
467 deal with writing from the first byte of the 4-byte character and pay
468 attention to the most significant bytes. For ENCODING="default" write the
469 lowest significant byte. If the 3 most significant bytes contain
470 non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value
471 to the UTF-8 encoded string before writing out. */
473 void
474 write_a_char4 (st_parameter_dt *dtp, const fnode *f, const char *source, size_t len)
476 size_t wlen;
477 gfc_char4_t *q;
479 wlen = f->u.string.length < 0
480 || (f->format == FMT_G && f->u.string.length == 0)
481 ? len : (size_t) f->u.string.length;
483 q = (gfc_char4_t *) source;
484 #ifdef HAVE_CRLF
485 /* If this is formatted STREAM IO convert any embedded line feed characters
486 to CR_LF on systems that use that sequence for newlines. See F2003
487 Standard sections 10.6.3 and 9.9 for further information. */
488 if (is_stream_io (dtp))
490 const gfc_char4_t crlf[] = {0x000d,0x000a};
491 size_t bytes;
492 gfc_char4_t *qq;
493 bytes = 0;
495 /* Write out any padding if needed. */
496 if (len < wlen)
498 char *p;
499 p = write_block (dtp, wlen - len);
500 if (p == NULL)
501 return;
502 memset (p, ' ', wlen - len);
505 /* Scan the source string looking for '\n' and convert it if found. */
506 qq = (gfc_char4_t *) source;
507 for (size_t i = 0; i < wlen; i++)
509 if (qq[i] == '\n')
511 /* Write out the previously scanned characters in the string. */
512 if (bytes > 0)
514 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
515 write_utf8_char4 (dtp, q, bytes, 0);
516 else
517 write_default_char4 (dtp, q, bytes, 0);
518 bytes = 0;
521 /* Write out the CR_LF sequence. */
522 write_default_char4 (dtp, crlf, 2, 0);
524 else
525 bytes++;
528 /* Write out any remaining bytes if no LF was found. */
529 if (bytes > 0)
531 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
532 write_utf8_char4 (dtp, q, bytes, 0);
533 else
534 write_default_char4 (dtp, q, bytes, 0);
537 else
539 #endif
540 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
541 write_utf8_char4 (dtp, q, len, wlen);
542 else
543 write_default_char4 (dtp, q, len, wlen);
544 #ifdef HAVE_CRLF
546 #endif
550 static GFC_INTEGER_LARGEST
551 extract_int (const void *p, int len)
553 GFC_INTEGER_LARGEST i = 0;
555 if (p == NULL)
556 return i;
558 switch (len)
560 case 1:
562 GFC_INTEGER_1 tmp;
563 memcpy ((void *) &tmp, p, len);
564 i = tmp;
566 break;
567 case 2:
569 GFC_INTEGER_2 tmp;
570 memcpy ((void *) &tmp, p, len);
571 i = tmp;
573 break;
574 case 4:
576 GFC_INTEGER_4 tmp;
577 memcpy ((void *) &tmp, p, len);
578 i = tmp;
580 break;
581 case 8:
583 GFC_INTEGER_8 tmp;
584 memcpy ((void *) &tmp, p, len);
585 i = tmp;
587 break;
588 #ifdef HAVE_GFC_INTEGER_16
589 case 16:
591 GFC_INTEGER_16 tmp;
592 memcpy ((void *) &tmp, p, len);
593 i = tmp;
595 break;
596 #endif
597 default:
598 internal_error (NULL, "bad integer kind");
601 return i;
604 static GFC_UINTEGER_LARGEST
605 extract_uint (const void *p, int len)
607 GFC_UINTEGER_LARGEST i = 0;
609 if (p == NULL)
610 return i;
612 switch (len)
614 case 1:
616 GFC_INTEGER_1 tmp;
617 memcpy ((void *) &tmp, p, len);
618 i = (GFC_UINTEGER_1) tmp;
620 break;
621 case 2:
623 GFC_INTEGER_2 tmp;
624 memcpy ((void *) &tmp, p, len);
625 i = (GFC_UINTEGER_2) tmp;
627 break;
628 case 4:
630 GFC_INTEGER_4 tmp;
631 memcpy ((void *) &tmp, p, len);
632 i = (GFC_UINTEGER_4) tmp;
634 break;
635 case 8:
637 GFC_INTEGER_8 tmp;
638 memcpy ((void *) &tmp, p, len);
639 i = (GFC_UINTEGER_8) tmp;
641 break;
642 #ifdef HAVE_GFC_INTEGER_16
643 case 10:
644 case 16:
646 GFC_INTEGER_16 tmp = 0;
647 memcpy ((void *) &tmp, p, len);
648 i = (GFC_UINTEGER_16) tmp;
650 break;
651 # ifdef HAVE_GFC_REAL_17
652 case 17:
654 GFC_INTEGER_16 tmp = 0;
655 memcpy ((void *) &tmp, p, 16);
656 i = (GFC_UINTEGER_16) tmp;
658 break;
659 # endif
660 #endif
661 default:
662 internal_error (NULL, "bad integer kind");
665 return i;
669 void
670 write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
672 char *p;
673 int wlen;
674 GFC_INTEGER_LARGEST n;
676 wlen = (f->format == FMT_G && f->u.w == 0) ? 1 : f->u.w;
678 p = write_block (dtp, wlen);
679 if (p == NULL)
680 return;
682 n = extract_int (source, len);
684 if (unlikely (is_char4_unit (dtp)))
686 gfc_char4_t *p4 = (gfc_char4_t *) p;
687 memset4 (p4, ' ', wlen -1);
688 p4[wlen - 1] = (n) ? 'T' : 'F';
689 return;
692 memset (p, ' ', wlen -1);
693 p[wlen - 1] = (n) ? 'T' : 'F';
696 static void
697 write_boz (st_parameter_dt *dtp, const fnode *f, const char *q, int n, int len)
699 int w, m, digits, nzero, nblank;
700 char *p;
702 w = f->u.integer.w;
703 m = f->u.integer.m;
705 /* Special case: */
707 if (m == 0 && n == 0)
709 if (w == 0)
710 w = 1;
712 p = write_block (dtp, w);
713 if (p == NULL)
714 return;
715 if (unlikely (is_char4_unit (dtp)))
717 gfc_char4_t *p4 = (gfc_char4_t *) p;
718 memset4 (p4, ' ', w);
720 else
721 memset (p, ' ', w);
722 goto done;
725 digits = strlen (q);
727 /* Select a width if none was specified. The idea here is to always
728 print something. */
730 if (w == DEFAULT_WIDTH)
731 w = default_width_for_integer (len);
733 if (w == 0)
734 w = ((digits < m) ? m : digits);
736 p = write_block (dtp, w);
737 if (p == NULL)
738 return;
740 nzero = 0;
741 if (digits < m)
742 nzero = m - digits;
744 /* See if things will work. */
746 nblank = w - (nzero + digits);
748 if (unlikely (is_char4_unit (dtp)))
750 gfc_char4_t *p4 = (gfc_char4_t *) p;
751 if (nblank < 0)
753 memset4 (p4, '*', w);
754 return;
757 if (!dtp->u.p.no_leading_blank)
759 memset4 (p4, ' ', nblank);
760 q += nblank;
761 memset4 (p4, '0', nzero);
762 q += nzero;
763 memcpy4 (p4, q, digits);
765 else
767 memset4 (p4, '0', nzero);
768 q += nzero;
769 memcpy4 (p4, q, digits);
770 q += digits;
771 memset4 (p4, ' ', nblank);
772 dtp->u.p.no_leading_blank = 0;
774 return;
777 if (nblank < 0)
779 star_fill (p, w);
780 goto done;
783 if (!dtp->u.p.no_leading_blank)
785 memset (p, ' ', nblank);
786 p += nblank;
787 memset (p, '0', nzero);
788 p += nzero;
789 memcpy (p, q, digits);
791 else
793 memset (p, '0', nzero);
794 p += nzero;
795 memcpy (p, q, digits);
796 p += digits;
797 memset (p, ' ', nblank);
798 dtp->u.p.no_leading_blank = 0;
801 done:
802 return;
805 static void
806 write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
807 int len)
809 GFC_INTEGER_LARGEST n = 0;
810 GFC_UINTEGER_LARGEST absn;
811 int w, m, digits, nsign, nzero, nblank;
812 char *p;
813 const char *q;
814 sign_t sign;
815 char itoa_buf[GFC_BTOA_BUF_SIZE];
817 w = f->u.integer.w;
818 m = f->format == FMT_G ? -1 : f->u.integer.m;
820 n = extract_int (source, len);
822 /* Special case: */
823 if (m == 0 && n == 0)
825 if (w == 0)
826 w = 1;
828 p = write_block (dtp, w);
829 if (p == NULL)
830 return;
831 if (unlikely (is_char4_unit (dtp)))
833 gfc_char4_t *p4 = (gfc_char4_t *) p;
834 memset4 (p4, ' ', w);
836 else
837 memset (p, ' ', w);
838 goto done;
841 sign = calculate_sign (dtp, n < 0);
842 if (n < 0)
843 /* Use unsigned to protect from overflow. */
844 absn = -(GFC_UINTEGER_LARGEST) n;
845 else
846 absn = n;
847 nsign = sign == S_NONE ? 0 : 1;
849 /* gfc_itoa() converts the nonnegative value to decimal representation. */
850 q = gfc_itoa (absn, itoa_buf, sizeof (itoa_buf));
851 digits = strlen (q);
853 /* Select a width if none was specified. The idea here is to always
854 print something. */
855 if (w == DEFAULT_WIDTH)
856 w = default_width_for_integer (len);
858 if (w == 0)
859 w = ((digits < m) ? m : digits) + nsign;
861 p = write_block (dtp, w);
862 if (p == NULL)
863 return;
865 nzero = 0;
866 if (digits < m)
867 nzero = m - digits;
869 /* See if things will work. */
871 nblank = w - (nsign + nzero + digits);
873 if (unlikely (is_char4_unit (dtp)))
875 gfc_char4_t *p4 = (gfc_char4_t *)p;
876 if (nblank < 0)
878 memset4 (p4, '*', w);
879 goto done;
882 if (!dtp->u.p.namelist_mode)
884 memset4 (p4, ' ', nblank);
885 p4 += nblank;
888 switch (sign)
890 case S_PLUS:
891 *p4++ = '+';
892 break;
893 case S_MINUS:
894 *p4++ = '-';
895 break;
896 case S_NONE:
897 break;
900 memset4 (p4, '0', nzero);
901 p4 += nzero;
903 memcpy4 (p4, q, digits);
904 return;
906 if (dtp->u.p.namelist_mode)
908 p4 += digits;
909 memset4 (p4, ' ', nblank);
913 if (nblank < 0)
915 star_fill (p, w);
916 goto done;
919 if (!dtp->u.p.namelist_mode)
921 memset (p, ' ', nblank);
922 p += nblank;
925 switch (sign)
927 case S_PLUS:
928 *p++ = '+';
929 break;
930 case S_MINUS:
931 *p++ = '-';
932 break;
933 case S_NONE:
934 break;
937 memset (p, '0', nzero);
938 p += nzero;
940 memcpy (p, q, digits);
942 if (dtp->u.p.namelist_mode)
944 p += digits;
945 memset (p, ' ', nblank);
948 done:
949 return;
953 /* Convert hexadecimal to ASCII. */
955 static const char *
956 xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
958 int digit;
959 char *p;
961 assert (len >= GFC_XTOA_BUF_SIZE);
963 if (n == 0)
964 return "0";
966 p = buffer + GFC_XTOA_BUF_SIZE - 1;
967 *p = '\0';
969 while (n != 0)
971 digit = n & 0xF;
972 if (digit > 9)
973 digit += 'A' - '0' - 10;
975 *--p = '0' + digit;
976 n >>= 4;
979 return p;
983 /* Convert unsigned octal to ASCII. */
985 static const char *
986 otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
988 char *p;
990 assert (len >= GFC_OTOA_BUF_SIZE);
992 if (n == 0)
993 return "0";
995 p = buffer + GFC_OTOA_BUF_SIZE - 1;
996 *p = '\0';
998 while (n != 0)
1000 *--p = '0' + (n & 7);
1001 n >>= 3;
1004 return p;
1008 /* Convert unsigned binary to ASCII. */
1010 static const char *
1011 btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
1013 char *p;
1015 assert (len >= GFC_BTOA_BUF_SIZE);
1017 if (n == 0)
1018 return "0";
1020 p = buffer + GFC_BTOA_BUF_SIZE - 1;
1021 *p = '\0';
1023 while (n != 0)
1025 *--p = '0' + (n & 1);
1026 n >>= 1;
1029 return p;
1032 /* The following three functions, btoa_big, otoa_big, and xtoa_big, are needed
1033 to convert large reals with kind sizes that exceed the largest integer type
1034 available on certain platforms. In these cases, byte by byte conversion is
1035 performed. Endianess is taken into account. */
1037 /* Conversion to binary. */
1039 static const char *
1040 btoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
1042 char *q;
1043 int i, j;
1045 q = buffer;
1046 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
1048 const char *p = s;
1049 for (i = 0; i < len; i++)
1051 char c = *p;
1053 /* Test for zero. Needed by write_boz later. */
1054 if (*p != 0)
1055 *n = 1;
1057 for (j = 0; j < 8; j++)
1059 *q++ = (c & 128) ? '1' : '0';
1060 c <<= 1;
1062 p++;
1065 else
1067 const char *p = s + len - 1;
1068 for (i = 0; i < len; i++)
1070 char c = *p;
1072 /* Test for zero. Needed by write_boz later. */
1073 if (*p != 0)
1074 *n = 1;
1076 for (j = 0; j < 8; j++)
1078 *q++ = (c & 128) ? '1' : '0';
1079 c <<= 1;
1081 p--;
1085 if (*n == 0)
1086 return "0";
1088 /* Move past any leading zeros. */
1089 while (*buffer == '0')
1090 buffer++;
1092 return buffer;
1096 /* Conversion to octal. */
1098 static const char *
1099 otoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
1101 char *q;
1102 int i, j, k;
1103 uint8_t octet;
1105 q = buffer + GFC_OTOA_BUF_SIZE - 1;
1106 *q = '\0';
1107 i = k = octet = 0;
1109 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
1111 const char *p = s + len - 1;
1112 char c = *p;
1113 while (i < len)
1115 /* Test for zero. Needed by write_boz later. */
1116 if (*p != 0)
1117 *n = 1;
1119 for (j = 0; j < 3 && i < len; j++)
1121 octet |= (c & 1) << j;
1122 c >>= 1;
1123 if (++k > 7)
1125 i++;
1126 k = 0;
1127 c = *--p;
1130 *--q = '0' + octet;
1131 octet = 0;
1134 else
1136 const char *p = s;
1137 char c = *p;
1138 while (i < len)
1140 /* Test for zero. Needed by write_boz later. */
1141 if (*p != 0)
1142 *n = 1;
1144 for (j = 0; j < 3 && i < len; j++)
1146 octet |= (c & 1) << j;
1147 c >>= 1;
1148 if (++k > 7)
1150 i++;
1151 k = 0;
1152 c = *++p;
1155 *--q = '0' + octet;
1156 octet = 0;
1160 if (*n == 0)
1161 return "0";
1163 /* Move past any leading zeros. */
1164 while (*q == '0')
1165 q++;
1167 return q;
1170 /* Conversion to hexadecimal. */
1172 static const char *
1173 xtoa_big (const char *s, char *buffer, int len, GFC_UINTEGER_LARGEST *n)
1175 static char a[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
1176 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
1178 char *q;
1179 uint8_t h, l;
1180 int i;
1182 /* write_z, which calls xtoa_big, is called from transfer.c,
1183 formatted_transfer_scalar_write. There it is passed the kind as
1184 'len' argument, which means a maximum of 16. The buffer is large
1185 enough, but the compiler does not know that, so shut up the
1186 warning here. */
1188 if (len > 16)
1189 __builtin_unreachable ();
1191 q = buffer;
1193 if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
1195 const char *p = s;
1196 for (i = 0; i < len; i++)
1198 /* Test for zero. Needed by write_boz later. */
1199 if (*p != 0)
1200 *n = 1;
1202 h = (*p >> 4) & 0x0F;
1203 l = *p++ & 0x0F;
1204 *q++ = a[h];
1205 *q++ = a[l];
1208 else
1210 const char *p = s + len - 1;
1211 for (i = 0; i < len; i++)
1213 /* Test for zero. Needed by write_boz later. */
1214 if (*p != 0)
1215 *n = 1;
1217 h = (*p >> 4) & 0x0F;
1218 l = *p-- & 0x0F;
1219 *q++ = a[h];
1220 *q++ = a[l];
1224 *q = '\0';
1226 if (*n == 0)
1227 return "0";
1229 /* Move past any leading zeros. */
1230 while (*buffer == '0')
1231 buffer++;
1233 return buffer;
1237 void
1238 write_i (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1240 write_decimal (dtp, f, p, len);
1244 void
1245 write_b (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
1247 const char *p;
1248 char itoa_buf[GFC_BTOA_BUF_SIZE];
1249 GFC_UINTEGER_LARGEST n = 0;
1251 /* Ensure we end up with a null terminated string. */
1252 memset(itoa_buf, '\0', GFC_BTOA_BUF_SIZE);
1254 if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
1256 p = btoa_big (source, itoa_buf, len, &n);
1257 write_boz (dtp, f, p, n, len);
1259 else
1261 n = extract_uint (source, len);
1262 p = btoa (n, itoa_buf, sizeof (itoa_buf));
1263 write_boz (dtp, f, p, n, len);
1268 void
1269 write_o (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
1271 const char *p;
1272 char itoa_buf[GFC_OTOA_BUF_SIZE];
1273 GFC_UINTEGER_LARGEST n = 0;
1275 if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
1277 p = otoa_big (source, itoa_buf, len, &n);
1278 write_boz (dtp, f, p, n, len);
1280 else
1282 n = extract_uint (source, len);
1283 p = otoa (n, itoa_buf, sizeof (itoa_buf));
1284 write_boz (dtp, f, p, n, len);
1288 void
1289 write_z (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
1291 const char *p;
1292 char itoa_buf[GFC_XTOA_BUF_SIZE];
1293 GFC_UINTEGER_LARGEST n = 0;
1295 if (len > (int) sizeof (GFC_UINTEGER_LARGEST))
1297 p = xtoa_big (source, itoa_buf, len, &n);
1298 write_boz (dtp, f, p, n, len);
1300 else
1302 n = extract_uint (source, len);
1303 p = xtoa (n, itoa_buf, sizeof (itoa_buf));
1304 write_boz (dtp, f, p, n, len);
1308 /* Take care of the X/TR descriptor. */
1310 void
1311 write_x (st_parameter_dt *dtp, int len, int nspaces)
1313 char *p;
1315 p = write_block (dtp, len);
1316 if (p == NULL)
1317 return;
1318 if (nspaces > 0 && len - nspaces >= 0)
1320 if (unlikely (is_char4_unit (dtp)))
1322 gfc_char4_t *p4 = (gfc_char4_t *) p;
1323 memset4 (&p4[len - nspaces], ' ', nspaces);
1325 else
1326 memset (&p[len - nspaces], ' ', nspaces);
1331 /* List-directed writing. */
1334 /* Write a single character to the output. Returns nonzero if
1335 something goes wrong. */
1337 static int
1338 write_char (st_parameter_dt *dtp, int c)
1340 char *p;
1342 p = write_block (dtp, 1);
1343 if (p == NULL)
1344 return 1;
1345 if (unlikely (is_char4_unit (dtp)))
1347 gfc_char4_t *p4 = (gfc_char4_t *) p;
1348 *p4 = c;
1349 return 0;
1352 *p = (uchar) c;
1354 return 0;
1358 /* Write a list-directed logical value. */
1360 static void
1361 write_logical (st_parameter_dt *dtp, const char *source, int length)
1363 write_char (dtp, extract_int (source, length) ? 'T' : 'F');
1367 /* Write a list-directed integer value. */
1369 static void
1370 write_integer (st_parameter_dt *dtp, const char *source, int kind)
1372 int width;
1373 fnode f;
1375 switch (kind)
1377 case 1:
1378 width = 4;
1379 break;
1381 case 2:
1382 width = 6;
1383 break;
1385 case 4:
1386 width = 11;
1387 break;
1389 case 8:
1390 width = 20;
1391 break;
1393 case 16:
1394 width = 40;
1395 break;
1397 default:
1398 width = 0;
1399 break;
1401 f.u.integer.w = width;
1402 f.u.integer.m = -1;
1403 f.format = FMT_NONE;
1404 write_decimal (dtp, &f, source, kind);
1408 /* Write a list-directed string. We have to worry about delimiting
1409 the strings if the file has been opened in that mode. */
1411 #define DELIM 1
1412 #define NODELIM 0
1414 static void
1415 write_character (st_parameter_dt *dtp, const char *source, int kind, size_t length, int mode)
1417 size_t extra;
1418 char *p, d;
1420 if (mode == DELIM)
1422 switch (dtp->u.p.current_unit->delim_status)
1424 case DELIM_APOSTROPHE:
1425 d = '\'';
1426 break;
1427 case DELIM_QUOTE:
1428 d = '"';
1429 break;
1430 default:
1431 d = ' ';
1432 break;
1435 else
1436 d = ' ';
1438 if (kind == 1)
1440 if (d == ' ')
1441 extra = 0;
1442 else
1444 extra = 2;
1446 for (size_t i = 0; i < length; i++)
1447 if (source[i] == d)
1448 extra++;
1451 p = write_block (dtp, length + extra);
1452 if (p == NULL)
1453 return;
1455 if (unlikely (is_char4_unit (dtp)))
1457 gfc_char4_t d4 = (gfc_char4_t) d;
1458 gfc_char4_t *p4 = (gfc_char4_t *) p;
1460 if (d4 == ' ')
1461 memcpy4 (p4, source, length);
1462 else
1464 *p4++ = d4;
1466 for (size_t i = 0; i < length; i++)
1468 *p4++ = (gfc_char4_t) source[i];
1469 if (source[i] == d)
1470 *p4++ = d4;
1473 *p4 = d4;
1475 return;
1478 if (d == ' ')
1479 memcpy (p, source, length);
1480 else
1482 *p++ = d;
1484 for (size_t i = 0; i < length; i++)
1486 *p++ = source[i];
1487 if (source[i] == d)
1488 *p++ = d;
1491 *p = d;
1494 else
1496 if (d == ' ')
1498 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
1499 write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
1500 else
1501 write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
1503 else
1505 p = write_block (dtp, 1);
1506 *p = d;
1508 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
1509 write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
1510 else
1511 write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
1513 p = write_block (dtp, 1);
1514 *p = d;
1519 /* Floating point helper functions. */
1521 #define BUF_STACK_SZ 384
1523 static int
1524 get_precision (st_parameter_dt *dtp, const fnode *f, const char *source, int kind)
1526 if (f->format != FMT_EN)
1527 return determine_precision (dtp, f, kind);
1528 else
1529 return determine_en_precision (dtp, f, source, kind);
1532 /* 4932 is the maximum exponent of long double and quad precision, 3
1533 extra characters for the sign, the decimal point, and the
1534 trailing null. Extra digits are added by the calling functions for
1535 requested precision. Likewise for float and double. F0 editing produces
1536 full precision output. */
1537 static int
1538 size_from_kind (st_parameter_dt *dtp, const fnode *f, int kind)
1540 int size;
1542 if ((f->format == FMT_F && f->u.real.w == 0) || f->u.real.w == DEFAULT_WIDTH)
1544 switch (kind)
1546 case 4:
1547 size = 38 + 3; /* These constants shown for clarity. */
1548 break;
1549 case 8:
1550 size = 308 + 3;
1551 break;
1552 case 10:
1553 size = 4932 + 3;
1554 break;
1555 case 16:
1556 #ifdef HAVE_GFC_REAL_17
1557 case 17:
1558 #endif
1559 size = 4932 + 3;
1560 break;
1561 default:
1562 internal_error (&dtp->common, "bad real kind");
1563 break;
1566 else
1567 size = f->u.real.w + 1; /* One byte for a NULL character. */
1569 return size;
1572 static char *
1573 select_buffer (st_parameter_dt *dtp, const fnode *f, int precision,
1574 char *buf, size_t *size, int kind)
1576 char *result;
1578 /* The buffer needs at least one more byte to allow room for
1579 normalizing and 1 to hold null terminator. */
1580 *size = size_from_kind (dtp, f, kind) + precision + 1 + 1;
1582 if (*size > BUF_STACK_SZ)
1583 result = xmalloc (*size);
1584 else
1585 result = buf;
1586 return result;
1589 static char *
1590 select_string (st_parameter_dt *dtp, const fnode *f, char *buf, size_t *size,
1591 int kind)
1593 char *result;
1594 *size = size_from_kind (dtp, f, kind) + f->u.real.d + 1;
1595 if (*size > BUF_STACK_SZ)
1596 result = xmalloc (*size);
1597 else
1598 result = buf;
1599 return result;
1602 static void
1603 write_float_string (st_parameter_dt *dtp, char *fstr, size_t len)
1605 char *p = write_block (dtp, len);
1606 if (p == NULL)
1607 return;
1609 if (unlikely (is_char4_unit (dtp)))
1611 gfc_char4_t *p4 = (gfc_char4_t *) p;
1612 memcpy4 (p4, fstr, len);
1613 return;
1615 memcpy (p, fstr, len);
1619 static void
1620 write_float_0 (st_parameter_dt *dtp, const fnode *f, const char *source, int kind)
1622 char buf_stack[BUF_STACK_SZ];
1623 char str_buf[BUF_STACK_SZ];
1624 char *buffer, *result;
1625 size_t buf_size, res_len, flt_str_len;
1627 /* Precision for snprintf call. */
1628 int precision = get_precision (dtp, f, source, kind);
1630 /* String buffer to hold final result. */
1631 result = select_string (dtp, f, str_buf, &res_len, kind);
1633 buffer = select_buffer (dtp, f, precision, buf_stack, &buf_size, kind);
1635 get_float_string (dtp, f, source , kind, 0, buffer,
1636 precision, buf_size, result, &flt_str_len);
1637 write_float_string (dtp, result, flt_str_len);
1639 if (buf_size > BUF_STACK_SZ)
1640 free (buffer);
1641 if (res_len > BUF_STACK_SZ)
1642 free (result);
1645 void
1646 write_d (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1648 write_float_0 (dtp, f, p, len);
1652 void
1653 write_e (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1655 write_float_0 (dtp, f, p, len);
1659 void
1660 write_f (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1662 write_float_0 (dtp, f, p, len);
1666 void
1667 write_en (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1669 write_float_0 (dtp, f, p, len);
1673 void
1674 write_es (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1676 write_float_0 (dtp, f, p, len);
1680 /* Set an fnode to default format. */
1682 static void
1683 set_fnode_default (st_parameter_dt *dtp, fnode *f, int length)
1685 f->format = FMT_G;
1686 switch (length)
1688 case 4:
1689 f->u.real.w = 16;
1690 f->u.real.d = 9;
1691 f->u.real.e = 2;
1692 break;
1693 case 8:
1694 f->u.real.w = 25;
1695 f->u.real.d = 17;
1696 f->u.real.e = 3;
1697 break;
1698 case 10:
1699 f->u.real.w = 30;
1700 f->u.real.d = 21;
1701 f->u.real.e = 4;
1702 break;
1703 case 16:
1704 /* Adjust decimal precision depending on binary precision, 106 or 113. */
1705 #if GFC_REAL_16_DIGITS == 113
1706 f->u.real.w = 45;
1707 f->u.real.d = 36;
1708 f->u.real.e = 4;
1709 #else
1710 f->u.real.w = 41;
1711 f->u.real.d = 32;
1712 f->u.real.e = 4;
1713 #endif
1714 break;
1715 #ifdef HAVE_GFC_REAL_17
1716 case 17:
1717 f->u.real.w = 45;
1718 f->u.real.d = 36;
1719 f->u.real.e = 4;
1720 break;
1721 #endif
1722 default:
1723 internal_error (&dtp->common, "bad real kind");
1724 break;
1728 /* Output a real number with default format.
1729 To guarantee that a binary -> decimal -> binary roundtrip conversion
1730 recovers the original value, IEEE 754-2008 requires 9, 17, 21 and 36
1731 significant digits for REAL kinds 4, 8, 10, and 16, respectively.
1732 Thus, we use 1PG16.9E2 for REAL(4), 1PG25.17E3 for REAL(8), 1PG30.21E4
1733 for REAL(10) and 1PG45.36E4 for REAL(16). The exception is that the
1734 Fortran standard requires outputting an extra digit when the scale
1735 factor is 1 and when the magnitude of the value is such that E
1736 editing is used. However, gfortran compensates for this, and thus
1737 for list formatted the same number of significant digits is
1738 generated both when using F and E editing. */
1740 void
1741 write_real (st_parameter_dt *dtp, const char *source, int kind)
1743 fnode f ;
1744 char buf_stack[BUF_STACK_SZ];
1745 char str_buf[BUF_STACK_SZ];
1746 char *buffer, *result;
1747 size_t buf_size, res_len, flt_str_len;
1748 int orig_scale = dtp->u.p.scale_factor;
1749 dtp->u.p.scale_factor = 1;
1750 set_fnode_default (dtp, &f, kind);
1752 /* Precision for snprintf call. */
1753 int precision = get_precision (dtp, &f, source, kind);
1755 /* String buffer to hold final result. */
1756 result = select_string (dtp, &f, str_buf, &res_len, kind);
1758 /* Scratch buffer to hold final result. */
1759 buffer = select_buffer (dtp, &f, precision, buf_stack, &buf_size, kind);
1761 get_float_string (dtp, &f, source , kind, 1, buffer,
1762 precision, buf_size, result, &flt_str_len);
1763 write_float_string (dtp, result, flt_str_len);
1765 dtp->u.p.scale_factor = orig_scale;
1766 if (buf_size > BUF_STACK_SZ)
1767 free (buffer);
1768 if (res_len > BUF_STACK_SZ)
1769 free (result);
1772 /* Similar to list formatted REAL output, for kPG0 where k > 0 we
1773 compensate for the extra digit. */
1775 void
1776 write_real_w0 (st_parameter_dt *dtp, const char *source, int kind,
1777 const fnode* f)
1779 fnode ff;
1780 char buf_stack[BUF_STACK_SZ];
1781 char str_buf[BUF_STACK_SZ];
1782 char *buffer, *result;
1783 size_t buf_size, res_len, flt_str_len;
1784 int comp_d = 0;
1786 set_fnode_default (dtp, &ff, kind);
1788 if (f->u.real.d > 0)
1789 ff.u.real.d = f->u.real.d;
1790 ff.format = f->format;
1792 /* For FMT_G, Compensate for extra digits when using scale factor, d
1793 is not specified, and the magnitude is such that E editing
1794 is used. */
1795 if (f->format == FMT_G)
1797 if (dtp->u.p.scale_factor > 0 && f->u.real.d == 0)
1798 comp_d = 1;
1799 else
1800 comp_d = 0;
1803 if (f->u.real.e >= 0)
1804 ff.u.real.e = f->u.real.e;
1806 dtp->u.p.g0_no_blanks = 1;
1808 /* Precision for snprintf call. */
1809 int precision = get_precision (dtp, &ff, source, kind);
1811 /* String buffer to hold final result. */
1812 result = select_string (dtp, &ff, str_buf, &res_len, kind);
1814 buffer = select_buffer (dtp, &ff, precision, buf_stack, &buf_size, kind);
1816 get_float_string (dtp, &ff, source , kind, comp_d, buffer,
1817 precision, buf_size, result, &flt_str_len);
1818 write_float_string (dtp, result, flt_str_len);
1820 dtp->u.p.g0_no_blanks = 0;
1821 if (buf_size > BUF_STACK_SZ)
1822 free (buffer);
1823 if (res_len > BUF_STACK_SZ)
1824 free (result);
1828 static void
1829 write_complex (st_parameter_dt *dtp, const char *source, int kind, size_t size)
1831 char semi_comma =
1832 dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
1834 /* Set for no blanks so we get a string result with no leading
1835 blanks. We will pad left later. */
1836 dtp->u.p.g0_no_blanks = 1;
1838 fnode f ;
1839 char buf_stack[BUF_STACK_SZ];
1840 char str1_buf[BUF_STACK_SZ];
1841 char str2_buf[BUF_STACK_SZ];
1842 char *buffer, *result1, *result2;
1843 size_t buf_size, res_len1, res_len2, flt_str_len1, flt_str_len2;
1844 int width, lblanks, orig_scale = dtp->u.p.scale_factor;
1846 dtp->u.p.scale_factor = 1;
1847 set_fnode_default (dtp, &f, kind);
1849 /* Set width for two values, parenthesis, and comma. */
1850 width = 2 * f.u.real.w + 3;
1852 /* Set for no blanks so we get a string result with no leading
1853 blanks. We will pad left later. */
1854 dtp->u.p.g0_no_blanks = 1;
1856 /* Precision for snprintf call. */
1857 int precision = get_precision (dtp, &f, source, kind);
1859 /* String buffers to hold final result. */
1860 result1 = select_string (dtp, &f, str1_buf, &res_len1, kind);
1861 result2 = select_string (dtp, &f, str2_buf, &res_len2, kind);
1863 buffer = select_buffer (dtp, &f, precision, buf_stack, &buf_size, kind);
1865 get_float_string (dtp, &f, source , kind, 0, buffer,
1866 precision, buf_size, result1, &flt_str_len1);
1867 get_float_string (dtp, &f, source + size / 2 , kind, 0, buffer,
1868 precision, buf_size, result2, &flt_str_len2);
1869 if (!dtp->u.p.namelist_mode)
1871 lblanks = width - flt_str_len1 - flt_str_len2 - 3;
1872 write_x (dtp, lblanks, lblanks);
1874 write_char (dtp, '(');
1875 write_float_string (dtp, result1, flt_str_len1);
1876 write_char (dtp, semi_comma);
1877 write_float_string (dtp, result2, flt_str_len2);
1878 write_char (dtp, ')');
1880 dtp->u.p.scale_factor = orig_scale;
1881 dtp->u.p.g0_no_blanks = 0;
1882 if (buf_size > BUF_STACK_SZ)
1883 free (buffer);
1884 if (res_len1 > BUF_STACK_SZ)
1885 free (result1);
1886 if (res_len2 > BUF_STACK_SZ)
1887 free (result2);
1891 /* Write the separator between items. */
1893 static void
1894 write_separator (st_parameter_dt *dtp)
1896 char *p;
1898 p = write_block (dtp, options.separator_len);
1899 if (p == NULL)
1900 return;
1901 if (unlikely (is_char4_unit (dtp)))
1903 gfc_char4_t *p4 = (gfc_char4_t *) p;
1904 memcpy4 (p4, options.separator, options.separator_len);
1906 else
1907 memcpy (p, options.separator, options.separator_len);
1911 /* Write an item with list formatting.
1912 TODO: handle skipping to the next record correctly, particularly
1913 with strings. */
1915 static void
1916 list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1917 size_t size)
1919 if (dtp->u.p.current_unit == NULL)
1920 return;
1922 if (dtp->u.p.first_item)
1924 dtp->u.p.first_item = 0;
1925 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN)
1926 write_char (dtp, ' ');
1928 else
1930 if (type != BT_CHARACTER || !dtp->u.p.char_flag ||
1931 (dtp->u.p.current_unit->delim_status != DELIM_NONE
1932 && dtp->u.p.current_unit->delim_status != DELIM_UNSPECIFIED))
1933 write_separator (dtp);
1936 switch (type)
1938 case BT_INTEGER:
1939 write_integer (dtp, p, kind);
1940 break;
1941 case BT_LOGICAL:
1942 write_logical (dtp, p, kind);
1943 break;
1944 case BT_CHARACTER:
1945 write_character (dtp, p, kind, size, DELIM);
1946 break;
1947 case BT_REAL:
1948 write_real (dtp, p, kind);
1949 break;
1950 case BT_COMPLEX:
1951 write_complex (dtp, p, kind, size);
1952 break;
1953 case BT_CLASS:
1955 GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number;
1956 char iotype[] = "LISTDIRECTED";
1957 gfc_charlen_type iotype_len = 12;
1958 char tmp_iomsg[IOMSG_LEN] = "";
1959 char *child_iomsg;
1960 gfc_charlen_type child_iomsg_len;
1961 GFC_INTEGER_4 noiostat;
1962 GFC_INTEGER_4 *child_iostat = NULL;
1963 gfc_full_array_i4 vlist;
1965 GFC_DESCRIPTOR_DATA(&vlist) = NULL;
1966 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
1968 /* Set iostat, intent(out). */
1969 noiostat = 0;
1970 child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT)
1971 ? dtp->common.iostat : &noiostat);
1973 /* Set iomsge, intent(inout). */
1974 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1976 child_iomsg = dtp->common.iomsg;
1977 child_iomsg_len = dtp->common.iomsg_len;
1979 else
1981 child_iomsg = tmp_iomsg;
1982 child_iomsg_len = IOMSG_LEN;
1985 /* Call the user defined formatted WRITE procedure. */
1986 dtp->u.p.current_unit->child_dtio++;
1987 dtp->u.p.fdtio_ptr (p, &unit, iotype, &vlist,
1988 child_iostat, child_iomsg,
1989 iotype_len, child_iomsg_len);
1990 dtp->u.p.current_unit->child_dtio--;
1992 break;
1993 default:
1994 internal_error (&dtp->common, "list_formatted_write(): Bad type");
1997 fbuf_flush_list (dtp->u.p.current_unit, LIST_WRITING);
1998 dtp->u.p.char_flag = (type == BT_CHARACTER);
2002 void
2003 list_formatted_write (st_parameter_dt *dtp, bt type, void *p, int kind,
2004 size_t size, size_t nelems)
2006 size_t elem;
2007 char *tmp;
2008 size_t stride = type == BT_CHARACTER ?
2009 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2011 tmp = (char *) p;
2013 /* Big loop over all the elements. */
2014 for (elem = 0; elem < nelems; elem++)
2016 dtp->u.p.item_count++;
2017 list_formatted_write_scalar (dtp, type, tmp + elem * stride, kind, size);
2021 /* NAMELIST OUTPUT
2023 nml_write_obj writes a namelist object to the output stream. It is called
2024 recursively for derived type components:
2025 obj = is the namelist_info for the current object.
2026 offset = the offset relative to the address held by the object for
2027 derived type arrays.
2028 base = is the namelist_info of the derived type, when obj is a
2029 component.
2030 base_name = the full name for a derived type, including qualifiers
2031 if any.
2032 The returned value is a pointer to the object beyond the last one
2033 accessed, including nested derived types. Notice that the namelist is
2034 a linear linked list of objects, including derived types and their
2035 components. A tree, of sorts, is implied by the compound names of
2036 the derived type components and this is how this function recurses through
2037 the list. */
2039 /* A generous estimate of the number of characters needed to print
2040 repeat counts and indices, including commas, asterices and brackets. */
2042 #define NML_DIGITS 20
2044 static void
2045 namelist_write_newline (st_parameter_dt *dtp)
2047 if (!is_internal_unit (dtp))
2049 #ifdef HAVE_CRLF
2050 write_character (dtp, "\r\n", 1, 2, NODELIM);
2051 #else
2052 write_character (dtp, "\n", 1, 1, NODELIM);
2053 #endif
2054 return;
2057 if (is_array_io (dtp))
2059 gfc_offset record;
2060 int finished;
2061 char *p;
2062 int length = dtp->u.p.current_unit->bytes_left;
2064 p = write_block (dtp, length);
2065 if (p == NULL)
2066 return;
2068 if (unlikely (is_char4_unit (dtp)))
2070 gfc_char4_t *p4 = (gfc_char4_t *) p;
2071 memset4 (p4, ' ', length);
2073 else
2074 memset (p, ' ', length);
2076 /* Now that the current record has been padded out,
2077 determine where the next record in the array is. */
2078 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2079 &finished);
2080 if (finished)
2081 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2082 else
2084 /* Now seek to this record */
2085 record = record * dtp->u.p.current_unit->recl;
2087 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
2089 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2090 return;
2093 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2096 else
2097 write_character (dtp, " ", 1, 1, NODELIM);
2101 static namelist_info *
2102 nml_write_obj (st_parameter_dt *dtp, namelist_info *obj, index_type offset,
2103 namelist_info *base, char *base_name)
2105 int rep_ctr;
2106 int num;
2107 int nml_carry;
2108 int len;
2109 index_type obj_size;
2110 index_type nelem;
2111 size_t dim_i;
2112 size_t clen;
2113 index_type elem_ctr;
2114 size_t obj_name_len;
2115 void *p;
2116 char cup;
2117 char *obj_name;
2118 char *ext_name;
2119 char *q;
2120 size_t ext_name_len;
2121 char rep_buff[NML_DIGITS];
2122 namelist_info *cmp;
2123 namelist_info *retval = obj->next;
2124 size_t base_name_len;
2125 size_t base_var_name_len;
2126 size_t tot_len;
2128 /* Set the character to be used to separate values
2129 to a comma or semi-colon. */
2131 char semi_comma =
2132 dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
2134 /* Write namelist variable names in upper case. If a derived type,
2135 nothing is output. If a component, base and base_name are set. */
2137 if (obj->type != BT_DERIVED || obj->dtio_sub != NULL)
2139 namelist_write_newline (dtp);
2140 write_character (dtp, " ", 1, 1, NODELIM);
2142 len = 0;
2143 if (base)
2145 len = strlen (base->var_name);
2146 base_name_len = strlen (base_name);
2147 for (dim_i = 0; dim_i < base_name_len; dim_i++)
2149 cup = safe_toupper (base_name[dim_i]);
2150 write_character (dtp, &cup, 1, 1, NODELIM);
2153 clen = strlen (obj->var_name);
2154 for (dim_i = len; dim_i < clen; dim_i++)
2156 cup = safe_toupper (obj->var_name[dim_i]);
2157 if (cup == '+')
2158 cup = '%';
2159 write_character (dtp, &cup, 1, 1, NODELIM);
2161 write_character (dtp, "=", 1, 1, NODELIM);
2164 /* Counts the number of data output on a line, including names. */
2166 num = 1;
2168 len = obj->len;
2170 switch (obj->type)
2173 case BT_REAL:
2174 obj_size = size_from_real_kind (len);
2175 break;
2177 case BT_COMPLEX:
2178 obj_size = size_from_complex_kind (len);
2179 break;
2181 case BT_CHARACTER:
2182 obj_size = obj->string_length;
2183 break;
2185 default:
2186 obj_size = len;
2189 if (obj->var_rank)
2190 obj_size = obj->size;
2192 /* Set the index vector and count the number of elements. */
2194 nelem = 1;
2195 for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
2197 obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj, dim_i);
2198 nelem = nelem * GFC_DESCRIPTOR_EXTENT (obj, dim_i);
2201 /* Main loop to output the data held in the object. */
2203 rep_ctr = 1;
2204 for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++)
2207 /* Build the pointer to the data value. The offset is passed by
2208 recursive calls to this function for arrays of derived types.
2209 Is NULL otherwise. */
2211 p = (void *)(obj->mem_pos + elem_ctr * obj_size);
2212 p += offset;
2214 /* Check for repeat counts of intrinsic types. */
2216 if ((elem_ctr < (nelem - 1)) &&
2217 (obj->type != BT_DERIVED) &&
2218 !memcmp (p, (void *)(p + obj_size ), obj_size ))
2220 rep_ctr++;
2223 /* Execute a repeated output. Note the flag no_leading_blank that
2224 is used in the functions used to output the intrinsic types. */
2226 else
2228 if (rep_ctr > 1)
2230 snprintf(rep_buff, NML_DIGITS, " %d*", rep_ctr);
2231 write_character (dtp, rep_buff, 1, strlen (rep_buff), NODELIM);
2232 dtp->u.p.no_leading_blank = 1;
2234 num++;
2236 /* Output the data, if an intrinsic type, or recurse into this
2237 routine to treat derived types. */
2239 switch (obj->type)
2242 case BT_INTEGER:
2243 write_integer (dtp, p, len);
2244 break;
2246 case BT_LOGICAL:
2247 write_logical (dtp, p, len);
2248 break;
2250 case BT_CHARACTER:
2251 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2252 write_character (dtp, p, 4, obj->string_length, DELIM);
2253 else
2254 write_character (dtp, p, 1, obj->string_length, DELIM);
2255 break;
2257 case BT_REAL:
2258 write_real (dtp, p, len);
2259 break;
2261 case BT_COMPLEX:
2262 dtp->u.p.no_leading_blank = 0;
2263 num++;
2264 write_complex (dtp, p, len, obj_size);
2265 break;
2267 case BT_DERIVED:
2268 case BT_CLASS:
2269 /* To treat a derived type, we need to build two strings:
2270 ext_name = the name, including qualifiers that prepends
2271 component names in the output - passed to
2272 nml_write_obj.
2273 obj_name = the derived type name with no qualifiers but %
2274 appended. This is used to identify the
2275 components. */
2277 /* First ext_name => get length of all possible components */
2278 if (obj->dtio_sub != NULL)
2280 GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number;
2281 char iotype[] = "NAMELIST";
2282 gfc_charlen_type iotype_len = 8;
2283 char tmp_iomsg[IOMSG_LEN] = "";
2284 char *child_iomsg;
2285 gfc_charlen_type child_iomsg_len;
2286 GFC_INTEGER_4 noiostat;
2287 GFC_INTEGER_4 *child_iostat = NULL;
2288 gfc_full_array_i4 vlist;
2289 formatted_dtio dtio_ptr = (formatted_dtio)obj->dtio_sub;
2291 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
2293 /* Set iostat, intent(out). */
2294 noiostat = 0;
2295 child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT)
2296 ? dtp->common.iostat : &noiostat);
2298 /* Set iomsg, intent(inout). */
2299 if (dtp->common.flags & IOPARM_HAS_IOMSG)
2301 child_iomsg = dtp->common.iomsg;
2302 child_iomsg_len = dtp->common.iomsg_len;
2304 else
2306 child_iomsg = tmp_iomsg;
2307 child_iomsg_len = IOMSG_LEN;
2310 /* Call the user defined formatted WRITE procedure. */
2311 dtp->u.p.current_unit->child_dtio++;
2312 if (obj->type == BT_DERIVED)
2314 /* Build a class container. */
2315 gfc_class list_obj;
2316 list_obj.data = p;
2317 list_obj.vptr = obj->vtable;
2318 list_obj.len = 0;
2319 dtio_ptr ((void *)&list_obj, &unit, iotype, &vlist,
2320 child_iostat, child_iomsg,
2321 iotype_len, child_iomsg_len);
2323 else
2325 dtio_ptr (p, &unit, iotype, &vlist,
2326 child_iostat, child_iomsg,
2327 iotype_len, child_iomsg_len);
2329 dtp->u.p.current_unit->child_dtio--;
2331 goto obj_loop;
2334 base_name_len = base_name ? strlen (base_name) : 0;
2335 base_var_name_len = base ? strlen (base->var_name) : 0;
2336 ext_name_len = base_name_len + base_var_name_len
2337 + strlen (obj->var_name) + obj->var_rank * NML_DIGITS + 1;
2338 ext_name = xmalloc (ext_name_len);
2340 if (base_name)
2341 memcpy (ext_name, base_name, base_name_len);
2342 clen = strlen (obj->var_name + base_var_name_len);
2343 memcpy (ext_name + base_name_len,
2344 obj->var_name + base_var_name_len, clen);
2346 /* Append the qualifier. */
2348 tot_len = base_name_len + clen;
2349 for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
2351 if (!dim_i)
2353 ext_name[tot_len] = '(';
2354 tot_len++;
2356 snprintf (ext_name + tot_len, ext_name_len - tot_len, "%d",
2357 (int) obj->ls[dim_i].idx);
2358 tot_len += strlen (ext_name + tot_len);
2359 ext_name[tot_len] = ((int) dim_i == obj->var_rank - 1) ? ')' : ',';
2360 tot_len++;
2363 ext_name[tot_len] = '\0';
2364 for (q = ext_name; *q; q++)
2365 if (*q == '+')
2366 *q = '%';
2368 /* Now obj_name. */
2370 obj_name_len = strlen (obj->var_name) + 1;
2371 obj_name = xmalloc (obj_name_len + 1);
2372 memcpy (obj_name, obj->var_name, obj_name_len-1);
2373 memcpy (obj_name + obj_name_len-1, "%", 2);
2375 /* Now loop over the components. Update the component pointer
2376 with the return value from nml_write_obj => this loop jumps
2377 past nested derived types. */
2379 for (cmp = obj->next;
2380 cmp && !strncmp (cmp->var_name, obj_name, obj_name_len);
2381 cmp = retval)
2383 retval = nml_write_obj (dtp, cmp,
2384 (index_type)(p - obj->mem_pos),
2385 obj, ext_name);
2388 free (obj_name);
2389 free (ext_name);
2390 goto obj_loop;
2392 default:
2393 internal_error (&dtp->common, "Bad type for namelist write");
2396 /* Reset the leading blank suppression, write a comma (or semi-colon)
2397 and, if 5 values have been output, write a newline and advance
2398 to column 2. Reset the repeat counter. */
2400 dtp->u.p.no_leading_blank = 0;
2401 if (obj->type == BT_CHARACTER)
2403 if (dtp->u.p.nml_delim != '\0')
2404 write_character (dtp, &semi_comma, 1, 1, NODELIM);
2406 else
2407 write_character (dtp, &semi_comma, 1, 1, NODELIM);
2408 if (num > 5)
2410 num = 0;
2411 if (dtp->u.p.nml_delim == '\0')
2412 write_character (dtp, &semi_comma, 1, 1, NODELIM);
2413 namelist_write_newline (dtp);
2414 write_character (dtp, " ", 1, 1, NODELIM);
2416 rep_ctr = 1;
2419 /* Cycle through and increment the index vector. */
2421 obj_loop:
2423 nml_carry = 1;
2424 for (dim_i = 0; nml_carry && (dim_i < (size_t) obj->var_rank); dim_i++)
2426 obj->ls[dim_i].idx += nml_carry ;
2427 nml_carry = 0;
2428 if (obj->ls[dim_i].idx > GFC_DESCRIPTOR_UBOUND(obj,dim_i))
2430 obj->ls[dim_i].idx = GFC_DESCRIPTOR_LBOUND(obj,dim_i);
2431 nml_carry = 1;
2436 /* Return a pointer beyond the furthest object accessed. */
2438 return retval;
2442 /* This is the entry function for namelist writes. It outputs the name
2443 of the namelist and iterates through the namelist by calls to
2444 nml_write_obj. The call below has dummys in the arguments used in
2445 the treatment of derived types. */
2447 void
2448 namelist_write (st_parameter_dt *dtp)
2450 namelist_info *t1, *t2, *dummy = NULL;
2451 index_type dummy_offset = 0;
2452 char c;
2453 char *dummy_name = NULL;
2455 /* Set the delimiter for namelist output. */
2456 switch (dtp->u.p.current_unit->delim_status)
2458 case DELIM_APOSTROPHE:
2459 dtp->u.p.nml_delim = '\'';
2460 break;
2461 case DELIM_QUOTE:
2462 case DELIM_UNSPECIFIED:
2463 dtp->u.p.nml_delim = '"';
2464 break;
2465 default:
2466 dtp->u.p.nml_delim = '\0';
2469 if (is_internal_unit (dtp))
2470 write_character (dtp, " ", 1, 1, NODELIM);
2471 write_character (dtp, "&", 1, 1, NODELIM);
2473 /* Write namelist name in upper case - f95 std. */
2474 for (gfc_charlen_type i = 0; i < dtp->namelist_name_len; i++ )
2476 c = safe_toupper (dtp->namelist_name[i]);
2477 write_character (dtp, &c, 1 ,1, NODELIM);
2480 if (dtp->u.p.ionml != NULL)
2482 t1 = dtp->u.p.ionml;
2483 while (t1 != NULL)
2485 t2 = t1;
2486 t1 = nml_write_obj (dtp, t2, dummy_offset, dummy, dummy_name);
2490 namelist_write_newline (dtp);
2491 write_character (dtp, " /", 1, 2, NODELIM);
2494 #undef NML_DIGITS