/**************************************************************************/
/*                                                                        */
/*                                 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_sval.c,v 1.2 1997/09/25 17:51:45 ae59 Exp $ */
/*
$Log: r_sval.c,v $
 * Revision 1.2  1997/09/25  17:51:45  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:15  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : r_sval.c                              */
/*                                                              */
/*      Entries         : a_real r_sval(s,rnd,r)                */
/*                        s_trng s;                             */
/*                        a_intg rnd;                           */
/*                        s_trng *r;                            */
/*                                                              */
/*      Arguments       : s   - input string                    */
/*                        rnd - rounding mode                   */
/*                              -1 = rounding downwards         */
/*                               0 = round to nearest           */
/*                               1 = round upwards              */
/*                        r   - remainder string                */
/*                                                              */
/*      Description     : Convert a character string            */
/*                        to IEEE double format                 */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : (  char *) cast in malloc    =b=           */
/*      1991-04-22 : real assignment replaced by R_ASSIGN()     */
/*      1991-02-21 : (a_char *) cast in r_conv()                */
/*      1991-02-01 : test for BUFFERSIZE removed                */
/*      1991-01-22 : r_ass() used                               */
/*      1991-01-17 : (char *) cast in pointer difference        */
/*                   type of ch -> char                         */
/*      1990-12-05 : remainder string starts at next            */
/*                   b_cp__ removed                             */
/*      1990-12-03 : one more allocated for direct use of '\0'  */
/*      1990-11-22 : replace strncpy by memcpy                  */
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*                   declared length of remainder considered    */
/*      1990-10-30 : arguments interchanged in r_conv           */
/*      1990-10-23 : new argument next in r_conv                */
/*      1990-10-22 : new argument r                             */
/*      1990-10-17 : (char *) cast in strncpy                   */
/****************************************************************/

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

#ifdef LINT_ARGS
local a_real r_sval(s_trng s,a_intg rnd,s_trng *r)
#else
local a_real r_sval(s,rnd,r)

s_trng s;
a_intg rnd;
s_trng *r;
#endif
        {
        a_real res;
        a_char *next;
        char ch;

        E_TPUSH("r_sval")

        if (s.clen<=0)
           {
           R_ASSIGN(res,*r_zero);
           e_trap(I_O_ERROR,2,E_TMSG,58);
           }
        else
           {
           if (s.suba) s_asgn(&s,s);
           ch = s.ptr[s.clen];
           s.ptr[s.clen] = '\0';

           r_conv((a_char *)s.ptr,&res,rnd,&next);

           if (r->alen<(r->clen = s.clen-((char *)next-s.ptr)))
              {
              if (r->fix==FALSE)
                 {
                 if (r->alen>0)
                    {
#ifdef HEAP_CHECK
b_freh((a_char *)&r->ptr,(a_char *)r->ptr,(a_char *)"r_sval");
#endif
                    free(r->ptr);
                    r->alen = 0;
                    }

                 /* one more allocated for direct use of '\0'   */
                 if ((r->ptr=(char*) malloc(r->clen+1))==NULL)
                    {
                    e_trap(ALLOCATION,2,E_TMSG,54);
                    r->alen = r->clen = 0;
                    }
                 else
                    r->alen = r->clen;
#ifdef HEAP_CHECK
b_geth((a_char *)&r->ptr,(a_char *)r->ptr,(a_char *)"r_sval");
#endif
                 }
              else
                 r->clen = r->alen;
              }
           if (r->clen>0) memcpy(r->ptr,next,r->clen);
           s.ptr[s.clen] = ch;
           }

        if (s.tmp) s_free(&s);

        E_TPOPP("r_sval")
        return(res);
        }
