* configure.in: Add --enable-libssp and --disable-libssp.
[official-gcc.git] / libgfortran / io / write.c
blob0e2a3ac5ccdf114a96b9e4a864ec7c2715532c2c
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist output contibuted 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)
10 any later version.
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
19 executable.)
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, 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
31 #include "config.h"
32 #include <string.h>
33 #include <ctype.h>
34 #include <float.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include "libgfortran.h"
38 #include "io.h"
40 #define star_fill(p, n) memset(p, '*', n)
43 typedef enum
44 { SIGN_NONE, SIGN_MINUS, SIGN_PLUS }
45 sign_t;
48 static int no_leading_blank = 0 ;
50 void
51 write_a (fnode * f, const char *source, int len)
53 int wlen;
54 char *p;
56 wlen = f->u.string.length < 0 ? len : f->u.string.length;
58 p = write_block (wlen);
59 if (p == NULL)
60 return;
62 if (wlen < len)
63 memcpy (p, source, wlen);
64 else
66 memset (p, ' ', wlen - len);
67 memcpy (p + wlen - len, source, len);
71 static GFC_INTEGER_LARGEST
72 extract_int (const void *p, int len)
74 GFC_INTEGER_LARGEST i = 0;
76 if (p == NULL)
77 return i;
79 switch (len)
81 case 1:
82 i = *((const GFC_INTEGER_1 *) p);
83 break;
84 case 2:
85 i = *((const GFC_INTEGER_2 *) p);
86 break;
87 case 4:
88 i = *((const GFC_INTEGER_4 *) p);
89 break;
90 case 8:
91 i = *((const GFC_INTEGER_8 *) p);
92 break;
93 #ifdef HAVE_GFC_INTEGER_16
94 case 16:
95 i = *((const GFC_INTEGER_16 *) p);
96 break;
97 #endif
98 default:
99 internal_error ("bad integer kind");
102 return i;
105 static GFC_REAL_LARGEST
106 extract_real (const void *p, int len)
108 GFC_REAL_LARGEST i = 0;
109 switch (len)
111 case 4:
112 i = *((const GFC_REAL_4 *) p);
113 break;
114 case 8:
115 i = *((const GFC_REAL_8 *) p);
116 break;
117 #ifdef HAVE_GFC_REAL_10
118 case 10:
119 i = *((const GFC_REAL_10 *) p);
120 break;
121 #endif
122 #ifdef HAVE_GFC_REAL_16
123 case 16:
124 i = *((const GFC_REAL_16 *) p);
125 break;
126 #endif
127 default:
128 internal_error ("bad real kind");
130 return i;
134 /* Given a flag that indicate if a value is negative or not, return a
135 sign_t that gives the sign that we need to produce. */
137 static sign_t
138 calculate_sign (int negative_flag)
140 sign_t s = SIGN_NONE;
142 if (negative_flag)
143 s = SIGN_MINUS;
144 else
145 switch (g.sign_status)
147 case SIGN_SP:
148 s = SIGN_PLUS;
149 break;
150 case SIGN_SS:
151 s = SIGN_NONE;
152 break;
153 case SIGN_S:
154 s = options.optional_plus ? SIGN_PLUS : SIGN_NONE;
155 break;
158 return s;
162 /* Returns the value of 10**d. */
164 static GFC_REAL_LARGEST
165 calculate_exp (int d)
167 int i;
168 GFC_REAL_LARGEST r = 1.0;
170 for (i = 0; i< (d >= 0 ? d : -d); i++)
171 r *= 10;
173 r = (d >= 0) ? r : 1.0 / r;
175 return r;
179 /* Generate corresponding I/O format for FMT_G output.
180 The rules to translate FMT_G to FMT_E or FMT_F from DEC fortran
181 LRM (table 11-2, Chapter 11, "I/O Formatting", P11-25) is:
183 Data Magnitude Equivalent Conversion
184 0< m < 0.1-0.5*10**(-d-1) Ew.d[Ee]
185 m = 0 F(w-n).(d-1), n' '
186 0.1-0.5*10**(-d-1)<= m < 1-0.5*10**(-d) F(w-n).d, n' '
187 1-0.5*10**(-d)<= m < 10-0.5*10**(-d+1) F(w-n).(d-1), n' '
188 10-0.5*10**(-d+1)<= m < 100-0.5*10**(-d+2) F(w-n).(d-2), n' '
189 ................ ..........
190 10**(d-1)-0.5*10**(-1)<= m <10**d-0.5 F(w-n).0,n(' ')
191 m >= 10**d-0.5 Ew.d[Ee]
193 notes: for Gw.d , n' ' means 4 blanks
194 for Gw.dEe, n' ' means e+2 blanks */
196 static fnode *
197 calculate_G_format (fnode *f, GFC_REAL_LARGEST value, int *num_blank)
199 int e = f->u.real.e;
200 int d = f->u.real.d;
201 int w = f->u.real.w;
202 fnode *newf;
203 GFC_REAL_LARGEST m, exp_d;
204 int low, high, mid;
205 int ubound, lbound;
207 newf = get_mem (sizeof (fnode));
209 /* Absolute value. */
210 m = (value > 0.0) ? value : -value;
212 /* In case of the two data magnitude ranges,
213 generate E editing, Ew.d[Ee]. */
214 exp_d = calculate_exp (d);
215 if ((m > 0.0 && m < 0.1 - 0.05 / exp_d) || (m >= exp_d - 0.5 ))
217 newf->format = FMT_E;
218 newf->u.real.w = w;
219 newf->u.real.d = d;
220 newf->u.real.e = e;
221 *num_blank = 0;
222 return newf;
225 /* Use binary search to find the data magnitude range. */
226 mid = 0;
227 low = 0;
228 high = d + 1;
229 lbound = 0;
230 ubound = d + 1;
232 while (low <= high)
234 GFC_REAL_LARGEST temp;
235 mid = (low + high) / 2;
237 /* 0.1 * 10**mid - 0.5 * 10**(mid-d-1) */
238 temp = 0.1 * calculate_exp (mid) - 0.5 * calculate_exp (mid - d - 1);
240 if (m < temp)
242 ubound = mid;
243 if (ubound == lbound + 1)
244 break;
245 high = mid - 1;
247 else if (m > temp)
249 lbound = mid;
250 if (ubound == lbound + 1)
252 mid ++;
253 break;
255 low = mid + 1;
257 else
258 break;
261 /* Pad with blanks where the exponent would be. */
262 if (e < 0)
263 *num_blank = 4;
264 else
265 *num_blank = e + 2;
267 /* Generate the F editing. F(w-n).(-(mid-d-1)), n' '. */
268 newf->format = FMT_F;
269 newf->u.real.w = f->u.real.w - *num_blank;
271 /* Special case. */
272 if (m == 0.0)
273 newf->u.real.d = d - 1;
274 else
275 newf->u.real.d = - (mid - d - 1);
277 /* For F editing, the scale factor is ignored. */
278 g.scale_factor = 0;
279 return newf;
283 /* Output a real number according to its format which is FMT_G free. */
285 static void
286 output_float (fnode *f, GFC_REAL_LARGEST value)
288 /* This must be large enough to accurately hold any value. */
289 char buffer[32];
290 char *out;
291 char *digits;
292 int e;
293 char expchar;
294 format_token ft;
295 int w;
296 int d;
297 int edigits;
298 int ndigits;
299 /* Number of digits before the decimal point. */
300 int nbefore;
301 /* Number of zeros after the decimal point. */
302 int nzero;
303 /* Number of digits after the decimal point. */
304 int nafter;
305 /* Number of zeros after the decimal point, whatever the precision. */
306 int nzero_real;
307 int leadzero;
308 int nblanks;
309 int i;
310 sign_t sign;
311 double abslog;
313 ft = f->format;
314 w = f->u.real.w;
315 d = f->u.real.d;
317 nzero_real = -1;
320 /* We should always know the field width and precision. */
321 if (d < 0)
322 internal_error ("Unspecified precision");
324 /* Use sprintf to print the number in the format +D.DDDDe+ddd
325 For an N digit exponent, this gives us (32-6)-N digits after the
326 decimal point, plus another one before the decimal point. */
327 sign = calculate_sign (value < 0.0);
328 if (value < 0)
329 value = -value;
331 /* Printf always prints at least two exponent digits. */
332 if (value == 0)
333 edigits = 2;
334 else
336 #if defined(HAVE_GFC_REAL_10) || defined(HAVE_GFC_REAL_16)
337 abslog = fabs((double) log10l(value));
338 #else
339 abslog = fabs(log10(value));
340 #endif
341 if (abslog < 100)
342 edigits = 2;
343 else
344 edigits = 1 + (int) log10(abslog);
347 if (ft == FMT_F || ft == FMT_EN
348 || ((ft == FMT_D || ft == FMT_E) && g.scale_factor != 0))
350 /* Always convert at full precision to avoid double rounding. */
351 ndigits = 27 - edigits;
353 else
355 /* We know the number of digits, so can let printf do the rounding
356 for us. */
357 if (ft == FMT_ES)
358 ndigits = d + 1;
359 else
360 ndigits = d;
361 if (ndigits > 27 - edigits)
362 ndigits = 27 - edigits;
365 /* # The result will always contain a decimal point, even if no
366 * digits follow it
368 * - The converted value is to be left adjusted on the field boundary
370 * + A sign (+ or -) always be placed before a number
372 * 31 minimum field width
374 * * (ndigits-1) is used as the precision
376 * e format: [-]d.ddde±dd where there is one digit before the
377 * decimal-point character and the number of digits after it is
378 * equal to the precision. The exponent always contains at least two
379 * digits; if the value is zero, the exponent is 00.
381 sprintf (buffer, "%+-#31.*" GFC_REAL_LARGEST_FORMAT "e",
382 ndigits - 1, value);
384 /* Check the resulting string has punctuation in the correct places. */
385 if (buffer[2] != '.' || buffer[ndigits + 2] != 'e')
386 internal_error ("printf is broken");
388 /* Read the exponent back in. */
389 e = atoi (&buffer[ndigits + 3]) + 1;
391 /* Make sure zero comes out as 0.0e0. */
392 if (value == 0.0)
393 e = 0;
395 /* Normalize the fractional component. */
396 buffer[2] = buffer[1];
397 digits = &buffer[2];
399 /* Figure out where to place the decimal point. */
400 switch (ft)
402 case FMT_F:
403 nbefore = e + g.scale_factor;
404 if (nbefore < 0)
406 nzero = -nbefore;
407 nzero_real = nzero;
408 if (nzero > d)
409 nzero = d;
410 nafter = d - nzero;
411 nbefore = 0;
413 else
415 nzero = 0;
416 nafter = d;
418 expchar = 0;
419 break;
421 case FMT_E:
422 case FMT_D:
423 i = g.scale_factor;
424 if (value != 0.0)
425 e -= i;
426 if (i < 0)
428 nbefore = 0;
429 nzero = -i;
430 nafter = d + i;
432 else if (i > 0)
434 nbefore = i;
435 nzero = 0;
436 nafter = (d - i) + 1;
438 else /* i == 0 */
440 nbefore = 0;
441 nzero = 0;
442 nafter = d;
445 if (ft == FMT_E)
446 expchar = 'E';
447 else
448 expchar = 'D';
449 break;
451 case FMT_EN:
452 /* The exponent must be a multiple of three, with 1-3 digits before
453 the decimal point. */
454 if (value != 0.0)
455 e--;
456 if (e >= 0)
457 nbefore = e % 3;
458 else
460 nbefore = (-e) % 3;
461 if (nbefore != 0)
462 nbefore = 3 - nbefore;
464 e -= nbefore;
465 nbefore++;
466 nzero = 0;
467 nafter = d;
468 expchar = 'E';
469 break;
471 case FMT_ES:
472 if (value != 0.0)
473 e--;
474 nbefore = 1;
475 nzero = 0;
476 nafter = d;
477 expchar = 'E';
478 break;
480 default:
481 /* Should never happen. */
482 internal_error ("Unexpected format token");
485 /* Round the value. */
486 if (nbefore + nafter == 0)
488 ndigits = 0;
489 if (nzero_real == d && digits[0] >= '5')
491 /* We rounded to zero but shouldn't have */
492 nzero--;
493 nafter = 1;
494 digits[0] = '1';
495 ndigits = 1;
498 else if (nbefore + nafter < ndigits)
500 ndigits = nbefore + nafter;
501 i = ndigits;
502 if (digits[i] >= '5')
504 /* Propagate the carry. */
505 for (i--; i >= 0; i--)
507 if (digits[i] != '9')
509 digits[i]++;
510 break;
512 digits[i] = '0';
515 if (i < 0)
517 /* The carry overflowed. Fortunately we have some spare space
518 at the start of the buffer. We may discard some digits, but
519 this is ok because we already know they are zero. */
520 digits--;
521 digits[0] = '1';
522 if (ft == FMT_F)
524 if (nzero > 0)
526 nzero--;
527 nafter++;
529 else
530 nbefore++;
532 else if (ft == FMT_EN)
534 nbefore++;
535 if (nbefore == 4)
537 nbefore = 1;
538 e += 3;
541 else
542 e++;
547 /* Calculate the format of the exponent field. */
548 if (expchar)
550 edigits = 1;
551 for (i = abs (e); i >= 10; i /= 10)
552 edigits++;
554 if (f->u.real.e < 0)
556 /* Width not specified. Must be no more than 3 digits. */
557 if (e > 999 || e < -999)
558 edigits = -1;
559 else
561 edigits = 4;
562 if (e > 99 || e < -99)
563 expchar = ' ';
566 else
568 /* Exponent width specified, check it is wide enough. */
569 if (edigits > f->u.real.e)
570 edigits = -1;
571 else
572 edigits = f->u.real.e + 2;
575 else
576 edigits = 0;
578 /* Pick a field size if none was specified. */
579 if (w <= 0)
580 w = nbefore + nzero + nafter + (sign != SIGN_NONE ? 2 : 1);
582 /* Create the ouput buffer. */
583 out = write_block (w);
584 if (out == NULL)
585 return;
587 /* Zero values always output as positive, even if the value was negative
588 before rounding. */
589 for (i = 0; i < ndigits; i++)
591 if (digits[i] != '0')
592 break;
594 if (i == ndigits)
595 sign = calculate_sign (0);
597 /* Work out how much padding is needed. */
598 nblanks = w - (nbefore + nzero + nafter + edigits + 1);
599 if (sign != SIGN_NONE)
600 nblanks--;
602 /* Check the value fits in the specified field width. */
603 if (nblanks < 0 || edigits == -1)
605 star_fill (out, w);
606 return;
609 /* See if we have space for a zero before the decimal point. */
610 if (nbefore == 0 && nblanks > 0)
612 leadzero = 1;
613 nblanks--;
615 else
616 leadzero = 0;
618 /* Padd to full field width. */
621 if ( ( nblanks > 0 ) && !no_leading_blank )
623 memset (out, ' ', nblanks);
624 out += nblanks;
627 /* Output the initial sign (if any). */
628 if (sign == SIGN_PLUS)
629 *(out++) = '+';
630 else if (sign == SIGN_MINUS)
631 *(out++) = '-';
633 /* Output an optional leading zero. */
634 if (leadzero)
635 *(out++) = '0';
637 /* Output the part before the decimal point, padding with zeros. */
638 if (nbefore > 0)
640 if (nbefore > ndigits)
641 i = ndigits;
642 else
643 i = nbefore;
645 memcpy (out, digits, i);
646 while (i < nbefore)
647 out[i++] = '0';
649 digits += i;
650 ndigits -= i;
651 out += nbefore;
653 /* Output the decimal point. */
654 *(out++) = '.';
656 /* Output leading zeros after the decimal point. */
657 if (nzero > 0)
659 for (i = 0; i < nzero; i++)
660 *(out++) = '0';
663 /* Output digits after the decimal point, padding with zeros. */
664 if (nafter > 0)
666 if (nafter > ndigits)
667 i = ndigits;
668 else
669 i = nafter;
671 memcpy (out, digits, i);
672 while (i < nafter)
673 out[i++] = '0';
675 digits += i;
676 ndigits -= i;
677 out += nafter;
680 /* Output the exponent. */
681 if (expchar)
683 if (expchar != ' ')
685 *(out++) = expchar;
686 edigits--;
688 #if HAVE_SNPRINTF
689 snprintf (buffer, 32, "%+0*d", edigits, e);
690 #else
691 sprintf (buffer, "%+0*d", edigits, e);
692 #endif
693 memcpy (out, buffer, edigits);
696 if ( no_leading_blank )
698 out += edigits;
699 memset( out , ' ' , nblanks );
700 no_leading_blank = 0;
705 void
706 write_l (fnode * f, char *source, int len)
708 char *p;
709 GFC_INTEGER_LARGEST n;
711 p = write_block (f->u.w);
712 if (p == NULL)
713 return;
715 memset (p, ' ', f->u.w - 1);
716 n = extract_int (source, len);
717 p[f->u.w - 1] = (n) ? 'T' : 'F';
720 /* Output a real number according to its format. */
722 static void
723 write_float (fnode *f, const char *source, int len)
725 GFC_REAL_LARGEST n;
726 int nb =0, res, save_scale_factor;
727 char * p, fin;
728 fnode *f2 = NULL;
730 n = extract_real (source, len);
732 if (f->format != FMT_B && f->format != FMT_O && f->format != FMT_Z)
734 /* TODO: there are some systems where isfinite is not able to work
735 with long double variables. We should detect this case and
736 provide our own version for isfinite. */
737 res = isfinite (n);
738 if (res == 0)
740 nb = f->u.real.w;
741 p = write_block (nb);
742 if (nb < 3)
744 memset (p, '*',nb);
745 return;
748 memset(p, ' ', nb);
749 res = !isnan (n);
750 if (res != 0)
752 if (signbit(n))
753 fin = '-';
754 else
755 fin = '+';
757 if (nb > 7)
758 memcpy(p + nb - 8, "Infinity", 8);
759 else
760 memcpy(p + nb - 3, "Inf", 3);
761 if (nb < 8 && nb > 3)
762 p[nb - 4] = fin;
763 else if (nb > 8)
764 p[nb - 9] = fin;
766 else
767 memcpy(p + nb - 3, "NaN", 3);
768 return;
772 if (f->format != FMT_G)
774 output_float (f, n);
776 else
778 save_scale_factor = g.scale_factor;
779 f2 = calculate_G_format(f, n, &nb);
780 output_float (f2, n);
781 g.scale_factor = save_scale_factor;
782 if (f2 != NULL)
783 free_mem(f2);
785 if (nb > 0)
787 p = write_block (nb);
788 memset (p, ' ', nb);
794 static void
795 write_int (fnode *f, const char *source, int len,
796 char *(*conv) (GFC_UINTEGER_LARGEST))
798 GFC_UINTEGER_LARGEST n = 0;
799 int w, m, digits, nzero, nblank;
800 char *p, *q;
802 w = f->u.integer.w;
803 m = f->u.integer.m;
805 n = extract_int (source, len);
807 /* Special case: */
809 if (m == 0 && n == 0)
811 if (w == 0)
812 w = 1;
814 p = write_block (w);
815 if (p == NULL)
816 return;
818 memset (p, ' ', w);
819 goto done;
822 q = conv (n);
823 digits = strlen (q);
825 /* Select a width if none was specified. The idea here is to always
826 print something. */
828 if (w == 0)
829 w = ((digits < m) ? m : digits);
831 p = write_block (w);
832 if (p == NULL)
833 return;
835 nzero = 0;
836 if (digits < m)
837 nzero = m - digits;
839 /* See if things will work. */
841 nblank = w - (nzero + digits);
843 if (nblank < 0)
845 star_fill (p, w);
846 goto done;
850 if (!no_leading_blank)
852 memset (p, ' ', nblank);
853 p += nblank;
854 memset (p, '0', nzero);
855 p += nzero;
856 memcpy (p, q, digits);
858 else
860 memset (p, '0', nzero);
861 p += nzero;
862 memcpy (p, q, digits);
863 p += digits;
864 memset (p, ' ', nblank);
865 no_leading_blank = 0;
868 done:
869 return;
872 static void
873 write_decimal (fnode *f, const char *source, int len,
874 char *(*conv) (GFC_INTEGER_LARGEST))
876 GFC_INTEGER_LARGEST n = 0;
877 int w, m, digits, nsign, nzero, nblank;
878 char *p, *q;
879 sign_t sign;
881 w = f->u.integer.w;
882 m = f->u.integer.m;
884 n = extract_int (source, len);
886 /* Special case: */
888 if (m == 0 && n == 0)
890 if (w == 0)
891 w = 1;
893 p = write_block (w);
894 if (p == NULL)
895 return;
897 memset (p, ' ', w);
898 goto done;
901 sign = calculate_sign (n < 0);
902 if (n < 0)
903 n = -n;
905 nsign = sign == SIGN_NONE ? 0 : 1;
906 q = conv (n);
908 digits = strlen (q);
910 /* Select a width if none was specified. The idea here is to always
911 print something. */
913 if (w == 0)
914 w = ((digits < m) ? m : digits) + nsign;
916 p = write_block (w);
917 if (p == NULL)
918 return;
920 nzero = 0;
921 if (digits < m)
922 nzero = m - digits;
924 /* See if things will work. */
926 nblank = w - (nsign + nzero + digits);
928 if (nblank < 0)
930 star_fill (p, w);
931 goto done;
934 memset (p, ' ', nblank);
935 p += nblank;
937 switch (sign)
939 case SIGN_PLUS:
940 *p++ = '+';
941 break;
942 case SIGN_MINUS:
943 *p++ = '-';
944 break;
945 case SIGN_NONE:
946 break;
949 memset (p, '0', nzero);
950 p += nzero;
952 memcpy (p, q, digits);
954 done:
955 return;
959 /* Convert unsigned octal to ascii. */
961 static char *
962 otoa (GFC_UINTEGER_LARGEST n)
964 char *p;
966 if (n == 0)
968 scratch[0] = '0';
969 scratch[1] = '\0';
970 return scratch;
973 p = scratch + sizeof (SCRATCH_SIZE) - 1;
974 *p-- = '\0';
976 while (n != 0)
978 *p = '0' + (n & 7);
979 p -- ;
980 n >>= 3;
983 return ++p;
987 /* Convert unsigned binary to ascii. */
989 static char *
990 btoa (GFC_UINTEGER_LARGEST n)
992 char *p;
994 if (n == 0)
996 scratch[0] = '0';
997 scratch[1] = '\0';
998 return scratch;
1001 p = scratch + sizeof (SCRATCH_SIZE) - 1;
1002 *p-- = '\0';
1004 while (n != 0)
1006 *p-- = '0' + (n & 1);
1007 n >>= 1;
1010 return ++p;
1014 void
1015 write_i (fnode * f, const char *p, int len)
1017 write_decimal (f, p, len, (void *) gfc_itoa);
1021 void
1022 write_b (fnode * f, const char *p, int len)
1024 write_int (f, p, len, btoa);
1028 void
1029 write_o (fnode * f, const char *p, int len)
1031 write_int (f, p, len, otoa);
1034 void
1035 write_z (fnode * f, const char *p, int len)
1037 write_int (f, p, len, xtoa);
1041 void
1042 write_d (fnode *f, const char *p, int len)
1044 write_float (f, p, len);
1048 void
1049 write_e (fnode *f, const char *p, int len)
1051 write_float (f, p, len);
1055 void
1056 write_f (fnode *f, const char *p, int len)
1058 write_float (f, p, len);
1062 void
1063 write_en (fnode *f, const char *p, int len)
1065 write_float (f, p, len);
1069 void
1070 write_es (fnode *f, const char *p, int len)
1072 write_float (f, p, len);
1076 /* Take care of the X/TR descriptor. */
1078 void
1079 write_x (fnode * f)
1081 char *p;
1083 p = write_block (f->u.n);
1084 if (p == NULL)
1085 return;
1087 memset (p, ' ', f->u.n);
1091 /* List-directed writing. */
1094 /* Write a single character to the output. Returns nonzero if
1095 something goes wrong. */
1097 static int
1098 write_char (char c)
1100 char *p;
1102 p = write_block (1);
1103 if (p == NULL)
1104 return 1;
1106 *p = c;
1108 return 0;
1112 /* Write a list-directed logical value. */
1114 static void
1115 write_logical (const char *source, int length)
1117 write_char (extract_int (source, length) ? 'T' : 'F');
1121 /* Write a list-directed integer value. */
1123 static void
1124 write_integer (const char *source, int length)
1126 char *p;
1127 const char *q;
1128 int digits;
1129 int width;
1131 q = gfc_itoa (extract_int (source, length));
1133 switch (length)
1135 case 1:
1136 width = 4;
1137 break;
1139 case 2:
1140 width = 6;
1141 break;
1143 case 4:
1144 width = 11;
1145 break;
1147 case 8:
1148 width = 20;
1149 break;
1151 default:
1152 width = 0;
1153 break;
1156 digits = strlen (q);
1158 if(width < digits )
1159 width = digits ;
1160 p = write_block (width) ;
1161 if (no_leading_blank)
1163 memcpy (p, q, digits);
1164 memset(p + digits ,' ', width - digits) ;
1166 else
1168 memset(p ,' ', width - digits) ;
1169 memcpy (p + width - digits, q, digits);
1174 /* Write a list-directed string. We have to worry about delimiting
1175 the strings if the file has been opened in that mode. */
1177 static void
1178 write_character (const char *source, int length)
1180 int i, extra;
1181 char *p, d;
1183 switch (current_unit->flags.delim)
1185 case DELIM_APOSTROPHE:
1186 d = '\'';
1187 break;
1188 case DELIM_QUOTE:
1189 d = '"';
1190 break;
1191 default:
1192 d = ' ';
1193 break;
1196 if (d == ' ')
1197 extra = 0;
1198 else
1200 extra = 2;
1202 for (i = 0; i < length; i++)
1203 if (source[i] == d)
1204 extra++;
1207 p = write_block (length + extra);
1208 if (p == NULL)
1209 return;
1211 if (d == ' ')
1212 memcpy (p, source, length);
1213 else
1215 *p++ = d;
1217 for (i = 0; i < length; i++)
1219 *p++ = source[i];
1220 if (source[i] == d)
1221 *p++ = d;
1224 *p = d;
1229 /* Output a real number with default format.
1230 This is 1PG14.7E2 for REAL(4) and 1PG23.15E3 for REAL(8). */
1232 static void
1233 write_real (const char *source, int length)
1235 fnode f ;
1236 int org_scale = g.scale_factor;
1237 f.format = FMT_G;
1238 g.scale_factor = 1;
1239 if (length < 8)
1241 f.u.real.w = 14;
1242 f.u.real.d = 7;
1243 f.u.real.e = 2;
1245 else
1247 f.u.real.w = 23;
1248 f.u.real.d = 15;
1249 f.u.real.e = 3;
1251 write_float (&f, source , length);
1252 g.scale_factor = org_scale;
1256 static void
1257 write_complex (const char *source, int len)
1259 if (write_char ('('))
1260 return;
1261 write_real (source, len);
1263 if (write_char (','))
1264 return;
1265 write_real (source + len, len);
1267 write_char (')');
1271 /* Write the separator between items. */
1273 static void
1274 write_separator (void)
1276 char *p;
1278 p = write_block (options.separator_len);
1279 if (p == NULL)
1280 return;
1282 memcpy (p, options.separator, options.separator_len);
1286 /* Write an item with list formatting.
1287 TODO: handle skipping to the next record correctly, particularly
1288 with strings. */
1290 void
1291 list_formatted_write (bt type, void *p, int len)
1293 static int char_flag;
1295 if (current_unit == NULL)
1296 return;
1298 if (g.first_item)
1300 g.first_item = 0;
1301 char_flag = 0;
1302 write_char (' ');
1304 else
1306 if (type != BT_CHARACTER || !char_flag ||
1307 current_unit->flags.delim != DELIM_NONE)
1308 write_separator ();
1311 switch (type)
1313 case BT_INTEGER:
1314 write_integer (p, len);
1315 break;
1316 case BT_LOGICAL:
1317 write_logical (p, len);
1318 break;
1319 case BT_CHARACTER:
1320 write_character (p, len);
1321 break;
1322 case BT_REAL:
1323 write_real (p, len);
1324 break;
1325 case BT_COMPLEX:
1326 write_complex (p, len);
1327 break;
1328 default:
1329 internal_error ("list_formatted_write(): Bad type");
1332 char_flag = (type == BT_CHARACTER);
1335 /* NAMELIST OUTPUT
1337 nml_write_obj writes a namelist object to the output stream. It is called
1338 recursively for derived type components:
1339 obj = is the namelist_info for the current object.
1340 offset = the offset relative to the address held by the object for
1341 derived type arrays.
1342 base = is the namelist_info of the derived type, when obj is a
1343 component.
1344 base_name = the full name for a derived type, including qualifiers
1345 if any.
1346 The returned value is a pointer to the object beyond the last one
1347 accessed, including nested derived types. Notice that the namelist is
1348 a linear linked list of objects, including derived types and their
1349 components. A tree, of sorts, is implied by the compound names of
1350 the derived type components and this is how this function recurses through
1351 the list. */
1353 /* A generous estimate of the number of characters needed to print
1354 repeat counts and indices, including commas, asterices and brackets. */
1356 #define NML_DIGITS 20
1358 /* Stores the delimiter to be used for character objects. */
1360 static const char * nml_delim;
1362 static namelist_info *
1363 nml_write_obj (namelist_info * obj, index_type offset,
1364 namelist_info * base, char * base_name)
1366 int rep_ctr;
1367 int num;
1368 int nml_carry;
1369 index_type len;
1370 index_type obj_size;
1371 index_type nelem;
1372 index_type dim_i;
1373 index_type clen;
1374 index_type elem_ctr;
1375 index_type obj_name_len;
1376 void * p ;
1377 char cup;
1378 char * obj_name;
1379 char * ext_name;
1380 char rep_buff[NML_DIGITS];
1381 namelist_info * cmp;
1382 namelist_info * retval = obj->next;
1384 /* Write namelist variable names in upper case. If a derived type,
1385 nothing is output. If a component, base and base_name are set. */
1387 if (obj->type != GFC_DTYPE_DERIVED)
1389 write_character ("\n ", 2);
1390 len = 0;
1391 if (base)
1393 len =strlen (base->var_name);
1394 for (dim_i = 0; dim_i < (index_type) strlen (base_name); dim_i++)
1396 cup = toupper (base_name[dim_i]);
1397 write_character (&cup, 1);
1400 for (dim_i =len; dim_i < (index_type) strlen (obj->var_name); dim_i++)
1402 cup = toupper (obj->var_name[dim_i]);
1403 write_character (&cup, 1);
1405 write_character ("=", 1);
1408 /* Counts the number of data output on a line, including names. */
1410 num = 1;
1412 len = obj->len;
1413 obj_size = len;
1414 if (obj->type == GFC_DTYPE_COMPLEX)
1415 obj_size = 2*len;
1416 if (obj->type == GFC_DTYPE_CHARACTER)
1417 obj_size = obj->string_length;
1418 if (obj->var_rank)
1419 obj_size = obj->size;
1421 /* Set the index vector and count the number of elements. */
1423 nelem = 1;
1424 for (dim_i=0; dim_i < obj->var_rank; dim_i++)
1426 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1427 nelem = nelem * (obj->dim[dim_i].ubound + 1 - obj->dim[dim_i].lbound);
1430 /* Main loop to output the data held in the object. */
1432 rep_ctr = 1;
1433 for (elem_ctr = 0; elem_ctr < nelem; elem_ctr++)
1436 /* Build the pointer to the data value. The offset is passed by
1437 recursive calls to this function for arrays of derived types.
1438 Is NULL otherwise. */
1440 p = (void *)(obj->mem_pos + elem_ctr * obj_size);
1441 p += offset;
1443 /* Check for repeat counts of intrinsic types. */
1445 if ((elem_ctr < (nelem - 1)) &&
1446 (obj->type != GFC_DTYPE_DERIVED) &&
1447 !memcmp (p, (void*)(p + obj_size ), obj_size ))
1449 rep_ctr++;
1452 /* Execute a repeated output. Note the flag no_leading_blank that
1453 is used in the functions used to output the intrinsic types. */
1455 else
1457 if (rep_ctr > 1)
1459 st_sprintf(rep_buff, " %d*", rep_ctr);
1460 write_character (rep_buff, strlen (rep_buff));
1461 no_leading_blank = 1;
1463 num++;
1465 /* Output the data, if an intrinsic type, or recurse into this
1466 routine to treat derived types. */
1468 switch (obj->type)
1471 case GFC_DTYPE_INTEGER:
1472 write_integer (p, len);
1473 break;
1475 case GFC_DTYPE_LOGICAL:
1476 write_logical (p, len);
1477 break;
1479 case GFC_DTYPE_CHARACTER:
1480 if (nml_delim)
1481 write_character (nml_delim, 1);
1482 write_character (p, obj->string_length);
1483 if (nml_delim)
1484 write_character (nml_delim, 1);
1485 break;
1487 case GFC_DTYPE_REAL:
1488 write_real (p, len);
1489 break;
1491 case GFC_DTYPE_COMPLEX:
1492 no_leading_blank = 0;
1493 num++;
1494 write_complex (p, len);
1495 break;
1497 case GFC_DTYPE_DERIVED:
1499 /* To treat a derived type, we need to build two strings:
1500 ext_name = the name, including qualifiers that prepends
1501 component names in the output - passed to
1502 nml_write_obj.
1503 obj_name = the derived type name with no qualifiers but %
1504 appended. This is used to identify the
1505 components. */
1507 /* First ext_name => get length of all possible components */
1509 ext_name = (char*)get_mem ( (base_name ? strlen (base_name) : 0)
1510 + (base ? strlen (base->var_name) : 0)
1511 + strlen (obj->var_name)
1512 + obj->var_rank * NML_DIGITS
1513 + 1);
1515 strcpy(ext_name, base_name ? base_name : "");
1516 clen = base ? strlen (base->var_name) : 0;
1517 strcat (ext_name, obj->var_name + clen);
1519 /* Append the qualifier. */
1521 for (dim_i = 0; dim_i < obj->var_rank; dim_i++)
1523 strcat (ext_name, dim_i ? "" : "(");
1524 clen = strlen (ext_name);
1525 st_sprintf (ext_name + clen, "%d", (int) obj->ls[dim_i].idx);
1526 strcat (ext_name, (dim_i == obj->var_rank - 1) ? ")" : ",");
1529 /* Now obj_name. */
1531 obj_name_len = strlen (obj->var_name) + 1;
1532 obj_name = get_mem (obj_name_len+1);
1533 strcpy (obj_name, obj->var_name);
1534 strcat (obj_name, "%");
1536 /* Now loop over the components. Update the component pointer
1537 with the return value from nml_write_obj => this loop jumps
1538 past nested derived types. */
1540 for (cmp = obj->next;
1541 cmp && !strncmp (cmp->var_name, obj_name, obj_name_len);
1542 cmp = retval)
1544 retval = nml_write_obj (cmp, (index_type)(p - obj->mem_pos),
1545 obj, ext_name);
1548 free_mem (obj_name);
1549 free_mem (ext_name);
1550 goto obj_loop;
1552 default:
1553 internal_error ("Bad type for namelist write");
1556 /* Reset the leading blank suppression, write a comma and, if 5
1557 values have been output, write a newline and advance to column
1558 2. Reset the repeat counter. */
1560 no_leading_blank = 0;
1561 write_character (",", 1);
1562 if (num > 5)
1564 num = 0;
1565 write_character ("\n ", 2);
1567 rep_ctr = 1;
1570 /* Cycle through and increment the index vector. */
1572 obj_loop:
1574 nml_carry = 1;
1575 for (dim_i = 0; nml_carry && (dim_i < obj->var_rank); dim_i++)
1577 obj->ls[dim_i].idx += nml_carry ;
1578 nml_carry = 0;
1579 if (obj->ls[dim_i].idx > (ssize_t)obj->dim[dim_i].ubound)
1581 obj->ls[dim_i].idx = obj->dim[dim_i].lbound;
1582 nml_carry = 1;
1587 /* Return a pointer beyond the furthest object accessed. */
1589 return retval;
1592 /* This is the entry function for namelist writes. It outputs the name
1593 of the namelist and iterates through the namelist by calls to
1594 nml_write_obj. The call below has dummys in the arguments used in
1595 the treatment of derived types. */
1597 void
1598 namelist_write (void)
1600 namelist_info * t1, *t2, *dummy = NULL;
1601 index_type i;
1602 index_type dummy_offset = 0;
1603 char c;
1604 char * dummy_name = NULL;
1605 unit_delim tmp_delim;
1607 /* Set the delimiter for namelist output. */
1609 tmp_delim = current_unit->flags.delim;
1610 current_unit->flags.delim = DELIM_NONE;
1611 switch (tmp_delim)
1613 case (DELIM_QUOTE):
1614 nml_delim = "\"";
1615 break;
1617 case (DELIM_APOSTROPHE):
1618 nml_delim = "'";
1619 break;
1621 default:
1622 nml_delim = NULL;
1625 write_character ("&",1);
1627 /* Write namelist name in upper case - f95 std. */
1629 for (i = 0 ;i < ioparm.namelist_name_len ;i++ )
1631 c = toupper (ioparm.namelist_name[i]);
1632 write_character (&c ,1);
1635 if (ionml != NULL)
1637 t1 = ionml;
1638 while (t1 != NULL)
1640 t2 = t1;
1641 t1 = nml_write_obj (t2, dummy_offset, dummy, dummy_name);
1644 write_character (" /\n", 4);
1646 /* Recover the original delimiter. */
1648 current_unit->flags.delim = tmp_delim;
1651 #undef NML_DIGITS