/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_conv_real[] = "@(#)conv_real.c	1.18 10/20/92 05:36:41  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_convert_Real_const


PURPOSE:
	This routine converts a digit string into an Real number

IN:
	valuelength - length in bytes of value variable
	constant - string of value expression
	type_lengths - parameters vector for lengths of this type

OUT:
	value - value specified by the constant argument


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/exceptions.h>
#include <elros/enc_dcd.h>
#include <elros/typeinfo.h>
#include <elros/extend_arith.h>

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <malloc.h>
#include <memory.h>



int __ROr_convert_Real_const(value,valuelength,maxlengthbits,maxlength2bits,constant,type_info)

	void *value;
	unsigned long valuelength;
	unsigned long maxlengthbits;
	unsigned long maxlength2bits;
	char *constant;
	__RO_Type_Info *type_info;

{
   unsigned long maxlength = maxlengthbits/8 + (maxlengthbits%8 > 0);
   unsigned long maxlength2 = maxlength2bits/8 + (maxlength2bits%8 > 0);
   unsigned char *byteptr = (unsigned char *)value;
   unsigned char *expptr = byteptr+1;
   unsigned long explen = maxlength2;
   unsigned char *exp10ptr = (unsigned char *)malloc(explen);
   unsigned char *mantptr = byteptr+explen+1;
   unsigned long mantlen = maxlength;
   int sign_value;
   int i,c;
   char *exponent = NULL;
   char *constant_string;

   constant_string = (char *)malloc(strlen(constant));

   no_underscores(constant,constant_string);
   
   if (!(match(constant,"^[0-9]*[.][0-9]*$") ||
         match(constant,"^[0-9]*$") ||
         match(constant,"^[+-][0-9]*[.][0-9]*$") ||
         match(constant,"^[+-][0-9]*$")))
   {
      if (!(match(constant,"^[0-9]*[.][0-9]*[Ee][0-9][0-9]*$") ||
            match(constant,"^[+-][0-9]*[.][0-9]*[Ee][0-9][0-9]*$") ||
            match(constant,"^[0-9]*[.][0-9]*[Ee][+-][0-9][0-9]*$") ||
            match(constant,"^[+-][0-9]*[.][0-9]*[Ee][+-][0-9][0-9]*$") ||
            match(constant,"^[0-9]*[Ee][0-9][0-9]*$") ||
            match(constant,"^[+-][0-9]*[Ee][0-9][0-9]*$") ||
            match(constant,"^[0-9]*[Ee][+-][0-9][0-9]*$") ||
            match(constant,"^[+-][0-9]*[Ee][+-][0-9][0-9]*$")))
         return(__ROx_CONST_REALERROR);
      exponent = strchr(constant,'E');
      if (!exponent)
         exponent = strchr(constant,'e');
      if (!exponent)
         return(__ROx_CONST_REALECONSISTENCY);
      exponent++; /* skip over the E */
   }
   else
      exponent = NULL;

   if (__RO_ISFLOAT(type_info,maxlength,maxlength2,valuelength))
   {
      *((float *)value) = atof(constant);
      return(0);
   }
   else if (__RO_ISDOUBLE(type_info,maxlength,maxlength2,valuelength))
   {
      *((double *)value) = atof(constant);
      return(0);
   }
   else if (__RO_ISLONGDOUBLE(type_info,maxlength,maxlength2,valuelength))
   {
      *((LONGEST_REAL *)value) = atof(constant);
      return(0);
   }

/*  This doesn't work, since 'valuelength' is not real.
   if (valuelength != maxlength+maxlength2+1)
      return(__ROx_CONST_REALLENGTHS);
*/

   memset(byteptr,0x00,valuelength);
   memset(exp10ptr,0x00,explen);
   sign_value = constant[0]=='-' ? __REAL_SIGN_BIT : 0;
   if (constant[0]=='+' || constant[0]=='-')
      constant++;

	/* Fill in the signature byte */

   *byteptr = __REAL_BINARY_ENC | (sign_value) | __REAL_BIN_BASE2;


   if (exponent)
   {
      if (atoI(exponent,exp10ptr,explen))
         return(__ROx_CONST_REALEXPONENTERROR);
   }

	/* accumulate value */

   {
      register int decpt_seen = 0, nzeroes = 0;

      while (isdigit(c = *constant++) || c == '.') 
      {
         if (c == '.')
         {
            decpt_seen = 1;
            continue;
         }


		/* handle trailing zeroes specially */

         if (decpt_seen)
         { 

		/* ignore zero for now */

            if (c == '0')
            {
               nzeroes++;
               continue;
            }

		/* put zeroes back in */

            while (nzeroes > 0)
            {
               decrement(exp10ptr,explen) /*exp--*/;
               if (1 /* ----precision not exceeded */)
                  times10(mantptr,mantlen);
               else
                  increment(exp10ptr,explen) /*exp++*/;
               nzeroes--;
            }
		/* decr exponent if decimal pt. seen */
            decrement(exp10ptr,explen) /*exp--*/;
         }
         else
         { 

		/* ignore zero for now */

            if (c == '0')
            {
               nzeroes++;
               increment(exp10ptr,explen) /*exp++*/;
               continue;
            }
         }

         if (1 /* ----precision not exceeded */)
         {
            times10(mantptr,mantlen);
            add_long(mantptr,mantlen,c-'0');
         }
         else
            increment(exp10ptr,explen) /*exp++*/;
      }
   }

	/* Test for and get out if ZERO */

   if (zeroeq(mantptr,mantlen))
      return(0);


	/* Establish the binary exponent, based on preliminary
	   normalization of mantissa. */

   add_long(expptr,explen,mantlen*8);
   while (!signof(mantptr,mantlen))
   {
      shiftleft(mantptr,mantlen);
      decrement(expptr,explen);
   }

	/* Take either fractional or exponential case, and adjust mantissa
	   for base 10 exponent; and keep normalizing the base 2 exponent */

   if (signof(exp10ptr,explen))
      while (!zeroeq(exp10ptr,explen))
      {
         unsigned long extra;

         increment(exp10ptr,explen);
         extra = (divide10(mantptr,mantlen)*256)/10;
         while (!(signof(mantptr,mantlen)))
         {
            rollleft(mantptr,mantlen,extra>>7);
            extra = (extra << 1) & 0xFF;
            decrement(expptr,explen);
         }
      }
   else
      while (!zeroeq(exp10ptr,explen))
      {
         unsigned long extra;

         decrement(exp10ptr,explen);
         extra = times10(mantptr,mantlen);
         while (extra)
         {
            rollright(mantptr,mantlen,extra&0x01);
            extra = extra >> 1;
            increment(expptr,explen);
         }
      }
   add_long(expptr,explen,-(mantlen*8));

   swapends(expptr,explen);
   swapends(mantptr,mantlen);

   free(exp10ptr);
   free(constant_string);

   return(SUCCESS);
}


