/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: r_writ.c,v 1.2 1997/09/25 17:52:37 ae59 Exp $ */
/*
$Log: r_writ.c,v $
 * Revision 1.2  1997/09/25  17:52:37  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:18  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : r_writ.c                              */
/*                                                              */
/*      Entries         : void r_writ                           */
/*                         (device,s,TotalWidth,FracDigits,rnd) */
/*                        FILE *device;                         */
/*                        a_real s;                             */
/*                        a_intg rnd,TotalWidth,FracDigits;     */
/*                                                              */
/*      Arguments       : device - output device writing string */
/*                        s - IEEE value                        */
/*                        Totalwidth - total length of string   */
/*                        FracDigits - number of fraction digits*/
/*                        rnd - rounding mode                   */
/*                              -1 = round downwards            */
/*                               0 = round to nearest           */
/*                               1 = round upwards              */
/*                                                              */
/*      Description     : Convert an IEEE double format number  */
/*                        to a character string and write to    */
/*                        output device                         */
/*                                                              */
/*      Date            : 1991-04-09                            */
/*      1991-04-09 : avoid loop without body                    */
/*      1991-01-24 : Absolute value of FracDigits used.         */
/*                   TotalWidth-2<FracDigits considered.        */
/*                   No special consideration of TotalWidth=0.  */
/*      1990-11-28 : Minimum length set to 9                    */
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*      1990-11-08 : default TotalWidth changed                 */
/*      1990-10-31 : left adjust output if width is negative    */
/*      1990-07-12 : LINT_ARGS prototype, cp renamed            */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern dotprecision b_cp__;
#endif

#ifdef LINT_ARGS
local void r_writ(FILE *device,a_real s,a_intg TotalWidth,a_intg FracDigits,
                  a_intg rnd)
#else
local void r_writ(device,s,TotalWidth,FracDigits,rnd)

FILE *device;
a_real s;
a_intg TotalWidth;
a_intg FracDigits;
a_intg rnd;
#endif
        {
        a_intg length;
        int k,sign;     /* !!! must be int !!! */
        char *buffer;

        E_TPUSH("r_writ")

        if (TotalWidth<0)
           {
           sign = 1;
           TotalWidth = -TotalWidth;
           }
        else
           sign = 0;

        if (FracDigits<0) FracDigits = -FracDigits;

/* Set default TotalWidth according to data format                   */
        if (FracDigits==0)
           {
/* --- No special consideration of case TotalWidth=0 ---
           if (TotalWidth==0)
              TotalWidth = ExpDigits+4+(301*MANTL/1000+1);
           else
  ---                                       --- */
           if (TotalWidth<9)
              TotalWidth = 9;
           }
        else if (TotalWidth-2<FracDigits)
           {
           TotalWidth = 2+FracDigits;
           }

        buffer = (char *)b_cp__;

/* generate output string                                            */
        r_outp(buffer,s,TotalWidth,FracDigits,rnd,&length);

/*                                                                   */
/* output of string                                                  */
/*                                                                   */
        if (sign)
           {
           for (k=0;buffer[k]==' ';k++) { /* empty */ }
           if (k>0 && buffer[k]!='-') k--;
           sign = k;
           while (k<(int)length)
              fputc(buffer[k++],device);
           for (k=0;k<sign;k++)
              fputc(' ',device);
           }
        else
           {
           for (k=0;k<(int)length;k++)
              fputc(buffer[k],device);
           }

        E_TPOPP("r_writ")
        return;
        }
