1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 95 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/>. */
35 #define star_fill(p, n) memset(p, '*', n)
37 #include "write_float.def"
39 typedef unsigned char uchar;
41 /* Write out default char4. */
44 write_default_char4 (st_parameter_dt *dtp, gfc_char4_t *source,
45 int src_len, int w_len)
52 /* Take care of preceding blanks. */
56 p = write_block (dtp, k);
62 /* Get ready to handle delimiters if needed. */
63 switch (dtp->u.p.current_unit->delim_status)
65 case DELIM_APOSTROPHE:
76 /* Now process the remaining characters, one at a time. */
77 for (j = k; j < src_len; j++)
81 /* Handle delimiters if any. */
82 if (c == d && d != ' ')
84 p = write_block (dtp, 2);
91 p = write_block (dtp, 1);
95 *p = c > 255 ? '?' : (uchar) c;
100 /* Write out UTF-8 converted from char4. */
103 write_utf8_char4 (st_parameter_dt *dtp, gfc_char4_t *source,
104 int src_len, int w_len)
109 static const uchar masks[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
110 static const uchar limits[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
114 /* Take care of preceding blanks. */
118 p = write_block (dtp, k);
124 /* Get ready to handle delimiters if needed. */
125 switch (dtp->u.p.current_unit->delim_status)
127 case DELIM_APOSTROPHE:
138 /* Now process the remaining characters, one at a time. */
139 for (j = k; j < src_len; j++)
144 /* Handle the delimiters if any. */
145 if (c == d && d != ' ')
147 p = write_block (dtp, 2);
154 p = write_block (dtp, 1);
162 /* Convert to UTF-8 sequence. */
168 *--q = ((c & 0x3F) | 0x80);
172 while (c >= 0x3F || (c & limits[nbytes-1]));
174 *--q = (c | masks[nbytes-1]);
176 p = write_block (dtp, nbytes);
188 write_a (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
193 wlen = f->u.string.length < 0
194 || (f->format == FMT_G && f->u.string.length == 0)
195 ? len : f->u.string.length;
198 /* If this is formatted STREAM IO convert any embedded line feed characters
199 to CR_LF on systems that use that sequence for newlines. See F2003
200 Standard sections 10.6.3 and 9.9 for further information. */
201 if (is_stream_io (dtp))
203 const char crlf[] = "\r\n";
207 /* Write out any padding if needed. */
210 p = write_block (dtp, wlen - len);
213 memset (p, ' ', wlen - len);
216 /* Scan the source string looking for '\n' and convert it if found. */
217 for (i = 0; i < wlen; i++)
219 if (source[i] == '\n')
221 /* Write out the previously scanned characters in the string. */
224 p = write_block (dtp, bytes);
227 memcpy (p, &source[q], bytes);
232 /* Write out the CR_LF sequence. */
234 p = write_block (dtp, 2);
243 /* Write out any remaining bytes if no LF was found. */
246 p = write_block (dtp, bytes);
249 memcpy (p, &source[q], bytes);
255 p = write_block (dtp, wlen);
260 memcpy (p, source, wlen);
263 memset (p, ' ', wlen - len);
264 memcpy (p + wlen - len, source, len);
272 /* The primary difference between write_a_char4 and write_a is that we have to
273 deal with writing from the first byte of the 4-byte character and pay
274 attention to the most significant bytes. For ENCODING="default" write the
275 lowest significant byte. If the 3 most significant bytes contain
276 non-zero values, emit a '?'. For ENCODING="utf-8", convert the UCS-32 value
277 to the UTF-8 encoded string before writing out. */
280 write_a_char4 (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
285 wlen = f->u.string.length < 0
286 || (f->format == FMT_G && f->u.string.length == 0)
287 ? len : f->u.string.length;
289 q = (gfc_char4_t *) source;
291 /* If this is formatted STREAM IO convert any embedded line feed characters
292 to CR_LF on systems that use that sequence for newlines. See F2003
293 Standard sections 10.6.3 and 9.9 for further information. */
294 if (is_stream_io (dtp))
296 const char crlf[] = "\r\n";
301 /* Write out any padding if needed. */
305 p = write_block (dtp, wlen - len);
308 memset (p, ' ', wlen - len);
311 /* Scan the source string looking for '\n' and convert it if found. */
312 qq = (gfc_char4_t *) source;
313 for (i = 0; i < wlen; i++)
317 /* Write out the previously scanned characters in the string. */
320 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
321 write_utf8_char4 (dtp, q, bytes, 0);
323 write_default_char4 (dtp, q, bytes, 0);
327 /* Write out the CR_LF sequence. */
328 write_default_char4 (dtp, crlf, 2, 0);
334 /* Write out any remaining bytes if no LF was found. */
337 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
338 write_utf8_char4 (dtp, q, bytes, 0);
340 write_default_char4 (dtp, q, bytes, 0);
346 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
347 write_utf8_char4 (dtp, q, len, wlen);
349 write_default_char4 (dtp, q, len, wlen);
356 static GFC_INTEGER_LARGEST
357 extract_int (const void *p, int len)
359 GFC_INTEGER_LARGEST i = 0;
369 memcpy ((void *) &tmp, p, len);
376 memcpy ((void *) &tmp, p, len);
383 memcpy ((void *) &tmp, p, len);
390 memcpy ((void *) &tmp, p, len);
394 #ifdef HAVE_GFC_INTEGER_16
398 memcpy ((void *) &tmp, p, len);
404 internal_error (NULL, "bad integer kind");
410 static GFC_UINTEGER_LARGEST
411 extract_uint (const void *p, int len)
413 GFC_UINTEGER_LARGEST i = 0;
423 memcpy ((void *) &tmp, p, len);
424 i = (GFC_UINTEGER_1) tmp;
430 memcpy ((void *) &tmp, p, len);
431 i = (GFC_UINTEGER_2) tmp;
437 memcpy ((void *) &tmp, p, len);
438 i = (GFC_UINTEGER_4) tmp;
444 memcpy ((void *) &tmp, p, len);
445 i = (GFC_UINTEGER_8) tmp;
448 #ifdef HAVE_GFC_INTEGER_16
452 memcpy ((void *) &tmp, p, len);
453 i = (GFC_UINTEGER_16) tmp;
458 internal_error (NULL, "bad integer kind");
466 write_l (st_parameter_dt *dtp, const fnode *f, char *source, int len)
470 GFC_INTEGER_LARGEST n;
472 wlen = (f->format == FMT_G && f->u.w == 0) ? 1 : f->u.w;
474 p = write_block (dtp, wlen);
478 memset (p, ' ', wlen - 1);
479 n = extract_int (source, len);
480 p[wlen - 1] = (n) ? 'T' : 'F';
485 write_int (st_parameter_dt *dtp, const fnode *f, const char *source, int len,
486 const char *(*conv) (GFC_UINTEGER_LARGEST, char *, size_t))
488 GFC_UINTEGER_LARGEST n = 0;
489 int w, m, digits, nzero, nblank;
492 char itoa_buf[GFC_BTOA_BUF_SIZE];
497 n = extract_uint (source, len);
501 if (m == 0 && n == 0)
506 p = write_block (dtp, w);
514 q = conv (n, itoa_buf, sizeof (itoa_buf));
517 /* Select a width if none was specified. The idea here is to always
521 w = ((digits < m) ? m : digits);
523 p = write_block (dtp, w);
531 /* See if things will work. */
533 nblank = w - (nzero + digits);
542 if (!dtp->u.p.no_leading_blank)
544 memset (p, ' ', nblank);
546 memset (p, '0', nzero);
548 memcpy (p, q, digits);
552 memset (p, '0', nzero);
554 memcpy (p, q, digits);
556 memset (p, ' ', nblank);
557 dtp->u.p.no_leading_blank = 0;
565 write_decimal (st_parameter_dt *dtp, const fnode *f, const char *source,
567 const char *(*conv) (GFC_INTEGER_LARGEST, char *, size_t))
569 GFC_INTEGER_LARGEST n = 0;
570 int w, m, digits, nsign, nzero, nblank;
574 char itoa_buf[GFC_BTOA_BUF_SIZE];
577 m = f->format == FMT_G ? -1 : f->u.integer.m;
579 n = extract_int (source, len);
582 if (m == 0 && n == 0)
587 p = write_block (dtp, w);
595 sign = calculate_sign (dtp, n < 0);
598 nsign = sign == S_NONE ? 0 : 1;
600 /* conv calls itoa which sets the negative sign needed
601 by write_integer. The sign '+' or '-' is set below based on sign
602 calculated above, so we just point past the sign in the string
603 before proceeding to avoid double signs in corner cases.
605 q = conv (n, itoa_buf, sizeof (itoa_buf));
611 /* Select a width if none was specified. The idea here is to always
615 w = ((digits < m) ? m : digits) + nsign;
617 p = write_block (dtp, w);
625 /* See if things will work. */
627 nblank = w - (nsign + nzero + digits);
635 memset (p, ' ', nblank);
650 memset (p, '0', nzero);
653 memcpy (p, q, digits);
660 /* Convert unsigned octal to ascii. */
663 otoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
667 assert (len >= GFC_OTOA_BUF_SIZE);
672 p = buffer + GFC_OTOA_BUF_SIZE - 1;
677 *--p = '0' + (n & 7);
685 /* Convert unsigned binary to ascii. */
688 btoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
692 assert (len >= GFC_BTOA_BUF_SIZE);
697 p = buffer + GFC_BTOA_BUF_SIZE - 1;
702 *--p = '0' + (n & 1);
710 /* itoa()-- Integer to decimal conversion. */
713 itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
717 GFC_UINTEGER_LARGEST t;
719 assert (len >= GFC_ITOA_BUF_SIZE);
729 t = -n; /*must use unsigned to protect from overflow*/
732 p = buffer + GFC_ITOA_BUF_SIZE - 1;
737 *--p = '0' + (t % 10);
748 write_i (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
750 write_decimal (dtp, f, p, len, (void *) itoa);
755 write_b (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
757 write_int (dtp, f, p, len, btoa);
762 write_o (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
764 write_int (dtp, f, p, len, otoa);
768 write_z (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
770 write_int (dtp, f, p, len, gfc_xtoa);
775 write_d (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
777 write_float (dtp, f, p, len);
782 write_e (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
784 write_float (dtp, f, p, len);
789 write_f (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
791 write_float (dtp, f, p, len);
796 write_en (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
798 write_float (dtp, f, p, len);
803 write_es (st_parameter_dt *dtp, const fnode *f, const char *p, int len)
805 write_float (dtp, f, p, len);
809 /* Take care of the X/TR descriptor. */
812 write_x (st_parameter_dt *dtp, int len, int nspaces)
816 p = write_block (dtp, len);
819 if (nspaces > 0 && len - nspaces >= 0)
820 memset (&p[len - nspaces], ' ', nspaces);
824 /* List-directed writing. */
827 /* Write a single character to the output. Returns nonzero if
828 something goes wrong. */
831 write_char (st_parameter_dt *dtp, char c)
835 p = write_block (dtp, 1);
845 /* Write a list-directed logical value. */
848 write_logical (st_parameter_dt *dtp, const char *source, int length)
850 write_char (dtp, extract_int (source, length) ? 'T' : 'F');
854 /* Write a list-directed integer value. */
857 write_integer (st_parameter_dt *dtp, const char *source, int length)
863 char itoa_buf[GFC_ITOA_BUF_SIZE];
865 q = itoa (extract_int (source, length), itoa_buf, sizeof (itoa_buf));
894 p = write_block (dtp, width);
897 if (dtp->u.p.no_leading_blank)
899 memcpy (p, q, digits);
900 memset (p + digits, ' ', width - digits);
904 memset (p, ' ', width - digits);
905 memcpy (p + width - digits, q, digits);
910 /* Write a list-directed string. We have to worry about delimiting
911 the strings if the file has been opened in that mode. */
914 write_character (st_parameter_dt *dtp, const char *source, int kind, int length)
919 switch (dtp->u.p.current_unit->delim_status)
921 case DELIM_APOSTROPHE:
940 for (i = 0; i < length; i++)
945 p = write_block (dtp, length + extra);
950 memcpy (p, source, length);
955 for (i = 0; i < length; i++)
969 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
970 write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
972 write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
976 p = write_block (dtp, 1);
979 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
980 write_utf8_char4 (dtp, (gfc_char4_t *) source, length, 0);
982 write_default_char4 (dtp, (gfc_char4_t *) source, length, 0);
984 p = write_block (dtp, 1);
991 /* Set an fnode to default format. */
994 set_fnode_default (st_parameter_dt *dtp, fnode *f, int length)
1020 internal_error (&dtp->common, "bad real kind");
1024 /* Output a real number with default format.
1025 This is 1PG14.7E2 for REAL(4), 1PG23.15E3 for REAL(8),
1026 1PG28.19E4 for REAL(10) and 1PG43.34E4 for REAL(16). */
1029 write_real (st_parameter_dt *dtp, const char *source, int length)
1032 int org_scale = dtp->u.p.scale_factor;
1033 dtp->u.p.scale_factor = 1;
1034 set_fnode_default (dtp, &f, length);
1035 write_float (dtp, &f, source , length);
1036 dtp->u.p.scale_factor = org_scale;
1041 write_real_g0 (st_parameter_dt *dtp, const char *source, int length, int d)
1044 set_fnode_default (dtp, &f, length);
1047 dtp->u.p.g0_no_blanks = 1;
1048 write_float (dtp, &f, source , length);
1049 dtp->u.p.g0_no_blanks = 0;
1054 write_complex (st_parameter_dt *dtp, const char *source, int kind, size_t size)
1057 dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
1059 if (write_char (dtp, '('))
1061 write_real (dtp, source, kind);
1063 if (write_char (dtp, semi_comma))
1065 write_real (dtp, source + size / 2, kind);
1067 write_char (dtp, ')');
1071 /* Write the separator between items. */
1074 write_separator (st_parameter_dt *dtp)
1078 p = write_block (dtp, options.separator_len);
1082 memcpy (p, options.separator, options.separator_len);
1086 /* Write an item with list formatting.
1087 TODO: handle skipping to the next record correctly, particularly
1091 list_formatted_write_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1094 if (dtp->u.p.current_unit == NULL)
1097 if (dtp->u.p.first_item)
1099 dtp->u.p.first_item = 0;
1100 write_char (dtp, ' ');
1104 if (type != BT_CHARACTER || !dtp->u.p.char_flag ||
1105 dtp->u.p.current_unit->delim_status != DELIM_NONE)
1106 write_separator (dtp);
1112 write_integer (dtp, p, kind);
1115 write_logical (dtp, p, kind);
1118 write_character (dtp, p, kind, size);
1121 write_real (dtp, p, kind);
1124 write_complex (dtp, p, kind, size);
1127 internal_error (&dtp->common, "list_formatted_write(): Bad type");
1130 dtp->u.p.char_flag = (type == BT_CHARACTER);
1135 list_formatted_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1136 size_t size, size_t nelems)
1140 size_t stride = type == BT_CHARACTER ?
1141 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1145 /* Big loop over all the elements. */
1146 for (elem = 0; elem < nelems; elem++)
1148 dtp->u.p.item_count++;
1149 list_formatted_write_scalar (dtp, type, tmp + elem * stride, kind, size);
1155 nml_write_obj writes a namelist object to the output stream. It is called
1156 recursively for derived type components:
1157 obj = is the namelist_info for the current object.
1158 offset = the offset relative to the address held by the object for
1159 derived type arrays.
1160 base = is the namelist_info of the derived type, when obj is a
1162 base_name = the full name for a derived type, including qualifiers
1164 The returned value is a pointer to the object beyond the last one
1165 accessed, including nested derived types. Notice that the namelist is
1166 a linear linked list of objects, including derived types and their
1167 components. A tree, of sorts, is implied by the compound names of
1168 the derived type components and this is how this function recurses through
1171 /* A generous estimate of the number of characters needed to print
1172 repeat counts and indices, including commas, asterices and brackets. */
1174 #define NML_DIGITS 20
1177 namelist_write_newline (st_parameter_dt *dtp)
1179 if (!is_internal_unit (dtp))
1182 write_character (dtp, "\r\n", 1, 2);
1184 write_character (dtp, "\n", 1, 1);
1189 if (is_array_io (dtp))
1192 int finished, length;
1194 length = (int) dtp->u.p.current_unit->bytes_left;
1196 /* Now that the current record has been padded out,
1197 determine where the next record in the array is. */
1198 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
1201 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1204 /* Now seek to this record */
1205 record = record * dtp->u.p.current_unit->recl;
1207 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
1209 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
1213 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1217 write_character (dtp, " ", 1, 1);
1221 static namelist_info *
1222 nml_write_obj (st_parameter_dt *dtp, namelist_info * obj, index_type offset,
1223 namelist_info * base, char * base_name)
1229 index_type obj_size;
1233 index_type elem_ctr;
1234 size_t obj_name_len;
1239 char rep_buff[NML_DIGITS];
1240 namelist_info * cmp;
1241 namelist_info * retval = obj->next;
1242 size_t base_name_len;
1243 size_t base_var_name_len;
1245 unit_delim tmp_delim;
1247 /* Set the character to be used to separate values
1248 to a comma or semi-colon. */
1251 dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';';
1253 /* Write namelist variable names in upper case. If a derived type,
1254 nothing is output. If a component, base and base_name are set. */
1256 if (obj->type != GFC_DTYPE_DERIVED)
1258 namelist_write_newline (dtp);
1259 write_character (dtp, " ", 1, 1);
1264 len = strlen (base->var_name);
1265 base_name_len = strlen (base_name);
1266 for (dim_i = 0; dim_i < base_name_len; dim_i++)
1268 cup = toupper (base_name[dim_i]);
1269 write_character (dtp, &cup, 1, 1);
1272 clen = strlen (obj->var_name);
1273 for (dim_i = len; dim_i < clen; dim_i++)
1275 cup = toupper (obj->var_name[dim_i]);
1276 write_character (dtp, &cup, 1, 1);
1278 write_character (dtp, "=", 1, 1);
1281 /* Counts the number of data output on a line, including names. */
1290 case GFC_DTYPE_REAL:
1291 obj_size = size_from_real_kind (len);
1294 case GFC_DTYPE_COMPLEX:
1295 obj_size = size_from_complex_kind (len);
1298 case GFC_DTYPE_CHARACTER:
1299 obj_size = obj->string_length;
1307 obj_size = obj->size;
1309 /* Set the index vector and count the number of elements. */
1312 for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
1314 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1315 nelem = nelem * (obj->dim[dim_i].ubound + 1 - obj->dim[dim_i].lbound);
1318 /* Main loop to output the data held in the object. */
1321 for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++)
1324 /* Build the pointer to the data value. The offset is passed by
1325 recursive calls to this function for arrays of derived types.
1326 Is NULL otherwise. */
1328 p = (void *)(obj->mem_pos + elem_ctr * obj_size);
1331 /* Check for repeat counts of intrinsic types. */
1333 if ((elem_ctr < (nelem - 1)) &&
1334 (obj->type != GFC_DTYPE_DERIVED) &&
1335 !memcmp (p, (void*)(p + obj_size ), obj_size ))
1340 /* Execute a repeated output. Note the flag no_leading_blank that
1341 is used in the functions used to output the intrinsic types. */
1347 sprintf(rep_buff, " %d*", rep_ctr);
1348 write_character (dtp, rep_buff, 1, strlen (rep_buff));
1349 dtp->u.p.no_leading_blank = 1;
1353 /* Output the data, if an intrinsic type, or recurse into this
1354 routine to treat derived types. */
1359 case GFC_DTYPE_INTEGER:
1360 write_integer (dtp, p, len);
1363 case GFC_DTYPE_LOGICAL:
1364 write_logical (dtp, p, len);
1367 case GFC_DTYPE_CHARACTER:
1368 tmp_delim = dtp->u.p.current_unit->delim_status;
1369 if (dtp->u.p.nml_delim == '"')
1370 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
1371 if (dtp->u.p.nml_delim == '\'')
1372 dtp->u.p.current_unit->delim_status = DELIM_APOSTROPHE;
1373 write_character (dtp, p, 1, obj->string_length);
1374 dtp->u.p.current_unit->delim_status = tmp_delim;
1377 case GFC_DTYPE_REAL:
1378 write_real (dtp, p, len);
1381 case GFC_DTYPE_COMPLEX:
1382 dtp->u.p.no_leading_blank = 0;
1384 write_complex (dtp, p, len, obj_size);
1387 case GFC_DTYPE_DERIVED:
1389 /* To treat a derived type, we need to build two strings:
1390 ext_name = the name, including qualifiers that prepends
1391 component names in the output - passed to
1393 obj_name = the derived type name with no qualifiers but %
1394 appended. This is used to identify the
1397 /* First ext_name => get length of all possible components */
1399 base_name_len = base_name ? strlen (base_name) : 0;
1400 base_var_name_len = base ? strlen (base->var_name) : 0;
1401 ext_name = (char*)get_mem ( base_name_len
1403 + strlen (obj->var_name)
1404 + obj->var_rank * NML_DIGITS
1407 memcpy (ext_name, base_name, base_name_len);
1408 clen = strlen (obj->var_name + base_var_name_len);
1409 memcpy (ext_name + base_name_len,
1410 obj->var_name + base_var_name_len, clen);
1412 /* Append the qualifier. */
1414 tot_len = base_name_len + clen;
1415 for (dim_i = 0; dim_i < (size_t) obj->var_rank; dim_i++)
1419 ext_name[tot_len] = '(';
1422 sprintf (ext_name + tot_len, "%d", (int) obj->ls[dim_i].idx);
1423 tot_len += strlen (ext_name + tot_len);
1424 ext_name[tot_len] = ((int) dim_i == obj->var_rank - 1) ? ')' : ',';
1428 ext_name[tot_len] = '\0';
1432 obj_name_len = strlen (obj->var_name) + 1;
1433 obj_name = get_mem (obj_name_len+1);
1434 memcpy (obj_name, obj->var_name, obj_name_len-1);
1435 memcpy (obj_name + obj_name_len-1, "%", 2);
1437 /* Now loop over the components. Update the component pointer
1438 with the return value from nml_write_obj => this loop jumps
1439 past nested derived types. */
1441 for (cmp = obj->next;
1442 cmp && !strncmp (cmp->var_name, obj_name, obj_name_len);
1445 retval = nml_write_obj (dtp, cmp,
1446 (index_type)(p - obj->mem_pos),
1450 free_mem (obj_name);
1451 free_mem (ext_name);
1455 internal_error (&dtp->common, "Bad type for namelist write");
1458 /* Reset the leading blank suppression, write a comma (or semi-colon)
1459 and, if 5 values have been output, write a newline and advance
1460 to column 2. Reset the repeat counter. */
1462 dtp->u.p.no_leading_blank = 0;
1463 write_character (dtp, &semi_comma, 1, 1);
1467 namelist_write_newline (dtp);
1468 write_character (dtp, " ", 1, 1);
1473 /* Cycle through and increment the index vector. */
1478 for (dim_i = 0; nml_carry && (dim_i < (size_t) obj->var_rank); dim_i++)
1480 obj->ls[dim_i].idx += nml_carry ;
1482 if (obj->ls[dim_i].idx > (index_type) obj->dim[dim_i].ubound)
1484 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1490 /* Return a pointer beyond the furthest object accessed. */
1496 /* This is the entry function for namelist writes. It outputs the name
1497 of the namelist and iterates through the namelist by calls to
1498 nml_write_obj. The call below has dummys in the arguments used in
1499 the treatment of derived types. */
1502 namelist_write (st_parameter_dt *dtp)
1504 namelist_info * t1, *t2, *dummy = NULL;
1506 index_type dummy_offset = 0;
1508 char * dummy_name = NULL;
1509 unit_delim tmp_delim = DELIM_UNSPECIFIED;
1511 /* Set the delimiter for namelist output. */
1512 tmp_delim = dtp->u.p.current_unit->delim_status;
1514 dtp->u.p.nml_delim = tmp_delim == DELIM_APOSTROPHE ? '\'' : '"';
1516 /* Temporarily disable namelist delimters. */
1517 dtp->u.p.current_unit->delim_status = DELIM_NONE;
1519 write_character (dtp, "&", 1, 1);
1521 /* Write namelist name in upper case - f95 std. */
1522 for (i = 0 ;i < dtp->namelist_name_len ;i++ )
1524 c = toupper (dtp->namelist_name[i]);
1525 write_character (dtp, &c, 1 ,1);
1528 if (dtp->u.p.ionml != NULL)
1530 t1 = dtp->u.p.ionml;
1534 t1 = nml_write_obj (dtp, t2, dummy_offset, dummy, dummy_name);
1538 namelist_write_newline (dtp);
1539 write_character (dtp, " /", 1, 2);
1540 /* Restore the original delimiter. */
1541 dtp->u.p.current_unit->delim_status = tmp_delim;