1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist output contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
37 #define star_fill(p, n) memset(p, '*', n)
39 typedef unsigned char uchar
;
41 /* Helper functions for character(kind=4) internal units. These are needed
42 by write_float.def. */
45 memcpy4 (gfc_char4_t
*dest
, const char *source
, int k
)
49 const char *p
= source
;
50 for (j
= 0; j
< k
; j
++)
51 *dest
++ = (gfc_char4_t
) *p
++;
54 /* This include contains the heart and soul of formatted floating point. */
55 #include "write_float.def"
57 /* Write out default char4. */
60 write_default_char4 (st_parameter_dt
*dtp
, const gfc_char4_t
*source
,
61 int src_len
, int w_len
)
68 /* Take care of preceding blanks. */
72 p
= write_block (dtp
, k
);
75 if (is_char4_unit (dtp
))
77 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
84 /* Get ready to handle delimiters if needed. */
85 switch (dtp
->u
.p
.current_unit
->delim_status
)
87 case DELIM_APOSTROPHE
:
98 /* Now process the remaining characters, one at a time. */
99 for (j
= 0; j
< src_len
; j
++)
102 if (is_char4_unit (dtp
))
105 /* Handle delimiters if any. */
106 if (c
== d
&& d
!= ' ')
108 p
= write_block (dtp
, 2);
111 q
= (gfc_char4_t
*) p
;
116 p
= write_block (dtp
, 1);
119 q
= (gfc_char4_t
*) p
;
125 /* Handle delimiters if any. */
126 if (c
== d
&& d
!= ' ')
128 p
= write_block (dtp
, 2);
135 p
= write_block (dtp
, 1);
139 *p
= c
> 255 ? '?' : (uchar
) c
;
145 /* Write out UTF-8 converted from char4. */
148 write_utf8_char4 (st_parameter_dt
*dtp
, gfc_char4_t
*source
,
149 int src_len
, int w_len
)
154 static const uchar masks
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
155 static const uchar limits
[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
159 /* Take care of preceding blanks. */
163 p
= write_block (dtp
, k
);
169 /* Get ready to handle delimiters if needed. */
170 switch (dtp
->u
.p
.current_unit
->delim_status
)
172 case DELIM_APOSTROPHE
:
183 /* Now process the remaining characters, one at a time. */
184 for (j
= k
; j
< src_len
; j
++)
189 /* Handle the delimiters if any. */
190 if (c
== d
&& d
!= ' ')
192 p
= write_block (dtp
, 2);
199 p
= write_block (dtp
, 1);
207 /* Convert to UTF-8 sequence. */
213 *--q
= ((c
& 0x3F) | 0x80);
217 while (c
>= 0x3F || (c
& limits
[nbytes
-1]));
219 *--q
= (c
| masks
[nbytes
-1]);
221 p
= write_block (dtp
, nbytes
);
233 write_a (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
238 wlen
= f
->u
.string
.length
< 0
239 || (f
->format
== FMT_G
&& f
->u
.string
.length
== 0)
240 ? len
: f
->u
.string
.length
;
243 /* If this is formatted STREAM IO convert any embedded line feed characters
244 to CR_LF on systems that use that sequence for newlines. See F2003
245 Standard sections 10.6.3 and 9.9 for further information. */
246 if (is_stream_io (dtp
))
248 const char crlf
[] = "\r\n";
252 /* Write out any padding if needed. */
255 p
= write_block (dtp
, wlen
- len
);
258 memset (p
, ' ', wlen
- len
);
261 /* Scan the source string looking for '\n' and convert it if found. */
262 for (i
= 0; i
< wlen
; i
++)
264 if (source
[i
] == '\n')
266 /* Write out the previously scanned characters in the string. */
269 p
= write_block (dtp
, bytes
);
272 memcpy (p
, &source
[q
], bytes
);
277 /* Write out the CR_LF sequence. */
279 p
= write_block (dtp
, 2);
288 /* Write out any remaining bytes if no LF was found. */
291 p
= write_block (dtp
, bytes
);
294 memcpy (p
, &source
[q
], bytes
);
300 p
= write_block (dtp
, wlen
);
304 if (unlikely (is_char4_unit (dtp
)))
306 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
308 memcpy4 (p4
, source
, wlen
);
311 memset4 (p4
, ' ', wlen
- len
);
312 memcpy4 (p4
+ wlen
- len
, source
, len
);
318 memcpy (p
, source
, wlen
);
321 memset (p
, ' ', wlen
- len
);
322 memcpy (p
+ wlen
- len
, source
, len
);
330 /* The primary difference between write_a_char4 and write_a is that we have to
331 deal with writing from the first byte of the 4-byte character and pay
332 attention to the most significant bytes. For ENCODING="default" write the
333 lowest significant byte. If the 3 most significant bytes contain
334 non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value
335 to the UTF-8 encoded string before writing out. */
338 write_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
343 wlen
= f
->u
.string
.length
< 0
344 || (f
->format
== FMT_G
&& f
->u
.string
.length
== 0)
345 ? len
: f
->u
.string
.length
;
347 q
= (gfc_char4_t
*) source
;
349 /* If this is formatted STREAM IO convert any embedded line feed characters
350 to CR_LF on systems that use that sequence for newlines. See F2003
351 Standard sections 10.6.3 and 9.9 for further information. */
352 if (is_stream_io (dtp
))
354 const gfc_char4_t crlf
[] = {0x000d,0x000a};
359 /* Write out any padding if needed. */
363 p
= write_block (dtp
, wlen
- len
);
366 memset (p
, ' ', wlen
- len
);
369 /* Scan the source string looking for '\n' and convert it if found. */
370 qq
= (gfc_char4_t
*) source
;
371 for (i
= 0; i
< wlen
; i
++)
375 /* Write out the previously scanned characters in the string. */
378 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
379 write_utf8_char4 (dtp
, q
, bytes
, 0);
381 write_default_char4 (dtp
, q
, bytes
, 0);
385 /* Write out the CR_LF sequence. */
386 write_default_char4 (dtp
, crlf
, 2, 0);
392 /* Write out any remaining bytes if no LF was found. */
395 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
396 write_utf8_char4 (dtp
, q
, bytes
, 0);
398 write_default_char4 (dtp
, q
, bytes
, 0);
404 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
405 write_utf8_char4 (dtp
, q
, len
, wlen
);
407 write_default_char4 (dtp
, q
, len
, wlen
);
414 static GFC_INTEGER_LARGEST
415 extract_int (const void *p
, int len
)
417 GFC_INTEGER_LARGEST i
= 0;
427 memcpy ((void *) &tmp
, p
, len
);
434 memcpy ((void *) &tmp
, p
, len
);
441 memcpy ((void *) &tmp
, p
, len
);
448 memcpy ((void *) &tmp
, p
, len
);
452 #ifdef HAVE_GFC_INTEGER_16
456 memcpy ((void *) &tmp
, p
, len
);
462 internal_error (NULL
, "bad integer kind");
468 static GFC_UINTEGER_LARGEST
469 extract_uint (const void *p
, int len
)
471 GFC_UINTEGER_LARGEST i
= 0;
481 memcpy ((void *) &tmp
, p
, len
);
482 i
= (GFC_UINTEGER_1
) tmp
;
488 memcpy ((void *) &tmp
, p
, len
);
489 i
= (GFC_UINTEGER_2
) tmp
;
495 memcpy ((void *) &tmp
, p
, len
);
496 i
= (GFC_UINTEGER_4
) tmp
;
502 memcpy ((void *) &tmp
, p
, len
);
503 i
= (GFC_UINTEGER_8
) tmp
;
506 #ifdef HAVE_GFC_INTEGER_16
510 GFC_INTEGER_16 tmp
= 0;
511 memcpy ((void *) &tmp
, p
, len
);
512 i
= (GFC_UINTEGER_16
) tmp
;
517 internal_error (NULL
, "bad integer kind");
525 write_l (st_parameter_dt
*dtp
, const fnode
*f
, char *source
, int len
)
529 GFC_INTEGER_LARGEST n
;
531 wlen
= (f
->format
== FMT_G
&& f
->u
.w
== 0) ? 1 : f
->u
.w
;
533 p
= write_block (dtp
, wlen
);
537 n
= extract_int (source
, len
);
539 if (unlikely (is_char4_unit (dtp
)))
541 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
542 memset4 (p4
, ' ', wlen
-1);
543 p4
[wlen
- 1] = (n
) ? 'T' : 'F';
547 memset (p
, ' ', wlen
-1);
548 p
[wlen
- 1] = (n
) ? 'T' : 'F';
553 write_boz (st_parameter_dt
*dtp
, const fnode
*f
, const char *q
, int n
)
555 int w
, m
, digits
, nzero
, nblank
;
563 if (m
== 0 && n
== 0)
568 p
= write_block (dtp
, w
);
571 if (unlikely (is_char4_unit (dtp
)))
573 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
574 memset4 (p4
, ' ', w
);
583 /* Select a width if none was specified. The idea here is to always
587 w
= ((digits
< m
) ? m
: digits
);
589 p
= write_block (dtp
, w
);
597 /* See if things will work. */
599 nblank
= w
- (nzero
+ digits
);
601 if (unlikely (is_char4_unit (dtp
)))
603 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
606 memset4 (p4
, '*', w
);
610 if (!dtp
->u
.p
.no_leading_blank
)
612 memset4 (p4
, ' ', nblank
);
614 memset4 (p4
, '0', nzero
);
616 memcpy4 (p4
, q
, digits
);
620 memset4 (p4
, '0', nzero
);
622 memcpy4 (p4
, q
, digits
);
624 memset4 (p4
, ' ', nblank
);
625 dtp
->u
.p
.no_leading_blank
= 0;
636 if (!dtp
->u
.p
.no_leading_blank
)
638 memset (p
, ' ', nblank
);
640 memset (p
, '0', nzero
);
642 memcpy (p
, q
, digits
);
646 memset (p
, '0', nzero
);
648 memcpy (p
, q
, digits
);
650 memset (p
, ' ', nblank
);
651 dtp
->u
.p
.no_leading_blank
= 0;
659 write_decimal (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
,
661 const char *(*conv
) (GFC_INTEGER_LARGEST
, char *, size_t))
663 GFC_INTEGER_LARGEST n
= 0;
664 int w
, m
, digits
, nsign
, nzero
, nblank
;
668 char itoa_buf
[GFC_BTOA_BUF_SIZE
];
671 m
= f
->format
== FMT_G
? -1 : f
->u
.integer
.m
;
673 n
= extract_int (source
, len
);
676 if (m
== 0 && n
== 0)
681 p
= write_block (dtp
, w
);
684 if (unlikely (is_char4_unit (dtp
)))
686 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
687 memset4 (p4
, ' ', w
);
694 sign
= calculate_sign (dtp
, n
< 0);
697 nsign
= sign
== S_NONE
? 0 : 1;
699 /* conv calls itoa which sets the negative sign needed
700 by write_integer. The sign '+' or '-' is set below based on sign
701 calculated above, so we just point past the sign in the string
702 before proceeding to avoid double signs in corner cases.
704 q
= conv (n
, itoa_buf
, sizeof (itoa_buf
));
710 /* Select a width if none was specified. The idea here is to always
714 w
= ((digits
< m
) ? m
: digits
) + nsign
;
716 p
= write_block (dtp
, w
);
724 /* See if things will work. */
726 nblank
= w
- (nsign
+ nzero
+ digits
);
728 if (unlikely (is_char4_unit (dtp
)))
730 gfc_char4_t
* p4
= (gfc_char4_t
*) p
;
733 memset4 (p4
, '*', w
);
737 memset4 (p4
, ' ', nblank
);
752 memset4 (p4
, '0', nzero
);
755 memcpy4 (p4
, q
, digits
);
765 memset (p
, ' ', nblank
);
780 memset (p
, '0', nzero
);
783 memcpy (p
, q
, digits
);
790 /* Convert unsigned octal to ascii. */
793 otoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
797 assert (len
>= GFC_OTOA_BUF_SIZE
);
802 p
= buffer
+ GFC_OTOA_BUF_SIZE
- 1;
807 *--p
= '0' + (n
& 7);
815 /* Convert unsigned binary to ascii. */
818 btoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
822 assert (len
>= GFC_BTOA_BUF_SIZE
);
827 p
= buffer
+ GFC_BTOA_BUF_SIZE
- 1;
832 *--p
= '0' + (n
& 1);
839 /* The following three functions, btoa_big, otoa_big, and ztoa_big, are needed
840 to convert large reals with kind sizes that exceed the largest integer type
841 available on certain platforms. In these cases, byte by byte conversion is
842 performed. Endianess is taken into account. */
844 /* Conversion to binary. */
847 btoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
856 for (i
= 0; i
< len
; i
++)
860 /* Test for zero. Needed by write_boz later. */
864 for (j
= 0; j
< 8; j
++)
866 *q
++ = (c
& 128) ? '1' : '0';
874 const char *p
= s
+ len
- 1;
875 for (i
= 0; i
< len
; i
++)
879 /* Test for zero. Needed by write_boz later. */
883 for (j
= 0; j
< 8; j
++)
885 *q
++ = (c
& 128) ? '1' : '0';
897 /* Move past any leading zeros. */
898 while (*buffer
== '0')
905 /* Conversion to octal. */
908 otoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
914 q
= buffer
+ GFC_OTOA_BUF_SIZE
- 1;
920 const char *p
= s
+ len
- 1;
924 /* Test for zero. Needed by write_boz later. */
928 for (j
= 0; j
< 3 && i
< len
; j
++)
930 octet
|= (c
& 1) << j
;
949 /* Test for zero. Needed by write_boz later. */
953 for (j
= 0; j
< 3 && i
< len
; j
++)
955 octet
|= (c
& 1) << j
;
972 /* Move past any leading zeros. */
979 /* Conversion to hexidecimal. */
982 ztoa_big (const char *s
, char *buffer
, int len
, GFC_UINTEGER_LARGEST
*n
)
984 static char a
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
985 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
996 for (i
= 0; i
< len
; i
++)
998 /* Test for zero. Needed by write_boz later. */
1002 h
= (*p
>> 4) & 0x0F;
1010 const char *p
= s
+ len
- 1;
1011 for (i
= 0; i
< len
; i
++)
1013 /* Test for zero. Needed by write_boz later. */
1017 h
= (*p
>> 4) & 0x0F;
1029 /* Move past any leading zeros. */
1030 while (*buffer
== '0')
1036 /* gfc_itoa()-- Integer to decimal conversion.
1037 The itoa function is a widespread non-standard extension to standard
1038 C, often declared in <stdlib.h>. Even though the itoa defined here
1039 is a static function we take care not to conflict with any prior
1040 non-static declaration. Hence the 'gfc_' prefix, which is normally
1041 reserved for functions with external linkage. */
1044 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
1048 GFC_UINTEGER_LARGEST t
;
1050 assert (len
>= GFC_ITOA_BUF_SIZE
);
1060 t
= -n
; /*must use unsigned to protect from overflow*/
1063 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
1068 *--p
= '0' + (t
% 10);
1079 write_i (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1081 write_decimal (dtp
, f
, p
, len
, (void *) gfc_itoa
);
1086 write_b (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1089 char itoa_buf
[GFC_BTOA_BUF_SIZE
];
1090 GFC_UINTEGER_LARGEST n
= 0;
1092 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1094 p
= btoa_big (source
, itoa_buf
, len
, &n
);
1095 write_boz (dtp
, f
, p
, n
);
1099 n
= extract_uint (source
, len
);
1100 p
= btoa (n
, itoa_buf
, sizeof (itoa_buf
));
1101 write_boz (dtp
, f
, p
, n
);
1107 write_o (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1110 char itoa_buf
[GFC_OTOA_BUF_SIZE
];
1111 GFC_UINTEGER_LARGEST n
= 0;
1113 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1115 p
= otoa_big (source
, itoa_buf
, len
, &n
);
1116 write_boz (dtp
, f
, p
, n
);
1120 n
= extract_uint (source
, len
);
1121 p
= otoa (n
, itoa_buf
, sizeof (itoa_buf
));
1122 write_boz (dtp
, f
, p
, n
);
1127 write_z (st_parameter_dt
*dtp
, const fnode
*f
, const char *source
, int len
)
1130 char itoa_buf
[GFC_XTOA_BUF_SIZE
];
1131 GFC_UINTEGER_LARGEST n
= 0;
1133 if (len
> (int) sizeof (GFC_UINTEGER_LARGEST
))
1135 p
= ztoa_big (source
, itoa_buf
, len
, &n
);
1136 write_boz (dtp
, f
, p
, n
);
1140 n
= extract_uint (source
, len
);
1141 p
= gfc_xtoa (n
, itoa_buf
, sizeof (itoa_buf
));
1142 write_boz (dtp
, f
, p
, n
);
1148 write_d (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1150 write_float (dtp
, f
, p
, len
, 0);
1155 write_e (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1157 write_float (dtp
, f
, p
, len
, 0);
1162 write_f (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1164 write_float (dtp
, f
, p
, len
, 0);
1169 write_en (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1171 write_float (dtp
, f
, p
, len
, 0);
1176 write_es (st_parameter_dt
*dtp
, const fnode
*f
, const char *p
, int len
)
1178 write_float (dtp
, f
, p
, len
, 0);
1182 /* Take care of the X/TR descriptor. */
1185 write_x (st_parameter_dt
*dtp
, int len
, int nspaces
)
1189 p
= write_block (dtp
, len
);
1192 if (nspaces
> 0 && len
- nspaces
>= 0)
1194 if (unlikely (is_char4_unit (dtp
)))
1196 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1197 memset4 (&p4
[len
- nspaces
], ' ', nspaces
);
1200 memset (&p
[len
- nspaces
], ' ', nspaces
);
1205 /* List-directed writing. */
1208 /* Write a single character to the output. Returns nonzero if
1209 something goes wrong. */
1212 write_char (st_parameter_dt
*dtp
, int c
)
1216 p
= write_block (dtp
, 1);
1219 if (unlikely (is_char4_unit (dtp
)))
1221 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1232 /* Write a list-directed logical value. */
1235 write_logical (st_parameter_dt
*dtp
, const char *source
, int length
)
1237 write_char (dtp
, extract_int (source
, length
) ? 'T' : 'F');
1241 /* Write a list-directed integer value. */
1244 write_integer (st_parameter_dt
*dtp
, const char *source
, int length
)
1250 char itoa_buf
[GFC_ITOA_BUF_SIZE
];
1252 q
= gfc_itoa (extract_int (source
, length
), itoa_buf
, sizeof (itoa_buf
));
1277 digits
= strlen (q
);
1281 p
= write_block (dtp
, width
);
1285 if (unlikely (is_char4_unit (dtp
)))
1287 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1288 if (dtp
->u
.p
.no_leading_blank
)
1290 memcpy4 (p4
, q
, digits
);
1291 memset4 (p4
+ digits
, ' ', width
- digits
);
1295 memset4 (p4
, ' ', width
- digits
);
1296 memcpy4 (p4
+ width
- digits
, q
, digits
);
1301 if (dtp
->u
.p
.no_leading_blank
)
1303 memcpy (p
, q
, digits
);
1304 memset (p
+ digits
, ' ', width
- digits
);
1308 memset (p
, ' ', width
- digits
);
1309 memcpy (p
+ width
- digits
, q
, digits
);
1314 /* Write a list-directed string. We have to worry about delimiting
1315 the strings if the file has been opened in that mode. */
1318 write_character (st_parameter_dt
*dtp
, const char *source
, int kind
, int length
)
1323 switch (dtp
->u
.p
.current_unit
->delim_status
)
1325 case DELIM_APOSTROPHE
:
1344 for (i
= 0; i
< length
; i
++)
1349 p
= write_block (dtp
, length
+ extra
);
1353 if (unlikely (is_char4_unit (dtp
)))
1355 gfc_char4_t d4
= (gfc_char4_t
) d
;
1356 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1359 memcpy4 (p4
, source
, length
);
1364 for (i
= 0; i
< length
; i
++)
1366 *p4
++ = (gfc_char4_t
) source
[i
];
1377 memcpy (p
, source
, length
);
1382 for (i
= 0; i
< length
; i
++)
1396 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
1397 write_utf8_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1399 write_default_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1403 p
= write_block (dtp
, 1);
1406 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
1407 write_utf8_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1409 write_default_char4 (dtp
, (gfc_char4_t
*) source
, length
, 0);
1411 p
= write_block (dtp
, 1);
1418 /* Set an fnode to default format. */
1421 set_fnode_default (st_parameter_dt
*dtp
, fnode
*f
, int length
)
1447 internal_error (&dtp
->common
, "bad real kind");
1452 /* Output a real number with default format. To guarantee that a
1453 binary -> decimal -> binary roundtrip conversion recovers the
1454 original value, IEEE 754-2008 requires 9, 17, 21 and 36 significant
1455 digits for REAL kinds 4, 8, 10, and 16, respectively. Thus, we use
1456 1PG16.9E2 for REAL(4), 1PG25.17E3 for REAL(8), 1PG30.21E4 for
1457 REAL(10) and 1PG45.36E4 for REAL(16). The exception is that the
1458 Fortran standard requires outputting an extra digit when the scale
1459 factor is 1 and when the magnitude of the value is such that E
1460 editing is used. However, gfortran compensates for this, and thus
1461 for list formatted the same number of significant digits is
1462 generated both when using F and E editing. */
1465 write_real (st_parameter_dt
*dtp
, const char *source
, int length
)
1468 int org_scale
= dtp
->u
.p
.scale_factor
;
1469 dtp
->u
.p
.scale_factor
= 1;
1470 set_fnode_default (dtp
, &f
, length
);
1471 write_float (dtp
, &f
, source
, length
, 1);
1472 dtp
->u
.p
.scale_factor
= org_scale
;
1475 /* Similar to list formatted REAL output, for kPG0 where k > 0 we
1476 compensate for the extra digit. */
1479 write_real_g0 (st_parameter_dt
*dtp
, const char *source
, int length
, int d
)
1483 set_fnode_default (dtp
, &f
, length
);
1487 /* Compensate for extra digits when using scale factor, d is not
1488 specified, and the magnitude is such that E editing is used. */
1489 if (dtp
->u
.p
.scale_factor
> 0 && d
== 0)
1493 dtp
->u
.p
.g0_no_blanks
= 1;
1494 write_float (dtp
, &f
, source
, length
, comp_d
);
1495 dtp
->u
.p
.g0_no_blanks
= 0;
1500 write_complex (st_parameter_dt
*dtp
, const char *source
, int kind
, size_t size
)
1503 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';';
1505 if (write_char (dtp
, '('))
1507 write_real (dtp
, source
, kind
);
1509 if (write_char (dtp
, semi_comma
))
1511 write_real (dtp
, source
+ size
/ 2, kind
);
1513 write_char (dtp
, ')');
1517 /* Write the separator between items. */
1520 write_separator (st_parameter_dt
*dtp
)
1524 p
= write_block (dtp
, options
.separator_len
);
1527 if (unlikely (is_char4_unit (dtp
)))
1529 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1530 memcpy4 (p4
, options
.separator
, options
.separator_len
);
1533 memcpy (p
, options
.separator
, options
.separator_len
);
1537 /* Write an item with list formatting.
1538 TODO: handle skipping to the next record correctly, particularly
1542 list_formatted_write_scalar (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1545 if (dtp
->u
.p
.current_unit
== NULL
)
1548 if (dtp
->u
.p
.first_item
)
1550 dtp
->u
.p
.first_item
= 0;
1551 write_char (dtp
, ' ');
1555 if (type
!= BT_CHARACTER
|| !dtp
->u
.p
.char_flag
||
1556 dtp
->u
.p
.current_unit
->delim_status
!= DELIM_NONE
)
1557 write_separator (dtp
);
1563 write_integer (dtp
, p
, kind
);
1566 write_logical (dtp
, p
, kind
);
1569 write_character (dtp
, p
, kind
, size
);
1572 write_real (dtp
, p
, kind
);
1575 write_complex (dtp
, p
, kind
, size
);
1578 internal_error (&dtp
->common
, "list_formatted_write(): Bad type");
1581 dtp
->u
.p
.char_flag
= (type
== BT_CHARACTER
);
1586 list_formatted_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1587 size_t size
, size_t nelems
)
1591 size_t stride
= type
== BT_CHARACTER
?
1592 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1596 /* Big loop over all the elements. */
1597 for (elem
= 0; elem
< nelems
; elem
++)
1599 dtp
->u
.p
.item_count
++;
1600 list_formatted_write_scalar (dtp
, type
, tmp
+ elem
* stride
, kind
, size
);
1606 nml_write_obj writes a namelist object to the output stream. It is called
1607 recursively for derived type components:
1608 obj = is the namelist_info for the current object.
1609 offset = the offset relative to the address held by the object for
1610 derived type arrays.
1611 base = is the namelist_info of the derived type, when obj is a
1613 base_name = the full name for a derived type, including qualifiers
1615 The returned value is a pointer to the object beyond the last one
1616 accessed, including nested derived types. Notice that the namelist is
1617 a linear linked list of objects, including derived types and their
1618 components. A tree, of sorts, is implied by the compound names of
1619 the derived type components and this is how this function recurses through
1622 /* A generous estimate of the number of characters needed to print
1623 repeat counts and indices, including commas, asterices and brackets. */
1625 #define NML_DIGITS 20
1628 namelist_write_newline (st_parameter_dt
*dtp
)
1630 if (!is_internal_unit (dtp
))
1633 write_character (dtp
, "\r\n", 1, 2);
1635 write_character (dtp
, "\n", 1, 1);
1640 if (is_array_io (dtp
))
1645 int length
= dtp
->u
.p
.current_unit
->bytes_left
;
1647 p
= write_block (dtp
, length
);
1651 if (unlikely (is_char4_unit (dtp
)))
1653 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
1654 memset4 (p4
, ' ', length
);
1657 memset (p
, ' ', length
);
1659 /* Now that the current record has been padded out,
1660 determine where the next record in the array is. */
1661 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
1664 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
1667 /* Now seek to this record */
1668 record
= record
* dtp
->u
.p
.current_unit
->recl
;
1670 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
1672 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
1676 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
1680 write_character (dtp
, " ", 1, 1);
1684 static namelist_info
*
1685 nml_write_obj (st_parameter_dt
*dtp
, namelist_info
* obj
, index_type offset
,
1686 namelist_info
* base
, char * base_name
)
1692 index_type obj_size
;
1696 index_type elem_ctr
;
1697 size_t obj_name_len
;
1702 size_t ext_name_len
;
1703 char rep_buff
[NML_DIGITS
];
1704 namelist_info
* cmp
;
1705 namelist_info
* retval
= obj
->next
;
1706 size_t base_name_len
;
1707 size_t base_var_name_len
;
1709 unit_delim tmp_delim
;
1711 /* Set the character to be used to separate values
1712 to a comma or semi-colon. */
1715 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';';
1717 /* Write namelist variable names in upper case. If a derived type,
1718 nothing is output. If a component, base and base_name are set. */
1720 if (obj
->type
!= BT_DERIVED
)
1722 namelist_write_newline (dtp
);
1723 write_character (dtp
, " ", 1, 1);
1728 len
= strlen (base
->var_name
);
1729 base_name_len
= strlen (base_name
);
1730 for (dim_i
= 0; dim_i
< base_name_len
; dim_i
++)
1732 cup
= toupper ((int) base_name
[dim_i
]);
1733 write_character (dtp
, &cup
, 1, 1);
1736 clen
= strlen (obj
->var_name
);
1737 for (dim_i
= len
; dim_i
< clen
; dim_i
++)
1739 cup
= toupper ((int) obj
->var_name
[dim_i
]);
1740 write_character (dtp
, &cup
, 1, 1);
1742 write_character (dtp
, "=", 1, 1);
1745 /* Counts the number of data output on a line, including names. */
1755 obj_size
= size_from_real_kind (len
);
1759 obj_size
= size_from_complex_kind (len
);
1763 obj_size
= obj
->string_length
;
1771 obj_size
= obj
->size
;
1773 /* Set the index vector and count the number of elements. */
1776 for (dim_i
= 0; dim_i
< (size_t) obj
->var_rank
; dim_i
++)
1778 obj
->ls
[dim_i
].idx
= GFC_DESCRIPTOR_LBOUND(obj
, dim_i
);
1779 nelem
= nelem
* GFC_DESCRIPTOR_EXTENT (obj
, dim_i
);
1782 /* Main loop to output the data held in the object. */
1785 for (elem_ctr
= 0; elem_ctr
< nelem
; elem_ctr
++)
1788 /* Build the pointer to the data value. The offset is passed by
1789 recursive calls to this function for arrays of derived types.
1790 Is NULL otherwise. */
1792 p
= (void *)(obj
->mem_pos
+ elem_ctr
* obj_size
);
1795 /* Check for repeat counts of intrinsic types. */
1797 if ((elem_ctr
< (nelem
- 1)) &&
1798 (obj
->type
!= BT_DERIVED
) &&
1799 !memcmp (p
, (void*)(p
+ obj_size
), obj_size
))
1804 /* Execute a repeated output. Note the flag no_leading_blank that
1805 is used in the functions used to output the intrinsic types. */
1811 snprintf(rep_buff
, NML_DIGITS
, " %d*", rep_ctr
);
1812 write_character (dtp
, rep_buff
, 1, strlen (rep_buff
));
1813 dtp
->u
.p
.no_leading_blank
= 1;
1817 /* Output the data, if an intrinsic type, or recurse into this
1818 routine to treat derived types. */
1824 write_integer (dtp
, p
, len
);
1828 write_logical (dtp
, p
, len
);
1832 tmp_delim
= dtp
->u
.p
.current_unit
->delim_status
;
1833 if (dtp
->u
.p
.nml_delim
== '"')
1834 dtp
->u
.p
.current_unit
->delim_status
= DELIM_QUOTE
;
1835 if (dtp
->u
.p
.nml_delim
== '\'')
1836 dtp
->u
.p
.current_unit
->delim_status
= DELIM_APOSTROPHE
;
1837 write_character (dtp
, p
, 1, obj
->string_length
);
1838 dtp
->u
.p
.current_unit
->delim_status
= tmp_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*)get_mem (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
= get_mem (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 write_character (dtp
, &semi_comma
, 1, 1);
1930 namelist_write_newline (dtp
);
1931 write_character (dtp
, " ", 1, 1);
1936 /* Cycle through and increment the index vector. */
1941 for (dim_i
= 0; nml_carry
&& (dim_i
< (size_t) obj
->var_rank
); dim_i
++)
1943 obj
->ls
[dim_i
].idx
+= nml_carry
;
1945 if (obj
->ls
[dim_i
].idx
> GFC_DESCRIPTOR_UBOUND(obj
,dim_i
))
1947 obj
->ls
[dim_i
].idx
= GFC_DESCRIPTOR_LBOUND(obj
,dim_i
);
1953 /* Return a pointer beyond the furthest object accessed. */
1959 /* This is the entry function for namelist writes. It outputs the name
1960 of the namelist and iterates through the namelist by calls to
1961 nml_write_obj. The call below has dummys in the arguments used in
1962 the treatment of derived types. */
1965 namelist_write (st_parameter_dt
*dtp
)
1967 namelist_info
* t1
, *t2
, *dummy
= NULL
;
1969 index_type dummy_offset
= 0;
1971 char * dummy_name
= NULL
;
1972 unit_delim tmp_delim
= DELIM_UNSPECIFIED
;
1974 /* Set the delimiter for namelist output. */
1975 tmp_delim
= dtp
->u
.p
.current_unit
->delim_status
;
1977 dtp
->u
.p
.nml_delim
= tmp_delim
== DELIM_APOSTROPHE
? '\'' : '"';
1979 /* Temporarily disable namelist delimters. */
1980 dtp
->u
.p
.current_unit
->delim_status
= DELIM_NONE
;
1982 write_character (dtp
, "&", 1, 1);
1984 /* Write namelist name in upper case - f95 std. */
1985 for (i
= 0 ;i
< dtp
->namelist_name_len
;i
++ )
1987 c
= toupper ((int) dtp
->namelist_name
[i
]);
1988 write_character (dtp
, &c
, 1 ,1);
1991 if (dtp
->u
.p
.ionml
!= NULL
)
1993 t1
= dtp
->u
.p
.ionml
;
1997 t1
= nml_write_obj (dtp
, t2
, dummy_offset
, dummy
, dummy_name
);
2001 namelist_write_newline (dtp
);
2002 write_character (dtp
, " /", 1, 2);
2003 /* Restore the original delimiter. */
2004 dtp
->u
.p
.current_unit
->delim_status
= tmp_delim
;