/**************************************************************************/
/*                                                                        */
/*                                 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: a_sval.c,v 1.3 1997/09/25 16:53:35 ae59 Exp $ */
/*
$Log: a_sval.c,v $
 * Revision 1.3  1997/09/25  16:53:35  ae59
 * Copyright-message
 *
 * Revision 1.2  1995/03/30  14:14:35  ae59
 * cast to cha* at malloc
 *
 * Revision 1.1  1994/01/27  13:20:50  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : a_sval.c                              */
/*                                                              */
/*      Entries         : a_intg a_sval(s,t)                    */
/*                        s_trng s;                             */
/*                        s_trng *t;                            */
/*                                                              */
/*      Arguments       : a = input string                      */
/*                        b = remainder string                  */
/*                                                              */
/*      Description     : Convert integer literal to integer    */
/*                                                              */
/*      Note            : 0 returned in case of error           */
/*                                                              */
/*      Date            : 1992-07-21                            */
/*      1992-07-21 : add cast to size in               =b=      */
/*                   t->clen = s.clen-len)                      */
/*      1991-02-13 : E_TSTG used in e_trap() call               */
/*      1991-01-22 : BUFFERSIZE removed                         */
/*      1991-01-16 : consider empty input string separately     */
/*      1990-12-05 : break supplied in while loop               */
/*      1990-12-03 : one more allocated for direct use of '\0'  */
/*      1990-11-22 : replace strncpy by memcpy                  */
/*      1990-11-20 : code changed for const-opt. compilers      */
/*                   declared length of remainder considered    */
/*      1990-11-15 : E_TPUSH+E_TPOPP                            */
/****************************************************************/

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

#ifdef LINT_ARGS
local a_intg a_sval(s_trng s,s_trng *t)
#else
local a_intg a_sval(s,t)

s_trng s;
s_trng *t;
#endif
        {
        a_intg dig,res,len;
        a_bool sign;
        a_char ch;

        E_TPUSH("a_sval")

        len = res = 0;
        sign = FALSE;

        /* ignore leading blanks                             */
        while (len<s.clen)
           if (s.ptr[len]==' ') len++;
           else break;

        if (len==s.clen)
           {
           e_trap(I_O_ERROR,2,E_TMSG,62);
           }
        else
           {

           /* check for sign                                 */
           if (s.ptr[len]=='+') len++;
           else if (s.ptr[len]=='-')
              {
              len++;
              sign = TRUE;
              }

           if (len==s.clen)
              {
              e_trap(I_O_ERROR,4,E_TMSG,63,E_TSTG+E_TEXT(10),&s);
              }

           /* handle first decimal digit                     */
           else if (!isdigit(s.ptr[len]))
              {
              ch = s.ptr[len];
              e_trap(I_O_ERROR,4,E_TMSG,21,E_TCHR,&ch);
              }

           /* convert decimal string to integer              */
           else
              {
              res = s.ptr[len]-'0';

              while (++len<s.clen)
              if (isdigit(s.ptr[len]))
                 {
                 dig = s.ptr[len]-'0';

                 /* check for integer overflow               */
                 if ((MAXINT-dig)/10<res)
                    {
                    if (sign && (MININT+dig)/10==-res)
                       res = MININT;
                    else
                       {
                       if (sign) res = MININT;
                       else res = MAXINT;
                       while (++len<s.clen)
                          if (!isdigit(s.ptr[len])) break;
                       e_trap(OVERFLOW,4,E_TMSG,15,E_TSTG+E_TEXT(10),&s);
                       break;
                       }
                    }

                 /* add digit                                */
                 else
                    res = res*10+dig;
                 }
              else
                 break;

              if (sign && res!=MININT) res = -res;
              }
           }

        if (t->alen<(t->clen = s.clen-(size_t)len))
           {
           if (t->fix==FALSE)
              {
              if (t->alen>0)
                 {
#ifdef HEAP_CHECK
b_freh((a_char *)&t->ptr,(a_char *)t->ptr,(a_char *)"a_sval");
#endif
                 free(t->ptr);
                 t->alen = 0;
                 }

              /* one more for direct use of '\0'     */
              if ((t->ptr=(char*) malloc(t->clen+1))==NULL)
                 {
                 e_trap(ALLOCATION,2,E_TMSG,54);
                 t->clen = 0;
                 }
              else
                 {
#ifdef HEAP_CHECK
b_geth((a_char *)&t->ptr,(a_char *)t->ptr,(a_char *)"a_sval");
#endif
                 t->alen = t->clen;
                 }
              }
           else
              t->clen = t->alen;
           }

        if (t->clen>0) memcpy(t->ptr,s.ptr+len,t->clen);

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

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

