1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist output contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
37 #include "libgfortran.h"
40 #define star_fill(p, n) memset(p, '*', n)
44 { SIGN_NONE, SIGN_MINUS, SIGN_PLUS }
49 write_a (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
54 wlen = f->u.string.length < 0 ? len : f->u.string.length;
57 /* If this is formatted STREAM IO convert any embedded line feed characters
58 to CR_LF on systems that use that sequence for newlines. See F2003
59 Standard sections 10.6.3 and 9.9 for further information. */
60 if (is_stream_io (dtp))
62 const char crlf[] = "\r\n";
66 /* Write out any padding if needed. */
69 p = write_block (dtp, wlen - len);
72 memset (p, ' ', wlen - len);
75 /* Scan the source string looking for '\n' and convert it if found. */
76 for (i = 0; i < wlen; i++)
78 if (source[i] == '\n')
80 /* Write out the previously scanned characters in the string. */
83 p = write_block (dtp, bytes);
86 memcpy (p, &source[q], bytes);
91 /* Write out the CR_LF sequence. */
93 p = write_block (dtp, 2);
102 /* Write out any remaining bytes if no LF was found. */
105 p = write_block (dtp, bytes);
108 memcpy (p, &source[q], bytes);
114 p = write_block (dtp, wlen);
119 memcpy (p, source, wlen);
122 memset (p, ' ', wlen - len);
123 memcpy (p + wlen - len, source, len);
130 static GFC_INTEGER_LARGEST
131 extract_int (const void *p, int len)
133 GFC_INTEGER_LARGEST i = 0;
143 memcpy ((void *) &tmp, p, len);
150 memcpy ((void *) &tmp, p, len);
157 memcpy ((void *) &tmp, p, len);
164 memcpy ((void *) &tmp, p, len);
168 #ifdef HAVE_GFC_INTEGER_16
172 memcpy ((void *) &tmp, p, len);
178 internal_error (NULL, "bad integer kind");
184 static GFC_UINTEGER_LARGEST
185 extract_uint (const void *p, int len)
187 GFC_UINTEGER_LARGEST i = 0;
197 memcpy ((void *) &tmp, p, len);
198 i = (GFC_UINTEGER_1) tmp;
204 memcpy ((void *) &tmp, p, len);
205 i = (GFC_UINTEGER_2) tmp;
211 memcpy ((void *) &tmp, p, len);
212 i = (GFC_UINTEGER_4) tmp;
218 memcpy ((void *) &tmp, p, len);
219 i = (GFC_UINTEGER_8) tmp;
222 #ifdef HAVE_GFC_INTEGER_16
226 memcpy ((void *) &tmp, p, len);
227 i = (GFC_UINTEGER_16) tmp;
232 internal_error (NULL, "bad integer kind");
238 static GFC_REAL_LARGEST
239 extract_real (const void *p, int len)
241 GFC_REAL_LARGEST i = 0;
247 memcpy ((void *) &tmp, p, len);
254 memcpy ((void *) &tmp, p, len);
258 #ifdef HAVE_GFC_REAL_10
262 memcpy ((void *) &tmp, p, len);
267 #ifdef HAVE_GFC_REAL_16
271 memcpy ((void *) &tmp, p, len);
277 internal_error (NULL, "bad real kind");
283 /* Given a flag that indicate if a value is negative or not, return a
284 sign_t that gives the sign that we need to produce. */
287 calculate_sign (st_parameter_dt *dtp, int negative_flag)
289 sign_t s = SIGN_NONE;
294 switch (dtp->u.p.sign_status)
303 s = options.optional_plus ? SIGN_PLUS : SIGN_NONE;
311 /* Returns the value of 10**d. */
313 static GFC_REAL_LARGEST
314 calculate_exp (int d)
317 GFC_REAL_LARGEST r = 1.0;
319 for (i = 0; i< (d >= 0 ? d : -d); i++)
322 r = (d >= 0) ? r : 1.0 / r;
328 /* Generate corresponding I/O format for FMT_G output.
329 The rules to translate FMT_G to FMT_E or FMT_F from DEC fortran
330 LRM (table 11-2, Chapter 11, "I/O Formatting", P11-25) is:
332 Data Magnitude Equivalent Conversion
333 0< m < 0.1-0.5*10**(-d-1) Ew.d[Ee]
334 m = 0 F(w-n).(d-1), n' '
335 0.1-0.5*10**(-d-1)<= m < 1-0.5*10**(-d) F(w-n).d, n' '
336 1-0.5*10**(-d)<= m < 10-0.5*10**(-d+1) F(w-n).(d-1), n' '
337 10-0.5*10**(-d+1)<= m < 100-0.5*10**(-d+2) F(w-n).(d-2), n' '
338 ................ ..........
339 10**(d-1)-0.5*10**(-1)<= m <10**d-0.5 F(w-n).0,n(' ')
340 m >= 10**d-0.5 Ew.d[Ee]
342 notes: for Gw.d , n' ' means 4 blanks
343 for Gw.dEe, n' ' means e+2 blanks */
346 calculate_G_format (st_parameter_dt *dtp, const fnode *f,
347 GFC_REAL_LARGEST value, int *num_blank)
353 GFC_REAL_LARGEST m, exp_d;
357 newf = get_mem (sizeof (fnode));
359 /* Absolute value. */
360 m = (value > 0.0) ? value : -value;
362 /* In case of the two data magnitude ranges,
363 generate E editing, Ew.d[Ee]. */
364 exp_d = calculate_exp (d);
365 if ((m > 0.0 && m < 0.1 - 0.05 / exp_d) || (m >= exp_d - 0.5 ) ||
366 ((m == 0.0) && !(compile_options.allow_std & GFC_STD_F2003)))
368 newf->format = FMT_E;
376 /* Use binary search to find the data magnitude range. */
385 GFC_REAL_LARGEST temp;
386 mid = (low + high) / 2;
388 /* 0.1 * 10**mid - 0.5 * 10**(mid-d-1) */
389 temp = 0.1 * calculate_exp (mid) - 0.5 * calculate_exp (mid - d - 1);
394 if (ubound == lbound + 1)
401 if (ubound == lbound + 1)
412 /* Pad with blanks where the exponent would be. */
418 /* Generate the F editing. F(w-n).(-(mid-d-1)), n' '. */
419 newf->format = FMT_F;
420 newf->u.real.w = f->u.real.w - *num_blank;
424 newf->u.real.d = d - 1;
426 newf->u.real.d = - (mid - d - 1);
428 /* For F editing, the scale factor is ignored. */
429 dtp->u.p.scale_factor = 0;
434 /* Output a real number according to its format which is FMT_G free. */
437 output_float (st_parameter_dt *dtp, const fnode *f, GFC_REAL_LARGEST value)
439 #if defined(HAVE_GFC_REAL_16) && __LDBL_DIG__ > 18
440 # define MIN_FIELD_WIDTH 46
442 # define MIN_FIELD_WIDTH 31
444 #define STR(x) STR1(x)
446 /* This must be large enough to accurately hold any value. */
447 char buffer[MIN_FIELD_WIDTH+1];
457 /* Number of digits before the decimal point. */
459 /* Number of zeros after the decimal point. */
461 /* Number of digits after the decimal point. */
463 /* Number of zeros after the decimal point, whatever the precision. */
478 /* We should always know the field width and precision. */
480 internal_error (&dtp->common, "Unspecified precision");
482 /* Use sprintf to print the number in the format +D.DDDDe+ddd
483 For an N digit exponent, this gives us (MIN_FIELD_WIDTH-5)-N digits
484 after the decimal point, plus another one before the decimal point. */
485 sign = calculate_sign (dtp, value < 0.0);
489 /* Special case when format specifies no digits after the decimal point. */
490 if (d == 0 && ft == FMT_F)
494 else if (value < 1.0)
498 /* Printf always prints at least two exponent digits. */
503 #if defined(HAVE_GFC_REAL_10) || defined(HAVE_GFC_REAL_16)
504 abslog = fabs((double) log10l(value));
506 abslog = fabs(log10(value));
511 edigits = 1 + (int) log10(abslog);
514 if (ft == FMT_F || ft == FMT_EN
515 || ((ft == FMT_D || ft == FMT_E) && dtp->u.p.scale_factor != 0))
517 /* Always convert at full precision to avoid double rounding. */
518 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
522 /* We know the number of digits, so can let printf do the rounding
528 if (ndigits > MIN_FIELD_WIDTH - 4 - edigits)
529 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
532 /* # The result will always contain a decimal point, even if no
535 * - The converted value is to be left adjusted on the field boundary
537 * + A sign (+ or -) always be placed before a number
539 * MIN_FIELD_WIDTH minimum field width
541 * * (ndigits-1) is used as the precision
543 * e format: [-]d.ddde±dd where there is one digit before the
544 * decimal-point character and the number of digits after it is
545 * equal to the precision. The exponent always contains at least two
546 * digits; if the value is zero, the exponent is 00.
549 snprintf (buffer, sizeof (buffer), "%+-#" STR(MIN_FIELD_WIDTH) ".*"
550 GFC_REAL_LARGEST_FORMAT "e", ndigits - 1, value);
552 sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*"
553 GFC_REAL_LARGEST_FORMAT "e", ndigits - 1, value);
556 /* Check the resulting string has punctuation in the correct places. */
557 if (d != 0 && (buffer[2] != '.' || buffer[ndigits + 2] != 'e'))
558 internal_error (&dtp->common, "printf is broken");
560 /* Read the exponent back in. */
561 e = atoi (&buffer[ndigits + 3]) + 1;
563 /* Make sure zero comes out as 0.0e0. */
567 /* Normalize the fractional component. */
568 buffer[2] = buffer[1];
571 /* Figure out where to place the decimal point. */
575 nbefore = e + dtp->u.p.scale_factor;
595 i = dtp->u.p.scale_factor;
608 nafter = (d - i) + 1;
624 /* The exponent must be a multiple of three, with 1-3 digits before
625 the decimal point. */
634 nbefore = 3 - nbefore;
653 /* Should never happen. */
654 internal_error (&dtp->common, "Unexpected format token");
657 /* Round the value. */
658 if (nbefore + nafter == 0)
661 if (nzero_real == d && digits[0] >= '5')
663 /* We rounded to zero but shouldn't have */
670 else if (nbefore + nafter < ndigits)
672 ndigits = nbefore + nafter;
674 if (digits[i] >= '5')
676 /* Propagate the carry. */
677 for (i--; i >= 0; i--)
679 if (digits[i] != '9')
689 /* The carry overflowed. Fortunately we have some spare space
690 at the start of the buffer. We may discard some digits, but
691 this is ok because we already know they are zero. */
704 else if (ft == FMT_EN)
719 /* Calculate the format of the exponent field. */
723 for (i = abs (e); i >= 10; i /= 10)
728 /* Width not specified. Must be no more than 3 digits. */
729 if (e > 999 || e < -999)
734 if (e > 99 || e < -99)
740 /* Exponent width specified, check it is wide enough. */
741 if (edigits > f->u.real.e)
744 edigits = f->u.real.e + 2;
750 /* Pick a field size if none was specified. */
752 w = nbefore + nzero + nafter + (sign != SIGN_NONE ? 2 : 1);
754 /* Create the ouput buffer. */
755 out = write_block (dtp, w);
759 /* Zero values always output as positive, even if the value was negative
761 for (i = 0; i < ndigits; i++)
763 if (digits[i] != '0')
767 sign = calculate_sign (dtp, 0);
769 /* Work out how much padding is needed. */
770 nblanks = w - (nbefore + nzero + nafter + edigits + 1);
771 if (sign != SIGN_NONE)
774 /* Check the value fits in the specified field width. */
775 if (nblanks < 0 || edigits == -1)
781 /* See if we have space for a zero before the decimal point. */
782 if (nbefore == 0 && nblanks > 0)
790 /* Pad to full field width. */
793 if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
795 memset (out, ' ', nblanks);
799 /* Output the initial sign (if any). */
800 if (sign == SIGN_PLUS)
802 else if (sign == SIGN_MINUS)
805 /* Output an optional leading zero. */
809 /* Output the part before the decimal point, padding with zeros. */
812 if (nbefore > ndigits)
817 memcpy (out, digits, i);
825 /* Output the decimal point. */
828 /* Output leading zeros after the decimal point. */
831 for (i = 0; i < nzero; i++)
835 /* Output digits after the decimal point, padding with zeros. */
838 if (nafter > ndigits)
843 memcpy (out, digits, i);
852 /* Output the exponent. */
861 snprintf (buffer, sizeof (buffer), "%+0*d", edigits, e);
863 sprintf (buffer, "%+0*d", edigits, e);
865 memcpy (out, buffer, edigits);
868 if (dtp->u.p.no_leading_blank)
871 memset( out , ' ' , nblanks );
872 dtp->u.p.no_leading_blank = 0;
876 #undef MIN_FIELD_WIDTH
881 write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
884 GFC_INTEGER_LARGEST n;
886 p = write_block (dtp, f->u.w);
890 memset (p, ' ', f->u.w - 1);
891 n = extract_int (source, len);
892 p[f->u.w - 1] = (n) ? 'T' : 'F';
895 /* Output a real number according to its format. */
898 write_float (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
901 int nb =0, res, save_scale_factor;
905 n = extract_real (source, len);
907 if (f->format != FMT_B && f->format != FMT_O && f->format != FMT_Z)
914 /* If the field width is zero, the processor must select a width
915 not zero. 4 is chosen to allow output of '-Inf' or '+Inf' */
918 p = write_block (dtp, nb);
934 /* If the sign is negative and the width is 3, there is
935 insufficient room to output '-Inf', so output asterisks */
943 /* The negative sign is mandatory */
949 /* The positive sign is optional, but we output it for
956 /* We have room, so output 'Infinity' */
958 memcpy(p + nb - 8, "Infinity", 8);
961 /* For the case of width equals 8, there is not enough room
962 for the sign and 'Infinity' so we go with 'Inf' */
964 memcpy(p + nb - 3, "Inf", 3);
965 if (nb < 9 && nb > 3)
966 p[nb - 4] = fin; /* Put the sign in front of Inf */
968 p[nb - 9] = fin; /* Put the sign in front of Infinity */
971 memcpy(p + nb - 3, "NaN", 3);
976 if (f->format != FMT_G)
977 output_float (dtp, f, n);
980 save_scale_factor = dtp->u.p.scale_factor;
981 f2 = calculate_G_format (dtp, f, n, &nb);
982 output_float (dtp, f2, n);
983 dtp->u.p.scale_factor = save_scale_factor;
989 p = write_block (dtp, nb);
999 write_int (st_parameter_dt *dtp, const fnode *f, const char *source, int len,
1000 const char *(*conv) (GFC_UINTEGER_LARGEST, char *, size_t))
1002 GFC_UINTEGER_LARGEST n = 0;
1003 int w, m, digits, nzero, nblank;
1006 char itoa_buf[GFC_BTOA_BUF_SIZE];
1011 n = extract_uint (source, len);
1015 if (m == 0 && n == 0)
1020 p = write_block (dtp, w);
1028 q = conv (n, itoa_buf, sizeof (itoa_buf));
1029 digits = strlen (q);
1031 /* Select a width if none was specified. The idea here is to always
1035 w = ((digits < m) ? m : digits);
1037 p = write_block (dtp, w);
1045 /* See if things will work. */
1047 nblank = w - (nzero + digits);
1056 if (!dtp->u.p.no_leading_blank)
1058 memset (p, ' ', nblank);
1060 memset (p, '0', nzero);
1062 memcpy (p, q, digits);
1066 memset (p, '0', nzero);
1068 memcpy (p, q, digits);
1070 memset (p, ' ', nblank);
1071 dtp->u.p.no_leading_blank = 0;
1079 write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
1081 const char *(*conv) (GFC_INTEGER_LARGEST, char *, size_t))
1083 GFC_INTEGER_LARGEST n = 0;
1084 int w, m, digits, nsign, nzero, nblank;
1088 char itoa_buf[GFC_BTOA_BUF_SIZE];
1093 n = extract_int (source, len);
1097 if (m == 0 && n == 0)
1102 p = write_block (dtp, w);
1110 sign = calculate_sign (dtp, n < 0);
1114 nsign = sign == SIGN_NONE ? 0 : 1;
1115 q = conv (n, itoa_buf, sizeof (itoa_buf));
1117 digits = strlen (q);
1119 /* Select a width if none was specified. The idea here is to always
1123 w = ((digits < m) ? m : digits) + nsign;
1125 p = write_block (dtp, w);
1133 /* See if things will work. */
1135 nblank = w - (nsign + nzero + digits);
1143 memset (p, ' ', nblank);
1158 memset (p, '0', nzero);
1161 memcpy (p, q, digits);
1168 /* Convert unsigned octal to ascii. */
1171 otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
1175 assert (len >= GFC_OTOA_BUF_SIZE);
1180 p = buffer + GFC_OTOA_BUF_SIZE - 1;
1185 *--p = '0' + (n & 7);
1193 /* Convert unsigned binary to ascii. */
1196 btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
1200 assert (len >= GFC_BTOA_BUF_SIZE);
1205 p = buffer + GFC_BTOA_BUF_SIZE - 1;
1210 *--p = '0' + (n & 1);
1219 write_i (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1221 write_decimal (dtp, f, p, len, (void *) gfc_itoa);
1226 write_b (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1228 write_int (dtp, f, p, len, btoa);
1233 write_o (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1235 write_int (dtp, f, p, len, otoa);
1239 write_z (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1241 write_int (dtp, f, p, len, xtoa);
1246 write_d (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1248 write_float (dtp, f, p, len);
1253 write_e (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1255 write_float (dtp, f, p, len);
1260 write_f (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1262 write_float (dtp, f, p, len);
1267 write_en (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1269 write_float (dtp, f, p, len);
1274 write_es (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
1276 write_float (dtp, f, p, len);
1280 /* Take care of the X/TR descriptor. */
1283 write_x (st_parameter_dt *dtp, int len, int nspaces)
1287 p = write_block (dtp, len);
1292 memset (&p[len - nspaces], ' ', nspaces);
1296 /* List-directed writing. */
1299 /* Write a single character to the output. Returns nonzero if
1300 something goes wrong. */
1303 write_char (st_parameter_dt *dtp, char c)
1307 p = write_block (dtp, 1);
1317 /* Write a list-directed logical value. */
1320 write_logical (st_parameter_dt *dtp, const char *source, int length)
1322 write_char (dtp, extract_int (source, length) ? 'T' : 'F');
1326 /* Write a list-directed integer value. */
1329 write_integer (st_parameter_dt *dtp, const char *source, int length)
1335 char itoa_buf[GFC_ITOA_BUF_SIZE];
1337 q = gfc_itoa (extract_int (source, length), itoa_buf, sizeof (itoa_buf));
1362 digits = strlen (q);
1366 p = write_block (dtp, width);
1369 if (dtp->u.p.no_leading_blank)
1371 memcpy (p, q, digits);
1372 memset (p + digits, ' ', width - digits);
1376 memset (p, ' ', width - digits);
1377 memcpy (p + width - digits, q, digits);
1382 /* Write a list-directed string. We have to worry about delimiting
1383 the strings if the file has been opened in that mode. */
1386 write_character (st_parameter_dt *dtp, const char *source, int length)
1391 switch (dtp->u.p.current_unit->flags.delim)
1393 case DELIM_APOSTROPHE:
1410 for (i = 0; i < length; i++)
1415 p = write_block (dtp, length + extra);
1420 memcpy (p, source, length);
1425 for (i = 0; i < length; i++)
1437 /* Output a real number with default format.
1438 This is 1PG14.7E2 for REAL(4), 1PG23.15E3 for REAL(8),
1439 1PG28.19E4 for REAL(10) and 1PG43.34E4 for REAL(16). */
1442 write_real (st_parameter_dt *dtp, const char *source, int length)
1445 int org_scale = dtp->u.p.scale_factor;
1447 dtp->u.p.scale_factor = 1;
1471 internal_error (&dtp->common, "bad real kind");
1474 write_float (dtp, &f, source , length);
1475 dtp->u.p.scale_factor = org_scale;
1480 write_complex (st_parameter_dt *dtp, const char *source, int kind, size_t size)
1482 if (write_char (dtp, '('))
1484 write_real (dtp, source, kind);
1486 if (write_char (dtp, ','))
1488 write_real (dtp, source + size / 2, kind);
1490 write_char (dtp, ')');
1494 /* Write the separator between items. */
1497 write_separator (st_parameter_dt *dtp)
1501 p = write_block (dtp, options.separator_len);
1505 memcpy (p, options.separator, options.separator_len);
1509 /* Write an item with list formatting.
1510 TODO: handle skipping to the next record correctly, particularly
1514 list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1517 if (dtp->u.p.current_unit == NULL)
1520 if (dtp->u.p.first_item)
1522 dtp->u.p.first_item = 0;
1523 write_char (dtp, ' ');
1527 if (type != BT_CHARACTER || !dtp->u.p.char_flag ||
1528 dtp->u.p.current_unit->flags.delim != DELIM_NONE)
1529 write_separator (dtp);
1535 write_integer (dtp, p, kind);
1538 write_logical (dtp, p, kind);
1541 write_character (dtp, p, kind);
1544 write_real (dtp, p, kind);
1547 write_complex (dtp, p, kind, size);
1550 internal_error (&dtp->common, "list_formatted_write(): Bad type");
1553 dtp->u.p.char_flag = (type == BT_CHARACTER);
1558 list_formatted_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1559 size_t size, size_t nelems)
1566 /* Big loop over all the elements. */
1567 for (elem = 0; elem < nelems; elem++)
1569 dtp->u.p.item_count++;
1570 list_formatted_write_scalar (dtp, type, tmp + size*elem, kind, size);
1576 nml_write_obj writes a namelist object to the output stream. It is called
1577 recursively for derived type components:
1578 obj = is the namelist_info for the current object.
1579 offset = the offset relative to the address held by the object for
1580 derived type arrays.
1581 base = is the namelist_info of the derived type, when obj is a
1583 base_name = the full name for a derived type, including qualifiers
1585 The returned value is a pointer to the object beyond the last one
1586 accessed, including nested derived types. Notice that the namelist is
1587 a linear linked list of objects, including derived types and their
1588 components. A tree, of sorts, is implied by the compound names of
1589 the derived type components and this is how this function recurses through
1592 /* A generous estimate of the number of characters needed to print
1593 repeat counts and indices, including commas, asterices and brackets. */
1595 #define NML_DIGITS 20
1597 static namelist_info *
1598 nml_write_obj (st_parameter_dt *dtp, namelist_info * obj, index_type offset,
1599 namelist_info * base, char * base_name)
1605 index_type obj_size;
1609 index_type elem_ctr;
1610 index_type obj_name_len;
1615 char rep_buff[NML_DIGITS];
1616 namelist_info * cmp;
1617 namelist_info * retval = obj->next;
1618 size_t base_name_len;
1619 size_t base_var_name_len;
1622 /* Write namelist variable names in upper case. If a derived type,
1623 nothing is output. If a component, base and base_name are set. */
1625 if (obj->type != GFC_DTYPE_DERIVED)
1628 write_character (dtp, "\r\n ", 3);
1630 write_character (dtp, "\n ", 2);
1635 len =strlen (base->var_name);
1636 for (dim_i = 0; dim_i < (index_type) strlen (base_name); dim_i++)
1638 cup = toupper (base_name[dim_i]);
1639 write_character (dtp, &cup, 1);
1642 for (dim_i =len; dim_i < (index_type) strlen (obj->var_name); dim_i++)
1644 cup = toupper (obj->var_name[dim_i]);
1645 write_character (dtp, &cup, 1);
1647 write_character (dtp, "=", 1);
1650 /* Counts the number of data output on a line, including names. */
1659 case GFC_DTYPE_REAL:
1660 obj_size = size_from_real_kind (len);
1663 case GFC_DTYPE_COMPLEX:
1664 obj_size = size_from_complex_kind (len);
1667 case GFC_DTYPE_CHARACTER:
1668 obj_size = obj->string_length;
1676 obj_size = obj->size;
1678 /* Set the index vector and count the number of elements. */
1681 for (dim_i=0; dim_i < obj->var_rank; dim_i++)
1683 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1684 nelem = nelem * (obj->dim[dim_i].ubound + 1 - obj->dim[dim_i].lbound);
1687 /* Main loop to output the data held in the object. */
1690 for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++)
1693 /* Build the pointer to the data value. The offset is passed by
1694 recursive calls to this function for arrays of derived types.
1695 Is NULL otherwise. */
1697 p = (void *)(obj->mem_pos + elem_ctr * obj_size);
1700 /* Check for repeat counts of intrinsic types. */
1702 if ((elem_ctr < (nelem - 1)) &&
1703 (obj->type != GFC_DTYPE_DERIVED) &&
1704 !memcmp (p, (void*)(p + obj_size ), obj_size ))
1709 /* Execute a repeated output. Note the flag no_leading_blank that
1710 is used in the functions used to output the intrinsic types. */
1716 st_sprintf(rep_buff, " %d*", rep_ctr);
1717 write_character (dtp, rep_buff, strlen (rep_buff));
1718 dtp->u.p.no_leading_blank = 1;
1722 /* Output the data, if an intrinsic type, or recurse into this
1723 routine to treat derived types. */
1728 case GFC_DTYPE_INTEGER:
1729 write_integer (dtp, p, len);
1732 case GFC_DTYPE_LOGICAL:
1733 write_logical (dtp, p, len);
1736 case GFC_DTYPE_CHARACTER:
1737 if (dtp->u.p.nml_delim)
1738 write_character (dtp, &dtp->u.p.nml_delim, 1);
1739 write_character (dtp, p, obj->string_length);
1740 if (dtp->u.p.nml_delim)
1741 write_character (dtp, &dtp->u.p.nml_delim, 1);
1744 case GFC_DTYPE_REAL:
1745 write_real (dtp, p, len);
1748 case GFC_DTYPE_COMPLEX:
1749 dtp->u.p.no_leading_blank = 0;
1751 write_complex (dtp, p, len, obj_size);
1754 case GFC_DTYPE_DERIVED:
1756 /* To treat a derived type, we need to build two strings:
1757 ext_name = the name, including qualifiers that prepends
1758 component names in the output - passed to
1760 obj_name = the derived type name with no qualifiers but %
1761 appended. This is used to identify the
1764 /* First ext_name => get length of all possible components */
1766 base_name_len = base_name ? strlen (base_name) : 0;
1767 base_var_name_len = base ? strlen (base->var_name) : 0;
1768 ext_name = (char*)get_mem ( base_name_len
1770 + strlen (obj->var_name)
1771 + obj->var_rank * NML_DIGITS
1774 memcpy (ext_name, base_name, base_name_len);
1775 clen = strlen (obj->var_name + base_var_name_len);
1776 memcpy (ext_name + base_name_len,
1777 obj->var_name + base_var_name_len, clen);
1779 /* Append the qualifier. */
1781 tot_len = base_name_len + clen;
1782 for (dim_i = 0; dim_i < obj->var_rank; dim_i++)
1786 ext_name[tot_len] = '(';
1789 st_sprintf (ext_name + tot_len, "%d", (int) obj->ls[dim_i].idx);
1790 tot_len += strlen (ext_name + tot_len);
1791 ext_name[tot_len] = (dim_i == obj->var_rank - 1) ? ')' : ',';
1795 ext_name[tot_len] = '\0';
1799 obj_name_len = strlen (obj->var_name) + 1;
1800 obj_name = get_mem (obj_name_len+1);
1801 memcpy (obj_name, obj->var_name, obj_name_len-1);
1802 memcpy (obj_name + obj_name_len-1, "%", 2);
1804 /* Now loop over the components. Update the component pointer
1805 with the return value from nml_write_obj => this loop jumps
1806 past nested derived types. */
1808 for (cmp = obj->next;
1809 cmp && !strncmp (cmp->var_name, obj_name, obj_name_len);
1812 retval = nml_write_obj (dtp, cmp,
1813 (index_type)(p - obj->mem_pos),
1817 free_mem (obj_name);
1818 free_mem (ext_name);
1822 internal_error (&dtp->common, "Bad type for namelist write");
1825 /* Reset the leading blank suppression, write a comma and, if 5
1826 values have been output, write a newline and advance to column
1827 2. Reset the repeat counter. */
1829 dtp->u.p.no_leading_blank = 0;
1830 write_character (dtp, ",", 1);
1835 write_character (dtp, "\r\n ", 3);
1837 write_character (dtp, "\n ", 2);
1843 /* Cycle through and increment the index vector. */
1848 for (dim_i = 0; nml_carry && (dim_i < obj->var_rank); dim_i++)
1850 obj->ls[dim_i].idx += nml_carry ;
1852 if (obj->ls[dim_i].idx > (ssize_t)obj->dim[dim_i].ubound)
1854 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1860 /* Return a pointer beyond the furthest object accessed. */
1865 /* This is the entry function for namelist writes. It outputs the name
1866 of the namelist and iterates through the namelist by calls to
1867 nml_write_obj. The call below has dummys in the arguments used in
1868 the treatment of derived types. */
1871 namelist_write (st_parameter_dt *dtp)
1873 namelist_info * t1, *t2, *dummy = NULL;
1875 index_type dummy_offset = 0;
1877 char * dummy_name = NULL;
1878 unit_delim tmp_delim;
1880 /* Set the delimiter for namelist output. */
1882 tmp_delim = dtp->u.p.current_unit->flags.delim;
1883 dtp->u.p.current_unit->flags.delim = DELIM_NONE;
1887 dtp->u.p.nml_delim = '"';
1890 case (DELIM_APOSTROPHE):
1891 dtp->u.p.nml_delim = '\'';
1895 dtp->u.p.nml_delim = '\0';
1899 write_character (dtp, "&", 1);
1901 /* Write namelist name in upper case - f95 std. */
1903 for (i = 0 ;i < dtp->namelist_name_len ;i++ )
1905 c = toupper (dtp->namelist_name[i]);
1906 write_character (dtp, &c ,1);
1909 if (dtp->u.p.ionml != NULL)
1911 t1 = dtp->u.p.ionml;
1915 t1 = nml_write_obj (dtp, t2, dummy_offset, dummy, dummy_name);
1919 write_character (dtp, " /\r\n", 5);
1921 write_character (dtp, " /\n", 4);
1924 /* Recover the original delimiter. */
1926 dtp->u.p.current_unit->flags.delim = tmp_delim;