1 /* Copyright (C) 2002-2014 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)
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/>. */
35 #define star_fill(p, n) memset(p, '*', n)
37 typedef unsigned char uchar
;
39 /* Helper functions for character(kind=4) internal units. These are needed
40 by write_float.def. */
43 memcpy4 (gfc_char4_t
*dest
, const char *source
, int k
)
47 const char *p
= source
;
48 for (j
= 0; j
< k
; j
++)
49 *dest
++ = (gfc_char4_t
) *p
++;
52 /* This include contains the heart and soul of formatted floating point. */
53 #include "write_float.def"
55 /* Write out default char4. */
58 write_default_char4 (st_parameter_dt
*dtp
, const gfc_char4_t
*source
,
59 int src_len
, int w_len
)
66 /* Take care of preceding blanks. */
70 p
= write_block (dtp
, k
);
73 if (is_char4_unit (dtp
))
75 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
82 /* Get ready to handle delimiters if needed. */
83 switch (dtp
->u
.p
.current_unit
->delim_status
)
85 case DELIM_APOSTROPHE
:
96 /* Now process the remaining characters, one at a time. */
97 for (j
= 0; j
< src_len
; j
++)
100 if (is_char4_unit (dtp
))
103 /* Handle delimiters if any. */
104 if (c
== d
&& d
!= ' ')
106 p
= write_block (dtp
, 2);
109 q
= (gfc_char4_t
*) p
;
114 p
= write_block (dtp
, 1);
117 q
= (gfc_char4_t
*) p
;
123 /* Handle delimiters if any. */
124 if (c
== d
&& d
!= ' ')
126 p
= write_block (dtp
, 2);
133 p
= write_block (dtp
, 1);
137 *p
= c
> 255 ? '?' : (uchar
) c
;
143 /* Write out UTF-8 converted from char4. */
146 write_utf8_char4 (st_parameter_dt
*dtp
, gfc_char4_t
*source
,
147 int src_len
, int w_len
)
152 static const uchar masks
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
153 static const uchar limits
[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
157 /* Take care of preceding blanks. */
161 p
= write_block (dtp
, k
);
167 /* Get ready to handle delimiters if needed. */
168 switch (dtp
->u
.p
.current_unit
->delim_status
)
170 case DELIM_APOSTROPHE
:
181 /* Now process the remaining characters, one at a time. */
182 for (j
= k
; j
< src_len
; j
++)
187 /* Handle the delimiters if any. */
188 if (c
== d
&& d
!= ' ')
190 p
= write_block (dtp
, 2);
197 p
= write_block (dtp
, 1);
205 /* Convert to UTF-8 sequence. */
211 *--q
= ((c
& 0x3F) | 0x80);
215 while (c
>= 0x3F || (c
& limits
[nbytes
-1]));
217 *--q
= (c
| masks
[nbytes
-1]);
219 p
= write_block (dtp
, nbytes
);
231 write_a (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
236 wlen
= f
->u
.string
.length
< 0
237 || (f
->format
== FMT_G
&& f
->u
.string
.length
== 0)
238 ? len
: f
->u
.string
.length
;
241 /* If this is formatted STREAM IO convert any embedded line feed characters
242 to CR_LF on systems that use that sequence for newlines. See F2003
243 Standard sections 10.6.3 and 9.9 for further information. */
244 if (is_stream_io (dtp
))
246 const char crlf
[] = "\r\n";
250 /* Write out any padding if needed. */
253 p
= write_block (dtp
, wlen
- len
);
256 memset (p
, ' ', wlen
- len
);
259 /* Scan the source string looking for '\n' and convert it if found. */
260 for (i
= 0; i
< wlen
; i
++)
262 if (source
[i
] == '\n')
264 /* Write out the previously scanned characters in the string. */
267 p
= write_block (dtp
, bytes
);
270 memcpy (p
, &source
[q
], bytes
);
275 /* Write out the CR_LF sequence. */
277 p
= write_block (dtp
, 2);
286 /* Write out any remaining bytes if no LF was found. */
289 p
= write_block (dtp
, bytes
);
292 memcpy (p
, &source
[q
], bytes
);
298 p
= write_block (dtp
, wlen
);
302 if (unlikely (is_char4_unit (dtp
)))
304 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
306 memcpy4 (p4
, source
, wlen
);
309 memset4 (p4
, ' ', wlen
- len
);
310 memcpy4 (p4
+ wlen
- len
, source
, len
);
316 memcpy (p
, source
, wlen
);
319 memset (p
, ' ', wlen
- len
);
320 memcpy (p
+ wlen
- len
, source
, len
);
328 /* The primary difference between write_a_char4 and write_a is that we have to
329 deal with writing from the first byte of the 4-byte character and pay
330 attention to the most significant bytes. For ENCODING="default" write the
331 lowest significant byte. If the 3 most significant bytes contain
332 non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value
333 to the UTF-8 encoded string before writing out. */
336 write_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
341 wlen
= f
->u
.string
.length
< 0
342 || (f
->format
== FMT_G
&& f
->u
.string
.length
== 0)
343 ? len
: f
->u
.string
.length
;
345 q
= (gfc_char4_t
*) source
;
347 /* If this is formatted STREAM IO convert any embedded line feed characters
348 to CR_LF on systems that use that sequence for newlines. See F2003
349 Standard sections 10.6.3 and 9.9 for further information. */
350 if (is_stream_io (dtp
))
352 const gfc_char4_t crlf
[] = {0x000d,0x000a};
357 /* Write out any padding if needed. */
361 p
= write_block (dtp
, wlen
- len
);
364 memset (p
, ' ', wlen
- len
);
367 /* Scan the source string looking for '\n' and convert it if found. */
368 qq
= (gfc_char4_t
*) source
;
369 for (i
= 0; i
< wlen
; i
++)
373 /* Write out the previously scanned characters in the string. */
376 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
377 write_utf8_char4 (dtp
, q
, bytes
, 0);
379 write_default_char4 (dtp
, q
, bytes
, 0);
383 /* Write out the CR_LF sequence. */
384 write_default_char4 (dtp
, crlf
, 2, 0);
390 /* Write out any remaining bytes if no LF was found. */
393 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
394 write_utf8_char4 (dtp
, q
, bytes
, 0);
396 write_default_char4 (dtp
, q
, bytes
, 0);
402 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
403 write_utf8_char4 (dtp
, q
, len
, wlen
);
405 write_default_char4 (dtp
, q
, len
, wlen
);
412 static GFC_INTEGER_LARGEST
413 extract_int (const void *p
, int len
)
415 GFC_INTEGER_LARGEST i
= 0;
425 memcpy ((void *) &tmp
, p
, len
);
432 memcpy ((void *) &tmp
, p
, len
);
439 memcpy ((void *) &tmp
, p
, len
);
446 memcpy ((void *) &tmp
, p
, len
);
450 #ifdef HAVE_GFC_INTEGER_16
454 memcpy ((void *) &tmp
, p
, len
);
460 internal_error (NULL
, "bad integer kind");
466 static GFC_UINTEGER_LARGEST
467 extract_uint (const void *p
, int len
)
469 GFC_UINTEGER_LARGEST i
= 0;
479 memcpy ((void *) &tmp
, p
, len
);
480 i
= (GFC_UINTEGER_1
) tmp
;
486 memcpy ((void *) &tmp
, p
, len
);
487 i
= (GFC_UINTEGER_2
) tmp
;
493 memcpy ((void *) &tmp
, p
, len
);
494 i
= (GFC_UINTEGER_4
) tmp
;
500 memcpy ((void *) &tmp
, p
, len
);
501 i
= (GFC_UINTEGER_8
) tmp
;
504 #ifdef HAVE_GFC_INTEGER_16
508 GFC_INTEGER_16 tmp
= 0;
509 memcpy ((void *) &tmp
, p
, len
);
510 i
= (GFC_UINTEGER_16
) tmp
;
515 internal_error (NULL
, "bad integer kind");
523 write_l (st_parameter_dt
*dtp
, const fnode
*f
, char *source
, int len
)
527 GFC_INTEGER_LARGEST n
;
529 wlen
= (f
->format
== FMT_G
&& f
->u
.w
== 0) ? 1 : f
->u
.w
;
531 p
= write_block (dtp
, wlen
);
535 n
= extract_int (source
, len
);
537 if (unlikely (is_char4_unit (dtp
)))
539 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
540 memset4 (p4
, ' ', wlen
-1);
541 p4
[wlen
- 1] = (n
) ? 'T' : 'F';
545 memset (p
, ' ', wlen
-1);
546 p
[wlen
- 1] = (n
) ? 'T' : 'F';
551 write_boz (st_parameter_dt
*dtp
, const fnode
*f
, const char *q
, int n
)
553 int w
, m
, digits
, nzero
, nblank
;
561 if (m
== 0 && n
== 0)
566 p
= write_block (dtp
, w
);
569 if (unlikely (is_char4_unit (dtp
)))
571 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
572 memset4 (p4
, ' ', w
);
581 /* Select a width if none was specified. The idea here is to always
585 w
= ((digits
< m
) ? m
: digits
);
587 p
= write_block (dtp
, w
);
595 /* See if things will work. */
597 nblank
= w
- (nzero
+ digits
);
599 if (unlikely (is_char4_unit (dtp
)))
601 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
604 memset4 (p4
, '*', w
);
608 if (!dtp
->u
.p
.no_leading_blank
)
610 memset4 (p4
, ' ', nblank
);
612 memset4 (p4
, '0', nzero
);
614 memcpy4 (p4
, q
, digits
);
618 memset4 (p4
, '0', nzero
);
620 memcpy4 (p4
, q
, digits
);
622 memset4 (p4
, ' ', nblank
);
623 dtp
->u
.p
.no_leading_blank
= 0;
634 if (!dtp
->u
.p
.no_leading_blank
)
636 memset (p
, ' ', nblank
);
638 memset (p
, '0', nzero
);
640 memcpy (p
, q
, digits
);
644 memset (p
, '0', nzero
);
646 memcpy (p
, q
, digits
);
648 memset (p
, ' ', nblank
);
649 dtp
->u
.p
.no_leading_blank
= 0;
657 write_decimal (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
,
659 const char *(*conv
) (GFC_INTEGER_LARGEST
, char *, size_t))
661 GFC_INTEGER_LARGEST n
= 0;
662 int w
, m
, digits
, nsign
, nzero
, nblank
;
666 char itoa_buf
[GFC_BTOA_BUF_SIZE
];
669 m
= f
->format
== FMT_G
? -1 : f
->u
.integer
.m
;
671 n
= extract_int (source
, len
);
674 if (m
== 0 && n
== 0)
679 p
= write_block (dtp
, w
);
682 if (unlikely (is_char4_unit (dtp
)))
684 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
685 memset4 (p4
, ' ', w
);
692 sign
= calculate_sign (dtp
, n
< 0);
695 nsign
= sign
== S_NONE
? 0 : 1;
697 /* conv calls itoa which sets the negative sign needed
698 by write_integer. The sign '+' or '-' is set below based on sign
699 calculated above, so we just point past the sign in the string
700 before proceeding to avoid double signs in corner cases.
702 q
= conv (n
, itoa_buf
, sizeof (itoa_buf
));
708 /* Select a width if none was specified. The idea here is to always
712 w
= ((digits
< m
) ? m
: digits
) + nsign
;
714 p
= write_block (dtp
, w
);
722 /* See if things will work. */
724 nblank
= w
- (nsign
+ nzero
+ digits
);
726 if (unlikely (is_char4_unit (dtp
)))
728 gfc_char4_t
* p4
= (gfc_char4_t
*) p
;
731 memset4 (p4
, '*', w
);
735 memset4 (p4
, ' ', nblank
);
750 memset4 (p4
, '0', nzero
);
753 memcpy4 (p4
, q
, digits
);
763 memset (p
, ' ', nblank
);
778 memset (p
, '0', nzero
);
781 memcpy (p
, q
, digits
);
788 /* Convert unsigned octal to ascii. */
791 otoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
795 assert (len
>= GFC_OTOA_BUF_SIZE
);
800 p
= buffer
+ GFC_OTOA_BUF_SIZE
- 1;
805 *--p
= '0' + (n
& 7);
813 /* Convert unsigned binary to ascii. */
816 btoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
820 assert (len
>= GFC_BTOA_BUF_SIZE
);
825 p
= buffer
+ GFC_BTOA_BUF_SIZE
- 1;
830 *--p
= '0' + (n
& 1);
837 /* The following three functions, btoa_big, otoa_big, and ztoa_big, are needed
838 to convert large reals with kind sizes that exceed the largest integer type
839 available on certain platforms. In these cases, byte by byte conversion is
840 performed. Endianess is taken into account. */
842 /* Conversion to binary. */
845 btoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
854 for (i
= 0; i
< len
; i
++)
858 /* Test for zero. Needed by write_boz later. */
862 for (j
= 0; j
< 8; j
++)
864 *q
++ = (c
& 128) ? '1' : '0';
872 const char *p
= s
+ len
- 1;
873 for (i
= 0; i
< len
; i
++)
877 /* Test for zero. Needed by write_boz later. */
881 for (j
= 0; j
< 8; j
++)
883 *q
++ = (c
& 128) ? '1' : '0';
895 /* Move past any leading zeros. */
896 while (*buffer
== '0')
903 /* Conversion to octal. */
906 otoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
912 q
= buffer
+ GFC_OTOA_BUF_SIZE
- 1;
918 const char *p
= s
+ len
- 1;
922 /* Test for zero. Needed by write_boz later. */
926 for (j
= 0; j
< 3 && i
< len
; j
++)
928 octet
|= (c
& 1) << j
;
947 /* Test for zero. Needed by write_boz later. */
951 for (j
= 0; j
< 3 && i
< len
; j
++)
953 octet
|= (c
& 1) << j
;
970 /* Move past any leading zeros. */
977 /* Conversion to hexidecimal. */
980 ztoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
982 static char a
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
983 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
994 for (i
= 0; i
< len
; i
++)
996 /* Test for zero. Needed by write_boz later. */
1000 h
= (*p
>> 4) & 0x0F;
1008 const char *p
= s
+ len
- 1;
1009 for (i
= 0; i
< len
; i
++)
1011 /* Test for zero. Needed by write_boz later. */
1015 h
= (*p
>> 4) & 0x0F;
1027 /* Move past any leading zeros. */
1028 while (*buffer
== '0')
1034 /* gfc_itoa()-- Integer to decimal conversion.
1035 The itoa function is a widespread non-standard extension to standard
1036 C, often declared in <stdlib.h>. Even though the itoa defined here
1037 is a static function we take care not to conflict with any prior
1038 non-static declaration. Hence the 'gfc_' prefix, which is normally
1039 reserved for functions with external linkage. */
1042 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
1046 GFC_UINTEGER_LARGEST t
;
1048 assert (len
>= GFC_ITOA_BUF_SIZE
);
1058 t
= -n
; /*must use unsigned to protect from overflow*/
1061 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
1066 *--p
= '0' + (t
% 10);
1077 write_i (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1079 write_decimal (dtp
, f
, p
, len
, (void *) gfc_itoa
);
1084 write_b (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1087 char itoa_buf
[GFC_BTOA_BUF_SIZE
];
1088 GFC_UINTEGER_LARGEST n
= 0;
1090 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1092 p
= btoa_big (source
, itoa_buf
, len
, &n
);
1093 write_boz (dtp
, f
, p
, n
);
1097 n
= extract_uint (source
, len
);
1098 p
= btoa (n
, itoa_buf
, sizeof (itoa_buf
));
1099 write_boz (dtp
, f
, p
, n
);
1105 write_o (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1108 char itoa_buf
[GFC_OTOA_BUF_SIZE
];
1109 GFC_UINTEGER_LARGEST n
= 0;
1111 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1113 p
= otoa_big (source
, itoa_buf
, len
, &n
);
1114 write_boz (dtp
, f
, p
, n
);
1118 n
= extract_uint (source
, len
);
1119 p
= otoa (n
, itoa_buf
, sizeof (itoa_buf
));
1120 write_boz (dtp
, f
, p
, n
);
1125 write_z (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1128 char itoa_buf
[GFC_XTOA_BUF_SIZE
];
1129 GFC_UINTEGER_LARGEST n
= 0;
1131 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1133 p
= ztoa_big (source
, itoa_buf
, len
, &n
);
1134 write_boz (dtp
, f
, p
, n
);
1138 n
= extract_uint (source
, len
);
1139 p
= gfc_xtoa (n
, itoa_buf
, sizeof (itoa_buf
));
1140 write_boz (dtp
, f
, p
, n
);
1146 write_d (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1148 write_float (dtp
, f
, p
, len
, 0);
1153 write_e (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1155 write_float (dtp
, f
, p
, len
, 0);
1160 write_f (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1162 write_float (dtp
, f
, p
, len
, 0);
1167 write_en (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1169 write_float (dtp
, f
, p
, len
, 0);
1174 write_es (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1176 write_float (dtp
, f
, p
, len
, 0);
1180 /* Take care of the X/TR descriptor. */
1183 write_x (st_parameter_dt
*dtp
, int len
, int nspaces
)
1187 p
= write_block (dtp
, len
);
1190 if (nspaces
> 0 && len
- nspaces
>= 0)
1192 if (unlikely (is_char4_unit (dtp
)))
1194 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1195 memset4 (&p4
[len
- nspaces
], ' ', nspaces
);
1198 memset (&p
[len
- nspaces
], ' ', nspaces
);
1203 /* List-directed writing. */
1206 /* Write a single character to the output. Returns nonzero if
1207 something goes wrong. */
1210 write_char (st_parameter_dt
*dtp
, int c
)
1214 p
= write_block (dtp
, 1);
1217 if (unlikely (is_char4_unit (dtp
)))
1219 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1230 /* Write a list-directed logical value. */
1233 write_logical (st_parameter_dt
*dtp
, const char *source
, int length
)
1235 write_char (dtp
, extract_int (source
, length
) ? 'T' : 'F');
1239 /* Write a list-directed integer value. */
1242 write_integer (st_parameter_dt
*dtp
, const char *source
, int length
)
1248 char itoa_buf
[GFC_ITOA_BUF_SIZE
];
1250 q
= gfc_itoa (extract_int (source
, length
), itoa_buf
, sizeof (itoa_buf
));
1275 digits
= strlen (q
);
1279 p
= write_block (dtp
, width
);
1283 if (unlikely (is_char4_unit (dtp
)))
1285 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1286 if (dtp
->u
.p
.no_leading_blank
)
1288 memcpy4 (p4
, q
, digits
);
1289 memset4 (p4
+ digits
, ' ', width
- digits
);
1293 memset4 (p4
, ' ', width
- digits
);
1294 memcpy4 (p4
+ width
- digits
, q
, digits
);
1299 if (dtp
->u
.p
.no_leading_blank
)
1301 memcpy (p
, q
, digits
);
1302 memset (p
+ digits
, ' ', width
- digits
);
1306 memset (p
, ' ', width
- digits
);
1307 memcpy (p
+ width
- digits
, q
, digits
);
1312 /* Write a list-directed string. We have to worry about delimiting
1313 the strings if the file has been opened in that mode. */
1319 write_character (st_parameter_dt
*dtp
, const char *source
, int kind
, int length
, int mode
)
1326 switch (dtp
->u
.p
.current_unit
->delim_status
)
1328 case DELIM_APOSTROPHE
:
1350 for (i
= 0; i
< length
; i
++)
1355 p
= write_block (dtp
, length
+ extra
);
1359 if (unlikely (is_char4_unit (dtp
)))
1361 gfc_char4_t d4
= (gfc_char4_t
) d
;
1362 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1365 memcpy4 (p4
, source
, length
);
1370 for (i
= 0; i
< length
; i
++)
1372 *p4
++ = (gfc_char4_t
) source
[i
];
1383 memcpy (p
, source
, length
);
1388 for (i
= 0; i
< length
; i
++)
1402 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
1403 write_utf8_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1405 write_default_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1409 p
= write_block (dtp
, 1);
1412 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
1413 write_utf8_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1415 write_default_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1417 p
= write_block (dtp
, 1);
1424 /* Set an fnode to default format. */
1427 set_fnode_default (st_parameter_dt
*dtp
, fnode
*f
, int length
)
1453 internal_error (&dtp
->common
, "bad real kind");
1458 /* Output a real number with default format. To guarantee that a
1459 binary -> decimal -> binary roundtrip conversion recovers the
1460 original value, IEEE 754-2008 requires 9, 17, 21 and 36 significant
1461 digits for REAL kinds 4, 8, 10, and 16, respectively. Thus, we use
1462 1PG16.9E2 for REAL(4), 1PG25.17E3 for REAL(8), 1PG30.21E4 for
1463 REAL(10) and 1PG45.36E4 for REAL(16). The exception is that the
1464 Fortran standard requires outputting an extra digit when the scale
1465 factor is 1 and when the magnitude of the value is such that E
1466 editing is used. However, gfortran compensates for this, and thus
1467 for list formatted the same number of significant digits is
1468 generated both when using F and E editing. */
1471 write_real (st_parameter_dt
*dtp
, const char *source
, int length
)
1474 int org_scale
= dtp
->u
.p
.scale_factor
;
1475 dtp
->u
.p
.scale_factor
= 1;
1476 set_fnode_default (dtp
, &f
, length
);
1477 write_float (dtp
, &f
, source
, length
, 1);
1478 dtp
->u
.p
.scale_factor
= org_scale
;
1481 /* Similar to list formatted REAL output, for kPG0 where k > 0 we
1482 compensate for the extra digit. */
1485 write_real_g0 (st_parameter_dt
*dtp
, const char *source
, int length
, int d
)
1489 set_fnode_default (dtp
, &f
, length
);
1493 /* Compensate for extra digits when using scale factor, d is not
1494 specified, and the magnitude is such that E editing is used. */
1495 if (dtp
->u
.p
.scale_factor
> 0 && d
== 0)
1499 dtp
->u
.p
.g0_no_blanks
= 1;
1500 write_float (dtp
, &f
, source
, length
, comp_d
);
1501 dtp
->u
.p
.g0_no_blanks
= 0;
1506 write_complex (st_parameter_dt
*dtp
, const char *source
, int kind
, size_t size
)
1509 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';';
1511 if (write_char (dtp
, '('))
1513 write_real (dtp
, source
, kind
);
1515 if (write_char (dtp
, semi_comma
))
1517 write_real (dtp
, source
+ size
/ 2, kind
);
1519 write_char (dtp
, ')');
1523 /* Write the separator between items. */
1526 write_separator (st_parameter_dt
*dtp
)
1530 p
= write_block (dtp
, options
.separator_len
);
1533 if (unlikely (is_char4_unit (dtp
)))
1535 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1536 memcpy4 (p4
, options
.separator
, options
.separator_len
);
1539 memcpy (p
, options
.separator
, options
.separator_len
);
1543 /* Write an item with list formatting.
1544 TODO: handle skipping to the next record correctly, particularly
1548 list_formatted_write_scalar (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1551 if (dtp
->u
.p
.current_unit
== NULL
)
1554 if (dtp
->u
.p
.first_item
)
1556 dtp
->u
.p
.first_item
= 0;
1557 write_char (dtp
, ' ');
1561 if (type
!= BT_CHARACTER
|| !dtp
->u
.p
.char_flag
||
1562 (dtp
->u
.p
.current_unit
->delim_status
!= DELIM_NONE
1563 && dtp
->u
.p
.current_unit
->delim_status
!= DELIM_UNSPECIFIED
))
1564 write_separator (dtp
);
1570 write_integer (dtp
, p
, kind
);
1573 write_logical (dtp
, p
, kind
);
1576 write_character (dtp
, p
, kind
, size
, DELIM
);
1579 write_real (dtp
, p
, kind
);
1582 write_complex (dtp
, p
, kind
, size
);
1585 internal_error (&dtp
->common
, "list_formatted_write(): Bad type");
1588 dtp
->u
.p
.char_flag
= (type
== BT_CHARACTER
);
1593 list_formatted_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1594 size_t size
, size_t nelems
)
1598 size_t stride
= type
== BT_CHARACTER
?
1599 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1603 /* Big loop over all the elements. */
1604 for (elem
= 0; elem
< nelems
; elem
++)
1606 dtp
->u
.p
.item_count
++;
1607 list_formatted_write_scalar (dtp
, type
, tmp
+ elem
* stride
, kind
, size
);
1613 nml_write_obj writes a namelist object to the output stream. It is called
1614 recursively for derived type components:
1615 obj = is the namelist_info for the current object.
1616 offset = the offset relative to the address held by the object for
1617 derived type arrays.
1618 base = is the namelist_info of the derived type, when obj is a
1620 base_name = the full name for a derived type, including qualifiers
1622 The returned value is a pointer to the object beyond the last one
1623 accessed, including nested derived types. Notice that the namelist is
1624 a linear linked list of objects, including derived types and their
1625 components. A tree, of sorts, is implied by the compound names of
1626 the derived type components and this is how this function recurses through
1629 /* A generous estimate of the number of characters needed to print
1630 repeat counts and indices, including commas, asterices and brackets. */
1632 #define NML_DIGITS 20
1635 namelist_write_newline (st_parameter_dt
*dtp
)
1637 if (!is_internal_unit (dtp
))
1640 write_character (dtp
, "\r\n", 1, 2, NODELIM
);
1642 write_character (dtp
, "\n", 1, 1, NODELIM
);
1647 if (is_array_io (dtp
))
1652 int length
= dtp
->u
.p
.current_unit
->bytes_left
;
1654 p
= write_block (dtp
, length
);
1658 if (unlikely (is_char4_unit (dtp
)))
1660 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1661 memset4 (p4
, ' ', length
);
1664 memset (p
, ' ', length
);
1666 /* Now that the current record has been padded out,
1667 determine where the next record in the array is. */
1668 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
1671 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
1674 /* Now seek to this record */
1675 record
= record
* dtp
->u
.p
.current_unit
->recl
;
1677 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
1679 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
1683 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
1687 write_character (dtp
, " ", 1, 1, NODELIM
);
1691 static namelist_info
*
1692 nml_write_obj (st_parameter_dt
*dtp
, namelist_info
* obj
, index_type offset
,
1693 namelist_info
* base
, char * base_name
)
1699 index_type obj_size
;
1703 index_type elem_ctr
;
1704 size_t obj_name_len
;
1709 size_t ext_name_len
;
1710 char rep_buff
[NML_DIGITS
];
1711 namelist_info
* cmp
;
1712 namelist_info
* retval
= obj
->next
;
1713 size_t base_name_len
;
1714 size_t base_var_name_len
;
1717 /* Set the character to be used to separate values
1718 to a comma or semi-colon. */
1721 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';';
1723 /* Write namelist variable names in upper case. If a derived type,
1724 nothing is output. If a component, base and base_name are set. */
1726 if (obj
->type
!= BT_DERIVED
)
1728 namelist_write_newline (dtp
);
1729 write_character (dtp
, " ", 1, 1, NODELIM
);
1734 len
= strlen (base
->var_name
);
1735 base_name_len
= strlen (base_name
);
1736 for (dim_i
= 0; dim_i
< base_name_len
; dim_i
++)
1738 cup
= toupper ((int) base_name
[dim_i
]);
1739 write_character (dtp
, &cup
, 1, 1, NODELIM
);
1742 clen
= strlen (obj
->var_name
);
1743 for (dim_i
= len
; dim_i
< clen
; dim_i
++)
1745 cup
= toupper ((int) obj
->var_name
[dim_i
]);
1746 write_character (dtp
, &cup
, 1, 1, NODELIM
);
1748 write_character (dtp
, "=", 1, 1, NODELIM
);
1751 /* Counts the number of data output on a line, including names. */
1761 obj_size
= size_from_real_kind (len
);
1765 obj_size
= size_from_complex_kind (len
);
1769 obj_size
= obj
->string_length
;
1777 obj_size
= obj
->size
;
1779 /* Set the index vector and count the number of elements. */
1782 for (dim_i
= 0; dim_i
< (size_t) obj
->var_rank
; dim_i
++)
1784 obj
->ls
[dim_i
].idx
= GFC_DESCRIPTOR_LBOUND(obj
, dim_i
);
1785 nelem
= nelem
* GFC_DESCRIPTOR_EXTENT (obj
, dim_i
);
1788 /* Main loop to output the data held in the object. */
1791 for (elem_ctr
= 0; elem_ctr
< nelem
; elem_ctr
++)
1794 /* Build the pointer to the data value. The offset is passed by
1795 recursive calls to this function for arrays of derived types.
1796 Is NULL otherwise. */
1798 p
= (void *)(obj
->mem_pos
+ elem_ctr
* obj_size
);
1801 /* Check for repeat counts of intrinsic types. */
1803 if ((elem_ctr
< (nelem
- 1)) &&
1804 (obj
->type
!= BT_DERIVED
) &&
1805 !memcmp (p
, (void*)(p
+ obj_size
), obj_size
))
1810 /* Execute a repeated output. Note the flag no_leading_blank that
1811 is used in the functions used to output the intrinsic types. */
1817 snprintf(rep_buff
, NML_DIGITS
, " %d*", rep_ctr
);
1818 write_character (dtp
, rep_buff
, 1, strlen (rep_buff
), NODELIM
);
1819 dtp
->u
.p
.no_leading_blank
= 1;
1823 /* Output the data, if an intrinsic type, or recurse into this
1824 routine to treat derived types. */
1830 write_integer (dtp
, p
, len
);
1834 write_logical (dtp
, p
, len
);
1838 write_character (dtp
, p
, 1, obj
->string_length
, DELIM
);
1842 write_real (dtp
, p
, len
);
1846 dtp
->u
.p
.no_leading_blank
= 0;
1848 write_complex (dtp
, p
, len
, obj_size
);
1853 /* To treat a derived type, we need to build two strings:
1854 ext_name = the name, including qualifiers that prepends
1855 component names in the output - passed to
1857 obj_name = the derived type name with no qualifiers but %
1858 appended. This is used to identify the
1861 /* First ext_name => get length of all possible components */
1863 base_name_len
= base_name
? strlen (base_name
) : 0;
1864 base_var_name_len
= base
? strlen (base
->var_name
) : 0;
1865 ext_name_len
= base_name_len
+ base_var_name_len
1866 + strlen (obj
->var_name
) + obj
->var_rank
* NML_DIGITS
+ 1;
1867 ext_name
= (char*)xmalloc (ext_name_len
);
1869 memcpy (ext_name
, base_name
, base_name_len
);
1870 clen
= strlen (obj
->var_name
+ base_var_name_len
);
1871 memcpy (ext_name
+ base_name_len
,
1872 obj
->var_name
+ base_var_name_len
, clen
);
1874 /* Append the qualifier. */
1876 tot_len
= base_name_len
+ clen
;
1877 for (dim_i
= 0; dim_i
< (size_t) obj
->var_rank
; dim_i
++)
1881 ext_name
[tot_len
] = '(';
1884 snprintf (ext_name
+ tot_len
, ext_name_len
- tot_len
, "%d",
1885 (int) obj
->ls
[dim_i
].idx
);
1886 tot_len
+= strlen (ext_name
+ tot_len
);
1887 ext_name
[tot_len
] = ((int) dim_i
== obj
->var_rank
- 1) ? ')' : ',';
1891 ext_name
[tot_len
] = '\0';
1895 obj_name_len
= strlen (obj
->var_name
) + 1;
1896 obj_name
= xmalloc (obj_name_len
+1);
1897 memcpy (obj_name
, obj
->var_name
, obj_name_len
-1);
1898 memcpy (obj_name
+ obj_name_len
-1, "%", 2);
1900 /* Now loop over the components. Update the component pointer
1901 with the return value from nml_write_obj => this loop jumps
1902 past nested derived types. */
1904 for (cmp
= obj
->next
;
1905 cmp
&& !strncmp (cmp
->var_name
, obj_name
, obj_name_len
);
1908 retval
= nml_write_obj (dtp
, cmp
,
1909 (index_type
)(p
- obj
->mem_pos
),
1918 internal_error (&dtp
->common
, "Bad type for namelist write");
1921 /* Reset the leading blank suppression, write a comma (or semi-colon)
1922 and, if 5 values have been output, write a newline and advance
1923 to column 2. Reset the repeat counter. */
1925 dtp
->u
.p
.no_leading_blank
= 0;
1926 if (obj
->type
== BT_CHARACTER
)
1928 if (dtp
->u
.p
.nml_delim
!= '\0')
1929 write_character (dtp
, &semi_comma
, 1, 1, NODELIM
);
1932 write_character (dtp
, &semi_comma
, 1, 1, NODELIM
);
1936 if (dtp
->u
.p
.nml_delim
== '\0')
1937 write_character (dtp
, &semi_comma
, 1, 1, NODELIM
);
1938 namelist_write_newline (dtp
);
1939 write_character (dtp
, " ", 1, 1, NODELIM
);
1944 /* Cycle through and increment the index vector. */
1949 for (dim_i
= 0; nml_carry
&& (dim_i
< (size_t) obj
->var_rank
); dim_i
++)
1951 obj
->ls
[dim_i
].idx
+= nml_carry
;
1953 if (obj
->ls
[dim_i
].idx
> GFC_DESCRIPTOR_UBOUND(obj
,dim_i
))
1955 obj
->ls
[dim_i
].idx
= GFC_DESCRIPTOR_LBOUND(obj
,dim_i
);
1961 /* Return a pointer beyond the furthest object accessed. */
1967 /* This is the entry function for namelist writes. It outputs the name
1968 of the namelist and iterates through the namelist by calls to
1969 nml_write_obj. The call below has dummys in the arguments used in
1970 the treatment of derived types. */
1973 namelist_write (st_parameter_dt
*dtp
)
1975 namelist_info
* t1
, *t2
, *dummy
= NULL
;
1977 index_type dummy_offset
= 0;
1979 char * dummy_name
= NULL
;
1981 /* Set the delimiter for namelist output. */
1982 switch (dtp
->u
.p
.current_unit
->delim_status
)
1984 case DELIM_APOSTROPHE
:
1985 dtp
->u
.p
.nml_delim
= '\'';
1988 case DELIM_UNSPECIFIED
:
1989 dtp
->u
.p
.nml_delim
= '"';
1992 dtp
->u
.p
.nml_delim
= '\0';
1995 write_character (dtp
, "&", 1, 1, NODELIM
);
1997 /* Write namelist name in upper case - f95 std. */
1998 for (i
= 0 ;i
< dtp
->namelist_name_len
;i
++ )
2000 c
= toupper ((int) dtp
->namelist_name
[i
]);
2001 write_character (dtp
, &c
, 1 ,1, NODELIM
);
2004 if (dtp
->u
.p
.ionml
!= NULL
)
2006 t1
= dtp
->u
.p
.ionml
;
2010 t1
= nml_write_obj (dtp
, t2
, dummy_offset
, dummy
, dummy_name
);
2014 namelist_write_newline (dtp
);
2015 write_character (dtp
, " /", 1, 2, NODELIM
);